Skip to content

resolve .ts/.tsx/.d.ts first, and then fallback to @types/* #20

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

Merged
merged 6 commits into from
Sep 27, 2019
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
49 changes: 41 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ function resolveFile(source, file, config) {
}

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

// setup tsconfig-paths
Expand Down Expand Up @@ -64,6 +62,25 @@ function resolveFile(source, file, config) {
foundNodePath = null;
}

// naive attempt at @types/* resolution,
// if path is neither absolute nor relative
if (
(/\.jsx?$/.test(foundNodePath) ||
(config.alwaysTryTypes && !foundNodePath)) &&
!/^@types[/\\]/.test(source) &&
!path.isAbsolute(source) &&
source[0] !== '.'
) {
const definitelyTyped = resolveFile(
'@types' + path.sep + mangleScopedPackage(source),
file,
config,
);
if (definitelyTyped.found) {
return definitelyTyped;
}
}

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

Expand All @@ -73,19 +90,35 @@ function resolveFile(source, file, config) {
};
}

log('didnt find', source);
log("didn't find", source);

return {
found: false,
};
}

function packageFilter(pkg) {
if (pkg['jsnext:main']) {
pkg['main'] = pkg['jsnext:main'];
}
pkg.main =
pkg.types || pkg.typings || pkg.module || pkg['jsnext:main'] || pkg.main;
return pkg;
}

/**
* For a scoped package, we must look in `@types/foo__bar` instead of `@types/@foo/bar`.
*
* @param {string} moduleName
* @returns {string}
*/
function mangleScopedPackage(moduleName) {
if (moduleName[0] === '@') {
const replaceSlash = moduleName.replace(path.sep, '__');
if (replaceSlash !== moduleName) {
return replaceSlash.slice(1); // Take off the "@"
}
}
return moduleName;
}

module.exports = {
interfaceVersion: 2,
resolve: resolveFile,
Expand Down
14 changes: 13 additions & 1 deletion package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"eslint-plugin-import": "*"
},
"devDependencies": {
"@types/unist": "^2.0.3",
"dummy.js": "file:dummy.js",
"eslint": "^5.6.1",
"eslint-plugin-import": "^2.14.0",
Expand Down
1 change: 1 addition & 0 deletions tests/baseEslintConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = dirname => ({
'import/resolver': {
[path.resolve(`${__dirname}/../index.js`)]: {
directory: dirname,
alwaysTryTypes: true
},
},
},
Expand Down
3 changes: 3 additions & 0 deletions tests/withoutPaths/dtsImportee.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare const content : 'yes';

export default content;
8 changes: 8 additions & 0 deletions tests/withoutPaths/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
// import relative
import './tsImportee'
import './tsxImportee'
import './dtsImportee'
import './subfolder/dtsImportee'
import './subfolder/tsImportee'
import './subfolder/tsxImportee'

// import from node_module
import 'typescript'
import 'dummy.js'

// import from `@types/`
import 'json5'

// enable alwaysTryTypes
import 'unist'
3 changes: 3 additions & 0 deletions tests/withoutPaths/subfolder/dtsImportee.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare const content : 'yes';

export default content;