Skip to content

Commit cddfae4

Browse files
committed
fix(core): improve cacheKey when using flat config
This change improves the logic for generating the cache key used for both the exportMap cache and resolver cache when using flat config. Prior to this change, the cache key was a combination of the `parserPath`, a hash of the `parserOptions`, a hash of the plugin settings, and the path of the file. When using flat config, `parserPath` isn't provided. So, there's the possibility of incorrect cache objects being used if someone ran with this plugin using different parsers within the same lint execution, if parserOptions and settings are the same. The equivalent cacheKey construction when using flat config is to use `languageOptions` as a component of the key, rather than `parserPath` and `parserOptions`. One caveat is that the `parser` property of `languageOptions` is an object that oftentimes has the same two functions (`parse` and `parseForESLint`). This won't be reliably distinct when using the base JSON.stringify function to detect changes. So, this implementation uses a replace function along with `JSON.stringify` to stringify function properties along with other property types. To ensure that this will work properly with v9, I also tested this against
1 parent 743ebca commit cddfae4

File tree

2 files changed

+100
-7
lines changed

2 files changed

+100
-7
lines changed

src/exportMap/childContext.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { hashObject } from 'eslint-module-utils/hash';
22

3-
let parserOptionsHash = '';
4-
let prevParserOptions = '';
3+
let optionsHash = '';
4+
let prevOptions = '';
55
let settingsHash = '';
66
let prevSettings = '';
77

@@ -17,13 +17,35 @@ export default function childContext(path, context) {
1717
prevSettings = JSON.stringify(settings);
1818
}
1919

20-
if (JSON.stringify(parserOptions) !== prevParserOptions) {
21-
parserOptionsHash = hashObject({ parserOptions }).digest('hex');
22-
prevParserOptions = JSON.stringify(parserOptions);
20+
// We'll use either a combination of `parserOptions` and `parserPath` or `languageOptions`
21+
// to construct the cache key, depending on whether this is using a flat config or not.
22+
let optionsToken;
23+
if (!parserPath && languageOptions) {
24+
// Replacer function helps us with serializing the parser nested within `languageOptions`.
25+
const replacerFn = (_, value) => {
26+
if (typeof value === 'function') {
27+
return value.toString();
28+
}
29+
return value;
30+
};
31+
if (JSON.stringify(languageOptions, replacerFn) !== prevOptions) {
32+
optionsHash = hashObject({ languageOptions }).digest('hex');
33+
prevOptions = JSON.stringify(languageOptions, replacerFn);
34+
}
35+
// For languageOptions, we're just using the hashed options as the options token
36+
optionsToken = optionsHash;
37+
} else {
38+
if (JSON.stringify(parserOptions) !== prevOptions) {
39+
optionsHash = hashObject({ parserOptions }).digest('hex');
40+
prevOptions = JSON.stringify(parserOptions);
41+
}
42+
// When not using flat config, we use a combination of the hashed parserOptions
43+
// and parserPath as the token
44+
optionsToken = String(parserPath) + optionsHash;
2345
}
2446

2547
return {
26-
cacheKey: String(parserPath) + parserOptionsHash + settingsHash + String(path),
48+
cacheKey: optionsToken + settingsHash + String(path),
2749
settings,
2850
parserOptions,
2951
parserPath,

tests/src/exportMap/childContext.js

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect } from 'chai';
2+
import { hashObject } from 'eslint-module-utils/hash';
23

34
import childContext from '../../../src/exportMap/childContext';
45

@@ -16,8 +17,13 @@ describe('childContext', () => {
1617
const languageOptions = {
1718
ecmaVersion: 2024,
1819
sourceType: 'module',
19-
parser: {},
20+
parser: {
21+
parseForESLint() {},
22+
},
2023
};
24+
const languageOptionsHash = hashObject({ languageOptions }).digest('hex');
25+
const parserOptionsHash = hashObject({ parserOptions }).digest('hex');
26+
const settingsHash = hashObject({ settings }).digest('hex');
2127

2228
// https://github.com/import-js/eslint-plugin-import/issues/3051
2329
it('should pass context properties through, if present', () => {
@@ -48,4 +54,69 @@ describe('childContext', () => {
4854
expect(result.path).to.equal(path);
4955
expect(result.cacheKey).to.be.a('string');
5056
});
57+
58+
it('should construct cache key out of languageOptions if present', () => {
59+
const mockContext = {
60+
settings,
61+
languageOptions,
62+
};
63+
64+
const result = childContext(path, mockContext);
65+
66+
expect(result.cacheKey).to.equal(languageOptionsHash + settingsHash + path);
67+
});
68+
69+
it('should use the same cache key upon multiple calls', () => {
70+
const mockContext = {
71+
settings,
72+
languageOptions,
73+
};
74+
75+
let result = childContext(path, mockContext);
76+
77+
const expectedCacheKey = languageOptionsHash + settingsHash + path;
78+
expect(result.cacheKey).to.equal(expectedCacheKey);
79+
80+
result = childContext(path, mockContext);
81+
expect(result.cacheKey).to.equal(expectedCacheKey);
82+
});
83+
84+
it('should update cacheKey if different languageOptions are passed in', () => {
85+
const mockContext = {
86+
settings,
87+
languageOptions,
88+
};
89+
90+
let result = childContext(path, mockContext);
91+
92+
const firstCacheKey = languageOptionsHash + settingsHash + path;
93+
expect(result.cacheKey).to.equal(firstCacheKey);
94+
95+
// Second run with different parser
96+
mockContext.languageOptions = {
97+
...languageOptions,
98+
parser: {
99+
parseForESLint() {},
100+
parse() {},
101+
},
102+
};
103+
104+
result = childContext(path, mockContext);
105+
106+
const secondCacheKey = hashObject({ languageOptions: mockContext.languageOptions }).digest('hex') + settingsHash + path;
107+
expect(result.cacheKey).to.not.equal(firstCacheKey);
108+
expect(result.cacheKey).to.equal(secondCacheKey);
109+
});
110+
111+
it('should construct cache key out of parserOptions and parserPath if no languageOptions', () => {
112+
const mockContext = {
113+
settings,
114+
parserOptions,
115+
parserPath,
116+
};
117+
118+
const result = childContext(path, mockContext);
119+
120+
expect(result.cacheKey).to.equal(String(parserPath) + parserOptionsHash + settingsHash + path);
121+
});
51122
});

0 commit comments

Comments
 (0)