Skip to content
New issue

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

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

Already on GitHub? # to your account

Docs: (languageOptions.globals) How to specify more than one globals.(env) #18803

Closed
1 task
codyswanner opened this issue Aug 22, 2024 · 1 comment · Fixed by #18805
Closed
1 task

Docs: (languageOptions.globals) How to specify more than one globals.(env) #18803

codyswanner opened this issue Aug 22, 2024 · 1 comment · Fixed by #18805
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion documentation Relates to ESLint's documentation

Comments

@codyswanner
Copy link

Docs page(s)

https://eslint.org/docs/latest/use/configure/migration-guide#configuring-language-options
and
https://eslint.org/docs/latest/use/configure/language-options

What documentation issue do you want to solve?

Currently the docs specify how to use a single globals.brower or globals.jest or globals.(env), but not more than one at a time. The docs suggest that using the spread operator should work, so maybe something like this:

export default [
  {files: ["**/*.{js,mjs,cjs,jsx}"]},
  {languageOptions: { globals: ...globals.browser, globals.jest }},
  pluginJs.configs.recommended,
  pluginReact.configs.flat.recommended,
  {rules: {"react/prop-types": "off"}},
  jestConfig.configs['flat/recommended']
];

But that does not work, and neither does

export default [
  {files: ["**/*.{js,mjs,cjs,jsx}"]},
  {languageOptions: { globals: ...globals.browser, ...globals.jest }},
  pluginJs.configs.recommended,
  pluginReact.configs.flat.recommended,
  {rules: {"react/prop-types": "off"}},
  jestConfig.configs['flat/recommended']
];

and neither does

export default [
  {files: ["**/*.{js,mjs,cjs,jsx}"]},
  {languageOptions: { globals: globals.browser, globals.jest }},
  pluginJs.configs.recommended,
  pluginReact.configs.flat.recommended,
  {rules: {"react/prop-types": "off"}},
  jestConfig.configs['flat/recommended']
];

or

export default [
  {files: ["**/*.{js,mjs,cjs,jsx}"]},
  {languageOptions: { globals: [globals.browser, globals.jest] }},
  pluginJs.configs.recommended,
  pluginReact.configs.flat.recommended,
  {rules: {"react/prop-types": "off"}},
  jestConfig.configs['flat/recommended']
];

I have tests that are using BOTH jest globals, and window (browser) globals, I need BOTH! How can I configure this?

What do you think is the correct solution?

I'd like the ESLint docs to specify how to do this. Even better, I'd like the error messages to give me more of a hint than Unexpected token: . because that isn't very helpful, there's plenty of . in my config file, can you give some context, a line number, SOMETHING, and tell me how to fix it? If Unexpected token: ... in languageOptions.globals, maybe I'm trying to do this specific thing (that the docs tell me should work) and you could link me to some information?

It seems like the underlying structure here is objects, so it seems like one of these should work, either with spread syntax:

export default [
  {files: ["**/*.{js,mjs,cjs,jsx}"]},
  {languageOptions: { globals: ...globals.browser, ...globals.jest }},
  pluginJs.configs.recommended,
  pluginReact.configs.flat.recommended,
  {rules: {"react/prop-types": "off"}},
  jestConfig.configs['flat/recommended']
];

which would be fine but kind of unintuitive for a new user, or without spread syntax:

export default [
  {files: ["**/*.{js,mjs,cjs,jsx}"]},
  {languageOptions: { globals: globals.browser, globals.jest }},
  pluginJs.configs.recommended,
  pluginReact.configs.flat.recommended,
  {rules: {"react/prop-types": "off"}},
  jestConfig.configs['flat/recommended']
];

which was the first thing I tried because it makes sense.

Participation

  • I am willing to submit a pull request for this change.

Additional comments

In general, I am in favor of MORE specificity and examples in documentation, even at the cost of brevity. Or a happy middle ground: I don't think anyone has ever complained about a documentation that said "find more examples here" and linked to a page full of examples showing how to make each piece of the system singular or plural or what all the options are and what they do, etc. I know that takes time but further examples help a TON when the concept is unclear/not intuitive.

@codyswanner codyswanner added the documentation Relates to ESLint's documentation label Aug 22, 2024
@github-project-automation github-project-automation bot moved this to Needs Triage in Triage Aug 22, 2024
@nzakas
Copy link
Member

nzakas commented Aug 22, 2024

Unexpected token: . - this is a parsing error coming from Node.js. We don't have any control over that. This means you have invalid JavaScript.

The second example in the migration guide shows how to use a globals object and an additional property. You'd want to use that as your baseline.

I can add another line in each example showing how to do more than one, but this is just standard JavaScript syntax, not anything that is specific to ESLint.

@nzakas nzakas self-assigned this Aug 22, 2024
@nzakas nzakas added the accepted There is consensus among the team that this change meets the criteria for inclusion label Aug 22, 2024
@nzakas nzakas moved this from Needs Triage to Ready to Implement in Triage Aug 22, 2024
nzakas added a commit that referenced this issue Aug 22, 2024
@nzakas nzakas moved this from Ready to Implement to Implementing in Triage Aug 22, 2024
@github-project-automation github-project-automation bot moved this from Implementing to Complete in Triage Aug 23, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion documentation Relates to ESLint's documentation
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants