Skip to content

Commit 9a744f7

Browse files
mrmckebljharb
authored andcommitted
[Fix] default, ExportMap: Resolve extended TypeScript configuration files
Fixes #1908.
1 parent dd81424 commit 9a744f7

File tree

6 files changed

+39
-9
lines changed

6 files changed

+39
-9
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1515
### Fixed
1616
- [`no-unresolved`]: ignore type-only imports ([#2220], thanks [@jablko])
1717
- [`order`]: fix sorting imports inside TypeScript module declarations ([#2226], thanks [@remcohaszing])
18+
- [`default`], `ExportMap`: Resolve extended TypeScript configuration files ([#2240], thanks [@mrmckeb])
1819

1920
### Changed
2021
- [Refactor] switch to an internal replacement for `pkg-up` and `read-pkg-up` ([#2047], thanks [@mgwalker])
@@ -915,6 +916,7 @@ for info on changes for earlier releases.
915916

916917
[`memo-parser`]: ./memo-parser/README.md
917918

919+
[#2240]: https://github.com/import-js/eslint-plugin-import/pull/2240
918920
[#2233]: https://github.com/import-js/eslint-plugin-import/pull/2233
919921
[#2226]: https://github.com/import-js/eslint-plugin-import/pull/2226
920922
[#2220]: https://github.com/import-js/eslint-plugin-import/pull/2220
@@ -1509,6 +1511,7 @@ for info on changes for earlier releases.
15091511
[@mgwalker]: https://github.com/mgwalker
15101512
[@MikeyBeLike]: https://github.com/MikeyBeLike
15111513
[@mplewis]: https://github.com/mplewis
1514+
[@mrmckeb]: https://github.com/mrmckeb
15121515
[@nickofthyme]: https://github.com/nickofthyme
15131516
[@nicolashenry]: https://github.com/nicolashenry
15141517
[@noelebrun]: https://github.com/noelebrun

Diff for: src/ExportMap.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs';
2+
import { dirname } from 'path';
23

34
import doctrine from 'doctrine';
45

@@ -18,7 +19,7 @@ import { tsConfigLoader } from 'tsconfig-paths/lib/tsconfig-loader';
1819

1920
import includes from 'array-includes';
2021

21-
let parseConfigFileTextToJson;
22+
let ts;
2223

2324
const log = debug('eslint-plugin-import:ExportMap');
2425

@@ -525,12 +526,15 @@ ExportMap.parse = function (path, content, context) {
525526
});
526527
try {
527528
if (tsConfigInfo.tsConfigPath !== undefined) {
528-
const jsonText = fs.readFileSync(tsConfigInfo.tsConfigPath).toString();
529-
if (!parseConfigFileTextToJson) {
530-
// this is because projects not using TypeScript won't have typescript installed
531-
({ parseConfigFileTextToJson } = require('typescript'));
532-
}
533-
return parseConfigFileTextToJson(tsConfigInfo.tsConfigPath, jsonText).config;
529+
// Projects not using TypeScript won't have `typescript` installed.
530+
if (!ts) { ts = require('typescript'); }
531+
532+
const configFile = ts.readConfigFile(tsConfigInfo.tsConfigPath, ts.sys.readFile);
533+
return ts.parseJsonConfigFileContent(
534+
configFile.config,
535+
ts.sys,
536+
dirname(tsConfigInfo.tsConfigPath),
537+
);
534538
}
535539
} catch (e) {
536540
// Catch any errors
@@ -545,11 +549,11 @@ ExportMap.parse = function (path, content, context) {
545549
}).digest('hex');
546550
let tsConfig = tsConfigCache.get(cacheKey);
547551
if (typeof tsConfig === 'undefined') {
548-
tsConfig = readTsConfig();
552+
tsConfig = readTsConfig(context);
549553
tsConfigCache.set(cacheKey, tsConfig);
550554
}
551555

552-
return tsConfig && tsConfig.compilerOptions ? tsConfig.compilerOptions.esModuleInterop : false;
556+
return tsConfig && tsConfig.options ? tsConfig.options.esModuleInterop : false;
553557
}
554558

555559
ast.body.forEach(function (n) {

Diff for: tests/files/typescript-extended-config/index.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export = FooBar;
2+
3+
declare namespace FooBar {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"esModuleInterop": true
4+
}
5+
}

Diff for: tests/files/typescript-extended-config/tsconfig.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"compilerOptions": {}
4+
}

Diff for: tests/src/rules/default.js

+11
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@ context('TypeScript', function () {
231231
tsconfigRootDir: path.resolve(__dirname, '../../files/typescript-export-react-test-renderer/'),
232232
},
233233
}),
234+
test({
235+
code: `import Foo from "./typescript-extended-config"`,
236+
parser,
237+
settings: {
238+
'import/parsers': { [parser]: ['.ts'] },
239+
'import/resolver': { 'eslint-import-resolver-typescript': true },
240+
},
241+
parserOptions: {
242+
tsconfigRootDir: path.resolve(__dirname, '../../files/typescript-extended-config/'),
243+
},
244+
}),
234245
test({
235246
code: `import foobar from "./typescript-export-assign-property"`,
236247
parser,

0 commit comments

Comments
 (0)