Skip to content

Commit 8a209a3

Browse files
committed
feat: add export type to usage report
1 parent 558dad0 commit 8a209a3

File tree

7 files changed

+40
-9
lines changed

7 files changed

+40
-9
lines changed

.eslintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@
99
"env": {
1010
"node": true,
1111
"jest": true
12+
},
13+
"rules": {
14+
"no-unused-vars": "off",
15+
"@typescript-eslint/no-unused-vars": "error"
1216
}
1317
}

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const usages: PackageUsage[] = getPackagesUsages({
2727
packages: ['react'],
2828
fileGlobs: `**/**.ts`,
2929
packageJsonCWD: './package.json',
30-
3130
});
3231

3332
console.log(usages);
@@ -36,7 +35,6 @@ console.log(usages);
3635
Package Usage types
3736

3837
```ts
39-
4038
export type JSXElementUsage = {
4139
line: number;
4240
props: string[];
@@ -65,7 +63,8 @@ export type Usages =
6563

6664
export type Import = {
6765
name: string;
68-
usages: Usages;
66+
type: ExportType;
67+
usages?: Usages;
6968
};
7069

7170
export type FileUsage = {

__tests__/utils.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { join, dirname } from 'path';
22
import { cwd } from 'process';
33
import { writeFileSync, existsSync, mkdirSync, rmdirSync } from 'fs';
44
import f from 'faker';
5-
import { Import } from '../src';
5+
import { ExportType, Import } from '../src';
66

77
export const MOCKS_DIR = '__mocks__';
88
export const MOCKS_DIR_CWD = join(join(cwd(), MOCKS_DIR));
@@ -48,6 +48,7 @@ function jsxElement(value: string, line: number): [Import, string] {
4848
return [
4949
{
5050
name: value,
51+
type: ExportType.named,
5152
usages: [
5253
{
5354
line: line + 2,
@@ -67,6 +68,7 @@ function propertyAccess(value: string, line: number): [Import, string] {
6768
return [
6869
{
6970
name: value,
71+
type: ExportType.named,
7072
usages: [{ line: line + 2, property, text: `\n${value}.${property}` }],
7173
},
7274
`${value}.${property}`,
@@ -75,14 +77,22 @@ function propertyAccess(value: string, line: number): [Import, string] {
7577

7678
function callExpression(value: string, line: number): [Import, string] {
7779
return [
78-
{ name: value, usages: [{ line: line + 2, text: `\n${value}()` }] },
80+
{
81+
name: value,
82+
type: ExportType.named,
83+
usages: [{ line: line + 2, text: `\n${value}()` }],
84+
},
7985
`${value}()`,
8086
];
8187
}
8288

8389
function valueUsage(value: string, line: number): [Import, string] {
8490
return [
85-
{ name: value, usages: [{ line: line + 2, text: `\n${value};` }] },
91+
{
92+
name: value,
93+
type: ExportType.named,
94+
usages: [{ line: line + 2, text: `\n${value};` }],
95+
},
8696
`${value};`,
8797
];
8898
}

example/run-in-ci.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const result = getPackagesUsages({
1010
packages: ['bla', '@scoped/package'],
1111
fileGlobs: './*{.ts,.tsx}',
1212
packageJsonCWD: './package.json',
13+
analyzeImportUsages: true,
1314
});
1415

1516
console.log(JSON.stringify(result, null, 2));

src/core.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from 'ts-morph';
88

99
import {
10+
getExportType,
1011
getJSXElementProps,
1112
getPackageVersion,
1213
getProperty,
@@ -27,6 +28,7 @@ import {
2728
CallExpressionUsage,
2829
PropertyAccessExpressionUsage,
2930
FileUsage,
31+
ExportType,
3032
} from './types';
3133

3234
const getImports = (importDeclaration: ImportDeclaration): Import[] => {
@@ -36,8 +38,10 @@ const getImports = (importDeclaration: ImportDeclaration): Import[] => {
3638
.map((named) => named.getName());
3739

3840
return namedImports
39-
.concat(defaultImport ? [defaultImport] : [])
40-
.map((name) => ({ name }));
41+
.map((name) => ({ name, type: ExportType.named }))
42+
.concat(
43+
defaultImport ? [{ name: defaultImport, type: ExportType.default }] : []
44+
);
4145
};
4246

4347
const getDetailsFromImports = (importDeclaration: ImportDeclaration) => {
@@ -64,6 +68,7 @@ const getDetailsFromReferences = (
6468
): Import[] =>
6569
references.map((referencedSymbol) => ({
6670
name: referencedSymbol.getDefinition().getNode().getText(),
71+
type: getExportType(referencedSymbol),
6772
usages: referencedSymbol
6873
.getReferences()
6974
.flatMap(

src/helpers.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import pkgup from 'pkg-up';
22

33
import { readFileSync } from 'fs';
4-
import { ReferenceEntry, SyntaxKind } from 'ts-morph';
4+
import { ReferencedSymbol, ReferenceEntry, SyntaxKind } from 'ts-morph';
5+
import { ExportType } from './types';
56

67
// istanbul ignore next
78
function getPackageJson(packageJsonCWD?: string) {
@@ -40,6 +41,11 @@ export function nonNullish<Value>(v: Value): v is NonNullable<Value> {
4041
return v !== undefined && v !== null;
4142
}
4243

44+
export const getExportType = (referencedSymbol: ReferencedSymbol) =>
45+
referencedSymbol.getDefinition().getDeclarationNode()?.getParentSyntaxList()
46+
? ExportType.named
47+
: ExportType.default;
48+
4349
export const getJSXElementProps = (reference: ReferenceEntry) =>
4450
reference
4551
.getNode()

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ export type FileUsage = {
1111
imports: Import[];
1212
};
1313

14+
export enum ExportType {
15+
named = 'named',
16+
default = 'default',
17+
}
18+
1419
export type Usages =
1520
| JSXElementUsage[]
1621
| (CallExpressionUsage | PropertyAccessExpressionUsage | ValueUsage)[];
1722

1823
export type Import = {
1924
name: string;
25+
type: ExportType;
2026
usages?: Usages;
2127
};
2228

0 commit comments

Comments
 (0)