Skip to content

[FEAT] #2 added support for tsconfig paths #3

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

Merged
merged 1 commit into from
Oct 5, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
yarn-error.log
yarn.lock
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
}
}
```

94 changes: 72 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -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,
}
57 changes: 55 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"author": "Alex Gorbatchev <[email protected]>",
"license": "ISC",
"dependencies": {
"resolve": "^1.4.0"
}
"debug": "^4.0.1",
"resolve": "^1.4.0",
"tsconfig-paths": "^3.6.0"
},
"devDependencies": {}
}