Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Migrating eslint-env configuration comments #17390

Merged
merged 3 commits into from Jul 22, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 57 additions & 0 deletions docs/src/use/configure/migration-guide.md
Expand Up @@ -255,6 +255,63 @@ export default [
];
```

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a new heading here like ### `eslint-env` Configuration Comments?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'd add a new heading.

In the eslintrc config system it was possible to use `eslint-env` configuration comments to define globals for a file.
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.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this part. This issue suggests that eslint-env config comments will start to throw errors (in Phase 5: Remove eslintrc), but I guess the actual intent was to have these comments reported as problems by the linter? The RFC only mentions that eslint-env comments should be ignored (this is the current behavior).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the intent was that these would be reported as linter warnings. We aren't doing that yet, but that's where we are going.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the intent was that these would be reported as linter warnings.

Thanks! I've updated the wording in issue #13481 to clarify.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll also try adding this next as I'm sure this will be helpful for people as they transition to the new config system.

For this reason, when migrating from eslintrc to flat config, `eslint-env` configuration comments must be removed from all files.
fasttime marked this conversation as resolved.
Show resolved Hide resolved
They can be either replaced with equivalent but more verbose `global` configuration comments, or dropped in favor of `globals` definitions in the config file.

For example, when using eslintrc, a file to be linted could look like this:

```javascript
// tests/my-file.js

/* eslint-env mocha */

describe("unit tests", () => {
it("should pass", () => {
// ...
});
});
```

In the above example, `describe` and `it` would be recognized as global identifiers because of the `/* eslint-env mocha */` comment.

The same effect can be achieved with flat config with a `global` configuration comment, e.g.:

```javascript
// tests/my-file.js

/* global describe, it -- Globals defined by Mocha */

describe("unit tests", () => {
it("should pass", () => {
// ...
});
});
```

Another option is to remove the comment from the file being linted and define the globals in the configuration, for example:

```javascript
// eslint.config.js

import globals from "globals";

export default [
// ...other config
{
files: [
"tests/**"
],
languageOptions: {
globals: {
...globals.mocha
}
}
}
];
```

### Predefined Configs

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