From 5d7848441bbb98dc27ac38fc8a8b598416ff0fd1 Mon Sep 17 00:00:00 2001 From: Finn Merlett Date: Thu, 11 Jul 2024 01:34:00 +0100 Subject: [PATCH 1/3] Expand javascript step definition queries to match compiled cjs style steps --- src/language/javascriptLanguage.ts | 25 +++++++++++++++++++ test/language/ExpressionBuilder.test.ts | 1 + .../testdata/javascript/StepDefinitions.js | 6 ++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/language/javascriptLanguage.ts b/src/language/javascriptLanguage.ts index 62a575d9..55943447 100644 --- a/src/language/javascriptLanguage.ts +++ b/src/language/javascriptLanguage.ts @@ -3,6 +3,31 @@ import { Language } from './types.js' export const javascriptLanguage: Language = { ...tsxLanguage, + defineStepDefinitionQueries: [ + ...tsxLanguage.defineStepDefinitionQueries, + // Matcher for compiled cjs format, with step definitions in the form: + // (0, some_cucumber_import.Given)("step title here", function...) + ` +(call_expression + function: (parenthesized_expression + (sequence_expression + (number) + (member_expression + property: (property_identifier) @function-name + ) + ) + ) + arguments: (arguments + [ + (string) @expression + (regex) @expression + (template_string) @expression + ] + ) + (#match? @function-name "Given|When|Then") +) @root +`, + ], defaultSnippetTemplate: ` {{ keyword }}('{{ expression }}', ({{ #parameters }}{{ #seenParameter }}, {{ /seenParameter }}{{ name }}{{ /parameters }}) => { // {{ blurb }} diff --git a/test/language/ExpressionBuilder.test.ts b/test/language/ExpressionBuilder.test.ts index 73766f6f..a61f3cd8 100644 --- a/test/language/ExpressionBuilder.test.ts +++ b/test/language/ExpressionBuilder.test.ts @@ -87,6 +87,7 @@ function defineContract(makeParserAdapter: () => ParserAdapter) { 'a {planet}', /^a regexp$/, "the bee's knees", + ...(languageName === 'javascript' ? ['a compiled format'] : []), ]) assert.deepStrictEqual(errors, [ 'There is already a parameter type with name int', diff --git a/test/language/testdata/javascript/StepDefinitions.js b/test/language/testdata/javascript/StepDefinitions.js index 34dc08bc..1986fcbc 100644 --- a/test/language/testdata/javascript/StepDefinitions.js +++ b/test/language/testdata/javascript/StepDefinitions.js @@ -1,4 +1,4 @@ -import { Given } from '@cucumber/cucumber' +import cucumber, { Given } from '@cucumber/cucumber' import assert from 'assert' import React from 'react' @@ -27,3 +27,7 @@ Given('an {undefined-parameter}', async function (date) { Given("the bee's knees", async function () { assert(true) }) + +;(0, cucumber.Given)('a compiled format', async function () { + assert(true) +}) From 1e38ca68b753a4460c866410f3467bc63d9810c9 Mon Sep 17 00:00:00 2001 From: Finn Merlett Date: Thu, 11 Jul 2024 02:08:45 +0100 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea434ff9..afd7f488 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added +- (Javascript) Support for compiled cjs style step definitions ([#222](https://github.com/cucumber/language-service/pull/222)) + ### Fixed - (Ruby) Support `And` and `But` step definition annotations ([#211](https://github.com/cucumber/language-service/pull/211)) - (Python) Title variants of Behave's decorators (`Step`, `Given`, `When`, `Then`) ([#213](https://github.com/cucumber/language-service/pull/213)) From e90d0b4149e8695c623a583f4c18f7e772fd4776 Mon Sep 17 00:00:00 2001 From: Kieran Ryan Date: Sun, 14 Jul 2024 18:14:13 +0100 Subject: [PATCH 3/3] Direct import of gherkin keywords with compiled cjs --- src/language/javascriptLanguage.ts | 56 ++++++++++++------- .../testdata/javascript/StepDefinitions.js | 2 +- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/language/javascriptLanguage.ts b/src/language/javascriptLanguage.ts index 55943447..da2a23d7 100644 --- a/src/language/javascriptLanguage.ts +++ b/src/language/javascriptLanguage.ts @@ -5,28 +5,42 @@ export const javascriptLanguage: Language = { ...tsxLanguage, defineStepDefinitionQueries: [ ...tsxLanguage.defineStepDefinitionQueries, - // Matcher for compiled cjs format, with step definitions in the form: - // (0, some_cucumber_import.Given)("step title here", function...) - ` -(call_expression - function: (parenthesized_expression - (sequence_expression - (number) - (member_expression - property: (property_identifier) @function-name + // Compiled cjs step definitions of the format: + // (0, some_cucumber_import.Given)("step pattern here", function...) + `(call_expression + function: (parenthesized_expression + (sequence_expression + (member_expression + property: (property_identifier) @function-name + ) + ) ) - ) - ) - arguments: (arguments - [ - (string) @expression - (regex) @expression - (template_string) @expression - ] - ) - (#match? @function-name "Given|When|Then") -) @root -`, + arguments: (arguments + [ + (string) @expression + (regex) @expression + (template_string) @expression + ] + ) + (#match? @function-name "Given|When|Then") + ) @root`, + // Compiled cjs step definitions of the format: + // (0, Given)("step pattern here", function...) + `(call_expression + function: (parenthesized_expression + (sequence_expression + (identifier) @function-name + ) + ) + arguments: (arguments + [ + (string) @expression + (regex) @expression + (template_string) @expression + ] + ) + (#match? @function-name "Given|When|Then") + ) @root`, ], defaultSnippetTemplate: ` {{ keyword }}('{{ expression }}', ({{ #parameters }}{{ #seenParameter }}, {{ /seenParameter }}{{ name }}{{ /parameters }}) => { diff --git a/test/language/testdata/javascript/StepDefinitions.js b/test/language/testdata/javascript/StepDefinitions.js index 1986fcbc..d70f816a 100644 --- a/test/language/testdata/javascript/StepDefinitions.js +++ b/test/language/testdata/javascript/StepDefinitions.js @@ -24,7 +24,7 @@ Given('an {undefined-parameter}', async function (date) { assert(date) }) -Given("the bee's knees", async function () { +;(0, Given)("the bee's knees", async function () { assert(true) })