Skip to content

Commit 9c37a6c

Browse files
authored
[FIX] Don't require baseUrl (import-js#8)
Fixes import-js#6
1 parent f82c1ff commit 9c37a6c

20 files changed

+89
-41
lines changed

dummy.js/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'dummy';

dummy.js/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "dummy.js",
33
"version": "1.0.0",
4+
"main": "index.js",
45
"private": true
56
}

index.js

+45-34
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,55 @@
1-
'use strict'
1+
'use strict';
22

3-
const path = require('path')
4-
const resolve = require('resolve')
5-
const tsconfigPaths = require('tsconfig-paths')
6-
const debug = require('debug')
3+
const path = require('path');
4+
const resolve = require('resolve');
5+
const tsconfigPaths = require('tsconfig-paths');
6+
const debug = require('debug');
77

8-
const log = debug('eslint-import-resolver-typescript')
8+
const log = debug('eslint-import-resolver-typescript');
99

1010
/**
1111
* @param {string} source the module to resolve; i.e './some-module'
1212
* @param {string} file the importing file's full path; i.e. '/usr/local/bin/file.js'
1313
*/
1414
function resolveFile(source, file, config) {
15-
log('looking for:', source)
15+
log('looking for:', source);
1616

1717
// don't worry about core node modules
1818
if (resolve.isCore(source)) {
19-
log('matched core:', source)
19+
log('matched core:', source);
2020

2121
return {
2222
found: true,
2323
path: null,
24-
}
24+
};
2525
}
2626

27+
let foundTsPath = null;
28+
const extensions = Object.keys(require.extensions).concat(
29+
'.ts',
30+
'.tsx',
31+
'.d.ts',
32+
);
33+
2734
// setup tsconfig-paths
28-
const searchStart = config.directory || process.cwd()
29-
const configLoaderResult = tsconfigPaths.loadConfig(searchStart)
30-
if (configLoaderResult.resultType !== 'success') {
31-
throw new Error(`Unable to find tsconfig in ${searchStart}: ${configLoaderResult.message}`)
32-
}
33-
const matchPath = tsconfigPaths.createMatchPath(
34-
configLoaderResult.absoluteBaseUrl,
35-
configLoaderResult.paths,
36-
)
35+
const searchStart = config.directory || process.cwd();
36+
const configLoaderResult = tsconfigPaths.loadConfig(searchStart);
37+
if (configLoaderResult.resultType === 'success') {
38+
const matchPath = tsconfigPaths.createMatchPath(
39+
configLoaderResult.absoluteBaseUrl,
40+
configLoaderResult.paths,
41+
);
3742

38-
// look for files based on setup tsconfig "paths"
39-
const extensions = Object.keys(require.extensions).concat('.ts', '.tsx', '.d.ts')
40-
const foundTsPath = matchPath(
41-
source,
42-
undefined,
43-
undefined,
44-
extensions,
45-
)
43+
// look for files based on setup tsconfig "paths"
44+
foundTsPath = matchPath(source, undefined, undefined, extensions);
4645

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

5155
// note that even if we match via tsconfig-paths, we still need to do a final resolve
@@ -54,28 +58,35 @@ function resolveFile(source, file, config) {
5458
foundNodePath = resolve.sync(foundTsPath || source, {
5559
extensions,
5660
basedir: path.dirname(path.resolve(file)),
57-
})
61+
packageFilter,
62+
});
5863
} catch (err) {
5964
foundNodePath = null;
6065
}
6166

6267
if (foundNodePath) {
63-
log('matched node path:', foundNodePath)
68+
log('matched node path:', foundNodePath);
6469

6570
return {
6671
found: true,
6772
path: foundNodePath,
68-
}
73+
};
6974
}
7075

71-
log('didnt find', source)
76+
log('didnt find', source);
7277

7378
return {
74-
found: false
79+
found: false,
80+
};
81+
}
82+
function packageFilter(pkg) {
83+
if (pkg['jsnext:main']) {
84+
pkg['main'] = pkg['jsnext:main'];
7585
}
86+
return pkg;
7687
}
7788

7889
module.exports = {
7990
interfaceVersion: 2,
8091
resolve: resolveFile,
81-
}
92+
};

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
"eslint-plugin-import": "*"
3030
},
3131
"devDependencies": {
32+
"dummy.js": "file:dummy.js",
3233
"eslint": "^5.6.1",
3334
"eslint-plugin-import": "^2.14.0",
3435
"typescript": "^3.1.1"
3536
},
3637
"scripts": {
37-
"test": "eslint ./tests/index.ts"
38+
"test": "eslint ./tests/withPaths/index.ts && eslint ./tests/withoutPaths/index.ts"
3839
}
3940
}

tests/.eslintrc.js tests/baseEslintConfig.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const path = require('path')
22

3-
module.exports = {
3+
module.exports = (dirname) => ({
44
env: {
55
es6: true,
66
},
@@ -13,13 +13,22 @@ module.exports = {
1313
],
1414
rules: {
1515
'import/no-unresolved': 'error',
16-
'import/extensions': 'error',
16+
'import/extensions': [
17+
'error',
18+
'ignorePackages',
19+
{
20+
js: 'never',
21+
jsx: 'never',
22+
ts: 'never',
23+
tsx: 'never',
24+
},
25+
],
1726
},
1827
settings: {
1928
'import/resolver': {
2029
[path.resolve(`${__dirname}/../index.js`)]: {
21-
directory: __dirname,
30+
directory: dirname,
2231
},
2332
},
2433
},
25-
}
34+
})

tests/withPaths/.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../baseEslintConfig')(__dirname)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/tsconfig.json tests/withPaths/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"jsx": "react",
55
"paths": {
66
"folder/*": ["*"],
7-
"*": ["../node_modules/*"]
7+
"*": ["../../node_modules/*"]
88
},
99
},
1010
"files": [
File renamed without changes.

tests/withoutPaths/.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../baseEslintConfig')(__dirname)

tests/withoutPaths/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// import relative
2+
import './tsImportee'
3+
import './tsxImportee'
4+
import './subfolder/tsImportee'
5+
import './subfolder/tsxImportee'
6+
7+
// import from node_module
8+
import 'typescript'
9+
import 'dummy.js'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'yes'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'React Component'

tests/withoutPaths/tsImportee.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'yes'

tests/withoutPaths/tsconfig.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react",
4+
},
5+
"files": [
6+
"index.ts",
7+
"tsImportee.ts",
8+
"tsxImportee.tsx"
9+
]
10+
}

tests/withoutPaths/tsxImportee.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'React Component'

0 commit comments

Comments
 (0)