Skip to content

Commit 10f5bd2

Browse files
committed
Apply fixes after running preprocessors (#763 #1420)
1 parent b2cbbd5 commit 10f5bd2

5 files changed

Lines changed: 31 additions & 25 deletions

File tree

packages/knip/src/IssueFixer.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
import { readFile, rm, writeFile } from 'node:fs/promises';
2+
import { formatly } from 'formatly';
23
import type { Fixes } from './types/exports.js';
34
import type { Issue, Issues } from './types/issues.js';
45
import { DEFAULT_CATALOG } from './util/catalog.js';
56
import type { MainOptions } from './util/create-options.js';
7+
import { debugLogArray, debugLogObject } from './util/debug.js';
68
import { load, save } from './util/package-json.js';
79
import { extname, join } from './util/path.js';
810
import { removeExport } from './util/remove-export.js';
911

12+
export const fix = async (issues: Issues, options: MainOptions) => {
13+
const fixer = new IssueFixer(options);
14+
const touchedFiles = await fixer.fixIssues(issues);
15+
if (options.isFormat) {
16+
const report = await formatly(Array.from(touchedFiles));
17+
if (report.ran && report.result && (report.result.runner === 'virtual' || report.result.code === 0)) {
18+
debugLogArray('*', `Formatted files using ${report.formatter.name} (${report.formatter.runner})`, touchedFiles);
19+
} else {
20+
debugLogObject('*', 'Formatting files failed', report);
21+
}
22+
}
23+
};
24+
1025
export class IssueFixer {
1126
options: MainOptions;
1227

packages/knip/src/cli.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// biome-ignore-all lint/suspicious/noConsole: ignore
2-
import { main } from './index.js';
2+
import { fix } from './IssueFixer.js';
3+
import { run } from './run.js';
34
import type { IssueType, ReporterOptions } from './types/issues.js';
45
import parseArgs, { helpText } from './util/cli-arguments.js';
56
import { createOptions } from './util/create-options.js';
@@ -22,7 +23,7 @@ try {
2223
throw error;
2324
}
2425

25-
const run = async () => {
26+
const main = async () => {
2627
try {
2728
if (args.help) {
2829
console.log(helpText);
@@ -36,8 +37,9 @@ const run = async () => {
3637

3738
const options = await createOptions({ args });
3839

39-
const { issues, counters, tagHints, configurationHints, includedWorkspaceDirs, enabledPlugins } =
40-
await main(options);
40+
const { results } = await run(options);
41+
42+
const { issues, counters, tagHints, configurationHints, includedWorkspaceDirs, enabledPlugins } = results;
4143

4244
// These modes have their own reporting mechanism
4345
if (options.isWatch || options.isTrace) return;
@@ -63,6 +65,8 @@ const run = async () => {
6365

6466
const finalData = await runPreprocessors(args.preprocessor ?? [], initialData);
6567

68+
if (options.isFix) await fix(finalData.issues, options);
69+
6670
await runReporters(args.reporter ?? ['symbols'], finalData);
6771

6872
const totalErrorCount = (Object.keys(finalData.report) as IssueType[])
@@ -110,4 +114,4 @@ const run = async () => {
110114
process.exit(0);
111115
};
112116

113-
await run();
117+
await main();

packages/knip/src/graph/analyze.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { DependencyDeputy } from '../DependencyDeputy.js';
55
import { createGraphExplorer } from '../graph-explorer/explorer.js';
66
import { getIssueType, hasStrictlyEnumReferences } from '../graph-explorer/utils.js';
77
import type { IssueCollector } from '../IssueCollector.js';
8-
import type { IssueFixer } from '../IssueFixer.js';
98
import type { PrincipalFactory } from '../PrincipalFactory.js';
109
import traceReporter from '../reporters/trace.js';
1110
import type { Export, ExportMember, ModuleGraph } from '../types/module-graph.js';
@@ -22,7 +21,6 @@ interface AnalyzeOptions {
2221
deputy: DependencyDeputy;
2322
entryPaths: Set<string>;
2423
factory: PrincipalFactory;
25-
fixer: IssueFixer;
2624
graph: ModuleGraph;
2725
streamer: ConsoleStreamer;
2826
unreferencedFiles: Set<string>;

packages/knip/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
import { fix } from './IssueFixer.js';
12
import { run } from './run.js';
23
import type { MainOptions } from './util/create-options.js';
34

4-
export const main = async (options: MainOptions) => (await run(options)).results;
5+
export const main = async (options: MainOptions) => {
6+
const { results } = await run(options);
7+
if (options.isFix) await fix(results.issues, options);
8+
return results;
9+
};

packages/knip/src/run.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { watch } from 'node:fs';
2-
import { formatly } from 'formatly';
32
import { CatalogCounselor } from './CatalogCounselor.js';
43
import { ConfigurationChief } from './ConfigurationChief.js';
54
import { ConsoleStreamer } from './ConsoleStreamer.js';
65
import { DependencyDeputy } from './DependencyDeputy.js';
76
import { analyze } from './graph/analyze.js';
87
import { build } from './graph/build.js';
98
import { IssueCollector } from './IssueCollector.js';
10-
import { IssueFixer } from './IssueFixer.js';
119
import { PrincipalFactory } from './PrincipalFactory.js';
1210
import watchReporter from './reporters/watch.js';
1311
import type { MainOptions } from './util/create-options.js';
14-
import { debugLogArray, debugLogObject } from './util/debug.js';
12+
import { debugLogObject } from './util/debug.js';
1513
import { getGitIgnoredHandler } from './util/glob-core.js';
1614
import { getSessionHandler, type OnFileChange, type SessionHandler } from './util/watch.js';
1715

@@ -25,7 +23,6 @@ export const run = async (options: MainOptions) => {
2523
const deputy = new DependencyDeputy(options);
2624
const factory = new PrincipalFactory();
2725
const streamer = new ConsoleStreamer(options);
28-
const fixer = new IssueFixer(options);
2926
const collector = new IssueCollector(options);
3027
const counselor = new CatalogCounselor(options);
3128

@@ -61,7 +58,6 @@ export const run = async (options: MainOptions) => {
6158
deputy,
6259
entryPaths,
6360
factory,
64-
fixer,
6561
graph,
6662
streamer,
6763
unreferencedFiles,
@@ -99,18 +95,6 @@ export const run = async (options: MainOptions) => {
9995

10096
const { issues, counters, tagHints, configurationHints } = collector.getIssues();
10197

102-
if (options.isFix && !options.isSession) {
103-
const touchedFiles = await fixer.fixIssues(issues);
104-
if (options.isFormat) {
105-
const report = await formatly(Array.from(touchedFiles));
106-
if (report.ran && report.result && (report.result.runner === 'virtual' || report.result.code === 0)) {
107-
debugLogArray('*', `Formatted files using ${report.formatter.name} (${report.formatter.runner})`, touchedFiles);
108-
} else {
109-
debugLogObject('*', 'Formatting files failed', report);
110-
}
111-
}
112-
}
113-
11498
if (!options.isWatch) streamer.clear();
11599

116100
return {

0 commit comments

Comments
 (0)