Skip to content

Commit 4db18ba

Browse files
committed
fix(fmt): preserve formatting check scope
1 parent 4287d7f commit 4db18ba

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

packages/rstack/src/cli/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ async function runCheckCLI(args: string[]): Promise<void> {
185185
/* rspackChunkName: 'fmt' */
186186
'../fmt/cli.ts'
187187
);
188-
await runFmtCLI(['--check'], { loadedConfig });
188+
await runFmtCLI(['--check'], { fixCommand: 'rs fmt', loadedConfig });
189189
}
190190

191191
export async function setupCommands(): Promise<void> {

packages/rstack/src/fmt/cli.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ interface ParsedFmtCLIArgs {
3030
}
3131

3232
type RunFmtCLIOptions = {
33+
/** Command shown to fix formatting issues found in check mode. */
34+
fixCommand?: string;
3335
/** Rstack config already loaded by the lint phase of `rs check`. */
3436
loadedConfig?: LoadedRstackConfig;
3537
};
@@ -206,6 +208,7 @@ const logFmtResult = (
206208
cwd: string,
207209
processedFileCount: number,
208210
durationSeconds: number,
211+
fixCommand?: string,
209212
): void => {
210213
let writtenCount = 0;
211214
let differentCount = 0;
@@ -248,10 +251,10 @@ const logFmtResult = (
248251
if (differentCount > 0) {
249252
const differentFiles = formatFileCount(differentCount, true);
250253
const processedFiles = formatFileCount(processedFileCount);
251-
const fmtCommand = color.cyan('rs fmt');
252-
logger.error(
253-
`Formatting issues found in ${differentFiles}. Run ${fmtCommand} to fix.`,
254-
);
254+
const fixHint = fixCommand
255+
? `Run ${color.cyan(fixCommand)} to fix.`
256+
: `Rerun this command without ${color.cyan('--check')} to fix.`;
257+
logger.error(`Formatting issues found in ${differentFiles}. ${fixHint}`);
255258
logger.info(`Checked ${processedFiles} in ${prettyTime(durationSeconds)}.`);
256259
} else if (result.exitCode === 0) {
257260
logger.success(
@@ -276,7 +279,7 @@ const loadFmtConfig = async (
276279

277280
const runFmtCLI = async (
278281
args: string[],
279-
{ loadedConfig }: RunFmtCLIOptions = {},
282+
{ fixCommand, loadedConfig }: RunFmtCLIOptions = {},
280283
): Promise<void> => {
281284
const cwd = process.cwd();
282285
const startTime = performance.now();
@@ -413,7 +416,14 @@ const runFmtCLI = async (
413416
}
414417

415418
const durationSeconds = (performance.now() - startTime) / 1000;
416-
logFmtResult(result, mode, cwd, result.processedFileCount, durationSeconds);
419+
logFmtResult(
420+
result,
421+
mode,
422+
cwd,
423+
result.processedFileCount,
424+
durationSeconds,
425+
fixCommand,
426+
);
417427
process.exitCode = result.exitCode;
418428
} catch (error) {
419429
logger.error(error);

packages/rstack/tests/cli/fmt/files.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ test('checks formatting without writing files', () => {
108108
);
109109
expect(result.stderr).toContain('error index.ts');
110110
expect(normalizeDuration(result.stderr)).toContain(
111-
'error Formatting issues found in 1 file. Run rs fmt to fix.',
111+
'error Formatting issues found in 1 file. Rerun this command without --check to fix.',
112112
);
113113
expect(readProjectFile('index.ts')).toBe(source);
114114

0 commit comments

Comments
 (0)