Skip to content

Commit f6c0ed0

Browse files
authored
feat!: report eslint-env comments as errors (#20128)
* feat!: report `eslint-env` comments as errors * add migration instructions
1 parent d972098 commit f6c0ed0

8 files changed

Lines changed: 78 additions & 30 deletions

File tree

‎docs/src/use/configure/migration-guide.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ You'll need to install the `globals` package from npm for this example to work.
369369
### `eslint-env` Configuration Comments
370370

371371
In the eslintrc config system it was possible to use `eslint-env` configuration comments to define globals for a file.
372-
These comments are no longer recognized when linting with flat config: in a future version of ESLint, `eslint-env` comments will be reported as errors.
372+
These comments are no longer recognized when linting with flat config and will be reported as errors.
373373
For this reason, when migrating from eslintrc to flat config, `eslint-env` configuration comments should be removed from all files.
374374
They can be either replaced with equivalent but more verbose `global` configuration comments, or dropped in favor of `globals` definitions in the config file.
375375

‎docs/src/use/migrate-to-10.0.0.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The lists below are ordered roughly by the number of users each change is expect
2020
- [`no-shadow-restricted-names` now reports `globalThis` by default](#no-shadow-restricted-names)
2121
- [`eslint:recommended` has been updated](#eslint-recommended)
2222
- [Jiti < v2.2.0 are no longer supported](#drop-old-jiti)
23+
- [`eslint-env` comments are reported as errors](#eslint-env-comments)
2324

2425
### Breaking changes for plugin developers
2526

@@ -114,3 +115,17 @@ In ESLint v10, the deprecated `nodeType` property on `LintMessage` objects has b
114115
**To address:** Remove all usages of `message.nodeType` in your integrations and formatters.
115116

116117
**Related issue(s):** [#19029](https://github.com/eslint/eslint/issues/19029)
118+
119+
## <a name="eslint-env-comments"></a> `eslint-env` comments are reported as errors
120+
121+
In the now obsolete ESLint v8 configuration system, `/* eslint-env */` comments could be used to define globals for a file. The current configuration system does not support such comments, and starting with ESLint v10, they are reported as errors during linting.
122+
123+
```text
124+
error: /* eslint-env */ comments are no longer supported at file.js:1:1:
125+
> 1 | /* eslint-env node -- Used in Node.js */
126+
| ^
127+
```
128+
129+
**To address:** Remove any `eslint-env` comments from your code. If you are still using the old configuration system and need help migrating, check the [migration guide](./configure/migration-guide#eslint-env-configuration-comments).
130+
131+
**Related issue(s):** [#13481](https://github.com/eslint/eslint/issues/13481)

‎lib/languages/js/source-code/source-code.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,15 @@ class SourceCode extends TokenStore {
12431243

12441244
break;
12451245
}
1246+
case "eslint-env": {
1247+
problems.push({
1248+
ruleId: null,
1249+
loc: comment.loc,
1250+
message:
1251+
"/* eslint-env */ comments are no longer supported.",
1252+
});
1253+
break;
1254+
}
12461255

12471256
// no default
12481257
}

‎tests/lib/linter/linter.js‎

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18108,6 +18108,42 @@ var a = "test2";
1810818108
}
1810918109
});
1811018110
});
18111+
18112+
describe("eslint-env comments", () => {
18113+
it("should produce an error", () => {
18114+
const code =
18115+
"/* eslint-env\nes6 */\nfoo/*eslint-env node -- TEST eslint-env*/,bar";
18116+
const messages = linter.verify(code);
18117+
const suppressedMessages = linter.getSuppressedMessages();
18118+
18119+
assert.strictEqual(messages.length, 2);
18120+
assert.strictEqual(messages[0].ruleId, null);
18121+
assert.strictEqual(messages[0].severity, 2);
18122+
assert.strictEqual(messages[0].fatal, true);
18123+
assert.strictEqual(messages[0].line, 1);
18124+
assert.strictEqual(messages[0].column, 1);
18125+
assert.strictEqual(messages[0].endLine, 2);
18126+
assert.strictEqual(messages[0].endColumn, 7);
18127+
assert.strictEqual(messages[1].ruleId, null);
18128+
assert.strictEqual(messages[1].severity, 2);
18129+
assert.strictEqual(messages[1].fatal, true);
18130+
assert.strictEqual(messages[1].line, 3);
18131+
assert.strictEqual(messages[1].column, 4);
18132+
assert.strictEqual(messages[1].endLine, 3);
18133+
assert.strictEqual(messages[1].endColumn, 42);
18134+
18135+
assert.strictEqual(suppressedMessages.length, 0);
18136+
});
18137+
18138+
it("should be ignored in inline comments", () => {
18139+
const code = "it('Test', () => {}); // eslint-env mocha";
18140+
const messages = linter.verify(code);
18141+
const suppressedMessages = linter.getSuppressedMessages();
18142+
18143+
assert.strictEqual(messages.length, 0);
18144+
assert.strictEqual(suppressedMessages.length, 0);
18145+
});
18146+
});
1811118147
});
1811218148

