From b11ede3c9fafbc548db011729fd64e958cde6e51 Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 30 Jun 2019 14:57:03 +0800 Subject: [PATCH] feat: resolve .ts/.tsx/.d.ts first, and then fallback to @types/* --- index.js | 24 ++++++++++++++++++------ package-lock.json | 8 +++++++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 7a14792..665223e 100644 --- a/index.js +++ b/index.js @@ -25,11 +25,9 @@ function resolveFile(source, file, config) { } let foundTsPath = null; - const extensions = Object.keys(require.extensions).concat( - '.ts', - '.tsx', - '.d.ts', - ); + const extensions = Object.keys(require.extensions); + + extensions.unshift('.ts', '.tsx', '.d.ts'); // setup tsconfig-paths const searchStart = config.directory || process.cwd(); @@ -64,6 +62,20 @@ function resolveFile(source, file, config) { foundNodePath = null; } + // naive attempt at @types/* resolution, + // iff path is neither absolute nor relative + if ( + /\.jsx?$/.test(foundNodePath) && + !/^@types[\/\\]/.test(source) && + !path.isAbsolute(source) && + source[0] !== '.' + ) { + const definitely = resolveFile('@types/' + source, file, config); + if (definitely.found) { + return definitely; + } + } + if (foundNodePath) { log('matched node path:', foundNodePath); @@ -73,7 +85,7 @@ function resolveFile(source, file, config) { }; } - log('didnt find', source); + log("didn't find", source); return { found: false, diff --git a/package-lock.json b/package-lock.json index c250156..abdb4fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "eslint-import-resolver-typescript", - "version": "1.1.0", + "version": "1.1.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1111,6 +1111,12 @@ "integrity": "sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=", "dev": true }, + "prettier": { + "version": "1.18.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.18.2.tgz", + "integrity": "sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==", + "dev": true + }, "progress": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/progress/-/progress-2.0.0.tgz",