Skip to content

Commit

Permalink
Refactor build helpers (#1826)
Browse files Browse the repository at this point in the history
* Extract functions into their own files

* Allow injection of exclusion filter to make easier to test

* Make sure we always exclude ourselves
  • Loading branch information
mattwynne authored Oct 29, 2021
1 parent 58bdbe8 commit dbcdde6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 51 deletions.
50 changes: 0 additions & 50 deletions src/support_code_library_builder/build_helpers.ts

This file was deleted.

21 changes: 21 additions & 0 deletions src/support_code_library_builder/build_parameter_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ParameterType } from '@cucumber/cucumber-expressions'
import { IParameterTypeDefinition } from './types'

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
)
}
33 changes: 33 additions & 0 deletions src/support_code_library_builder/get_definition_line_and_uri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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'

export function getDefinitionLineAndUri(
cwd: string,
isExcluded = isFileNameInCucumber
): ILineAndUri {
let line: number
let uri: string
try {
const stackframes = StackTrace.getSync()
const stackframe = stackframes.find(
(frame) =>
frame.getFileName() !== __filename && !isExcluded(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'),
}
}
3 changes: 2 additions & 1 deletion src/support_code_library_builder/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buildParameterType, getDefinitionLineAndUri } from './build_helpers'
import { buildParameterType } from './build_parameter_type'
import { getDefinitionLineAndUri } from './get_definition_line_and_uri'
import { IdGenerator } from '@cucumber/messages'
import * as messages from '@cucumber/messages'
import TestCaseHookDefinition from '../models/test_case_hook_definition'
Expand Down

0 comments on commit dbcdde6

Please sign in to comment.