Skip to content

Commit 558dad0

Browse files
committed
feat: add a flag to control usage analysis
1 parent 39287e7 commit 558dad0

File tree

5 files changed

+71
-18
lines changed

5 files changed

+71
-18
lines changed

__tests__/pkg-usage.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,46 @@ beforeEach(() => {
1717
describe('getPackagesUsages()', () => {
1818
describe('given a wrong file glob', () => {
1919
it('should THROW an Error', () => {
20-
const { pkg } = mockPackageUsageFile();
20+
const { pkg } = mockPackageUsageFile({});
2121

2222
expect(() =>
2323
getPackagesUsages({
2424
packages: [pkg],
2525
fileGlobs: `WRONG_FILE_GLOB/**.xyz`,
26+
analyzeImportUsages: false,
2627
})
2728
).toThrow();
2829
});
2930
});
3031

3132
describe('given a file without import declaration', () => {
3233
it('should return a package usage without a single count', () => {
33-
const { pkg, version } = mockPackageUsageFile(
34-
` file data without import declaration `
35-
);
34+
const { pkg, version } = mockPackageUsageFile({
35+
customData: ` file data without import declaration `,
36+
});
3637

3738
expect(
3839
getPackagesUsages({
3940
packages: [pkg],
4041
fileGlobs: `${MOCKS_DIR}/**.tsx`,
42+
analyzeImportUsages: false,
4143
packageJsonCWD: MOCKS_DIR_CWD,
4244
})
4345
).toStrictEqual([{ count: 0, files: [], name: pkg, version }]);
4446
});
4547
});
4648

47-
describe('given any package name and named imports', () => {
49+
describe('given any package name and imports', () => {
4850
it('should return the right package usage', () => {
49-
const { fileName, imports, pkg, version } = mockPackageUsageFile();
51+
const { fileName, imports, pkg, version } = mockPackageUsageFile({
52+
analyzeImportUsages: true,
53+
});
5054

5155
expect(
5256
getPackagesUsages({
5357
packages: [pkg],
5458
fileGlobs: `${MOCKS_DIR}/**.tsx`,
59+
analyzeImportUsages: true,
5560
packageJsonCWD: MOCKS_DIR_CWD,
5661
})
5762
).toStrictEqual([

__tests__/utils.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +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';
56

67
export const MOCKS_DIR = '__mocks__';
78
export const MOCKS_DIR_CWD = join(join(cwd(), MOCKS_DIR));
@@ -42,7 +43,7 @@ function generatePropList(props: string[]) {
4243
return props.map((prop) => `${prop}="${prop}"`).join(' ');
4344
}
4445

45-
function jsxElement(value: string, line: number) {
46+
function jsxElement(value: string, line: number): [Import, string] {
4647
const props = mockUniqueList(() => word().toLowerCase());
4748
return [
4849
{
@@ -61,7 +62,7 @@ function jsxElement(value: string, line: number) {
6162
];
6263
}
6364

64-
function propertyAccess(value: string, line: number) {
65+
function propertyAccess(value: string, line: number): [Import, string] {
6566
const property = word();
6667
return [
6768
{
@@ -72,14 +73,14 @@ function propertyAccess(value: string, line: number) {
7273
];
7374
}
7475

75-
function callExpression(value: string, line: number) {
76+
function callExpression(value: string, line: number): [Import, string] {
7677
return [
7778
{ name: value, usages: [{ line: line + 2, text: `\n${value}()` }] },
7879
`${value}()`,
7980
];
8081
}
8182

82-
function valueUsage(value: string, line: number) {
83+
function valueUsage(value: string, line: number): [Import, string] {
8384
return [
8485
{ name: value, usages: [{ line: line + 2, text: `\n${value};` }] },
8586
`${value};`,
@@ -103,7 +104,15 @@ function mockTSFile(pkgName: string, imports: string[]) {
103104
};
104105
}
105106

106-
export function mockPackageUsageFile(customData?: string) {
107+
type mockPackageUsageFileAttributes = {
108+
customData?: string;
109+
analyzeImportUsages?: boolean;
110+
};
111+
112+
export function mockPackageUsageFile({
113+
customData,
114+
analyzeImportUsages = false,
115+
}: mockPackageUsageFileAttributes) {
107116
const importNames = mockUniqueList(() => capitalize(word()));
108117
const fileName = f.datatype.uuid();
109118
const pkg = f.hacker.noun();
@@ -128,8 +137,18 @@ export function mockPackageUsageFile(customData?: string) {
128137
})
129138
);
130139

140+
if (customData) {
141+
return {
142+
fileName,
143+
pkg,
144+
version,
145+
};
146+
}
147+
131148
return {
132-
imports: !customData ? imports : undefined,
149+
imports: analyzeImportUsages
150+
? imports
151+
: imports.map(({ name }) => ({ name })),
133152
fileName,
134153
pkg,
135154
version,

scripts/pkg-usage.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
const { getPackagesUsages } = require('pkg-usage');
4-
// const { getPackagesUsages } = require('../dist/src');
4+
55
const { Command } = require('commander');
66

77
const program = new Command();
@@ -24,10 +24,15 @@ program
2424
'-cwd, --package-json-CWD <value>',
2525
'Directory to start from ex: -cwd "/path/to/start/from"'
2626
)
27-
.action(({ packages, fileGlobs, packageJsonCWD }) => {
27+
.option(
28+
'-u, --analyze-import-usages',
29+
'Experimental feature that analyzes usages of imported JSXElements, function calls, object properties, and value usages'
30+
)
31+
.action(({ packages, fileGlobs, analyzeImportUsages, packageJsonCWD }) => {
2832
const usages = getPackagesUsages({
2933
packages,
3034
fileGlobs,
35+
analyzeImportUsages,
3136
packageJsonCWD,
3237
});
3338

src/core.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ import {
2929
FileUsage,
3030
} from './types';
3131

32+
const getImports = (importDeclaration: ImportDeclaration): Import[] => {
33+
const defaultImport = importDeclaration.getDefaultImport()?.getText();
34+
const namedImports = importDeclaration
35+
.getNamedImports()
36+
.map((named) => named.getName());
37+
38+
return namedImports
39+
.concat(defaultImport ? [defaultImport] : [])
40+
.map((name) => ({ name }));
41+
};
42+
3243
const getDetailsFromImports = (importDeclaration: ImportDeclaration) => {
3344
const defaultReferences = importDeclaration
3445
.getDefaultImport()
@@ -100,7 +111,8 @@ const getDetailsFromReferences = (
100111

101112
function getPackageUsage(
102113
pkg: string,
103-
sourceFile: SourceFile
114+
sourceFile: SourceFile,
115+
analyzeImportUsages: boolean
104116
): FileUsage | undefined {
105117
const importDeclaration = sourceFile.getImportDeclaration(
106118
(importDeclaration) => {
@@ -116,8 +128,16 @@ function getPackageUsage(
116128
return undefined;
117129
}
118130

119-
const imports = getDetailsFromImports(importDeclaration);
131+
if (analyzeImportUsages) {
132+
const imports = getDetailsFromImports(importDeclaration);
133+
return {
134+
name: sourceFile.getBaseName(),
135+
filePath: sourceFile.getFilePath(),
136+
imports,
137+
};
138+
}
120139

140+
const imports = getImports(importDeclaration);
121141
return {
122142
name: sourceFile.getBaseName(),
123143
filePath: sourceFile.getFilePath(),
@@ -128,6 +148,7 @@ function getPackageUsage(
128148
export function getPackagesUsages({
129149
packages,
130150
fileGlobs,
151+
analyzeImportUsages,
131152
packageJsonCWD,
132153
}: Options): PackageUsage[] | undefined {
133154
const project = new Project();
@@ -139,7 +160,9 @@ export function getPackagesUsages({
139160

140161
const result = packages.map((pkg) => {
141162
const fileUsages = sourceFiles
142-
.map((sourceFile) => getPackageUsage(pkg, sourceFile))
163+
.map((sourceFile) =>
164+
getPackageUsage(pkg, sourceFile, analyzeImportUsages)
165+
)
143166
.filter(nonNullish);
144167

145168
return {

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export type Options = {
22
fileGlobs: string;
33
packages: string[];
4+
analyzeImportUsages: boolean;
45
packageJsonCWD?: string;
56
};
67

@@ -16,7 +17,7 @@ export type Usages =
1617

1718
export type Import = {
1819
name: string;
19-
usages: Usages;
20+
usages?: Usages;
2021
};
2122

2223
export type PackageUsage = {

0 commit comments

Comments
 (0)