Skip to content

Commit

Permalink
refactor: remove globals dependency (#18115)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Feb 13, 2024
1 parent d8068ec commit 9aa4df3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
5 changes: 2 additions & 3 deletions lib/rules/no-constant-binary-expression.js
Expand Up @@ -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(["+", "-", "*", "/", "%", "|", "^", "&", "**", "<<", ">>", ">>>"]);

Expand Down Expand Up @@ -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":
Expand Down
3 changes: 1 addition & 2 deletions lib/rules/no-extend-native.js
Expand Up @@ -10,7 +10,6 @@
//------------------------------------------------------------------------------

const astUtils = require("./utils/ast-utils");
const globals = require("globals");

//------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -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))
);
Expand Down
9 changes: 9 additions & 0 deletions lib/rules/utils/ast-utils.js
Expand Up @@ -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
Expand Down Expand Up @@ -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<string,boolean>} 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.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/rules/utils/ast-utils.js
Expand Up @@ -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", {
Expand Down

0 comments on commit 9aa4df3

Please sign in to comment.