From 1157ad9e79c60b2f4155f686e279b66f6f229a2f Mon Sep 17 00:00:00 2001 From: "M.P. Korstanje" Date: Thu, 4 Sep 2025 13:05:35 +0200 Subject: [PATCH 1/4] Use RegExp.source to represent regular expression in messages The forward slash is used to denote the start and end of the regex expression in TypeScript, but isn't part of the regex expression itself. Fixes: https://github.com/cucumber/compatibility-kit/issues/153 --- .gitignore | 3 ++- src/SupportCodeBuilderImpl.ts | 2 +- src/buildSupportCode.spec.ts | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index d343be5..1f83da1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ coverage dist node_modules tmp -tsdoc-metadata.json \ No newline at end of file +tsdoc-metadata.json +.idea/ \ No newline at end of file diff --git a/src/SupportCodeBuilderImpl.ts b/src/SupportCodeBuilderImpl.ts index 348a2ef..a683692 100644 --- a/src/SupportCodeBuilderImpl.ts +++ b/src/SupportCodeBuilderImpl.ts @@ -131,7 +131,7 @@ export class SupportCodeBuilderImpl implements SupportCodeBuilder { this.expression.compiled instanceof CucumberExpression ? StepDefinitionPatternType.CUCUMBER_EXPRESSION : StepDefinitionPatternType.REGULAR_EXPRESSION, - source: pattern.toString(), + source: pattern instanceof RegExp ? pattern.source : pattern, }, sourceReference, } diff --git a/src/buildSupportCode.spec.ts b/src/buildSupportCode.spec.ts index be03ab7..3150ac8 100644 --- a/src/buildSupportCode.spec.ts +++ b/src/buildSupportCode.spec.ts @@ -226,7 +226,7 @@ describe('buildSupportCode', () => { stepDefinition: { id: '0', pattern: { - source: '/there are (\\d+) widgets/', + source: 'there are (\\d+) widgets', type: StepDefinitionPatternType.REGULAR_EXPRESSION, }, sourceReference: { From 4ea99eac8647ce5ebc98eb43cfecc8eed4965a46 Mon Sep 17 00:00:00 2001 From: "M.P. Korstanje" Date: Thu, 4 Sep 2025 13:14:13 +0200 Subject: [PATCH 2/4] Update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb84ac8..1e169c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Use RegExp.source to represent regular expression in messages ([#12](https://github.com/cucumber/javascript-core/pull/12)) ## [0.4.0] - 2025-08-26 ### Added From 6d66be6e9eb374c8ab68437683544769f493a2c0 Mon Sep 17 00:00:00 2001 From: David Goss Date: Fri, 5 Sep 2025 14:44:49 +0100 Subject: [PATCH 3/4] include full source if flags are used --- src/SupportCodeBuilderImpl.ts | 10 +++++++++- src/buildSupportCode.spec.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/SupportCodeBuilderImpl.ts b/src/SupportCodeBuilderImpl.ts index a683692..22d06db 100644 --- a/src/SupportCodeBuilderImpl.ts +++ b/src/SupportCodeBuilderImpl.ts @@ -115,6 +115,7 @@ export class SupportCodeBuilderImpl implements SupportCodeBuilder { if (!compiled) { return undefined } + const source = this.extractPatternSource(pattern) return { id, expression: { @@ -131,7 +132,7 @@ export class SupportCodeBuilderImpl implements SupportCodeBuilder { this.expression.compiled instanceof CucumberExpression ? StepDefinitionPatternType.CUCUMBER_EXPRESSION : StepDefinitionPatternType.REGULAR_EXPRESSION, - source: pattern instanceof RegExp ? pattern.source : pattern, + source, }, sourceReference, } @@ -141,6 +142,13 @@ export class SupportCodeBuilderImpl implements SupportCodeBuilder { .filter((step) => !!step) } + private extractPatternSource(pattern: string | RegExp) { + if (pattern instanceof RegExp) { + return pattern.flags ? pattern.toString() : pattern.source + } + return pattern + } + private compileExpression( text: string | RegExp ): CucumberExpression | RegularExpression | undefined { diff --git a/src/buildSupportCode.spec.ts b/src/buildSupportCode.spec.ts index 3150ac8..a1fbf79 100644 --- a/src/buildSupportCode.spec.ts +++ b/src/buildSupportCode.spec.ts @@ -240,6 +240,40 @@ describe('buildSupportCode', () => { }, ]) }) + + it('handles regular expressions with flags', () => { + const library = buildSupportCode({ newId }) + .step({ + pattern: /there are (\d+) widgets/i, + fn: sinon.stub(), + sourceReference: { uri: 'steps.js', location: { line: 1, column: 1 } }, + }) + .build() + const matchedSteps = library.findAllStepsBy('there ARE 17 widgets') + + expect(matchedSteps.length).to.eq(1) + expect(matchedSteps[0].def.expression.compiled).to.be.instanceof(RegularExpression) + expect(matchedSteps[0].args.length).to.eq(1) + expect(matchedSteps[0].args[0].getValue(undefined)).to.eq(17) + expect(library.toEnvelopes()).to.deep.eq([ + { + stepDefinition: { + id: '0', + pattern: { + source: '/there are (\\d+) widgets/i', + type: StepDefinitionPatternType.REGULAR_EXPRESSION, + }, + sourceReference: { + location: { + column: 1, + line: 1, + }, + uri: 'steps.js', + }, + }, + }, + ]) + }) }) describe('parameter types', () => { From 69cee40a8dab35e7f091ae53278a4b26110fb187 Mon Sep 17 00:00:00 2001 From: David Goss Date: Fri, 5 Sep 2025 15:46:50 +0100 Subject: [PATCH 4/4] tool-specific ignores should be user level --- .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 1f83da1..d343be5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,4 @@ coverage dist node_modules tmp -tsdoc-metadata.json -.idea/ \ No newline at end of file +tsdoc-metadata.json \ No newline at end of file