Skip to content

Commit b2a5d56

Browse files
committed
fix(cli): preserve check file scope in fix hint
1 parent cca0746 commit b2a5d56

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

packages/rstack/src/cli/commands.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ async function runRspressCLI(args: string[]): Promise<void> {
141141

142142
const RSLINT_CONFIG_PATH = join(import.meta.dirname, 'rslintConfig.js');
143143

144+
const SHELL_SAFE_ARGUMENT_REGEXP = /^[\w@%+=:,./-]+$/u;
145+
146+
const quoteShellArgument = (argument: string): string =>
147+
SHELL_SAFE_ARGUMENT_REGEXP.test(argument)
148+
? argument
149+
: `'${argument.replaceAll("'", "'\"'\"'")}'`;
150+
151+
const formatCommand = (args: readonly string[]): string =>
152+
args.map(quoteShellArgument).join(' ');
153+
144154
async function runRslintCLI(args: string[]): Promise<void> {
145155
if (hasHelpFlag(args)) {
146156
return printCommandHelp('lint');
@@ -192,7 +202,7 @@ async function runCheckCLI(args: string[]): Promise<void> {
192202
'../fmt/cli.ts'
193203
);
194204
await runFmtCLI(['--check', ...fileArgs], {
195-
fixCommand: 'rs fmt',
205+
fixCommand: formatCommand(['rs', 'fmt', ...fileArgs]),
196206
loadedConfig,
197207
});
198208
}

packages/rstack/tests/cli/check.test.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,28 @@ test('passes file arguments to lint and the formatting check', () => {
6363
expect(result.stderr).toBe('');
6464
});
6565

66+
test('preserves file arguments in the formatting fix command', () => {
67+
writeLintConfig();
68+
writeProjectFile("src/selected file's.ts", 'const selected=true');
69+
70+
const result = runCheck(["src/selected file's.ts"]);
71+
72+
expect(result.status).toBe(1);
73+
expect(result.stderr).toContain(
74+
`Run rs fmt -- 'src/selected file'"'"'s.ts' to fix.`,
75+
);
76+
});
77+
6678
test('supports file arguments after the option terminator', () => {
6779
writeLintConfig();
68-
writeProjectFile('--selected.ts', 'const selected = true;\n');
80+
writeProjectFile('--selected.ts', 'const selected=true');
6981

7082
const result = runCheck(['--', '--selected.ts']);
7183

72-
expect(result.status).toBe(0);
73-
expect(result.stdout).toContain('Format check passed in');
74-
expect(result.stdout).toContain('(1 file)');
75-
expect(result.stderr).toBe('');
84+
expect(result.status).toBe(1);
85+
expect(result.stderr).toContain(
86+
'Formatting issues found in 1 file. Run rs fmt -- --selected.ts to fix.',
87+
);
7688
});
7789

7890
test('enables type checking only with --type-check', () => {

0 commit comments

Comments
 (0)