diff --git a/.gitignore b/.gitignore index 3c3629e6..9adf50ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ node_modules +yarn-error.log +yarn.lock diff --git a/README.md b/README.md index efc7921f..ea70da06 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,23 @@ # eslint-import-resolver-typescript -This plugin allows you to use `eslint-plugin-import` with `.ts` and `.tsx` files. +This plugin allows you to resolve `typescript` files with `eslint-plugin-import`. + +The resolution respects the [`paths`](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping) you have defined in your `tsconfig.json`. ![](screenshot.png) ## Installation -``` +```bash npm install --save-dev eslint-import-resolver-typescript ``` Add the following to your eslint config: -``` +```JSON "settings": { "import/resolver": { - "node": true, - "eslint-import-resolver-typescript": true + "typescript": true } } ``` - diff --git a/index.js b/index.js index 75d0712b..92e412a1 100644 --- a/index.js +++ b/index.js @@ -1,26 +1,76 @@ -const resolve = require('resolve'); -const path = require('path'); - -function opts(file, config) { - return Object.assign( - { extensions: ['.ts', '.tsx', '.d.ts'] }, - config, - // path.resolve will handle paths relative to CWD - { basedir: path.dirname(path.resolve(file)) } - ); -} +'use strict' -module.exports = { - interfaceVersion: 2, - resolve: function(source, file, config) { - if (resolve.isCore(source)) { - return { found: true, path: null }; +const path = require('path') +const resolve = require('resolve') +const tsconfigPaths = require('tsconfig-paths') +const debug = require('debug') + +const log = debug('eslint-import-resolver-typescript') + +/** + * @param {string} source the module to resolve; i.e './some-module' + * @param {string} file the importing file's full path; i.e. '/usr/local/bin/file.js' + */ +function resolveFile(source, file, config) { + log('looking for:', source) + + // don't worry about core node modules + if (resolve.isCore(source)) { + log('matched core:', source) + + return { + found: true, + path: null, } + } + + // setup tsconfig-paths + const searchStart = config.directory || process.cwd() + const configLoaderResult = tsconfigPaths.loadConfig(searchStart) + if (configLoaderResult.resultType !== 'success') { + throw new Error(`Unable to find tsconfig in ${searchStart}: ${configLoaderResult.message}`) + } + const matchPath = tsconfigPaths.createMatchPath( + configLoaderResult.absoluteBaseUrl, + configLoaderResult.paths, + ) + + // look for files based on setup tsconfig "paths" + const extensions = Object.keys(require.extensions).concat('.ts', '.tsx', '.d.ts') + const foundTsPath = matchPath( + source, + undefined, + undefined, + extensions, + ) - try { - return { found: true, path: resolve.sync(source, opts(file, config)) }; - } catch (err) { - return { found: false }; + if (foundTsPath) { + log('matched ts path:', foundTsPath) + } + + // note that even if we match via tsconfig-paths, we still need to do a final resolve + const foundNodePath = resolve.sync(foundTsPath || source, { + extensions, + basedir: path.dirname(path.resolve(file)), + }) + + if (foundNodePath) { + log('matched node path:', foundNodePath) + + return { + found: true, + path: foundNodePath, } - }, -}; + } + + log('didnt find', source) + + return { + found: false + } +} + +module.exports = { + interfaceVersion: 2, + resolve: resolveFile, +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 79e3f913..2195e42a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,9 +1,45 @@ { - "name": "eslint-plugin-import-typescript-resolver", + "name": "eslint-import-resolver-typescript", "version": "1.0.2", "lockfileVersion": 1, "requires": true, "dependencies": { + "@types/json5": { + "version": "0.0.29", + "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", + "integrity": "sha1-7ihweulOEdK4J7y+UnC86n8+ce4=" + }, + "debug": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.0.1.tgz", + "integrity": "sha512-K23FHJ/Mt404FSlp6gSZCevIbTMLX0j3fmHhUEhQ3Wq0FMODW3+cUSoLdy1Gx4polAf4t/lphhmHH35BB8cLYw==", + "requires": { + "ms": "^2.1.1" + } + }, + "deepmerge": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-2.1.1.tgz", + "integrity": "sha512-urQxA1smbLZ2cBbXbaYObM1dJ82aJ2H57A1C/Kklfh/ZN1bgH4G/n5KWhdNfOK11W98gqZfyYj7W4frJJRwA2w==" + }, + "json5": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", + "integrity": "sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==", + "requires": { + "minimist": "^1.2.0" + } + }, + "minimist": { + "version": "1.2.0", + "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" + }, + "ms": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", + "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==" + }, "path-parse": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.5.tgz", @@ -14,7 +50,24 @@ "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.4.0.tgz", "integrity": "sha512-aW7sVKPufyHqOmyyLzg/J+8606v5nevBgaliIlV7nUpVMsDnoBGV/cbSLNjZAg9q0Cfd/+easKVKQ8vOu8fn1Q==", "requires": { - "path-parse": "1.0.5" + "path-parse": "^1.0.5" + } + }, + "strip-bom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", + "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=" + }, + "tsconfig-paths": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.6.0.tgz", + "integrity": "sha512-mrqQIP2F4e03aMTCiPdedCIT300//+q0ET53o5WqqtQjmEICxP9yfz/sHTpPqXpssuJEzODsEzJaLRaf5J2X1g==", + "requires": { + "@types/json5": "^0.0.29", + "deepmerge": "^2.0.1", + "json5": "^1.0.1", + "minimist": "^1.2.0", + "strip-bom": "^3.0.0" } } } diff --git a/package.json b/package.json index dc0667eb..211bebc1 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,9 @@ "author": "Alex Gorbatchev ", "license": "ISC", "dependencies": { - "resolve": "^1.4.0" - } + "debug": "^4.0.1", + "resolve": "^1.4.0", + "tsconfig-paths": "^3.6.0" + }, + "devDependencies": {} }