Skip to content

Commit 4a70654

Browse files
committed
fix(utils): export map cache is tainted by unreliable parse results
1 parent 58c7341 commit 4a70654

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/exportMap/builder.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ export default class ExportMapBuilder {
9292

9393
exportMap.mtime = stats.mtime;
9494

95-
exportCache.set(cacheKey, exportMap);
95+
// If the visitor keys were not populated, then we shouldn't save anything to the cache,
96+
// since the parse results may not be reliable.
97+
if (exportMap.visitorKeys) {
98+
exportCache.set(cacheKey, exportMap);
99+
}
96100
return exportMap;
97101
}
98102

tests/src/core/getExports.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import * as fs from 'fs';
1111
import { getFilename } from '../utils';
1212
import { test as testUnambiguous } from 'eslint-module-utils/unambiguous';
1313

14+
const doesBabelHaveVisitorKeys = () => {
15+
const babelPath = require.resolve('babel-eslint');
16+
const hypotheticalLocation = babelPath.replace('index.js', 'visitor-keys.js');
17+
return fs.existsSync(hypotheticalLocation);
18+
};
19+
1420
describe('ExportMap', function () {
1521
const fakeContext = Object.assign(
1622
semver.satisfies(eslintPkg.version, '>= 7.28') ? {
@@ -21,10 +27,12 @@ describe('ExportMap', function () {
2127
},
2228
{
2329
settings: {},
24-
parserPath: 'babel-eslint',
30+
parserPath: require.resolve('babel-eslint'),
2531
},
2632
);
2733

34+
const isVisitorKeysSupported = doesBabelHaveVisitorKeys();
35+
2836
it('handles ExportAllDeclaration', function () {
2937
let imports;
3038
expect(function () {
@@ -36,11 +44,20 @@ describe('ExportMap', function () {
3644

3745
});
3846

39-
it('returns a cached copy on subsequent requests', function () {
47+
isVisitorKeysSupported && it('returns a cached copy on subsequent requests', function () {
4048
expect(ExportMapBuilder.get('./named-exports', fakeContext))
4149
.to.exist.and.equal(ExportMapBuilder.get('./named-exports', fakeContext));
4250
});
4351

52+
it('does not return a cached copy if the parse does not yield a visitor keys', function () {
53+
const mockContext = {
54+
...fakeContext,
55+
parserPath: 'not-real',
56+
};
57+
expect(ExportMapBuilder.get('./named-exports', mockContext))
58+
.to.exist.and.not.equal(ExportMapBuilder.get('./named-exports', mockContext));
59+
});
60+
4461
it('does not return a cached copy after modification', (done) => {
4562
const firstAccess = ExportMapBuilder.get('./mutator', fakeContext);
4663
expect(firstAccess).to.exist;

0 commit comments

Comments
 (0)