Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve extended TypeScript configuration files #2240

Merged
merged 1 commit into from Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
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
@@ -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(
ljharb marked this conversation as resolved.
Show resolved Hide resolved
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
@@ -0,0 +1,3 @@
export = FooBar;

declare namespace FooBar {}
5 changes: 5 additions & 0 deletions tests/files/typescript-extended-config/tsconfig.base.json
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"esModuleInterop": true
}
}
4 changes: 4 additions & 0 deletions tests/files/typescript-extended-config/tsconfig.json
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {}
}
11 changes: 11 additions & 0 deletions tests/src/rules/default.js
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