S6759 [prefer-read-only-props] Failing on return outside function

Hi there,

Take this file:

const fs = require('fs');
const path = require('path');

const certOutputBase = path.resolve('out', 'cert');

if (fs.existsSync(certOutputBase)) {
    return;
}

/** do something */

Linting this file will fail with the following error message:

TypeError: Cannot read properties of undefined (reading 'returns')
Occurred while linting /gen/out/module/scripts/download-cert/index.js:37
Rule: "sonarjs/prefer-read-only-props"
at ReturnStatement (/gen/out/module/node_modules/eslint-plugin-sonarjs/cjs/S6759/rule.js:81:51)
at ruleErrorHandler (/gen/out/module/node_modules/eslint/lib/linter/linter.js:1098:48)
at /gen/out/module/node_modules/eslint/lib/linter/safe-emitter.js:45:58
at Array.forEach (<anonymous>)
at Object.emit (/gen/out/module/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
at NodeEventGenerator.applySelector (/gen/out/module/node_modules/eslint/lib/linter/node-event-generator.js:297:26)
at NodeEventGenerator.applySelectors (/gen/out/module/node_modules/eslint/lib/linter/node-event-generator.js:326:22)
at NodeEventGenerator.enterNode (/gen/out/module/node_modules/eslint/lib/linter/node-event-generator.js:337:14)
at runRules (/gen/out/module/node_modules/eslint/lib/linter/linter.js:1142:40)
at #flatVerifyWithoutProcessors (/gen/out/module/node_modules/eslint/lib/linter/linter.js:1911:31)

It seems the rule can’t handle a return statement outside of a function.

Hi,

What version of SonarQube Server / Community Build are you using?

 
Thx,
Ann

Hi Ann,

Thanks for your reply. I’m afraid I’m not following, I’m using the latest version, 3.0.0, of eslint-plugin-sonarjs.

1 Like

Hi,

Thanks for the clarification. That’s what I needed to know: that you’re using the latest implementation of the rule. I’ll flag this for the language experts.

 
Ann

1 Like

Hello ej612,

Thank you for the feedback.

I have opened an issue to track this: JS-487

1 Like