Skip to content

Commit 8fece83

Browse files
authored
include source reference on parameter type message (#2287)
1 parent 54ac047 commit 8fece83

File tree

8 files changed

+97
-79
lines changed

8 files changed

+97
-79
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Please see [CONTRIBUTING.md](./CONTRIBUTING.md) on how to contribute to Cucumber
1010
## [Unreleased]
1111
### Added
1212
- New option for JUnit test suite name to be passed in `formatOptions` ([#2265](https://github.com/cucumber/cucumber-js/issues/2265))
13+
- Include source reference in emitted messages for parameter types ([#2287](https://github.com/cucumber/cucumber-js/pull/2287))
1314

1415
## [9.1.2] - 2023-05-07
1516
### Changed

package-lock.json

+31-26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,14 @@
207207
"node": "14 || 16 || 18 || 19"
208208
},
209209
"dependencies": {
210-
"@cucumber/ci-environment": "9.1.0",
211-
"@cucumber/cucumber-expressions": "16.1.1",
212-
"@cucumber/gherkin": "26.0.3",
210+
"@cucumber/ci-environment": "9.2.0",
211+
"@cucumber/cucumber-expressions": "16.1.2",
212+
"@cucumber/gherkin": "26.2.0",
213213
"@cucumber/gherkin-streams": "5.0.1",
214214
"@cucumber/gherkin-utils": "8.0.2",
215-
"@cucumber/html-formatter": "20.2.1",
215+
"@cucumber/html-formatter": "20.3.0",
216216
"@cucumber/message-streams": "4.0.1",
217-
"@cucumber/messages": "21.0.1",
217+
"@cucumber/messages": "22.0.0",
218218
"@cucumber/tag-expressions": "5.0.1",
219219
"assertion-error-formatter": "^3.0.0",
220220
"capital-case": "^1.0.4",
@@ -249,7 +249,7 @@
249249
"yup": "^0.32.11"
250250
},
251251
"devDependencies": {
252-
"@cucumber/compatibility-kit": "11.2.0",
252+
"@cucumber/compatibility-kit": "11.3.0",
253253
"@cucumber/query": "12.0.1",
254254
"@microsoft/api-documenter": "7.19.27",
255255
"@microsoft/api-extractor": "7.33.7",

src/cli/helpers.ts

+15-20
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import { ISupportCodeLibrary } from '../support_code_library_builder/types'
1313
import TestCaseHookDefinition from '../models/test_case_hook_definition'
1414
import TestRunHookDefinition from '../models/test_run_hook_definition'
1515
import { PickleOrder } from '../models/pickle_order'
16-
import { builtinParameterTypes } from '../support_code_library_builder'
1716
import { version } from '../version'
1817
import { ILogger } from '../logger'
18+
import { ILineAndUri } from '../types'
1919

2020
interface IParseGherkinMessageStreamRequest {
2121
cwd?: string
@@ -119,23 +119,33 @@ export async function emitMetaMessage(
119119
})
120120
}
121121

122+
const makeSourceReference = (source: ILineAndUri) => ({
123+
uri: source.uri,
124+
location: {
125+
line: source.line,
126+
},
127+
})
128+
122129
function emitParameterTypes(
123130
supportCodeLibrary: ISupportCodeLibrary,
124131
eventBroadcaster: EventEmitter,
125132
newId: IdGenerator.NewId
126133
): void {
127134
for (const parameterType of supportCodeLibrary.parameterTypeRegistry
128135
.parameterTypes) {
129-
if (builtinParameterTypes.includes(parameterType.name)) {
136+
if (parameterType.builtin) {
130137
continue
131138
}
139+
const source =
140+
supportCodeLibrary.parameterTypeRegistry.lookupSource(parameterType)
132141
const envelope: messages.Envelope = {
133142
parameterType: {
134143
id: newId(),
135144
name: parameterType.name,
136145
preferForRegularExpressionMatch: parameterType.preferForRegexpMatch,
137146
regularExpressions: parameterType.regexpStrings,
138147
useForSnippets: parameterType.useForSnippets,
148+
sourceReference: makeSourceReference(source),
139149
},
140150
}
141151
eventBroadcaster.emit('envelope', envelope)
@@ -169,12 +179,7 @@ function emitStepDefinitions(
169179
? messages.StepDefinitionPatternType.CUCUMBER_EXPRESSION
170180
: messages.StepDefinitionPatternType.REGULAR_EXPRESSION,
171181
},
172-
sourceReference: {
173-
uri: stepDefinition.uri,
174-
location: {
175-
line: stepDefinition.line,
176-
},
177-
},
182+
sourceReference: makeSourceReference(stepDefinition),
178183
},
179184
}
180185
eventBroadcaster.emit('envelope', envelope)
@@ -196,12 +201,7 @@ function emitTestCaseHooks(
196201
id: testCaseHookDefinition.id,
197202
name: testCaseHookDefinition.name,
198203
tagExpression: testCaseHookDefinition.tagExpression,
199-
sourceReference: {
200-
uri: testCaseHookDefinition.uri,
201-
location: {
202-
line: testCaseHookDefinition.line,
203-
},
204-
},
204+
sourceReference: makeSourceReference(testCaseHookDefinition),
205205
},
206206
}
207207
eventBroadcaster.emit('envelope', envelope)
@@ -221,12 +221,7 @@ function emitTestRunHooks(
221221
const envelope: messages.Envelope = {
222222
hook: {
223223
id: testRunHookDefinition.id,
224-
sourceReference: {
225-
uri: testRunHookDefinition.uri,
226-
location: {
227-
line: testRunHookDefinition.line,
228-
},
229-
},
224+
sourceReference: makeSourceReference(testRunHookDefinition),
230225
},
231226
}
232227
eventBroadcaster.emit('envelope', envelope)

src/cli/helpers_spec.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import StepDefinition from '../models/step_definition'
1616
import {
1717
CucumberExpression,
1818
ParameterType,
19-
ParameterTypeRegistry,
2019
RegularExpression,
2120
} from '@cucumber/cucumber-expressions'
2221
import { ISupportCodeLibrary } from '../support_code_library_builder/types'
2322
import TestCaseHookDefinition from '../models/test_case_hook_definition'
2423
import TestRunHookDefinition from '../models/test_run_hook_definition'
2524
import { PickleOrder } from '../models/pickle_order'
25+
import { SourcedParameterTypeRegistry } from '../support_code_library_builder/sourced_parameter_type_registry'
2626

2727
const noopFunction = (): void => {
2828
// no code
@@ -79,7 +79,7 @@ function testEmitSupportCodeMessages(
7979
afterTestCaseHookDefinitions: [],
8080
afterTestStepHookDefinitions: [],
8181
defaultTimeout: 0,
82-
parameterTypeRegistry: new ParameterTypeRegistry(),
82+
parameterTypeRegistry: new SourcedParameterTypeRegistry(),
8383
undefinedParameterTypes: [],
8484
World: null,
8585
parallelCanAssign: () => true,
@@ -105,16 +105,20 @@ describe('helpers', () => {
105105
})
106106
describe('emitSupportCodeMessages', () => {
107107
it('emits messages for parameter types', () => {
108-
const parameterTypeRegistry = new ParameterTypeRegistry()
109-
parameterTypeRegistry.defineParameterType(
108+
const parameterTypeRegistry = new SourcedParameterTypeRegistry()
109+
parameterTypeRegistry.defineSourcedParameterType(
110110
new ParameterType<string>(
111111
'flight',
112112
['([A-Z]{3})-([A-Z]{3})'],
113113
null,
114114
() => 'argh',
115115
true,
116116
false
117-
)
117+
),
118+
{
119+
line: 4,
120+
uri: 'features/support/parameter-types.js',
121+
}
118122
)
119123

120124
const envelopes = testEmitSupportCodeMessages({
@@ -129,6 +133,12 @@ describe('helpers', () => {
129133
preferForRegularExpressionMatch: false,
130134
regularExpressions: ['([A-Z]{3})-([A-Z]{3})'],
131135
useForSnippets: true,
136+
sourceReference: {
137+
uri: 'features/support/parameter-types.js',
138+
location: {
139+
line: 4,
140+
},
141+
},
132142
},
133143
},
134144
]
@@ -149,7 +159,7 @@ describe('helpers', () => {
149159
pattern: 'I have {int} cukes in my belly',
150160
expression: new CucumberExpression(
151161
'I have {int} cukes in my belly',
152-
new ParameterTypeRegistry()
162+
new SourcedParameterTypeRegistry()
153163
),
154164
}),
155165
],
@@ -188,7 +198,7 @@ describe('helpers', () => {
188198
pattern: /I have (\d+) cukes in my belly/,
189199
expression: new RegularExpression(
190200
/I have (\d+) cukes in my belly/,
191-
new ParameterTypeRegistry()
201+
new SourcedParameterTypeRegistry()
192202
),
193203
}),
194204
],

src/support_code_library_builder/index.ts

+5-18
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import arity from 'util-arity'
1212

1313
import {
1414
CucumberExpression,
15-
ParameterTypeRegistry,
1615
RegularExpression,
1716
} from '@cucumber/cucumber-expressions'
1817
import { doesHaveValue, doesNotHaveValue } from '../value_checker'
@@ -34,6 +33,7 @@ import {
3433
import World from './world'
3534
import { ICanonicalSupportCodeIds } from '../runtime/parallel/command_types'
3635
import { GherkinStepKeyword } from '../models/gherkin_step_keyword'
36+
import { SourcedParameterTypeRegistry } from './sourced_parameter_type_registry'
3737

3838
interface IStepDefinitionConfig {
3939
code: any
@@ -65,20 +65,6 @@ interface ITestRunHookDefinitionConfig {
6565
uri: string
6666
}
6767

68-
export const builtinParameterTypes = [
69-
'bigdecimal',
70-
'biginteger',
71-
'byte',
72-
'double',
73-
'float',
74-
'int',
75-
'long',
76-
'short',
77-
'string',
78-
'word',
79-
'',
80-
]
81-
8268
export class SupportCodeLibraryBuilder {
8369
public readonly methods: IDefineSupportCodeMethods
8470

@@ -93,7 +79,7 @@ export class SupportCodeLibraryBuilder {
9379
private defaultTimeout: number
9480
private definitionFunctionWrapper: any
9581
private newId: IdGenerator.NewId
96-
private parameterTypeRegistry: ParameterTypeRegistry
82+
private parameterTypeRegistry: SourcedParameterTypeRegistry
9783
private stepDefinitionConfigs: IStepDefinitionConfig[]
9884
private World: any
9985
private parallelCanAssign: ParallelAssignmentValidator
@@ -166,7 +152,8 @@ export class SupportCodeLibraryBuilder {
166152

167153
defineParameterType(options: IParameterTypeDefinition<any>): void {
168154
const parameterType = buildParameterType(options)
169-
this.parameterTypeRegistry.defineParameterType(parameterType)
155+
const source = getDefinitionLineAndUri(this.cwd)
156+
this.parameterTypeRegistry.defineSourcedParameterType(parameterType, source)
170157
}
171158

172159
defineStep(
@@ -481,7 +468,7 @@ export class SupportCodeLibraryBuilder {
481468
this.beforeTestStepHookDefinitionConfigs = []
482469
this.definitionFunctionWrapper = null
483470
this.defaultTimeout = 5000
484-
this.parameterTypeRegistry = new ParameterTypeRegistry()
471+
this.parameterTypeRegistry = new SourcedParameterTypeRegistry()
485472
this.stepDefinitionConfigs = []
486473
this.parallelCanAssign = () => true
487474
this.World = World

0 commit comments

Comments
 (0)