Skip to content

Commit

Permalink
[Fix] default, ExportMap: Resolve extended TypeScript configurati…
Browse files Browse the repository at this point in the history
…on files

Fixes #1908.
  • Loading branch information
mrmckeb authored and ljharb committed Sep 28, 2021
1 parent dd81424 commit 9a744f7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Fixed
- [`no-unresolved`]: ignore type-only imports ([#2220], thanks [@jablko])
- [`order`]: fix sorting imports inside TypeScript module declarations ([#2226], thanks [@remcohaszing])
- [`default`], `ExportMap`: Resolve extended TypeScript configuration files ([#2240], thanks [@mrmckeb])

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

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

[#2240]: https://github.com/import-js/eslint-plugin-import/pull/2240
[#2233]: https://github.com/import-js/eslint-plugin-import/pull/2233
[#2226]: https://github.com/import-js/eslint-plugin-import/pull/2226
[#2220]: https://github.com/import-js/eslint-plugin-import/pull/2220
Expand Down Expand Up @@ -1509,6 +1511,7 @@ for info on changes for earlier releases.
[@mgwalker]: https://github.com/mgwalker
[@MikeyBeLike]: https://github.com/MikeyBeLike
[@mplewis]: https://github.com/mplewis
[@mrmckeb]: https://github.com/mrmckeb
[@nickofthyme]: https://github.com/nickofthyme
[@nicolashenry]: https://github.com/nicolashenry
[@noelebrun]: https://github.com/noelebrun
Expand Down
22 changes: 13 additions & 9 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import { dirname } from 'path';

import doctrine from 'doctrine';

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

import includes from 'array-includes';

let parseConfigFileTextToJson;
let ts;

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

Expand Down Expand Up @@ -525,12 +526,15 @@ ExportMap.parse = function (path, content, context) {
});
try {
if (tsConfigInfo.tsConfigPath !== undefined) {
const jsonText = fs.readFileSync(tsConfigInfo.tsConfigPath).toString();
if (!parseConfigFileTextToJson) {
// this is because projects not using TypeScript won't have typescript installed
({ parseConfigFileTextToJson } = require('typescript'));
}
return parseConfigFileTextToJson(tsConfigInfo.tsConfigPath, jsonText).config;
// Projects not using TypeScript won't have `typescript` installed.
if (!ts) { ts = require('typescript'); }

const configFile = ts.readConfigFile(tsConfigInfo.tsConfigPath, ts.sys.readFile);
return ts.parseJsonConfigFileContent(
configFile.config,
ts.sys,
dirname(tsConfigInfo.tsConfigPath),
);
}
} catch (e) {
// Catch any errors
Expand All @@ -545,11 +549,11 @@ ExportMap.parse = function (path, content, context) {
}).digest('hex');
let tsConfig = tsConfigCache.get(cacheKey);
if (typeof tsConfig === 'undefined') {
tsConfig = readTsConfig();
tsConfig = readTsConfig(context);
tsConfigCache.set(cacheKey, tsConfig);
}

return tsConfig && tsConfig.compilerOptions ? tsConfig.compilerOptions.esModuleInterop : false;
return tsConfig && tsConfig.options ? tsConfig.options.esModuleInterop : false;
}

ast.body.forEach(function (n) {
Expand Down
3 changes: 3 additions & 0 deletions tests/files/typescript-extended-config/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export = FooBar;

declare namespace FooBar {}
5 changes: 5 additions & 0 deletions tests/files/typescript-extended-config/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"esModuleInterop": true
}
}
4 changes: 4 additions & 0 deletions tests/files/typescript-extended-config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {}
}
11 changes: 11 additions & 0 deletions tests/src/rules/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ context('TypeScript', function () {
tsconfigRootDir: path.resolve(__dirname, '../../files/typescript-export-react-test-renderer/'),
},
}),
test({
code: `import Foo from "./typescript-extended-config"`,
parser,
settings: {
'import/parsers': { [parser]: ['.ts'] },
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
parserOptions: {
tsconfigRootDir: path.resolve(__dirname, '../../files/typescript-extended-config/'),
},
}),
test({
code: `import foobar from "./typescript-export-assign-property"`,
parser,
Expand Down

0 comments on commit 9a744f7

Please sign in to comment.