Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
feat: show source file that tries to include unresolved file (#105)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephan Meijer <[email protected]>
  • Loading branch information
Tbhesswebber and smeijer authored Apr 21, 2023
1 parent f637d98 commit 23f6080
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 17 deletions.
9 changes: 9 additions & 0 deletions src/__tests__/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 190,15 @@ cases(
exitCode: 1,
stdout: /1 unresolved imports.*.\/foo/s,
},
{
name: 'should show the source of the unresolved import',
files: [
{ name: 'package.json', content: '{ "main": "index.js" }' },
{ name: 'index.js', content: `import foo from './foo';` },
],
exitCode: 1,
stdout: /\.\/foo.*at index\.js/s,
},
{
name: 'should ignore untracked files that are not imported',
files: [
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 74,7 @@ describe('removeUnused', () => {
const rm = jest.spyOn(fs, 'rm');

const { removedDeps, deletedFiles, error } = await removeUnused(
{ ...result, unresolved: ['unused-package'] },
{ ...result, unresolved: [['unused-package', []]] },
context,
);

Expand Down
9 changes: 4 additions & 5 deletions src/__tests__/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 48,7 @@ describe('printResults', () => {

it('should print summary and unresolved, unimported, and unused tables populated', () => {
const expectedProcessedResult = {
unresolved: ['string', 'string'],
unresolved: <[string, string[]][]>[['string', ['string']]],
unimported: ['string', 'string', 'string', 'string'],
unused: ['string', 'string', 'string'],
clean: false,
Expand All @@ -61,16 61,15 @@ describe('printResults', () => {
────────────────────────────────────────────────────────────────────────────────
entry file : src/client/main.js
unresolved imports : 2
unresolved imports : 1
unused dependencies : 3
unimported files : 4
─────┬──────────────────────────────────────────────────────────────────────────
2 unresolved imports
1 unresolved imports
─────┼──────────────────────────────────────────────────────────────────────────
1 │ string
2 │ string
1 │ string at string
─────┴──────────────────────────────────────────────────────────────────────────
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 191,7 @@ export async function main(args: CliArguments): Promise<void> {
preset: config.preset,
dependencies,
pathTransforms: config.pathTransforms,
root: context.cwd,
};

// we can't use the third argument here, to keep feeding to traverseResult
Expand All @@ -217,8 218,8 @@ export async function main(args: CliArguments): Promise<void> {
subResult.modules.forEach((module) => {
traverseResult.modules.add(module);
});
subResult.unresolved.forEach((unresolved) => {
traverseResult.unresolved.add(unresolved);
subResult.unresolved.forEach((unresolved, key) => {
traverseResult.unresolved.set(key, unresolved);
});

for (const [key, stat] of subResult.files) {
Expand Down
10 changes: 8 additions & 2 deletions src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 26,11 @@ export function formatList(caption: string, records: string[]): string {

export function formatMetaTable(
caption: string,
data: { unresolved: string[]; unimported: string[]; unused: string[] },
data: {
unresolved: [string, string[]][];
unimported: string[];
unused: string[];
},
context: Context,
): string {
const entryFiles = context.config.entryFiles;
Expand Down Expand Up @@ -129,7 133,9 @@ export function printResults(result: ProcessedResult, context: Context): void {
console.log(
formatList(
chalk.redBright(`${unresolved.length} unresolved imports`),
unresolved,
unresolved.map(([item, sources]) => {
return `${item} ${chalk.gray(`at ${sources.join(', ')}`)}`;
}),
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 3,7 @@ import { Context } from './index';
import { ensureArray } from './ensureArray';

export interface ProcessedResult {
unresolved: string[];
unresolved: [string, string[]][];
unimported: string[];
unused: string[];
clean: boolean;
Expand All @@ -26,7 26,7 @@ export async function processResults(
const ignoreUnimportedIdx = index(context.config.ignoreUnimported);

const unresolved = Array.from(traverseResult.unresolved).filter(
(x) => !ignoreUnresolvedIdx[x],
([x]) => !ignoreUnresolvedIdx[x],
);

const unused = Object.keys(context.dependencies).filter(
Expand Down
14 changes: 9 additions & 5 deletions src/traverse.ts
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
import { dirname, extname } from 'path';
import { dirname, extname, relative } from 'path';

import {
AST_NODE_TYPES,
Expand Down Expand Up @@ -29,7 29,7 @@ export interface FileStats {
}

export interface TraverseResult {
unresolved: Set<string>;
unresolved: Map<string, string[]>;
files: Map<string, FileStats>;
modules: Set<string>;
}
Expand Down Expand Up @@ -328,7 328,7 @@ async function parse(path: string, config: TraverseConfig): Promise<FileStats> {
}

export const getResultObject = () => ({
unresolved: new Set<string>(),
unresolved: new Map<string, string[]>(),
modules: new Set<string>(),
files: new Map<string, FileStats>(),
});
Expand All @@ -342,6 342,7 @@ export interface TraverseConfig {
preset?: string;
dependencies: MapLike<string>;
pathTransforms?: MapLike<string>;
root: string;
}

export async function traverse(
Expand All @@ -366,7 367,7 @@ export async function traverse(
return result;
}

let parseResult;
let parseResult: FileStats;
try {
const generator = () => parse(String(path), config);

Expand All @@ -381,7 382,10 @@ export async function traverse(
result.modules.add(file.name);
break;
case 'unresolved':
result.unresolved.add(file.path);
const current = result.unresolved.get(file.path) || [];
const path = relative(config.root, parseResult.path);
const next = current.includes(path) ? current : [...current, path];
result.unresolved.set(file.path, next);
break;
case 'source_file':
if (result.files.has(file.path)) {
Expand Down

0 comments on commit 23f6080

Please sign in to comment.