1811318149
describe("Default Global Variables", () => {
@@ -18484,17 +18520,22 @@ var a = "test2";
1848418520
});
1848518521

1848618522
it("should default to flat config mode when a config isn't passed", () => {
18487-
// eslint-env should not be honored
18523+
// eslint-env should not be honored and should produce an error
1848818524
const messages = linter.verify(
1848918525
"/*eslint no-undef:error*//*eslint-env browser*/\nwindow;",
1849018526
);
1849118527
const suppressedMessages = linter.getSuppressedMessages();
1849218528

18493-
assert.strictEqual(messages.length, 1);
18494-
assert.strictEqual(messages[0].ruleId, "no-undef");
18529+
assert.strictEqual(messages.length, 2);
18530+
assert.strictEqual(messages[0].ruleId, null);
1849518531
assert.strictEqual(messages[0].severity, 2);
18496-
assert.strictEqual(messages[0].line, 2);
18497-
assert.strictEqual(messages[0].column, 1);
18532+
assert.strictEqual(messages[0].fatal, true);
18533+
assert.strictEqual(messages[0].line, 1);
18534+
assert.strictEqual(messages[0].column, 26);
18535+
assert.strictEqual(messages[1].ruleId, "no-undef");
18536+
assert.strictEqual(messages[1].severity, 2);
18537+
assert.strictEqual(messages[1].line, 2);
18538+
assert.strictEqual(messages[1].column, 1);
1849818539

1849918540
assert.strictEqual(suppressedMessages.length, 0);
1850018541
});

