Skip to content

Commit f25592f

Browse files
authored
chore: export checkSyntax as utils (#16)
1 parent 14a5e79 commit f25592f

File tree

3 files changed

+102
-86
lines changed

3 files changed

+102
-86
lines changed

src/CheckSyntaxPlugin.ts

Lines changed: 3 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1-
import fs from 'node:fs';
21
import { resolve } from 'node:path';
32
import type { Rspack } from '@rsbuild/core';
4-
import { parse } from 'acorn';
5-
import { browserslistToESVersion } from 'browserslist-to-es-version';
6-
import { generateError } from './generateError.js';
7-
import { generateHtmlScripts } from './generateHtmlScripts.js';
3+
import { CheckSyntax } from './checkSyntax.js';
84
import { printErrors } from './printErrors.js';
9-
import type {
10-
AcornParseError,
11-
CheckSyntaxExclude,
12-
CheckSyntaxOptions,
13-
ECMASyntaxError,
14-
EcmaVersion,
15-
} from './types.js';
165
import { checkIsExclude } from './utils.js';
176

187
type Compiler = Rspack.Compiler;
@@ -21,36 +10,10 @@ type Compilation = Rspack.Compilation;
2110
const HTML_REGEX = /\.html$/;
2211
export const JS_REGEX: RegExp = /\.(?:js|mjs|cjs|jsx)$/;
2312

24-
export class CheckSyntaxPlugin {
25-
errors: ECMASyntaxError[] = [];
26-
27-
ecmaVersion: EcmaVersion;
28-
29-
targets: string[];
30-
31-
rootPath: string;
32-
33-
exclude: CheckSyntaxExclude | undefined;
34-
35-
excludeOutput: CheckSyntaxExclude | undefined;
36-
37-
constructor(
38-
options: CheckSyntaxOptions &
39-
Required<Pick<CheckSyntaxOptions, 'targets'>> & {
40-
rootPath: string;
41-
},
42-
) {
43-
this.targets = options.targets;
44-
this.exclude = options.exclude;
45-
this.excludeOutput = options.excludeOutput;
46-
this.rootPath = options.rootPath;
47-
this.ecmaVersion =
48-
options.ecmaVersion || browserslistToESVersion(this.targets);
49-
}
50-
13+
export class CheckSyntaxRspackPlugin extends CheckSyntax {
5114
apply(compiler: Compiler): void {
5215
compiler.hooks.afterEmit.tapPromise(
53-
CheckSyntaxPlugin.name,
16+
CheckSyntaxRspackPlugin.name,
5417
async (compilation: Compilation) => {
5518
const outputPath = compilation.outputOptions.path || 'dist';
5619

@@ -81,42 +44,4 @@ export class CheckSyntaxPlugin {
8144
},
8245
);
8346
}
84-
85-
private async check(filepath: string) {
86-
if (HTML_REGEX.test(filepath)) {
87-
const htmlScripts = await generateHtmlScripts(filepath);
88-
await Promise.all(
89-
htmlScripts.map(async (script) => {
90-
if (!checkIsExclude(filepath, this.exclude)) {
91-
await this.tryParse(filepath, script);
92-
}
93-
}),
94-
);
95-
}
96-
97-
if (JS_REGEX.test(filepath)) {
98-
const jsScript = await fs.promises.readFile(filepath, 'utf-8');
99-
await this.tryParse(filepath, jsScript);
100-
}
101-
}
102-
103-
private async tryParse(filepath: string, code: string) {
104-
try {
105-
parse(code, { ecmaVersion: this.ecmaVersion });
106-
} catch (_: unknown) {
107-
const err = _ as AcornParseError;
108-
109-
const error = await generateError({
110-
err,
111-
code,
112-
filepath,
113-
exclude: this.exclude,
114-
rootPath: this.rootPath,
115-
});
116-
117-
if (error) {
118-
this.errors.push(error);
119-
}
120-
}
121-
}
12247
}

src/checkSyntax.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import fs from 'node:fs';
2+
import { parse } from 'acorn';
3+
import { browserslistToESVersion } from 'browserslist-to-es-version';
4+
import { generateError } from './generateError.js';
5+
import { generateHtmlScripts } from './generateHtmlScripts.js';
6+
import type {
7+
AcornParseError,
8+
CheckSyntaxExclude,
9+
CheckSyntaxOptions,
10+
ECMASyntaxError,
11+
EcmaVersion,
12+
} from './types.js';
13+
import { checkIsExclude } from './utils.js';
14+
15+
const HTML_REGEX = /\.html$/;
16+
export const JS_REGEX: RegExp = /\.(?:js|mjs|cjs|jsx)$/;
17+
18+
export class CheckSyntax {
19+
errors: ECMASyntaxError[] = [];
20+
21+
ecmaVersion: EcmaVersion;
22+
23+
targets: string[];
24+
25+
rootPath: string;
26+
27+
exclude: CheckSyntaxExclude | undefined;
28+
29+
excludeOutput: CheckSyntaxExclude | undefined;
30+
31+
constructor(
32+
options: CheckSyntaxOptions &
33+
Required<Pick<CheckSyntaxOptions, 'targets'>> & {
34+
rootPath: string;
35+
},
36+
) {
37+
this.targets = options.targets;
38+
this.exclude = options.exclude;
39+
this.excludeOutput = options.excludeOutput;
40+
this.rootPath = options.rootPath;
41+
this.ecmaVersion =
42+
options.ecmaVersion || browserslistToESVersion(this.targets);
43+
}
44+
45+
async check(filepath: string, code?: string) {
46+
// If the code is provided, no need to read file.
47+
if (code) {
48+
return await this.tryParse(filepath, code);
49+
}
50+
51+
if (HTML_REGEX.test(filepath)) {
52+
const htmlScripts = await generateHtmlScripts(filepath);
53+
await Promise.all(
54+
htmlScripts.map(async (script) => {
55+
if (!checkIsExclude(filepath, this.exclude)) {
56+
await this.tryParse(filepath, script);
57+
}
58+
}),
59+
);
60+
}
61+
62+
if (JS_REGEX.test(filepath)) {
63+
const jsScript = await fs.promises.readFile(filepath, 'utf-8');
64+
await this.tryParse(filepath, jsScript);
65+
}
66+
}
67+
68+
async tryParse(filepath: string, code: string) {
69+
try {
70+
parse(code, { ecmaVersion: this.ecmaVersion });
71+
} catch (_: unknown) {
72+
const err = _ as AcornParseError;
73+
74+
const error = await generateError({
75+
err,
76+
code,
77+
filepath,
78+
exclude: this.exclude,
79+
rootPath: this.rootPath,
80+
});
81+
82+
if (error) {
83+
this.errors.push(error);
84+
}
85+
}
86+
}
87+
}

src/index.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RsbuildPlugin } from '@rsbuild/core';
2-
import { CheckSyntaxPlugin } from './CheckSyntaxPlugin.js';
2+
import { CheckSyntaxRspackPlugin } from './CheckSyntaxPlugin.js';
33
import type { CheckSyntaxOptions } from './types.js';
44

55
export type PluginCheckSyntaxOptions = CheckSyntaxOptions;
@@ -22,14 +22,18 @@ export function pluginCheckSyntax(
2222

2323
const targets = options.targets ?? environment.browserslist;
2424

25-
chain.plugin(CheckSyntaxPlugin.name).use(CheckSyntaxPlugin, [
26-
{
27-
targets,
28-
rootPath,
29-
...(typeof options === 'object' ? options : {}),
30-
},
31-
]);
25+
chain
26+
.plugin(CheckSyntaxRspackPlugin.name)
27+
.use(CheckSyntaxRspackPlugin, [
28+
{
29+
targets,
30+
rootPath,
31+
...(typeof options === 'object' ? options : {}),
32+
},
33+
]);
3234
});
3335
},
3436
};
3537
}
38+
39+
export { CheckSyntax } from './checkSyntax.js';

0 commit comments

Comments
 (0)