gherkin-lint icon indicating copy to clipboard operation
gherkin-lint copied to clipboard

Using Rule Keyword Fails

Open treyhendon opened this issue 5 years ago • 5 comments

We use the keyword Rule in some of our features This is causing the following exception.

(node:9584) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'location' of null
    at C:\git\myapp\node_modules\gherkin-lint\dist\rules\indentation.js:83:27
    at Array.forEach (<anonymous>)
    at Object.run (C:\git\myapp\node_modules\gherkin-lint\dist\rules\indentation.js:78:20)
    at C:\git\myapp\node_modules\gherkin-lint\dist\rules.js:46:26
    at Array.forEach (<anonymous>)
    at Object.runAllEnabledRules (C:\git\myapp\node_modules\gherkin-lint\dist\rules.js:41:22)
    at readAndParseFile.then.perFileErrors (C:\git\myapp\node_modules\gherkin-lint\dist\linter.js:74:29)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Promise.all (index 1)
(node:9584) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:9584) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.      

When I open rules\indentation.js and modify:

    if (child.background) {
      test(child.background.location, 'Background');
      child.background.steps.forEach(testStep);
    } else {
      test(child.scenario.location, 'Scenario');
      testTags(child.scenario.tags, 'scenario tag');
      child.scenario.steps.forEach(testStep);
      child.scenario.examples.forEach(examples => {
        test(examples.location, 'Examples');

        if (examples.tableHeader) {
          test(examples.tableHeader.location, 'example');
          examples.tableBody.forEach(row => {
            test(row.location, 'example');
          });
        }
      });
    }

to be:

    if (child.background) {
      test(child.background.location, 'Background');
      child.background.steps.forEach(testStep);
    }  else if (child.rule) {
      console.log("Rule Tag");
    } else {
      test(child.scenario.location, 'Scenario');
      testTags(child.scenario.tags, 'scenario tag');
      child.scenario.steps.forEach(testStep);
      child.scenario.examples.forEach(examples => {
        test(examples.location, 'Examples');

        if (examples.tableHeader) {
          test(examples.tableHeader.location, 'example');
          examples.tableBody.forEach(row => {
            test(row.location, 'example');
          });
        }
      });
    }

Then I see Rule Tag logged, no exceptions, but no linting on these files.

I would propose support of the Rule keyword or a skip option if test(child.scenario.location, 'Scenario'); fails.

treyhendon avatar Sep 09 '20 19:09 treyhendon

Thanks @treyhendon for the fix. However, I am in the latest version 4.2.1 but still have rule relevant error.

In my case, keywords-in-logical-order throws below error when I have Rule in a feature file.

(node:5206) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'steps' of null
    at /Volumes/Dev/sdp-nightwatch-2/node_modules/gherkin-lint/dist/rules/keywords-in-logical-order.js:17:10
    at Array.forEach (<anonymous>)
    at Object.run (/Volumes/Dev/sdp-nightwatch-2/node_modules/gherkin-lint/dist/rules/keywords-in-logical-order.js:13:20)
    at /Volumes/Dev/sdp-nightwatch-2/node_modules/gherkin-lint/dist/rules.js:46:26
    at Array.forEach (<anonymous>)
    at Object.runAllEnabledRules (/Volumes/Dev/sdp-nightwatch-2/node_modules/gherkin-lint/dist/rules.js:41:22)
    at readAndParseFile.then.perFileErrors (/Volumes/Dev/sdp-nightwatch-2/node_modules/gherkin-lint/dist/linter.js:74:29)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Promise.all (index 5)

tim-yao avatar May 26 '21 01:05 tim-yao

Also, Rule will cause no-files-without-scenarios fail:

Feature file does not have any Scenarios    no-files-without-scenarios

Basically that means, most of lint won't work if I use Rule in my feature.

I am happy to add a pull request but if someone can give me a guide. Thanks!

tim-yao avatar May 26 '21 01:05 tim-yao

@tim-yao would you mind opening a new issue for the no-files-without-scenarios problem? While the fix for the original issue is incomplete, it's still a good fix. And it's already merged. It's much easier to track as a separate issue. Once you open it, I might be able to make a suggestion there as to how to approach addressing it.

dsm2005 avatar Jun 21 '21 22:06 dsm2005

@tim-yao I created Issue #254 for the no-files-without-scenarios problem. If you're okay tracking that issue there, it probably makes sense to close this issue.

dsm2005 avatar Jun 22 '21 05:06 dsm2005

Thanks @dsm2005 , I will update the issue with keywords-in-logical-order issue as well.

tim-yao avatar Jun 22 '21 23:06 tim-yao