Skip to content
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

TypeScript: detect node built-in type usage #445

Merged
merged 1 commit into from Nov 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/utils/typescript.js
@@ -1,10 +1,17 @@
import lodash from 'lodash';
import builtInModules from 'builtin-modules';

/* eslint-disable import/prefer-default-export */
const orgDepRegex = /@(.*?)\/(.*)/;

// The name of the DefinitelyTyped package for a given package
export function getAtTypesName(dep) {
const match = orgDepRegex.exec(dep);
const pkgName = match ? `${match[1]}__${match[2]}` : dep;

let pkgName;
if (lodash.includes(builtInModules, dep)) {
pkgName = 'node';
} else {
const match = orgDepRegex.exec(dep);
pkgName = match ? `${match[1]}__${match[2]}` : dep;
}
return `@types/${pkgName}`;
}
2 changes: 2 additions & 0 deletions test/fake_modules/typescript/esnext.ts
@@ -1,9 +1,11 @@
import Dep from 'ts-dep-esnext';
import '@org/org-pkg';
import * as path from 'path';

export const ObjRestSpread = () => {
const a = {a: 1, b: 2};
const { b, ...rest } = a;
const c = {...a, ...rest};
path.resolve('/', 'home');
new Dep(c);
}
1 change: 1 addition & 0 deletions test/fake_modules/typescript/package.json
Expand Up @@ -3,6 +3,7 @@
"@org/org-pkg": "0.0.1",
"@types/org__org-pkg": "0.0.1",
"@types/react": "0.0.1",
"@types/node": "0.0.1",
"react": "0.0.1",
"ts-dep-1": "0.0.1",
"ts-dep-2": "0.0.1",
Expand Down
1 change: 1 addition & 0 deletions test/spec.js
Expand Up @@ -223,6 +223,7 @@ export default [
using: {
react: ['component.tsx'],
'@types/react': ['component.tsx'],
'@types/node': ['esnext.ts'],
'@types/org__org-pkg': ['esnext.ts'],
'@org/org-pkg': ['esnext.ts'],
'ts-dep-1': ['index.ts'],
Expand Down