diff --git a/lib/config/default-config.js b/lib/config/default-config.js index 8529d455af8..a655a6d83ca 100644 --- a/lib/config/default-config.js +++ b/lib/config/default-config.js @@ -46,9 +46,16 @@ exports.defaultConfig = [ ".git/**" ], languageOptions: { - sourceType: "script", + ecmaVersion: "latest", + sourceType: "module", parser: "@/espree", parserOptions: {} } + }, + { + files: ["**/*.cjs"], + languageOptions: { + sourceType: "commonjs" + } } ]; diff --git a/tests/lib/linter/linter.js b/tests/lib/linter/linter.js index f7d2f0cf26a..25a80672935 100644 --- a/tests/lib/linter/linter.js +++ b/tests/lib/linter/linter.js @@ -6305,7 +6305,8 @@ describe("Linter with FlatConfigArray", () => { it("should error when accessing a global that isn't available in ecmaVersion 5", () => { const messages = linter.verify("new Map()", { languageOptions: { - ecmaVersion: 5 + ecmaVersion: 5, + sourceType: "script" }, rules: { "no-undef": "error" @@ -6319,7 +6320,8 @@ describe("Linter with FlatConfigArray", () => { it("should error when accessing a global that isn't available in ecmaVersion 3", () => { const messages = linter.verify("JSON.stringify({})", { languageOptions: { - ecmaVersion: 3 + ecmaVersion: 3, + sourceType: "script" }, rules: { "no-undef": "error" @@ -6377,6 +6379,27 @@ describe("Linter with FlatConfigArray", () => { linter.verify("foo", config, filename); }); + it("ecmaVersion should be normalized to to latest year by default", () => { + const config = { + plugins: { + test: { + rules: { + checker(context) { + return { + Program() { + assert.strictEqual(context.languageOptions.ecmaVersion, espree.latestEcmaVersion + 2009); + } + }; + } + } + } + }, + rules: { "test/checker": "error" } + }; + + linter.verify("foo", config, filename); + }); + it("ecmaVersion should not be normalized to year name for ES 5", () => { const config = { plugins: { @@ -6430,6 +6453,49 @@ describe("Linter with FlatConfigArray", () => { describe("sourceType", () => { + it("should be module by default", () => { + const config = { + plugins: { + test: { + rules: { + checker(context) { + return { + Program() { + assert.strictEqual(context.languageOptions.sourceType, "module"); + } + }; + } + } + } + }, + rules: { "test/checker": "error" } + }; + + linter.verify("import foo from 'bar'", config, filename); + }); + + it("should default to commonjs when passed a .cjs filename", () => { + const config = { + plugins: { + test: { + rules: { + checker(context) { + return { + Program() { + assert.strictEqual(context.languageOptions.sourceType, "commonjs"); + } + }; + } + } + } + }, + rules: { "test/checker": "error" } + }; + + linter.verify("import foo from 'bar'", config, `${filename}.cjs`); + }); + + it("should error when import is used in a script", () => { const messages = linter.verify("import foo from 'bar';", { languageOptions: { @@ -7460,6 +7526,7 @@ describe("Linter with FlatConfigArray", () => { }); describe("context.markVariableAsUsed()", () => { + it("should mark variables in current scope as used", () => { const code = "var a = 1, b = 2;"; let spy; @@ -7483,6 +7550,9 @@ describe("Linter with FlatConfigArray", () => { } } }, + languageOptions: { + sourceType: "script" + }, rules: { "test/checker": "error" } }; @@ -7544,6 +7614,9 @@ describe("Linter with FlatConfigArray", () => { } } }, + languageOptions: { + sourceType: "script" + }, rules: { "test/checker": "error" } };