Skip to content

Commit

Permalink
feat: account for sourceType: "commonjs" in the strict rule (#16308)
Browse files Browse the repository at this point in the history
Fixes #16304
  • Loading branch information
mdjermanovic committed Sep 16, 2022
1 parent b0d72c9 commit 1729f9e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rules/strict.js
Expand Up @@ -105,7 +105,7 @@ module.exports = {
if (ecmaFeatures.impliedStrict) {
mode = "implied";
} else if (mode === "safe") {
mode = ecmaFeatures.globalReturn ? "global" : "function";
mode = ecmaFeatures.globalReturn || context.languageOptions.sourceType === "commonjs" ? "global" : "function";
}

/**
Expand Down
46 changes: 45 additions & 1 deletion tests/lib/rules/strict.js
Expand Up @@ -10,7 +10,8 @@
//------------------------------------------------------------------------------

const rule = require("../../../lib/rules/strict"),
{ RuleTester } = require("../../../lib/rule-tester");
{ RuleTester } = require("../../../lib/rule-tester"),
FlatRuleTester = require("../../../lib/rule-tester/flat-rule-tester");

//------------------------------------------------------------------------------
// Tests
Expand Down Expand Up @@ -659,3 +660,46 @@ ruleTester.run("strict", rule, {
}
]
});

const flatRuleTester = new FlatRuleTester();

// TODO: merge these tests into `ruleTester.run` once we switch to FlatRuleTester (when FlatRuleTester becomes RuleTester).
flatRuleTester.run("strict", rule, {
valid: [
{
code: "'use strict'; module.exports = function identity (value) { return value; }",
languageOptions: {
sourceType: "commonjs"
}
},
{
code: "'use strict'; module.exports = function identity (value) { return value; }",
options: ["safe"],
languageOptions: {
sourceType: "commonjs"
}
}
],

invalid: [
{
code: "module.exports = function identity (value) { return value; }",
options: ["safe"],
languageOptions: {
sourceType: "commonjs"
},
errors: [
{ messageId: "global", line: 1 }
]
},
{
code: "module.exports = function identity (value) { return value; }",
languageOptions: {
sourceType: "commonjs"
},
errors: [
{ messageId: "global", line: 1 }
]
}
]
});

0 comments on commit 1729f9e

Please sign in to comment.