Skip to content

Commit bb5da38

Browse files
committed
fix: Eliminate comment interference
1 parent 8fabcb0 commit bb5da38

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/core/compiler.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ export function parse(code: string) {
1717
code
1818
}
1919

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+
2225
if (script.length) {
2326
if (script.length > 1) {
2427
descriptor.script = true
2528
descriptor.scriptSetup = true
2629
} else {
2730
const [input = ""] = script[0] ?? []
2831
descriptor.scriptSetup = !!~input.indexOf('setup')
29-
descriptor.code = input
3032
}
3133
}
3234

@@ -37,8 +39,11 @@ export function parse(code: string) {
3739

3840
export function compileScript(descriptor: Descriptor) {
3941
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+
4247
return {
4348
attrs: attrs.reduce((acc: Attrs, attr) => {
4449
const { key, value } = attr.groups ?? {}

test/compiler.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,24 @@ describe('compiler', () => {
5050
lang: "ts"
5151
})
5252
})
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+
})
5373
})

0 commit comments

Comments
 (0)