‎tests/lib/rules/capitalized-comments.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ ruleTester.run("capitalized-comments", rule, {
6060
"// eslint-disable-line",
6161
"// eslint-disable-next-line",
6262
"/* eslint semi:off */",
63-
"/* eslint-env node */",
63+
"/* eslint-enable */",
6464
"/* istanbul ignore next */",
6565
"/* jshint asi:true */",
6666
"/* jscs: enable */",
@@ -121,7 +121,7 @@ ruleTester.run("capitalized-comments", rule, {
121121
{ code: "// eslint-disable-line", options: ["always"] },
122122
{ code: "// eslint-disable-next-line", options: ["always"] },
123123
{ code: "/* eslint semi:off */", options: ["always"] },
124-
{ code: "/* eslint-env node */", options: ["always"] },
124+
{ code: "/* eslint-enable */", options: ["always"] },
125125
{ code: "/* istanbul ignore next */", options: ["always"] },
126126
{ code: "/* jshint asi:true */", options: ["always"] },
127127
{ code: "/* jscs: enable */", options: ["always"] },

‎tests/lib/rules/spaced-comment.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ ruleTester.run("spaced-comment", rule, {
202202
options: ["always", { markers: ["global"] }],
203203
},
204204
{
205-
code: "/*eslint-env node*/",
206-
options: ["always", { markers: ["eslint-env"] }],
205+
code: "/*eslint-disable-line unicode-bom*/",
206+
options: ["always", { markers: ["eslint-disable-line"] }],
207207
},
208208
{
209209
code: "/*eslint eqeqeq:0, curly: 2*/",

‎tests/tools/check-rule-examples.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ describe("check-rule-examples", () => {
8989
"\x1B[0m \x1B[2m51:1\x1B[22m \x1B[31merror\x1B[39m Duplicate /* eslint no-restricted-syntax */ configuration comment. Each example should contain only one. Split this example into multiple examples\x1B[0m\n" +
9090
'\x1B[0m \x1B[2m56:1\x1B[22m \x1B[31merror\x1B[39m Remove unnecessary "ecmaVersion":"latest"\x1B[0m\n' +
9191
`\x1B[0m \x1B[2m64:1\x1B[22m \x1B[31merror\x1B[39m "ecmaVersion" must be one of ${[3, 5, ...Array.from({ length: LATEST_ECMA_VERSION - 2015 + 1 }, (_, index) => index + 2015)].join(", ")}\x1B[0m\n` +
92-
"\x1B[0m \x1B[2m76:1\x1B[22m \x1B[31merror\x1B[39m /* eslint-env */ comments are no longer supported. Remove the comment\x1B[0m\n" +
93-
"\x1B[0m \x1B[2m78:1\x1B[22m \x1B[31merror\x1B[39m /* eslint-env */ comments are no longer supported. Remove the comment\x1B[0m\n" +
94-
"\x1B[0m \x1B[2m79:1\x1B[22m \x1B[31merror\x1B[39m /* eslint-env */ comments are no longer supported. Remove the comment\x1B[0m\n" +
92+
"\x1B[0m \x1B[2m76:1\x1B[22m \x1B[31merror\x1B[39m Unexpected lint error found: /* eslint-env */ comments are no longer supported\x1B[0m\n" +
93+
"\x1B[0m \x1B[2m78:1\x1B[22m \x1B[31merror\x1B[39m Unexpected lint error found: /* eslint-env */ comments are no longer supported\x1B[0m\n" +
94+
"\x1B[0m \x1B[2m79:1\x1B[22m \x1B[31merror\x1B[39m Unexpected lint error found: /* eslint-env */ comments are no longer supported\x1B[0m\n" +
9595
'\x1B[0m \x1B[2m84:1\x1B[22m \x1B[31merror\x1B[39m Configuration error: Key "languageOptions": Unexpected key "foo" found\x1B[0m\n' +
9696
"\x1B[0m \x1B[2m97:7\x1B[22m \x1B[31merror\x1B[39m Unexpected lint error found: Using 'ArrayPattern' is not allowed \x1B[2mno-restricted-syntax\x1B[22m\x1B[0m\n" +
9797
"\x1B[0m \x1B[2m105:1\x1B[22m \x1B[31merror\x1B[39m Incorrect examples should have at least one error reported by the rule\x1B[0m\n" +

‎tools/check-rule-examples.js‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,6 @@ async function findProblems(filename) {
177177
let hasRuleConfigComment = false;
178178

179179
for (const comment of ast.comments) {
180-
if (
181-
comment.type === "Block" &&
182-
/^\s*eslint-env(?!\S)/u.test(comment.value)
183-
) {
184-
problems.push({
185-
fatal: false,
186-
severity: 2,
187-
message:
188-
"/* eslint-env */ comments are no longer supported. Remove the comment.",
189-
line:
190-
codeBlockToken.map[0] +
191-
1 +
192-
comment.loc.start.line,
193-
column: comment.loc.start.column + 1,
194-
});
195-
}
196-
197180
if (
198181
comment.type !== "Block" ||
199182
!/^\s*eslint(?!\S)/u.test(comment.value)

0 commit comments

Comments
 (0)