Skip to content

Commit 6bb231e

Browse files
Merge pull request import-js#20 from rx-ts/feat/resolve_dts
resolve .ts/.tsx/.d.ts first, and then fallback to @types/*
2 parents 18eafa2 + 23e2e8c commit 6bb231e

File tree

7 files changed

+70
-9
lines changed

7 files changed

+70
-9
lines changed

index.js

+41-8
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ function resolveFile(source, file, config) {
2525
}
2626

2727
let foundTsPath = null;
28-
const extensions = Object.keys(require.extensions).concat(
29-
'.ts',
30-
'.tsx',
31-
'.d.ts',
28+
const extensions = ['.ts', '.tsx', '.d.ts'].concat(
29+
Object.keys(require.extensions),
3230
);
3331

3432
// setup tsconfig-paths
@@ -64,6 +62,25 @@ function resolveFile(source, file, config) {
6462
foundNodePath = null;
6563
}
6664

65+
// naive attempt at @types/* resolution,
66+
// if path is neither absolute nor relative
67+
if (
68+
(/\.jsx?$/.test(foundNodePath) ||
69+
(config.alwaysTryTypes && !foundNodePath)) &&
70+
!/^@types[/\\]/.test(source) &&
71+
!path.isAbsolute(source) &&
72+
source[0] !== '.'
73+
) {
74+
const definitelyTyped = resolveFile(
75+
'@types' + path.sep + mangleScopedPackage(source),
76+
file,
77+
config,
78+
);
79+
if (definitelyTyped.found) {
80+
return definitelyTyped;
81+
}
82+
}
83+
6784
if (foundNodePath) {
6885
log('matched node path:', foundNodePath);
6986

@@ -73,19 +90,35 @@ function resolveFile(source, file, config) {
7390
};
7491
}
7592

76-
log('didnt find', source);
93+
log("didn't find", source);
7794

7895
return {
7996
found: false,
8097
};
8198
}
99+
82100
function packageFilter(pkg) {
83-
if (pkg['jsnext:main']) {
84-
pkg['main'] = pkg['jsnext:main'];
85-
}
101+
pkg.main =
102+
pkg.types || pkg.typings || pkg.module || pkg['jsnext:main'] || pkg.main;
86103
return pkg;
87104
}
88105

106+
/**
107+
* For a scoped package, we must look in `@types/foo__bar` instead of `@types/@foo/bar`.
108+
*
109+
* @param {string} moduleName
110+
* @returns {string}
111+
*/
112+
function mangleScopedPackage(moduleName) {
113+
if (moduleName[0] === '@') {
114+
const replaceSlash = moduleName.replace(path.sep, '__');
115+
if (replaceSlash !== moduleName) {
116+
return replaceSlash.slice(1); // Take off the "@"
117+
}
118+
}
119+
return moduleName;
120+
}
121+
89122
module.exports = {
90123
interfaceVersion: 2,
91124
resolve: resolveFile,

package-lock.json

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

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"eslint-plugin-import": "*"
3030
},
3131
"devDependencies": {
32+
"@types/unist": "^2.0.3",
3233
"dummy.js": "file:dummy.js",
3334
"eslint": "^5.6.1",
3435
"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)