From f3c1c4ca41f0ca33bacec20e5e3c47895f583065 Mon Sep 17 00:00:00 2001 From: Brody McKee Date: Tue, 28 Sep 2021 21:42:39 +0300 Subject: [PATCH] Address PR feedback --- src/ExportMap.js | 57 +++++++++---------- .../typescript-extended-config/index.d.ts | 3 + .../tsconfig.base.json | 5 +- .../typescript-extended-config/tsconfig.json | 4 +- tests/src/ExportMap.js | 21 ------- tests/src/rules/default.js | 11 ++++ 6 files changed, 43 insertions(+), 58 deletions(-) create mode 100644 tests/files/typescript-extended-config/index.d.ts delete mode 100644 tests/src/ExportMap.js diff --git a/src/ExportMap.js b/src/ExportMap.js index 8788b32a13..250343eb69 100644 --- a/src/ExportMap.js +++ b/src/ExportMap.js @@ -1,5 +1,5 @@ import fs from 'fs'; -import path from 'path'; +import { dirname } from 'path'; import doctrine from 'doctrine'; @@ -517,6 +517,32 @@ ExportMap.parse = function (path, content, context) { const source = makeSourceCode(content, ast); + function readTsConfig() { + const tsConfigInfo = tsConfigLoader({ + cwd: + (context.parserOptions && context.parserOptions.tsconfigRootDir) || + process.cwd(), + getEnv: (key) => process.env[key], + }); + try { + if (tsConfigInfo.tsConfigPath !== undefined) { + // 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 + } + + return null; + } + function isEsModuleInterop() { const cacheKey = hashObject({ tsconfigRootDir: context.parserOptions && context.parserOptions.tsconfigRootDir, @@ -763,32 +789,3 @@ function makeSourceCode(text, ast) { return new SourceCode({ text, ast }); } } - -/** - * Returns a project's TypeScript configuration. - */ -export function readTsConfig(context) { - const tsConfigInfo = tsConfigLoader({ - cwd: - (context.parserOptions && context.parserOptions.tsconfigRootDir) || - process.cwd(), - getEnv: (key) => process.env[key], - }); - try { - if (tsConfigInfo.tsConfigPath !== undefined) { - // 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, - path.dirname(tsConfigInfo.tsConfigPath), - ); - } - } catch (e) { - // Catch any errors - } - - return null; -} 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 0000000000..2ad4822f7c --- /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 index f8d8329212..2f98042715 100644 --- a/tests/files/typescript-extended-config/tsconfig.base.json +++ b/tests/files/typescript-extended-config/tsconfig.base.json @@ -1,8 +1,5 @@ { "compilerOptions": { - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, - "skipLibCheck": true, - "strict": true + "esModuleInterop": true } } diff --git a/tests/files/typescript-extended-config/tsconfig.json b/tests/files/typescript-extended-config/tsconfig.json index 4979fccb5e..97a3309607 100644 --- a/tests/files/typescript-extended-config/tsconfig.json +++ b/tests/files/typescript-extended-config/tsconfig.json @@ -1,6 +1,4 @@ { "extends": "./tsconfig.base.json", - "compilerOptions": { - "noEmit": true - } + "compilerOptions": {} } diff --git a/tests/src/ExportMap.js b/tests/src/ExportMap.js deleted file mode 100644 index d504de2219..0000000000 --- a/tests/src/ExportMap.js +++ /dev/null @@ -1,21 +0,0 @@ -import { expect } from 'chai'; -import { readTsConfig } from '../../src/ExportMap'; - -describe('ExportMap', function () { - describe('readTsConfig', function () { - const config = readTsConfig({ - parserOptions: { - tsconfigRootDir: './tests/files/typescript-extended-config', - }, - }); - expect(config.options).to.contain({ - // Rules from primary config. - noEmit: true, - // Rules from extended config. - esModuleInterop: true, - forceConsistentCasingInFileNames: true, - skipLibCheck: true, - strict: true, - }); - }); -}); diff --git a/tests/src/rules/default.js b/tests/src/rules/default.js index 15101fc0cd..0274e43745 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,