Skip to content

Commit

Permalink
Get globals directly from globals package (#2395)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jul 12, 2024
1 parent 3c33820 commit cb7abc5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
6 changes: 2 additions & 4 deletions configs/flat-config-base.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';
const eslintrc = require('@eslint/eslintrc');

const {globals} = eslintrc.Legacy.environments.get('es2024');
const globals = require('globals');

module.exports = {
languageOptions: {
globals,
globals: globals.builtin,
},
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
"dependencies": {
"@babel/helper-validator-identifier": "^7.24.5",
"@eslint-community/eslint-utils": "^4.4.0",
"@eslint/eslintrc": "^3.0.2",
"ci-info": "^4.0.0",
"clean-regexp": "^1.0.0",
"core-js-compat": "^3.37.0",
"esquery": "^1.5.0",
"globals": "^15.7.0",
"indent-string": "^4.0.0",
"is-builtin-module": "^3.2.1",
"jsesc": "^3.0.2",
Expand All @@ -72,13 +72,14 @@
"@babel/code-frame": "^7.24.2",
"@babel/core": "^7.24.5",
"@babel/eslint-parser": "^7.24.5",
"@eslint/eslintrc": "^3.1.0",
"@lubien/fixture-beta-package": "^1.0.0-beta.1",
"@typescript-eslint/parser": "^8.0.0-alpha.12",
"ava": "^6.1.3",
"c8": "^9.1.0",
"chalk": "^5.3.0",
"enquirer": "^2.4.1",
"eslint": "^9.2.0",
"eslint": "^9.6.0",
"eslint-ava-rule-tester": "^5.0.1",
"eslint-doc-generator": "1.7.0",
"eslint-plugin-eslint-plugin": "^6.1.0",
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ If you don't use the preset, ensure you use the same `languageOptions` config as

```js
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
import * as eslintrc from '@eslint/eslintrc';
import globals from 'globals';

export default [
{
languageOptions: {
globals: eslintrc.Legacy.environments.get('es2024'),
globals: globals.builtin,
},
plugins: {
unicorn: eslintPluginUnicorn,
Expand All @@ -51,12 +51,12 @@ export default [
```js
'use strict';
const eslintPluginUnicorn = require('eslint-plugin-unicorn');
const eslintrc = require('@eslint/eslintrc');
const globals = require('globals');

module.exports = [
{
languageOptions: {
globals: eslintrc.Legacy.environments.get('es2024'),
globals: globals.builtin,
},
plugins: {
unicorn: eslintPluginUnicorn,
Expand Down
9 changes: 9 additions & 0 deletions test/package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import process from 'node:process';
import test from 'ava';
import eslintExperimentalApis from 'eslint/use-at-your-own-risk';
import * as eslintrc from '@eslint/eslintrc';
import globals from 'globals';
import eslintPluginUnicorn from '../index.js';

const {FlatESLint} = eslintExperimentalApis;
Expand Down Expand Up @@ -157,6 +158,14 @@ function getCompactConfig(config) {
// https://eslint.org/docs/latest/use/configure/configuration-files-new#configuration-objects
delete languageOptions.ecmaVersion;
delete languageOptions.sourceType;
languageOptions.globals = {
...languageOptions.globals,
// When use `env.es*: true` in legacy config, `es5` globals are not included
...globals.es5,
// `Intl` was added to ESLint https://github.com/eslint/eslint/pull/18318
// But `@eslint/eslintrc` choose not to update `globals` https://github.com/eslint/eslintrc/pull/164
Intl: false,
};
result[key] = languageOptions;
} else if (key === 'plugins') {
result[key] = undefined;
Expand Down
11 changes: 6 additions & 5 deletions test/utils/language-options.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {Legacy} from '@eslint/eslintrc';
import * as espree from 'espree';
import globals from 'globals';

const DEFAULT_LANGUAGE_OPTIONS = {
// When `parser` in `undefined`, `languageOptions` seems has no effect
parser: espree,
globals: Object.fromEntries(
['es2024', 'node', 'browser']
.flatMap(environment => Object.entries(Legacy.environments.get(environment).globals)),
),
globals: {
...globals.builtin,
...globals.node,
...globals.browser,
},
};

function cleanLanguageOptions(languageOptions) {
Expand Down

0 comments on commit cb7abc5

Please sign in to comment.