cucumber / vscode

Official Visual Studio Code Extension for Cucumber
MIT License
66 stars 17 forks source link

Reference variables in parameter type regular expressions #167

Open MiladSadinam opened 1 year ago

MiladSadinam commented 1 year ago

šŸ¤” What's the problem you're trying to solve?

We have some large parameter-types which we would like to use both in *.feature files and also in our typescript code. Small example how we have to do it currently (some of our types are much, much larger):

defineParameterType({
    name: 'MyType',
    regexp: /"(Value1|Value2|Value3)"/,
    transformer: (s) => s,
});

type MyType = 'Value1' | 'Value2' | 'Value3';

As you can see, we need to duplicate all type values.

āœØ What's your proposed solution?

We tried a solution like this:

const typeValues = [ 'Value1', 'Value2', 'Value3' ];

defineParameterType({
    name: 'MyType',
    regexp: new RegExp(`"(${typeValues.join('|')})"`),
    transformer: (s) => s,
});

type MyType = Exclude<typeof typeValues[number], undefined>;

Here the values are only defined once in typeValues without needing to repeat them. The CucumberJS runner also runs the scenario fine. But, the VSCode extension does not recognize the step anymore and marks it as not existing.

Is it possible that defineParameterType() can accept a variable for the regexp parameter?

ā› Have you considered any alternatives or workarounds?

-

šŸ“š Any additional context?

-