Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multilanguage support to step definitions #153

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/language/csharpLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StringOrRegExp } from '@cucumber/cucumber-expressions'

import { childrenToString, NO_QUOTES } from './helpers.js'
import { childrenToString, functionNames, NO_QUOTES } from './helpers.js'
import { Language, TreeSitterSyntaxNode } from './types.js'

export const csharpLanguage: Language = {
Expand Down Expand Up @@ -77,7 +77,7 @@ export const csharpLanguage: Language = {
)
)
)
(#match? @annotation-name "Given|When|Then|And|But|StepDefinition")
(#match? @annotation-name "${functionNames}|And|But|StepDefinition")
) @root
`,
`
Expand All @@ -92,7 +92,7 @@ export const csharpLanguage: Language = {
)
)
)
(#match? @annotation-name "Given|When|Then|And|But|StepDefinition")
(#match? @annotation-name "${functionNames}|And|But|StepDefinition")
) @root
`,
],
Expand Down
7 changes: 7 additions & 0 deletions src/language/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ParameterType, RegExps } from '@cucumber/cucumber-expressions'
import { dialects } from '@cucumber/gherkin'
import { DocumentUri, LocationLink, Range } from 'vscode-languageserver-types'

import { Link, NodePredicate, TreeSitterQueryMatch, TreeSitterSyntaxNode } from './types'
Expand Down Expand Up @@ -70,3 +71,9 @@ export function filter(
function flatten(node: TreeSitterSyntaxNode): TreeSitterSyntaxNode[] {
return node.children.reduce((r, o) => [...r, ...flatten(o)], [node])
}

export const functionNames = Object.values(dialects)
.flatMap((dialect) => [...dialect.given, ...dialect.when, ...dialect.then])
.map((keyword) => keyword.trim())
.filter((keyword) => keyword !== '*')
.join('|')
3 changes: 2 additions & 1 deletion src/language/javaLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { functionNames } from './helpers.js'
import { Language, TreeSitterSyntaxNode } from './types.js'

export const javaLanguage: Language = {
Expand Down Expand Up @@ -90,7 +91,7 @@ export const javaLanguage: Language = {
)
)
)
(#match? @annotation-name "Given|When|Then")
(#match? @annotation-name "${functionNames}")
) @root
`,
],
Expand Down
3 changes: 2 additions & 1 deletion src/language/phpLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { functionNames } from './helpers.js'
import { Language } from './types.js'

export const phpLanguage: Language = {
Expand All @@ -20,7 +21,7 @@ export const phpLanguage: Language = {
`
(
(comment)+ @expression
(#match? @expression "Given|When|Then")
(#match? @expression "${functionNames}")
) @root
`,
],
Expand Down
3 changes: 2 additions & 1 deletion src/language/pythonLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { StringOrRegExp } from '@cucumber/cucumber-expressions'

import { functionNames } from './helpers.js'
import { Language, TreeSitterSyntaxNode } from './types.js'

export const pythonLanguage: Language = {
Expand Down Expand Up @@ -72,7 +73,7 @@ export const pythonLanguage: Language = {
arguments: (argument_list (string) @expression)
)
)
(#match? @method "(given|when|then)")
(#match? @method "(${functionNames.toLowerCase()})")
) @root
`,
],
Expand Down
4 changes: 2 additions & 2 deletions src/language/rubyLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StringOrRegExp } from '@cucumber/cucumber-expressions'
import { RegExps } from '@cucumber/cucumber-expressions/dist/cjs/src/ParameterType'

import { childrenToString, filter, NO_QUOTES } from './helpers.js'
import { childrenToString, filter, functionNames, NO_QUOTES } from './helpers.js'
import { Language, NodePredicate, TreeSitterSyntaxNode } from './types.js'

export const rubyLanguage: Language = {
Expand Down Expand Up @@ -76,7 +76,7 @@ export const rubyLanguage: Language = {
(regex) @expression
]
)
(#match? @method "(Given|When|Then)$")
(#match? @method "(${functionNames})$")
) @root
`,
],
Expand Down
3 changes: 2 additions & 1 deletion src/language/rustLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { functionNames } from './helpers.js'
import { Language, TreeSitterSyntaxNode } from './types.js'

export const rustLanguage: Language = {
Expand Down Expand Up @@ -82,7 +83,7 @@ export const rustLanguage: Language = {
)
)
)
(#match? @meta-name "given|when|then")
(#match? @meta-name "${functionNames.toLowerCase()}")
)
(function_item) ) @root
`,
Expand Down
4 changes: 2 additions & 2 deletions src/language/tsxLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StringOrRegExp } from '@cucumber/cucumber-expressions'
import { RegExps } from '@cucumber/cucumber-expressions'

import { childrenToString, filter, NO_QUOTES } from './helpers.js'
import { childrenToString, filter, functionNames, NO_QUOTES } from './helpers.js'
import { NO_EXPRESSION } from './SourceAnalyzer.js'
import { Language, TreeSitterSyntaxNode } from './types.js'

Expand Down Expand Up @@ -80,7 +80,7 @@ export const tsxLanguage: Language = {
(template_string) @expression
]
)
(#match? @function-name "Given|When|Then")
(#match? @function-name "${functionNames}")
) @root
`,
],
Expand Down
4 changes: 3 additions & 1 deletion test/language/testdata/javascript/StepDefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Given } from '@cucumber/cucumber'
import assert from 'assert'
import React from 'react'

const Gegeven = Given;

const dummyJsx = <span>Hello</span>

Given('a {uuid}', async function (uuid) {
Expand All @@ -24,6 +26,6 @@ Given('an {undefined-parameter}', async function (date) {
assert(date)
})

Given("the bee's knees", async function () {
Gegeven("the bee's knees", async function () {
assert(true)
})
Loading