1- import fs from 'node:fs' ;
21import { resolve } from 'node:path' ;
32import 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' ;
84import { printErrors } from './printErrors.js' ;
9- import type {
10- AcornParseError ,
11- CheckSyntaxExclude ,
12- CheckSyntaxOptions ,
13- ECMASyntaxError ,
14- EcmaVersion ,
15- } from './types.js' ;
165import { checkIsExclude } from './utils.js' ;
176
187type Compiler = Rspack . Compiler ;
@@ -21,36 +10,10 @@ type Compilation = Rspack.Compilation;
2110const HTML_REGEX = / \. h t m l $ / ;
2211export const JS_REGEX : RegExp = / \. (?: j s | m j s | c j s | j s x ) $ / ;
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}
0 commit comments