Skip to content

Commit

Permalink
TypeScript: detect node built-in type usage (#445)
Browse files Browse the repository at this point in the history
Issue #444
  • Loading branch information
sveyret authored and rumpl committed Nov 1, 2019
1 parent 0a11bd1 commit c9045d1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
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

0 comments on commit c9045d1

Please sign in to comment.