Skip to content

Commit

Permalink
fix: Eliminate comment interference
Browse files Browse the repository at this point in the history
  • Loading branch information
yifengyoujian committed Nov 22, 2022
1 parent 8fabcb0 commit bb5da38
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/core/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ export function parse(code: string) {
code
}

const [...script] = code.matchAll(ScriptStartRegExp)

// Eliminate comment interference
const codeWithoutComment = code.replace(/<!--[\s\S]*?-->/g, '');

const [...script] = codeWithoutComment.matchAll(ScriptStartRegExp)

if (script.length) {
if (script.length > 1) {
descriptor.script = true
descriptor.scriptSetup = true
} else {
const [input = ""] = script[0] ?? []
descriptor.scriptSetup = !!~input.indexOf('setup')
descriptor.code = input
}
}

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

export function compileScript(descriptor: Descriptor) {
const { code } = descriptor
const [...attrs] = code.matchAll(/(?<key>\w[^\r\n\s]+?)=["'](?<value>\w[^\r\n\s]+?)["']/g) ?? []

// Eliminate comment interference
const codeWithoutComment = code.replace(/<!--[\s\S]*?-->/g, '');

const [...attrs] = codeWithoutComment.matchAll(/(?<key>\w[^\r\n\s]+?)=["'](?<value>\w[^\r\n\s]+?)["']/g) ?? []

return {
attrs: attrs.reduce((acc: Attrs, attr) => {
const { key, value } = attr.groups ?? {}
Expand Down
20 changes: 20 additions & 0 deletions test/compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,24 @@ describe('compiler', () => {
lang: "ts"
})
})

it('Eliminate comment interference', async () => {
const code = `<script setup name='App' lang='ts'></script>
<!-- <script></script> -->
<!-- <script name="BUG" lang="js"></script> -->
`
const descriptor = parse(code).descriptor

expect(descriptor).toEqual({
scriptSetup: true,
script: false,
code
})

expect(compileScript(descriptor).attrs).toEqual({
name: "App",
lang: "ts"
})

})
})

0 comments on commit bb5da38

Please sign in to comment.