diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b0f95e28..264b20c73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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]) @@ -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 @@ -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 diff --git a/src/ExportMap.js b/src/ExportMap.js index 53091e466..d818fa6ca 100644 --- a/src/ExportMap.js +++ b/src/ExportMap.js @@ -1,4 +1,5 @@ import fs from 'fs'; +import { dirname } from 'path'; import doctrine from 'doctrine'; @@ -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'); @@ -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 @@ -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) { diff --git a/tests/files/typescript-extended-config/index.d.ts b/tests/files/typescript-extended-config/index.d.ts new file mode 100644 index 000000000..2ad4822f7 --- /dev/null +++ b/tests/files/typescript-extended-config/index.d.ts @@ -0,0 +1,3 @@ +export = FooBar; + +declare namespace FooBar {} diff --git a/tests/files/typescript-extended-config/tsconfig.base.json b/tests/files/typescript-extended-config/tsconfig.base.json new file mode 100644 index 000000000..2f9804271 --- /dev/null +++ b/tests/files/typescript-extended-config/tsconfig.base.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "esModuleInterop": true + } +} diff --git a/tests/files/typescript-extended-config/tsconfig.json b/tests/files/typescript-extended-config/tsconfig.json new file mode 100644 index 000000000..97a330960 --- /dev/null +++ b/tests/files/typescript-extended-config/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": {} +} diff --git a/tests/src/rules/default.js b/tests/src/rules/default.js index 15101fc0c..0274e4374 100644 --- a/tests/src/rules/default.js +++ b/tests/src/rules/default.js @@ -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,