cucumber / language-service

Cucumber Language Service
MIT License
12 stars 22 forks source link

Parsing error for step definition written in Java #111

Open DanielSCy opened 1 year ago

DanielSCy commented 1 year ago

👓 What did you see?

I have below step defined in my steps file:

@Given("I have a menu item with name \"([^\"]+)\" and price (\\d+)")

The \ before " is required by Java but the language server should only see ". Also, when I run the tests everything is well, so it's not an issue with the definition of the step or the feature file. However, the language server gives the below exception and the step is not recognized in the feature file:

[Error - 5:03:40 PM] * Step Definition errors: Error: This Cucumber Expression has a problem at column 31:

I have a menu item with name \"([^\"]+)\" and price (\d+)
                              ^
Only the characters '{', '}', '(', ')', '\', '/' and whitespace can be escaped.
If you did mean to use an '\' you can use '\\' to escape it
    at RT (c:\Users\dstanciu\.vscode\extensions\cucumberopen.cucumber-official-1.5.1\out\extension.js:67:79988)
    at d (c:\Users\dstanciu\.vscode\extensions\cucumberopen.cucumber-official-1.5.1\out\extension.js:84:501)
    at c:\Users\dstanciu\.vscode\extensions\cucumberopen.cucumber-official-1.5.1\out\extension.js:84:702
    at Array.forEach (<anonymous>)
    at ac.tokenize (c:\Users\dstanciu\.vscode\extensions\cucumberopen.cucumber-official-1.5.1\out\extension.js:84:636)
    at uc.parse (c:\Users\dstanciu\.vscode\extensions\cucumberopen.cucumber-official-1.5.1\out\extension.js:84:2357)
    at new ji (c:\Users\dstanciu\.vscode\extensions\cucumberopen.cucumber-official-1.5.1\out\extension.js:84:5199)
    at ga.createExpression (c:\Users\dstanciu\.vscode\extensions\cucumberopen.cucumber-official-1.5.1\out\extension.js:84:11717)
    at c:\Users\dstanciu\.vscode\extensions\cucumberopen.cucumber-official-1.5.1\out\extension.js:427:748
    at $f.eachStepDefinitionExpression (c:\Users\dstanciu\.vscode\extensions\cucumberopen.cucumber-official-1.5.1\out\extension.js:424:1769)
    at Wf.build (c:\Users\dstanciu\.vscode\extensions\cucumberopen.cucumber-official-1.5.1\out\extension.js:427:695)
    at Sc.<anonymous> (c:\Users\dstanciu\.vscode\extensions\cucumberopen.cucumber-official-1.5.1\out\extension.js:433:19176)
    at Generator.next (<anonymous>)
    at u (c:\Users\dstanciu\.vscode\extensions\cucumberopen.cucumber-official-1.5.1\out\extension.js:433:11215)
    at process.processTicksAndRejections (node:internal/process/task_queues:96:5)

✅ What did you expect to see?

The step definition would be parsed without error.

📦 Which tool/library version are you using?

I am running VSCode with cucumber extension 1,5,1 as seen also in the stack trace.

🔬 How could we reproduce it?

Steps to reproduce the behavior:

  1. Install VSCode with Cucumber extension version '1.5.1'
  2. Create a Java based Cucumber project, with one feature and one step definition. Use \" in the step definition to allow " characters
  3. Create a features file using that step.
  4. The step is not recognized and the 'Cucumber Server Language' output window shows the exception mentioned above.

aslakhellesoy commented 1 year ago

Hi @DanielSCy thanks for reporting this. It sounds like we need to handle escapes better in unescapeString: https://github.com/cucumber/language-service/blob/3bfbad6c7e323bc917f231519b6e78b82b655d88/src/language/javaLanguage.ts#L126

Currently it's only escaping \\ back to \, but it should handle all Java String escapes. A test for this should be added to https://github.com/cucumber/language-service/blob/main/test/language/javaLanguage.test.ts

We might take inspiration from https://github.com/iamakulov/unescape-js (I don't want to depend on the library which is unlikely to be maintained).

There are also some more details about this in this SO post

Would you be able to help with a pull request with this @DanielSCy?