@@ -9,10 +9,8 @@ const isGlob = require('is-glob');
99
1010const 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+ ( / \. j s x ? $ / . test ( foundNodePath ) ||
55+ ( options . alwaysTryTypes && ! foundNodePath ) ) &&
56+ ! / ^ @ t y p e s [ / \\ ] / . 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
6986function 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+
144176module . exports = {
145177 interfaceVersion : 2 ,
146178 resolve : resolveFile ,
0 commit comments