Skip to content
Permalink
Browse files
feat: account for sourceType: "commonjs" in the strict rule (#16308)
Fixes #16304
  • Loading branch information
mdjermanovic committed Sep 16, 2022
1 parent b0d72c9 commit 1729f9ea4d7b2945b2b701d72027fd4aace954cf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
@@ -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";
}

/**
@@ -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
@@ -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.