Skip to content

Commit

Permalink
Update defaults for flat config (refs #14588)
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Nov 12, 2021
1 parent cee5632 commit ac3f270
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/config/default-config.js
Expand Up @@ -46,9 +46,16 @@ exports.defaultConfig = [
".git/**"
],
languageOptions: {
sourceType: "script",
ecmaVersion: "latest",
sourceType: "module",
parser: "@/espree",
parserOptions: {}
}
},
{
files: ["**/*.cjs"],
languageOptions: {
sourceType: "commonjs"
}
}
];
77 changes: 75 additions & 2 deletions tests/lib/linter/linter.js
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
Expand All @@ -7483,6 +7550,9 @@ describe("Linter with FlatConfigArray", () => {
}
}
},
languageOptions: {
sourceType: "script"
},
rules: { "test/checker": "error" }
};

Expand Down Expand Up @@ -7544,6 +7614,9 @@ describe("Linter with FlatConfigArray", () => {
}
}
},
languageOptions: {
sourceType: "script"
},
rules: { "test/checker": "error" }
};

Expand Down

0 comments on commit ac3f270

Please sign in to comment.