From 4ccce6f58ed2595c89a1e872c5788c69489ab85d Mon Sep 17 00:00:00 2001 From: Nikita Stenin Date: Wed, 24 Aug 2022 14:14:41 +0300 Subject: [PATCH] [Fix] `ExportMap`: add caching after parsing for an ambiguous module --- src/ExportMap.js | 6 +++++- tests/files/typescript-declare-module.ts | 3 +++ tests/src/core/getExports.js | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/files/typescript-declare-module.ts diff --git a/src/ExportMap.js b/src/ExportMap.js index e18797a4d7..885801fbbb 100644 --- a/src/ExportMap.js +++ b/src/ExportMap.js @@ -346,7 +346,11 @@ ExportMap.for = function (context) { exportMap = ExportMap.parse(path, content, context); // ambiguous modules return null - if (exportMap == null) return null; + if (exportMap == null) { + log('ignored path due to ambiguous parse:', path); + exportCache.set(cacheKey, null); + return null; + } exportMap.mtime = stats.mtime; diff --git a/tests/files/typescript-declare-module.ts b/tests/files/typescript-declare-module.ts new file mode 100644 index 0000000000..8a9e304e91 --- /dev/null +++ b/tests/files/typescript-declare-module.ts @@ -0,0 +1,3 @@ +declare module "typescript-declare-module-foo" { + export const foo: string; +} diff --git a/tests/src/core/getExports.js b/tests/src/core/getExports.js index 867644bc19..9b1100adfd 100644 --- a/tests/src/core/getExports.js +++ b/tests/src/core/getExports.js @@ -104,6 +104,27 @@ describe('ExportMap', function () { expect(imports.has('Bar')).to.be.true; }); + it('should cache after parsing for an ambiguous module', function () { + const context = Object.assign({}, fakeContext, { + settings: { + 'import/extensions': ['.js'], + 'import/parsers': { + '@typescript-eslint/parser': ['.ts', '.tsx'], + }, + }, + }); + const source = './typescript-declare-module.ts'; + const parseSpy = sinon.spy(ExportMap, 'parse'); + + expect(ExportMap.get(source, context)).to.be.null; + + ExportMap.get(source, context); + + expect(parseSpy.callCount).to.equal(1); + + parseSpy.restore(); + }); + context('deprecation metadata', function () { function jsdocTests(parseContext, lineEnding) {