-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
build_helpers.ts
50 lines (48 loc) · 1.4 KB
/
build_helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { ParameterType } from '@cucumber/cucumber-expressions'
import path from 'path'
import StackTrace from 'stacktrace-js'
import { isFileNameInCucumber } from '../stack_trace_filter'
import { doesHaveValue, valueOrDefault } from '../value_checker'
import { ILineAndUri } from '../types'
import { IParameterTypeDefinition } from './types'
export function getDefinitionLineAndUri(cwd: string): ILineAndUri {
let line: number
let uri: string
try {
const stackframes = StackTrace.getSync()
const stackframe = stackframes.find((frame) => {
return !isFileNameInCucumber(frame.getFileName())
})
if (stackframe != null) {
line = stackframe.getLineNumber()
uri = stackframe.getFileName()
if (doesHaveValue(uri)) {
uri = path.relative(cwd, uri)
}
}
} catch (e) {
console.warn('Warning: unable to get definition line and uri', e)
}
return {
line: valueOrDefault(line, 0),
uri: valueOrDefault(uri, 'unknown'),
}
}
export function buildParameterType({
name,
regexp,
transformer,
useForSnippets,
preferForRegexpMatch,
}: IParameterTypeDefinition<any>): ParameterType<any> {
if (typeof useForSnippets !== 'boolean') useForSnippets = true
if (typeof preferForRegexpMatch !== 'boolean') preferForRegexpMatch = false
return new ParameterType(
name,
regexp,
null,
transformer,
useForSnippets,
preferForRegexpMatch
)
}