Skip to content

Commit

Permalink
fix(flatConfig): include languageOptions in context
Browse files Browse the repository at this point in the history
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
  • Loading branch information
michaelfaith committed Sep 5, 2024
1 parent 18787d3 commit 6ba9c0d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/exportMap/childContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let prevSettings = '';
* also calculate a cacheKey, where parts of the cacheKey hash are memoized
*/
export default function childContext(path, context) {
const { settings, parserOptions, parserPath } = context;
const { settings, parserOptions, parserPath, languageOptions } = context;

if (JSON.stringify(settings) !== prevSettings) {
settingsHash = hashObject({ settings }).digest('hex');
Expand All @@ -28,5 +28,6 @@ export default function childContext(path, context) {
parserOptions,
parserPath,
path,
languageOptions,
};
}
51 changes: 51 additions & 0 deletions tests/src/exportMap/childContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { expect } from 'chai';

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

describe('childContext', () => {
const settings = {
setting1: true,
setting2: false,
};
const parserOptions = {
ecmaVersion: 'latest',
sourceType: 'module',
};
const parserPath = 'path/to/parser';
const path = 'path/to/src/file';
const languageOptions = {
ecmaVersion: 2024,
sourceType: 'module',
parser: {},
};

// https://github.com/import-js/eslint-plugin-import/issues/3051
it('should pass context properties through, if present', () => {
const mockContext = {
settings,
parserOptions,
parserPath,
languageOptions,
};

const result = childContext(path, mockContext);

expect(result.settings).to.deep.equal(settings);
expect(result.parserOptions).to.deep.equal(parserOptions);
expect(result.parserPath).to.equal(parserPath);
expect(result.languageOptions).to.deep.equal(languageOptions);
});

it('should add path and cacheKey to context', () => {
const mockContext = {
settings,
parserOptions,
parserPath,
};

const result = childContext(path, mockContext);

expect(result.path).to.equal(path);
expect(result.cacheKey).to.be.a('string');
});
});

0 comments on commit 6ba9c0d

Please sign in to comment.