Skip to content

Commit dbcdde6

Browse files
authored
Refactor build helpers (#1826)
* Extract functions into their own files * Allow injection of exclusion filter to make easier to test * Make sure we always exclude ourselves
1 parent 58bdbe8 commit dbcdde6

File tree

4 files changed

+56
-51
lines changed

4 files changed

+56
-51
lines changed

src/support_code_library_builder/build_helpers.ts

-50
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ParameterType } from '@cucumber/cucumber-expressions'
2+
import { IParameterTypeDefinition } from './types'
3+
4+
export function buildParameterType({
5+
name,
6+
regexp,
7+
transformer,
8+
useForSnippets,
9+
preferForRegexpMatch,
10+
}: IParameterTypeDefinition<any>): ParameterType<any> {
11+
if (typeof useForSnippets !== 'boolean') useForSnippets = true
12+
if (typeof preferForRegexpMatch !== 'boolean') preferForRegexpMatch = false
13+
return new ParameterType(
14+
name,
15+
regexp,
16+
null,
17+
transformer,
18+
useForSnippets,
19+
preferForRegexpMatch
20+
)
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import path from 'path'
2+
import StackTrace from 'stacktrace-js'
3+
import { isFileNameInCucumber } from '../stack_trace_filter'
4+
import { doesHaveValue, valueOrDefault } from '../value_checker'
5+
import { ILineAndUri } from '../types'
6+
7+
export function getDefinitionLineAndUri(
8+
cwd: string,
9+
isExcluded = isFileNameInCucumber
10+
): ILineAndUri {
11+
let line: number
12+
let uri: string
13+
try {
14+
const stackframes = StackTrace.getSync()
15+
const stackframe = stackframes.find(
16+
(frame) =>
17+
frame.getFileName() !== __filename && !isExcluded(frame.getFileName())
18+
)
19+
if (stackframe != null) {
20+
line = stackframe.getLineNumber()
21+
uri = stackframe.getFileName()
22+
if (doesHaveValue(uri)) {
23+
uri = path.relative(cwd, uri)
24+
}
25+
}
26+
} catch (e) {
27+
console.warn('Warning: unable to get definition line and uri', e)
28+
}
29+
return {
30+
line: valueOrDefault(line, 0),
31+
uri: valueOrDefault(uri, 'unknown'),
32+
}
33+
}

src/support_code_library_builder/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { buildParameterType, getDefinitionLineAndUri } from './build_helpers'
1+
import { buildParameterType } from './build_parameter_type'
2+
import { getDefinitionLineAndUri } from './get_definition_line_and_uri'
23
import { IdGenerator } from '@cucumber/messages'
34
import * as messages from '@cucumber/messages'
45
import TestCaseHookDefinition from '../models/test_case_hook_definition'

0 commit comments

Comments
 (0)