Skip to content

Commit bfaea12

Browse files
authored
perf: cache normalized config globals per languageOptions (#21364)
Files that match the same configs share one languageOptions object, so compute the merged and normalized globals once per object instead of once per file. When a file has no inline global comments, also reuse the list of enabled global names instead of copying and filtering again.
1 parent 322209e commit bfaea12

2 files changed

Lines changed: 184 additions & 27 deletions

File tree

‎lib/languages/js/source-code/source-code.js‎

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,38 @@ function findLineNumberBinarySearch(lineStartIndices, target) {
194194
// Directive Comments
195195
//-----------------------------------------------------------------------------
196196

197+
/**
198+
* Normalized globals for `ecmaVersion`, `sourceType`, and `globals`, keyed by `languageOptions`.
199+
* Files matching the same configs share one `languageOptions` object, so each set is built only once.
200+
* @type {WeakMap<Object, Record<string, "readonly"|"writable"|"off">>}
201+
*/
202+
const configGlobalsCache = new WeakMap();
203+
204+
/**
205+
* Names of enabled (not `"off"`) globals, keyed by a normalized globals object from `configGlobalsCache`.
206+
* Lets `addDeclaredGlobals()` skip copying and filtering globals for files without inline global comments.
207+
* @type {WeakMap<Object, string[]>}
208+
*/
209+
const enabledConfigGlobalNamesCache = new WeakMap();
210+
211+
/**
212+
* Gets the names of the globals in a normalized globals object that aren't turned off.
213+
* @param {Object} configGlobals The normalized globals declared in configuration.
214+
* @returns {string[]} The names of the enabled globals.
215+
*/
216+
function getEnabledConfigGlobalNames(configGlobals) {
217+
let names = enabledConfigGlobalNamesCache.get(configGlobals);
218+
219+
if (!names) {
220+
names = Object.keys(configGlobals).filter(
221+
name => configGlobals[name] !== "off",
222+
);
223+
enabledConfigGlobalNamesCache.set(configGlobals, names);
224+
}
225+
226+
return names;
227+
}
228+
197229
/**
198230
* Ensures that variables representing built-in properties of the Global Object,
199231
* and any globals declared by special block comments, are present in the global
@@ -208,15 +240,23 @@ function addDeclaredGlobals(
208240
configGlobals = Object.create(null),
209241
inlineGlobals = Object.create(null),
210242
) {
211-
const finalGlobals = { __proto__: null, ...configGlobals };
243+
let finalGlobals = configGlobals;
244+
let names;
212245

213-
for (const [name, data] of Object.entries(inlineGlobals)) {
214-
finalGlobals[name] = data.value;
215-
}
246+
// Most files have no inline globals, so their names depend only on the (cached) config globals.
247+
if (Object.keys(inlineGlobals).length === 0) {
248+
names = getEnabledConfigGlobalNames(configGlobals);
249+
} else {
250+
finalGlobals = { __proto__: null, ...configGlobals };
216251

217-
const names = Object.keys(finalGlobals).filter(
218-
name => finalGlobals[name] !== "off",
219-
);
252+
for (const [name, data] of Object.entries(inlineGlobals)) {
253+
finalGlobals[name] = data.value;
254+
}
255+
256+
names = Object.keys(finalGlobals).filter(
257+
name => finalGlobals[name] !== "off",
258+
);
259+
}
220260

221261
scopeManager.addGlobals(names);
222262

@@ -944,27 +984,33 @@ class SourceCode extends TokenStore {
944984
* @returns {void}
945985
*/
946986
applyLanguageOptions(languageOptions) {
947-
/*
948-
* Add configured globals and language globals
949-
*
950-
* Using Object.assign instead of object spread for performance reasons
951-
* https://github.com/eslint/eslint/issues/16302
952-
*/
953-
const configGlobals = Object.assign(
954-
Object.create(null), // https://github.com/eslint/eslint/issues/18363
955-
getGlobalsForEcmaVersion(languageOptions.ecmaVersion),
956-
languageOptions.sourceType === "commonjs"
957-
? globals.commonjs
958-
: void 0,
959-
languageOptions.globals,
960-
);
987+
let configGlobals = configGlobalsCache.get(languageOptions);
988+
989+
if (!configGlobals) {
990+
/*
991+
* Add configured globals and language globals
992+
*
993+
* Using Object.assign instead of object spread for performance reasons
994+
* https://github.com/eslint/eslint/issues/16302
995+
*/
996+
configGlobals = Object.assign(
997+
Object.create(null), // https://github.com/eslint/eslint/issues/18363
998+
getGlobalsForEcmaVersion(languageOptions.ecmaVersion),
999+
languageOptions.sourceType === "commonjs"
1000+
? globals.commonjs
1001+
: void 0,
1002+
languageOptions.globals,
1003+
);
9611004

962-
/*
963-
* `normalizeConfigGlobal` will throw an error if a configured global value is invalid. However, these errors would
964-
* typically be caught when validating a config anyway (validity for inline global comments is checked separately).
965-
*/
966-
for (const [name, value] of Object.entries(configGlobals)) {
967-
configGlobals[name] = normalizeConfigGlobal(value);
1005+
/*
1006+
* `normalizeConfigGlobal` will throw an error if a configured global value is invalid. However, these errors would
1007+
* typically be caught when validating a config anyway (validity for inline global comments is checked separately).
1008+
*/
1009+
for (const [name, value] of Object.entries(configGlobals)) {
1010+
configGlobals[name] = normalizeConfigGlobal(value);
1011+
}
1012+
1013+
configGlobalsCache.set(languageOptions, configGlobals);
9681014
}
9691015

9701016
const varsCache = this[caches].get("vars");

‎tests/lib/linter/linter.js‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3279,6 +3279,117 @@ describe("Linter with FlatConfigArray", () => {
32793279
});
32803280
});
32813281

3282+
describe("when evaluating multiple files that share a config", () => {
3283+
it("should not leak inline globals into later files", () => {
3284+
const configs = createFlatConfigArray({
3285+
files: ["**/*.js"],
3286+
rules: { "no-undef": "error" },
3287+
});
3288+
3289+
configs.normalizeSync();
3290+
3291+
const first = linter.verify(
3292+
"/* global foo */ foo;",
3293+
configs,
3294+
"a.js",
3295+
);
3296+
const second = linter.verify("foo;", configs, "b.js");
3297+
3298+
assert.strictEqual(first.length, 0);
3299+
assert.strictEqual(second.length, 1);
3300+
assert.strictEqual(second[0].ruleId, "no-undef");
3301+
});
3302+
3303+
it("should not let an inline global change a configured global in later files", () => {
3304+
const configs = createFlatConfigArray({
3305+
files: ["**/*.js"],
3306+
languageOptions: { globals: { foo: "writable" } },
3307+
rules: { "no-global-assign": "error" },
3308+
});
3309+
3310+
configs.normalizeSync();
3311+
3312+
const readonlyInline =
3313+
"/* global foo:readonly */ foo = 1;";
3314+
const first = linter.verify(
3315+
readonlyInline,
3316+
configs,
3317+
"a.js",
3318+
);
3319+
const second = linter.verify(
3320+
"foo = 1;",
3321+
configs,
3322+
"b.js",
3323+
);
3324+
const third = linter.verify(
3325+
readonlyInline,
3326+
configs,
3327+
"c.js",
3328+
);
3329+
3330+
assert.strictEqual(first.length, 1);
3331+
assert.strictEqual(first[0].ruleId, "no-global-assign");
3332+
assert.strictEqual(second.length, 0);
3333+
assert.strictEqual(third.length, 1);
3334+
});
3335+
3336+
it("should keep configured globals that are off excluded in every file", () => {
3337+
const configs = createFlatConfigArray({
3338+
files: ["**/*.js"],
3339+
languageOptions: {
3340+
globals: { foo: "readonly", bar: "off" },
3341+
},
3342+
rules: { "no-undef": "error" },
3343+
});
3344+
3345+
configs.normalizeSync();
3346+
3347+
for (const [code, file] of [
3348+
["foo; bar;", "a.js"],
3349+
["/* global baz */ foo; bar; baz;", "b.js"],
3350+
["foo; bar;", "c.js"],
3351+
]) {
3352+
const messages = linter.verify(code, configs, file);
3353+
3354+
assert.strictEqual(messages.length, 1, file);
3355+
assert.strictEqual(
3356+
messages[0].message,
3357+
"'bar' is not defined.",
3358+
);
3359+
}
3360+
});
3361+
3362+
it("should give files matching different configs their own globals", () => {
3363+
const configs = createFlatConfigArray([
3364+
{
3365+
files: ["**/*.cjs"],
3366+
languageOptions: { sourceType: "commonjs" },
3367+
},
3368+
{
3369+
files: ["**/*.js", "**/*.cjs"],
3370+
rules: { "no-undef": "error" },
3371+
},
3372+
]);
3373+
3374+
configs.normalizeSync();
3375+
3376+
const code = "module.exports = {};";
3377+
3378+
assert.strictEqual(
3379+
linter.verify(code, configs, "a.cjs").length,
3380+
0,
3381+
);
3382+
assert.strictEqual(
3383+
linter.verify(code, configs, "b.js").length,
3384+
1,
3385+
);
3386+
assert.strictEqual(
3387+
linter.verify(code, configs, "c.cjs").length,
3388+
0,
3389+
);
3390+
});
3391+
});
3392+
32823393
describe("when evaluating code containing a /*global */ block with sloppy whitespace", () => {
32833394
const code = "/* global a b : true c: false*/";
32843395

0 commit comments

Comments
 (0)