From cb7abc529d3bd503836a7afca07b982b65698e1c Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 12 Jul 2024 19:27:29 +0800 Subject: [PATCH] Get `globals` directly from `globals` package (#2395) --- configs/flat-config-base.js | 6 ++---- package.json | 5 +++-- readme.md | 8 ++++---- test/package.mjs | 9 +++++++++ test/utils/language-options.mjs | 11 ++++++----- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/configs/flat-config-base.js b/configs/flat-config-base.js index bdf7adaa7c..98575f4ecc 100644 --- a/configs/flat-config-base.js +++ b/configs/flat-config-base.js @@ -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, }, }; diff --git a/package.json b/package.json index c96f415050..e04d29ff57 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/readme.md b/readme.md index d890ebfeb1..de6fecd473 100644 --- a/readme.md +++ b/readme.md @@ -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, @@ -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, diff --git a/test/package.mjs b/test/package.mjs index 33cd39cfe9..96bfcb1351 100644 --- a/test/package.mjs +++ b/test/package.mjs @@ -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; @@ -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; diff --git a/test/utils/language-options.mjs b/test/utils/language-options.mjs index dca65a3a5e..1c29a01549 100644 --- a/test/utils/language-options.mjs +++ b/test/utils/language-options.mjs @@ -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) {