Skip to content

Commit 45ac950

Browse files
woshiaruizhengxiaorui1234chenjiahan
authored
feat: add excludeErrorLogs option (#18)
Co-authored-by: zhengxiaorui.1234 <[email protected]> Co-authored-by: neverland <[email protected]>
1 parent d5a7fd3 commit 45ac950

File tree

7 files changed

+83
-16
lines changed

7 files changed

+83
-16
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,23 @@ pluginCheckSyntax({
176176
});
177177
```
178178
179+
### excludeErrorLogs
180+
181+
- **Type:** `('source' | 'output' | 'reason' | 'code')[]`
182+
- **Default:** `[]`
183+
184+
`excludeErrorLogs` is used to ignore specified syntax error messages after detection. You can pass in one or more error message types to ignore.
185+
186+
- **Example:**
187+
188+
For example, to ignore the reason and code displayed in the terminal.
189+
190+
```ts
191+
pluginCheckSyntax({
192+
excludeErrorLogs: ["reason", "code"],
193+
});
194+
```
195+
179196
## Limitations
180197
181198
1. Check Syntax plugin only checks incompatible syntax in the outputs and cannot detect missing polyfills.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
"main": "./dist/index.js",
1515
"module": "./dist/index.mjs",
1616
"types": "./dist/index.d.ts",
17-
"files": [
18-
"dist"
19-
],
17+
"files": ["dist"],
2018
"scripts": {
2119
"build": "rslib build",
2220
"dev": "rslib build --watch",

src/CheckSyntaxPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class CheckSyntaxRspackPlugin extends CheckSyntax {
4040
}),
4141
);
4242

43-
printErrors(this.errors, this.ecmaVersion);
43+
printErrors(this.errors, this.ecmaVersion, this.excludeErrorLogs);
4444
},
4545
);
4646
}

src/checkSyntax.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
CheckSyntaxOptions,
1010
ECMASyntaxError,
1111
EcmaVersion,
12+
SyntaxErrorKey,
1213
} from './types.js';
1314
import { checkIsExclude } from './utils.js';
1415

@@ -28,6 +29,8 @@ export class CheckSyntax {
2829

2930
excludeOutput: CheckSyntaxExclude | undefined;
3031

32+
excludeErrorLogs: SyntaxErrorKey[];
33+
3134
constructor(
3235
options: CheckSyntaxOptions &
3336
Required<Pick<CheckSyntaxOptions, 'targets'>> & {
@@ -40,6 +43,7 @@ export class CheckSyntax {
4043
this.rootPath = options.rootPath;
4144
this.ecmaVersion =
4245
options.ecmaVersion || browserslistToESVersion(this.targets);
46+
this.excludeErrorLogs = options.excludeErrorLogs || [];
4347
}
4448

4549
async check(filepath: string, code?: string) {

src/printErrors.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
import { logger } from '@rsbuild/core';
22
import color from 'picocolors';
3-
import type { ECMASyntaxError, EcmaVersion } from './types.js';
4-
5-
type Error = {
6-
source: string;
7-
output?: string;
8-
reason: string;
9-
code: string;
10-
};
3+
import type {
4+
ECMASyntaxError,
5+
EcmaVersion,
6+
SyntaxErrorInfo,
7+
SyntaxErrorKey,
8+
} from './types.js';
119

1210
export function printErrors(
1311
errors: ECMASyntaxError[],
1412
ecmaVersion: EcmaVersion,
13+
excludeErrorLogs: SyntaxErrorKey[],
1514
): void {
1615
if (errors.length === 0) {
1716
logger.success('[@rsbuild/plugin-check-syntax] Syntax check passed.');
1817
return;
1918
}
2019

21-
const errs: Error[] = errors.map((error) => ({
20+
const errs: SyntaxErrorInfo[] = errors.map((error) => ({
2221
source: `${error.source.path}:${error.source.line}:${error.source.column}`,
2322
output: error.output
2423
? `${error.output.path}:${error.output.line}:${error.output.column}`
@@ -36,7 +35,7 @@ export function printErrors(
3635

3736
errs.forEach((err, index) => {
3837
console.log(color.bold(color.red(` ERROR ${index + 1}`)));
39-
printMain(err, longest);
38+
printMain(err, longest, excludeErrorLogs);
4039
});
4140

4241
throw new Error(
@@ -48,7 +47,11 @@ export function printErrors(
4847
);
4948
}
5049

51-
function printMain(error: Error, longest: number) {
50+
function printMain(
51+
error: SyntaxErrorInfo,
52+
longest: number,
53+
excludeErrorLogs: SyntaxErrorKey[],
54+
) {
5255
const fillWhiteSpace = (s: string, longest: number) => {
5356
if (s.length < longest) {
5457
const rightPadding = ' '.repeat(longest - s.length);
@@ -58,7 +61,7 @@ function printMain(error: Error, longest: number) {
5861
};
5962

6063
for (const [key, content] of Object.entries(error)) {
61-
if (!content) {
64+
if (!content || excludeErrorLogs.includes(key as SyntaxErrorKey)) {
6265
continue;
6366
}
6467
const title = color.magenta(`${fillWhiteSpace(`${key}:`, longest + 1)}`);

src/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export type CheckSyntaxOptions = {
2525
* The priority of `ecmaVersion` is higher than `targets`.
2626
*/
2727
ecmaVersion?: EcmaVersion;
28+
/**
29+
* Used to ignore specified syntax error messages after detection.
30+
* You can pass in one or more error message types to ignore.
31+
*/
32+
excludeErrorLogs?: SyntaxErrorKey[];
2833
};
2934

3035
export interface Location {
@@ -60,3 +65,12 @@ export type AcornParseError = {
6065
pos: number;
6166
loc: Location;
6267
};
68+
69+
export type SyntaxErrorInfo = {
70+
source: string;
71+
output?: string;
72+
reason: string;
73+
code: string;
74+
};
75+
76+
export type SyntaxErrorKey = keyof SyntaxErrorInfo;

test/esnext/index.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,34 @@ test('should not throw error when using optional chaining and ecmaVersion is 202
106106

107107
await expect(rsbuild.build()).resolves.toBeTruthy();
108108
});
109+
110+
test('should allow to exclude the output and reason details from the error message.', async () => {
111+
const { logs, restore } = proxyConsole();
112+
113+
const rsbuild = await createRsbuild({
114+
cwd: __dirname,
115+
rsbuildConfig: {
116+
...(await loadConfig({ cwd: __dirname })).content,
117+
plugins: [
118+
pluginCheckSyntax({
119+
targets: ['chrome >= 53'],
120+
excludeErrorLogs: ['source', 'output', 'reason', 'code'],
121+
}),
122+
],
123+
},
124+
});
125+
126+
await expect(rsbuild.build()).rejects.toThrowError(
127+
'[@rsbuild/plugin-check-syntax]',
128+
);
129+
130+
restore();
131+
132+
expect(logs.find((log) => log.includes('ERROR 1'))).toBeTruthy();
133+
expect(logs.find((log) => log.includes('source:'))).toBeFalsy();
134+
expect(logs.find((log) => log.includes('output:'))).toBeFalsy();
135+
expect(logs.find((log) => log.includes('reason:'))).toBeFalsy();
136+
expect(
137+
logs.find((log) => log.includes('> 3 | console.log(arr, arr?.flat());')),
138+
).toBeFalsy();
139+
});

0 commit comments

Comments
 (0)