Skip to content

Commit 6e54fc8

Browse files
committed
Merge remote-tracking branch 'upstream2/master' into support-multiple-tsconfigs-2
2 parents af7cfad + 0fa2628 commit 6e54fc8

File tree

7 files changed

+74
-17
lines changed

7 files changed

+74
-17
lines changed

index.js

+40-8
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ const isGlob = require('is-glob');
99

1010
const log = debug('eslint-import-resolver-typescript');
1111

12-
const extensions = Object.keys(require.extensions).concat(
13-
'.ts',
14-
'.tsx',
15-
'.d.ts',
12+
const extensions = ['.ts', '.tsx', '.d.ts'].concat(
13+
Object.keys(require.extensions),
1614
);
1715

1816
/**
@@ -50,6 +48,25 @@ function resolveFile(source, file, options = {}) {
5048
foundNodePath = null;
5149
}
5250

51+
// naive attempt at @types/* resolution,
52+
// if path is neither absolute nor relative
53+
if (
54+
(/\.jsx?$/.test(foundNodePath) ||
55+
(options.alwaysTryTypes && !foundNodePath)) &&
56+
!/^@types[/\\]/.test(source) &&
57+
!path.isAbsolute(source) &&
58+
source[0] !== '.'
59+
) {
60+
const definitelyTyped = resolveFile(
61+
'@types' + path.sep + mangleScopedPackage(source),
62+
file,
63+
options,
64+
);
65+
if (definitelyTyped.found) {
66+
return definitelyTyped;
67+
}
68+
}
69+
5370
if (foundNodePath) {
5471
log('matched node path:', foundNodePath);
5572

@@ -59,17 +76,16 @@ function resolveFile(source, file, options = {}) {
5976
};
6077
}
6178

62-
log('didnt find', source);
79+
log("didn't find", source);
6380

6481
return {
6582
found: false,
6683
};
6784
}
6885

6986
function packageFilter(pkg) {
70-
if (pkg['jsnext:main']) {
71-
pkg['main'] = pkg['jsnext:main'];
72-
}
87+
pkg.main =
88+
pkg.types || pkg.typings || pkg.module || pkg['jsnext:main'] || pkg.main;
7389
return pkg;
7490
}
7591

@@ -141,6 +157,22 @@ function initMappers(options) {
141157
});
142158
}
143159

160+
/*
161+
* For a scoped package, we must look in `@types/foo__bar` instead of `@types/@foo/bar`.
162+
*
163+
* @param {string} moduleName
164+
* @returns {string}
165+
*/
166+
function mangleScopedPackage(moduleName) {
167+
if (moduleName[0] === '@') {
168+
const replaceSlash = moduleName.replace(path.sep, '__');
169+
if (replaceSlash !== moduleName) {
170+
return replaceSlash.slice(1); // Take off the "@"
171+
}
172+
}
173+
return moduleName;
174+
}
175+
144176
module.exports = {
145177
interfaceVersion: 2,
146178
resolve: resolveFile,

package-lock.json

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

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
2-
"name": "eslint-import-resolver-typescript",
3-
"version": "1.1.1",
2+
"name": "eslint-import-resolver-ts",
3+
"version": "0.1.0",
44
"description": "TypeScript .ts .tsx module resolver for `eslint-plugin-import`.",
55
"main": "index.js",
66
"files": [
77
"index.js"
88
],
99
"repository": {
1010
"type": "git",
11-
"url": "https://github.com/alexgorbatchev/eslint-import-resolver-typescript"
11+
"url": "https://github.com/rx-ts/eslint-import-resolver-ts"
1212
},
1313
"keywords": [
1414
"typescript",
@@ -31,6 +31,7 @@
3131
"eslint-plugin-import": "*"
3232
},
3333
"devDependencies": {
34+
"@types/unist": "^2.0.3",
3435
"dummy.js": "file:dummy.js",
3536
"eslint": "^5.6.1",
3637
"eslint-plugin-import": "^2.14.0",

tests/baseEslintConfig.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = dirname => ({
2626
'import/resolver': {
2727
[path.resolve(`${__dirname}/../index.js`)]: {
2828
directory: dirname,
29+
alwaysTryTypes: true
2930
},
3031
},
3132
},

tests/withoutPaths/dtsImportee.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare const content : 'yes';
2+
3+
export default content;

tests/withoutPaths/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
// import relative
22
import './tsImportee'
33
import './tsxImportee'
4+
import './dtsImportee'
5+
import './subfolder/dtsImportee'
46
import './subfolder/tsImportee'
57
import './subfolder/tsxImportee'
68

79
// import from node_module
810
import 'typescript'
911
import 'dummy.js'
12+
13+
// import from `@types/`
14+
import 'json5'
15+
16+
// enable alwaysTryTypes
17+
import 'unist'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare const content : 'yes';
2+
3+
export default content;

0 commit comments

Comments
 (0)