Skip to content

Commit

Permalink
Support @types typescript typedef packages
Browse files Browse the repository at this point in the history
  • Loading branch information
conartist6 committed Oct 21, 2019
1 parent 75a9e64 commit 371004d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import builtInModules from 'builtin-modules';
import requirePackageName from 'require-package-name';
import { readJSON } from './utils';
import getNodes from './utils/parser';
import { getAtTypesName } from './utils/typescript';
import { availableParsers } from './constants';

function isModule(dir) {
try {
Expand Down Expand Up @@ -71,6 +73,19 @@ function getDependencies(dir, filename, deps, parser, detectors) {
.flatten()
.uniq()
.map(requirePackageName)
.thru(_dependencies => (
parser === availableParsers.typescript
// If this is a typescript file, importing foo would also use @types/foo, but
// only if @types/foo is already a specified dependency.
? lodash(_dependencies)
.map((dependency) => {
const atTypesName = getAtTypesName(dependency);
return deps.includes(atTypesName) ? [dependency, atTypesName] : [dependency];
})
.flatten()
.value()
: _dependencies
))
.value();

const discover = lodash.partial(discoverPropertyDep, dir, deps);
Expand Down
10 changes: 10 additions & 0 deletions src/utils/typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* 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;

return `@types/${pkgName}`;
}
1 change: 1 addition & 0 deletions test/fake_modules/typescript/esnext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Dep from 'ts-dep-esnext';
import '@org/org-pkg';

export const ObjRestSpread = () => {
const a = {a: 1, b: 2};
Expand Down
3 changes: 3 additions & 0 deletions test/fake_modules/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"dependencies": {
"@org/org-pkg": "0.0.1",
"@types/org__org-pkg": "0.0.1",
"@types/react": "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/fake_modules/typescript/typedef.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'ts-dep-typedef';
3 changes: 3 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ export default [
missing: {},
using: {
react: ['component.tsx'],
'@types/react': ['component.tsx'],
'@types/org__org-pkg': ['esnext.ts'],
'@org/org-pkg': ['esnext.ts'],
'ts-dep-1': ['index.ts'],
'ts-dep-2': ['index.ts'],
'ts-dep-esnext': ['esnext.ts'],
Expand Down

0 comments on commit 371004d

Please sign in to comment.