Skip to content

Commit 6ba9c0d

Browse files
committed
fix(flatConfig): include languageOptions in context
This change fixes a bug with flat config support. There is a function called `childContext` that's used by the ExportBuilder to "cleanse" the context object. This function wasn't including the new `languageOptions` object, which contains the parser. So by the time this cleansed context made it to the parse function, `languageOptions` wasn't there anymore. Since `parserPath` was still being included in non-flat config scenarios, the parse function made it through ok and used `parserPath`. However, once you shift to flat config, `parserPath` is no longer defined, and the actual parser object needs to be there. Fixes #3051
1 parent 18787d3 commit 6ba9c0d

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/exportMap/childContext.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let prevSettings = '';
1010
* also calculate a cacheKey, where parts of the cacheKey hash are memoized
1111
*/
1212
export default function childContext(path, context) {
13-
const { settings, parserOptions, parserPath } = context;
13+
const { settings, parserOptions, parserPath, languageOptions } = context;
1414

1515
if (JSON.stringify(settings) !== prevSettings) {
1616
settingsHash = hashObject({ settings }).digest('hex');
@@ -28,5 +28,6 @@ export default function childContext(path, context) {
2828
parserOptions,
2929
parserPath,
3030
path,
31+
languageOptions,
3132
};
3233
}

tests/src/exportMap/childContext.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { expect } from 'chai';
2+
3+
import childContext from '../../../src/exportMap/childContext';
4+
5+
describe('childContext', () => {
6+
const settings = {
7+
setting1: true,
8+
setting2: false,
9+
};
10+
const parserOptions = {
11+
ecmaVersion: 'latest',
12+
sourceType: 'module',
13+
};
14+
const parserPath = 'path/to/parser';
15+
const path = 'path/to/src/file';
16+
const languageOptions = {
17+
ecmaVersion: 2024,
18+
sourceType: 'module',
19+
parser: {},
20+
};
21+
22+
// https://github.com/import-js/eslint-plugin-import/issues/3051
23+
it('should pass context properties through, if present', () => {
24+
const mockContext = {
25+
settings,
26+
parserOptions,
27+
parserPath,
28+
languageOptions,
29+
};
30+
31+
const result = childContext(path, mockContext);
32+
33+
expect(result.settings).to.deep.equal(settings);
34+
expect(result.parserOptions).to.deep.equal(parserOptions);
35+
expect(result.parserPath).to.equal(parserPath);
36+
expect(result.languageOptions).to.deep.equal(languageOptions);
37+
});
38+
39+
it('should add path and cacheKey to context', () => {
40+
const mockContext = {
41+
settings,
42+
parserOptions,
43+
parserPath,
44+
};
45+
46+
const result = childContext(path, mockContext);
47+
48+
expect(result.path).to.equal(path);
49+
expect(result.cacheKey).to.be.a('string');
50+
});
51+
});

0 commit comments

Comments
 (0)