Skip to content

Commit 342edce

Browse files
authored
fix: run formatting checks after lint errors (#558)
1 parent 5f000bc commit 342edce

4 files changed

Lines changed: 67 additions & 66 deletions

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ async function runCheckCLI(args: string[]): Promise<void> {
187187
...(values.typeCheck ? ['--type-check'] : []),
188188
...fileArgs,
189189
]);
190-
if (process.exitCode) {
190+
191+
const lintExitCode = Number(process.exitCode ?? 0);
192+
if (values.fix && lintExitCode !== 0) {
191193
return;
192194
}
193195

@@ -200,10 +202,17 @@ async function runCheckCLI(args: string[]): Promise<void> {
200202
/* rspackChunkName: 'fmt' */
201203
'../fmt/cli.ts'
202204
);
205+
206+
// Keep each phase's status separate: fmt may succeed or return without
207+
// setting an exit code, but neither should clear a lint failure.
208+
process.exitCode = 0;
209+
203210
await runFmtCLI([values.fix ? '--write' : '--check', ...fileArgs], {
204211
fixOption: '--fix',
205212
loadedConfig,
206213
});
214+
215+
process.exitCode = Math.max(lintExitCode, Number(process.exitCode ?? 0));
207216
}
208217

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

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,57 @@ test('enables type checking only with --type-check', () => {
165165
expect(`${withTypeCheck.stdout}\n${withTypeCheck.stderr}`).toContain(
166166
'TS2322',
167167
);
168+
expect(withTypeCheck.stdout).toContain('Format check passed in');
168169
});
169170

