Skip to content

Commit da73e58

Browse files
fasttimenzakas
andauthored
docs: Migrating eslint-env configuration comments (#17390)
* docs: Migrating `eslint-env` configuration comments * "must" -> "should" Co-authored-by: Nicholas C. Zakas <[email protected]> * Add heading --------- Co-authored-by: Nicholas C. Zakas <[email protected]>
1 parent 10e9cfa commit da73e58

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

Diff for: docs/src/use/configure/migration-guide.md

+59
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,65 @@ export default [
255255
];
256256
```
257257

258+
### `eslint-env` Configuration Comments
259+
260+
In the eslintrc config system it was possible to use `eslint-env` configuration comments to define globals for a file.
261+
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.
262+
For this reason, when migrating from eslintrc to flat config, `eslint-env` configuration comments should be removed from all files.
263+
They can be either replaced with equivalent but more verbose `global` configuration comments, or dropped in favor of `globals` definitions in the config file.
264+
265+
For example, when using eslintrc, a file to be linted could look like this:
266+
267+
```javascript
268+
// tests/my-file.js
269+
270+
/* eslint-env mocha */
271+
272+
describe("unit tests", () => {
273+
it("should pass", () => {
274+
// ...
275+
});
276+
});
277+
```
278+
279+
In the above example, `describe` and `it` would be recognized as global identifiers because of the `/* eslint-env mocha */` comment.
280+
281+
The same effect can be achieved with flat config with a `global` configuration comment, e.g.:
282+
283+
```javascript
284+
// tests/my-file.js
285+
286+
/* global describe, it -- Globals defined by Mocha */
287+
288+
describe("unit tests", () => {
289+
it("should pass", () => {
290+
// ...
291+
});
292+
});
293+
```
294+
295+
Another option is to remove the comment from the file being linted and define the globals in the configuration, for example:
296+
297+
```javascript
298+
// eslint.config.js
299+
300+
import globals from "globals";
301+
302+
export default [
303+
// ...other config
304+
{
305+
files: [
306+
"tests/**"
307+
],
308+
languageOptions: {
309+
globals: {
310+
...globals.mocha
311+
}
312+
}
313+
}
314+
];
315+
```
316+
258317
### Predefined Configs
259318

260319
In eslintrc files, use the `extends` property to use predefined configs. ESLint comes with two predefined configs that you can access as strings:

0 commit comments

Comments
 (0)