Skip to content
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

[FIX] Don't require baseUrl #8

Merged
merged 4 commits into from
Oct 26, 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
1 change: 1 addition & 0 deletions dummy.js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'dummy';
1 change: 1 addition & 0 deletions dummy.js/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "dummy.js",
"version": "1.0.0",
"main": "index.js",
"private": true
}
79 changes: 45 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
'use strict'
'use strict';

const path = require('path')
const resolve = require('resolve')
const tsconfigPaths = require('tsconfig-paths')
const debug = require('debug')
const path = require('path');
const resolve = require('resolve');
const tsconfigPaths = require('tsconfig-paths');
const debug = require('debug');

const log = debug('eslint-import-resolver-typescript')
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)
log('looking for:', source);

// don't worry about core node modules
if (resolve.isCore(source)) {
log('matched core:', source)
log('matched core:', source);

return {
found: true,
path: null,
}
};
}

let foundTsPath = null;
const extensions = Object.keys(require.extensions).concat(
'.ts',
'.tsx',
'.d.ts',
);

// 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,
)
const searchStart = config.directory || process.cwd();
const configLoaderResult = tsconfigPaths.loadConfig(searchStart);
if (configLoaderResult.resultType === 'success') {
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,
)
// look for files based on setup tsconfig "paths"
foundTsPath = matchPath(source, undefined, undefined, extensions);

if (foundTsPath) {
log('matched ts path:', foundTsPath)
if (foundTsPath) {
log('matched ts path:', foundTsPath);
}
} else {
log('failed to init tsconfig-paths:', configLoaderResult.message);
// this can happen if the user has problems with their tsconfig
// or if it's valid, but they don't have baseUrl set
}

// note that even if we match via tsconfig-paths, we still need to do a final resolve
Expand All @@ -54,28 +58,35 @@ function resolveFile(source, file, config) {
foundNodePath = resolve.sync(foundTsPath || source, {
extensions,
basedir: path.dirname(path.resolve(file)),
})
packageFilter,
});
} catch (err) {
foundNodePath = null;
}

if (foundNodePath) {
log('matched node path:', foundNodePath)
log('matched node path:', foundNodePath);

return {
found: true,
path: foundNodePath,
}
};
}

log('didnt find', source)
log('didnt find', source);

return {
found: false
found: false,
};
}
function packageFilter(pkg) {
if (pkg['jsnext:main']) {
pkg['main'] = pkg['jsnext:main'];
}
return pkg;
}

module.exports = {
interfaceVersion: 2,
resolve: resolveFile,
}
};
2 changes: 1 addition & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
"eslint-plugin-import": "*"
},
"devDependencies": {
"dummy.js": "file:dummy.js",
"eslint": "^5.6.1",
"eslint-plugin-import": "^2.14.0",
"typescript": "^3.1.1"
},
"scripts": {
"test": "eslint ./tests/index.ts"
"test": "eslint ./tests/withPaths/index.ts && eslint ./tests/withoutPaths/index.ts"
}
}
17 changes: 13 additions & 4 deletions tests/.eslintrc.js → tests/baseEslintConfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path')

module.exports = {
module.exports = (dirname) => ({
env: {
es6: true,
},
Expand All @@ -13,13 +13,22 @@ module.exports = {
],
rules: {
'import/no-unresolved': 'error',
'import/extensions': 'error',
'import/extensions': [
'error',
'ignorePackages',
{
js: 'never',
jsx: 'never',
ts: 'never',
tsx: 'never',
},
],
},
settings: {
'import/resolver': {
[path.resolve(`${__dirname}/../index.js`)]: {
directory: __dirname,
directory: dirname,
},
},
},
}
})
1 change: 1 addition & 0 deletions tests/withPaths/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../baseEslintConfig')(__dirname)
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/tsconfig.json → tests/withPaths/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"jsx": "react",
"paths": {
"folder/*": ["*"],
"*": ["../node_modules/*"]
"*": ["../../node_modules/*"]
},
},
"files": [
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions tests/withoutPaths/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../baseEslintConfig')(__dirname)
9 changes: 9 additions & 0 deletions tests/withoutPaths/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// import relative
import './tsImportee'
import './tsxImportee'
import './subfolder/tsImportee'
import './subfolder/tsxImportee'

// import from node_module
import 'typescript'
import 'dummy.js'
1 change: 1 addition & 0 deletions tests/withoutPaths/subfolder/tsImportee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'yes'
1 change: 1 addition & 0 deletions tests/withoutPaths/subfolder/tsxImportee.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'React Component'
1 change: 1 addition & 0 deletions tests/withoutPaths/tsImportee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'yes'
10 changes: 10 additions & 0 deletions tests/withoutPaths/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"jsx": "react",
},
"files": [
"index.ts",
"tsImportee.ts",
"tsxImportee.tsx"
]
}
1 change: 1 addition & 0 deletions tests/withoutPaths/tsxImportee.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'React Component'