170-
test('does not run the formatting check when lint fails', () => {
171+
test.each([
172+
{ source: 'debugger;\n', formatted: true },
173+
{ source: 'debugger;const value=true', formatted: false },
174+
])(
175+
'checks formatting after lint fails (formatted: $formatted)',
176+
({ source, formatted }) => {
177+
writeLintConfig();
178+
writeProjectFile('src/index.js', source);
179+
180+
const result = runCheck(['src/index.js']);
181+
182+
expect(result.status).toBe(1);
183+
expect(`${result.stdout}\n${result.stderr}`).toContain(
184+
"Unexpected 'debugger' statement",
185+
);
186+
if (formatted) {
187+
expect(result.stdout).toContain('Format check passed in');
188+
} else {
189+
expect(result.stderr).toContain('Formatting issues found in 1 file.');
190+
}
191+
expect(readProjectFile('src/index.js')).toBe(source);
192+
},
193+
);
194+
195+
test('preserves a formatter error exit code after lint fails', () => {
171196
writeLintConfig();
172197
writeProjectFile('src/index.js', 'debugger;\n');
198+
writeProjectFile('src/broken.json', '{ "value": }');
199+
200+
const result = runCheck(['src']);
201+
202+
expect(result.status).toBe(2);
203+
expect(`${result.stdout}\n${result.stderr}`).toContain(
204+
"Unexpected 'debugger' statement",
205+
);
206+
expect(result.stderr).toContain('broken.json');
207+
});
208+
209+
test('does not format when lint fails in fix mode', () => {
210+
writeLintConfig();
211+
writeProjectFile('src/index.js', 'debugger;const value=true');
173212

174-
const result = runCheck();
213+
const result = runCheck(['--fix', 'src/index.js']);
175214

176215
expect(result.status).toBe(1);
177216
expect(`${result.stdout}\n${result.stderr}`).toContain(
178217
"Unexpected 'debugger' statement",
179218
);
180-
expect(result.stdout).not.toContain('Checking formatting...');
219+
expect(result.stdout).not.toContain('Formatting...');
220+
expect(readProjectFile('src/index.js')).toBe('debugger;const value=true');
181221
});

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

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: 'Run linting and formatting checks together, with optional TypeScri
44

55
# check
66

7-
The `rs check` command combines linting, formatting, and optional TypeScript type checking for the current project. By default, it runs [`rs lint`](./lint) followed by [`rs fmt --check`](./fmt#--check).
7+
The `rs check` command runs lint and formatting checks for the current project, with optional TypeScript type checking.
88

99
## Usage
1010

@@ -18,21 +18,11 @@ Pass files or directories to limit both linting and formatting to those paths:
1818
rs check src/index.ts packages/utils
1919
```
2020

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
25-
```
26-
2721
## Checks
2822

29-
Running `rs check` is equivalent to:
23+
By default, `rs check` runs [`rs lint`](./lint) followed by [`rs fmt --check`](./fmt#--check). It reports issues without modifying files.
3024

31-
```bash
32-
rs lint && rs fmt --check
33-
```
34-
35-
Checks run sequentially. If linting fails, `rs check` stops without running the formatting check. The command exits successfully only when every enabled check passes.
25+
Formatting is still checked if linting or type checking reports errors. The command exits successfully only when all enabled checks pass.
3626

3727
## Options
3828

@@ -44,30 +34,16 @@ Automatically fix linting and formatting issues:
4434
rs check --fix
4535
```
4636

47-
This is equivalent to:
48-
49-
```bash
50-
rs lint --fix && rs fmt --write
51-
```
52-
53-
If linting still fails after applying available fixes, `rs check` stops without running the formatter.
37+
This first runs `rs lint --fix` to apply available lint fixes. If linting and any enabled type checking pass, it then runs `rs fmt --write` to format the updated files.
5438

5539
### `--type-check`
5640

57-
Enable TypeScript type checking as part of linting:
41+
Add `--type-check` to enable TypeScript type checking alongside linting:
5842

5943
```bash
6044
rs check --type-check
6145
```
6246

63-
This is equivalent to:
64-
65-
```bash
66-
rs lint --type-check && rs fmt --check
67-
```
68-
69-
Type checking is disabled when this option is omitted.
70-
7147
### `-h, --help`
7248

7349
Display usage and option information without running checks:
@@ -78,4 +54,4 @@ rs check --help
7854

7955
## Configuration
8056

81-
`rs check` has no separate `define.check()` configuration. It uses the linting configuration from [`define.lint()`](../configuration#define-lint) and the formatting configuration from [`define.fmt()`](../configuration#define-fmt).
57+
`rs check` uses the linting configuration from [`define.lint()`](../configuration#define-lint) and the formatting configuration from [`define.fmt()`](../configuration#define-fmt).
Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
11
---
2-
description: '同时运行 lint 和格式检查,并可选启用 TypeScript 类型检查。'
2+
description: '统一运行 lint 和格式检查,并可选启用 TypeScript 类型检查。'
33
---
44

55
# check
66

7-
`rs check` 命令可统一运行当前项目的 lint、格式和可选的 TypeScript 类型检查。默认情况下,它会先运行 [`rs lint`](./lint),再运行 [`rs fmt --check`](./fmt#--check)。
7+
`rs check` 命令用于检查当前项目的 lint 和格式问题,也可以启用 TypeScript 类型检查。
88

99
## 用法 \{#usage}
1010

1111
```bash
1212
rs check [options] [files...]
1313
```
1414

15-
传入文件或目录,可以将 lint 和格式检查都限制在这些路径中:
15+
传入文件或目录,可以只检查指定路径下的 lint 和格式问题:
1616

1717
```bash
1818
rs check src/index.ts packages/utils
1919
```
2020

21-
相同的文件参数会同时传给两个命令,因此上述示例等同于:
22-
23-
```bash
24-
rs lint src/index.ts packages/utils && rs fmt --check src/index.ts packages/utils
25-
```
26-
2721
## 检查内容 \{#checks}
2822

29-
运行 `rs check` 等同于:
23+
默认情况下,`rs check` 会先运行 [`rs lint`](./lint),再运行 [`rs fmt --check`](./fmt#--check),仅报告问题,不修改文件。
3024

31-
```bash
32-
rs lint && rs fmt --check
33-
```
34-
35-
各项检查会按顺序运行。如果 lint 失败,`rs check` 会停止运行,不再检查格式。只有所有已启用的检查均通过时,该命令才会成功退出。
25+
即使 lint 或类型检查报告错误,也会继续检查格式。只有所有已启用的检查都通过,命令才会成功退出。
3626

3727
## 选项 \{#options}
3828

@@ -44,30 +34,16 @@ rs lint && rs fmt --check
4434
rs check --fix
4535
```
4636

47-
该命令等同于:
48-
49-
```bash
50-
rs lint --fix && rs fmt --write
51-
```
52-
53-
如果应用可用的修复后 lint 仍然失败,`rs check` 会停止运行,不再执行格式化。
37+
该选项会先运行 `rs lint --fix`,自动修复 lint 问题。只有 lint 和已启用的类型检查都通过,才会继续运行 `rs fmt --write`,格式化修复后的文件。
5438

5539
### `--type-check`
5640

57-
在 lint 过程中启用 TypeScript 类型检查:
41+
添加 `--type-check`,可在 lint 过程中启用 TypeScript 类型检查:
5842

5943
```bash
6044
rs check --type-check
6145
```
6246

63-
该命令等同于:
64-
65-
```bash
66-
rs lint --type-check && rs fmt --check
67-
```
68-
69-
省略此选项时,不会运行类型检查。
70-
7147
### `-h, --help`
7248

7349
显示命令用法和选项信息,但不运行检查:
@@ -78,4 +54,4 @@ rs check --help
7854

7955
## 配置 \{#configuration}
8056

81-
`rs check` 没有独立的 `define.check()` 配置。它会使用 [`define.lint()`](../configuration#define-lint) 中的 lint 配置,以及 [`define.fmt()`](../configuration#define-fmt) 中的格式化配置。
57+
`rs check` 使用 [`define.lint()`](../configuration#define-lint) 中的 lint 配置,以及 [`define.fmt()`](../configuration#define-fmt) 中的格式化配置。

0 commit comments

Comments
 (0)