Skip to content

Commit 6cc547b

Browse files
authored
feat(cli): support file arguments in rs check (#453)
1 parent 7475a74 commit 6cc547b

6 files changed

Lines changed: 68 additions & 8 deletions

File tree

‎packages/rstack/src/cli/commandHelp.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ const HELP_DEFINITIONS = {
148148
],
149149
},
150150
check: {
151-
usage: 'rs check [options]',
151+
usage: 'rs check [options] [files...]',
152152
description: 'Run static checks, including lint and format',
153153
sections: [
154154
{

‎packages/rstack/src/cli/commands.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,27 @@ async function runRslintCLI(args: string[]): Promise<void> {
157157
}
158158

159159
async function runCheckCLI(args: string[]): Promise<void> {
160-
const { values } = parseArgs({
160+
const { values, positionals } = parseArgs({
161161
args,
162162
options: {
163163
'type-check': { type: 'boolean' },
164164
help: { type: 'boolean', short: 'h' },
165165
},
166-
allowPositionals: false,
166+
allowPositionals: true,
167167
strict: true,
168168
});
169169

170170
if (values.help) {
171171
return printCommandHelp('check');
172172
}
173173

174-
await runRslintCLI(values.typeCheck ? ['--type-check'] : []);
174+
// Keep file arguments after `--` when forwarding them so names beginning
175+
// with a hyphen are not reinterpreted as child-command options.
176+
const fileArgs = positionals.length > 0 ? ['--', ...positionals] : [];
177+
await runRslintCLI([
178+
...(values.typeCheck ? ['--type-check'] : []),
179+
...fileArgs,
180+
]);
175181
if (process.exitCode) {
176182
return;
177183
}
@@ -185,7 +191,10 @@ async function runCheckCLI(args: string[]): Promise<void> {
185191
/* rspackChunkName: 'fmt' */
186192
'../fmt/cli.ts'
187193
);
188-
await runFmtCLI(['--check'], { fixCommand: 'rs fmt', loadedConfig });
194+
await runFmtCLI(['--check', ...fileArgs], {
195+
fixCommand: 'rs fmt',
196+
loadedConfig,
197+
});
189198
}
190199

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

‎packages/rstack/tests/cli/__snapshots__/check.test.ts.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`displays check help without loading config 1`] = `
44
"Rstack v<version>
55
66
Usage:
7-
$ rs check [options]
7+
$ rs check [options] [files...]
88
99
Run static checks, including lint and format
1010

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,33 @@ test('runs lint followed by a formatting check', () => {
4848
expect(formatted.stderr).toBe('');
4949
});
5050

51+
test('passes file arguments to lint and the formatting check', () => {
52+
writeLintConfig();
53+
writeProjectFile('src/selected-a.ts', 'const selectedA = true;\n');
54+
writeProjectFile('src/selected-b.ts', 'const selectedB = true;\n');
55+
writeProjectFile('src/unselected-lint-error.ts', 'debugger;\n');
56+
writeProjectFile('src/unselected-format-error.ts', 'const value=true');
57+
58+
const result = runCheck(['src/selected-a.ts', 'src/selected-b.ts']);
59+
60+
expect(result.status).toBe(0);
61+
expect(result.stdout).toContain('Format check passed in');
62+
expect(result.stdout).toContain('(2 files)');
63+
expect(result.stderr).toBe('');
64+
});
65+
66+
test('supports file arguments after the option terminator', () => {
67+
writeLintConfig();
68+
writeProjectFile('--selected.ts', 'const selected = true;\n');
69+
70+
const result = runCheck(['--', '--selected.ts']);
71+
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('');
76+
});
77+
5178
test('enables type checking only with --type-check', () => {
5279
writeLintConfig();
5380
writeProjectFile(

‎website/docs/en/guide/cli/check.mdx‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,19 @@ The `rs check` command combines linting, formatting, and optional TypeScript typ
99
## Usage
1010

1111
```bash
12-
rs check [options]
12+
rs check [options] [files...]
13+
```
14+
15+
Pass files or directories to limit both linting and formatting to those paths:
16+
17+
```bash
18+
rs check src/index.ts packages/utils
19+
```
20+
21+
The same file arguments are passed to both commands, so the example above is equivalent to:
22+
23+
```bash
24+
rs lint src/index.ts packages/utils && rs fmt --check src/index.ts packages/utils
1325
```
1426

1527
## Checks

‎website/docs/zh/guide/cli/check.mdx‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,19 @@ description: '同时运行 lint 和格式检查,并可选启用 TypeScript 类
99
## 用法 \{#usage}
1010

1111
```bash
12-
rs check [options]
12+
rs check [options] [files...]
13+
```
14+
15+
传入文件或目录,可以将 lint 和格式检查都限制在这些路径中:
16+
17+
```bash
18+
rs check src/index.ts packages/utils
19+
```
20+
21+
相同的文件参数会同时传给两个命令,因此上述示例等同于:
22+
23+
```bash
24+
rs lint src/index.ts packages/utils && rs fmt --check src/index.ts packages/utils
1325
```
1426

1527
## 检查内容 \{#checks}

0 commit comments

Comments
 (0)