File tree Expand file tree Collapse file tree 2 files changed +30
-5
lines changed Expand file tree Collapse file tree 2 files changed +30
-5
lines changed Original file line number Diff line number Diff line change @@ -17,16 +17,18 @@ export function parse(code: string) {
17
17
code
18
18
}
19
19
20
- const [ ...script ] = code . matchAll ( ScriptStartRegExp )
21
-
20
+ // Eliminate comment interference
21
+ const codeWithoutComment = code . replace ( / < ! - - [ \s \S ] * ?- - > / g, '' ) ;
22
+
23
+ const [ ...script ] = codeWithoutComment . matchAll ( ScriptStartRegExp )
24
+
22
25
if ( script . length ) {
23
26
if ( script . length > 1 ) {
24
27
descriptor . script = true
25
28
descriptor . scriptSetup = true
26
29
} else {
27
30
const [ input = "" ] = script [ 0 ] ?? [ ]
28
31
descriptor . scriptSetup = ! ! ~ input . indexOf ( 'setup' )
29
- descriptor . code = input
30
32
}
31
33
}
32
34
@@ -37,8 +39,11 @@ export function parse(code: string) {
37
39
38
40
export function compileScript ( descriptor : Descriptor ) {
39
41
const { code } = descriptor
40
- const [ ...attrs ] = code . matchAll ( / (?< key > \w [ ^ \r \n \s ] + ?) = [ " ' ] (?< value > \w [ ^ \r \n \s ] + ?) [ " ' ] / g) ?? [ ]
41
-
42
+ // Eliminate comment interference
43
+ const codeWithoutComment = code . replace ( / < ! - - [ \s \S ] * ?- - > / g, '' ) ;
44
+
45
+ const [ ...attrs ] = codeWithoutComment . matchAll ( / (?< key > \w [ ^ \r \n \s ] + ?) = [ " ' ] (?< value > \w [ ^ \r \n \s ] + ?) [ " ' ] / g) ?? [ ]
46
+
42
47
return {
43
48
attrs : attrs . reduce ( ( acc : Attrs , attr ) => {
44
49
const { key, value } = attr . groups ?? { }
Original file line number Diff line number Diff line change @@ -50,4 +50,24 @@ describe('compiler', () => {
50
50
lang : "ts"
51
51
} )
52
52
} )
53
+
54
+ it ( 'Eliminate comment interference' , async ( ) => {
55
+ const code = `<script setup name='App' lang='ts'></script>
56
+ <!-- <script></script> -->
57
+ <!-- <script name="BUG" lang="js"></script> -->
58
+ `
59
+ const descriptor = parse ( code ) . descriptor
60
+
61
+ expect ( descriptor ) . toEqual ( {
62
+ scriptSetup : true ,
63
+ script : false ,
64
+ code
65
+ } )
66
+
67
+ expect ( compileScript ( descriptor ) . attrs ) . toEqual ( {
68
+ name : "App" ,
69
+ lang : "ts"
70
+ } )
71
+
72
+ } )
53
73
} )
You can’t perform that action at this time.
0 commit comments