diff --git a/lib/rules/no-constant-binary-expression.js b/lib/rules/no-constant-binary-expression.js index 775265f190d..2f3013b74f6 100644 --- a/lib/rules/no-constant-binary-expression.js +++ b/lib/rules/no-constant-binary-expression.js @@ -5,8 +5,7 @@ "use strict"; -const globals = require("globals"); -const { isNullLiteral, isConstant, isReferenceToGlobalVariable, isLogicalAssignmentOperator } = require("./utils/ast-utils"); +const { isNullLiteral, isConstant, isReferenceToGlobalVariable, isLogicalAssignmentOperator, ECMASCRIPT_GLOBALS } = require("./utils/ast-utils"); const NUMERIC_OR_STRING_BINARY_OPERATORS = new Set(["+", "-", "*", "/", "%", "|", "^", "&", "**", "<<", ">>", ">>>"]); @@ -376,7 +375,7 @@ function isAlwaysNew(scope, node) { * Catching these is especially useful for primitive constructors * which return boxed values, a surprising gotcha' in JavaScript. */ - return Object.hasOwn(globals.builtin, node.callee.name) && + return Object.hasOwn(ECMASCRIPT_GLOBALS, node.callee.name) && isReferenceToGlobalVariable(scope, node.callee); } case "Literal": diff --git a/lib/rules/no-extend-native.js b/lib/rules/no-extend-native.js index fcbb3855725..a69dd690039 100644 --- a/lib/rules/no-extend-native.js +++ b/lib/rules/no-extend-native.js @@ -10,7 +10,6 @@ //------------------------------------------------------------------------------ const astUtils = require("./utils/ast-utils"); -const globals = require("globals"); //------------------------------------------------------------------------------ // Rule Definition @@ -54,7 +53,7 @@ module.exports = { const sourceCode = context.sourceCode; const exceptions = new Set(config.exceptions || []); const modifiedBuiltins = new Set( - Object.keys(globals.builtin) + Object.keys(astUtils.ECMASCRIPT_GLOBALS) .filter(builtin => builtin[0].toUpperCase() === builtin[0]) .filter(builtin => !exceptions.has(builtin)) ); diff --git a/lib/rules/utils/ast-utils.js b/lib/rules/utils/ast-utils.js index 4b074e0198f..ed9a31af34c 100644 --- a/lib/rules/utils/ast-utils.js +++ b/lib/rules/utils/ast-utils.js @@ -19,6 +19,8 @@ const { lineBreakPattern, shebangPattern } = require("../../shared/ast-utils"); +const globals = require("../../../conf/globals"); +const { LATEST_ECMA_VERSION } = require("../../../conf/ecma-version"); //------------------------------------------------------------------------------ // Helpers @@ -46,6 +48,12 @@ const OCTAL_OR_NON_OCTAL_DECIMAL_ESCAPE_PATTERN = /^(?:[^\\]|\\.)*\\(?:[1-9]|0[0 const LOGICAL_ASSIGNMENT_OPERATORS = new Set(["&&=", "||=", "??="]); +/** + * All builtin global variables defined in the latest ECMAScript specification. + * @type {Record} Key is the name of the variable. Value is `true` if the variable is considered writable, `false` otherwise. + */ +const ECMASCRIPT_GLOBALS = globals[`es${LATEST_ECMA_VERSION}`]; + /** * Checks reference if is non initializer and writable. * @param {Reference} reference A reference to check. @@ -1133,6 +1141,7 @@ module.exports = { LINEBREAK_MATCHER: lineBreakPattern, SHEBANG_MATCHER: shebangPattern, STATEMENT_LIST_PARENTS, + ECMASCRIPT_GLOBALS, /** * Determines whether two adjacent tokens are on the same line. diff --git a/package.json b/package.json index 24a14e710cf..9cf566264fb 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,6 @@ "file-entry-cache": "^8.0.0", "find-up": "^5.0.0", "glob-parent": "^6.0.2", - "globals": "^13.19.0", "graphemer": "^1.4.0", "ignore": "^5.2.0", "imurmurhash": "^0.1.4", @@ -128,6 +127,7 @@ "fast-glob": "^3.2.11", "fs-teardown": "^0.1.3", "glob": "^10.0.0", + "globals": "^14.0.0", "got": "^11.8.3", "gray-matter": "^4.0.3", "js-yaml": "^4.1.0", diff --git a/tests/lib/rules/utils/ast-utils.js b/tests/lib/rules/utils/ast-utils.js index 6684df35f00..7700f2ce7e9 100644 --- a/tests/lib/rules/utils/ast-utils.js +++ b/tests/lib/rules/utils/ast-utils.js @@ -59,6 +59,32 @@ describe("ast-utils", () => { }); }); + describe("ECMASCRIPT_GLOBALS", () => { + it("should contain es3 globals", () => { + assert.ownInclude(astUtils.ECMASCRIPT_GLOBALS, { Object: false }); + }); + + it("should contain es5 globals", () => { + assert.ownInclude(astUtils.ECMASCRIPT_GLOBALS, { JSON: false }); + }); + + it("should contain es2015 globals", () => { + assert.ownInclude(astUtils.ECMASCRIPT_GLOBALS, { Promise: false }); + }); + + it("should contain es2017 globals", () => { + assert.ownInclude(astUtils.ECMASCRIPT_GLOBALS, { SharedArrayBuffer: false }); + }); + + it("should contain es2020 globals", () => { + assert.ownInclude(astUtils.ECMASCRIPT_GLOBALS, { BigInt: false }); + }); + + it("should contain es2021 globals", () => { + assert.ownInclude(astUtils.ECMASCRIPT_GLOBALS, { WeakRef: false }); + }); + }); + describe("isTokenOnSameLine", () => { it("should return false if the tokens are not on the same line", () => { linter.defineRule("checker", {