diff --git a/src/core/compiler.ts b/src/core/compiler.ts index 64f89f7..144536c 100644 --- a/src/core/compiler.ts +++ b/src/core/compiler.ts @@ -17,8 +17,11 @@ export function parse(code: string) { code } - const [...script] = code.matchAll(ScriptStartRegExp) - + // Eliminate comment interference + const codeWithoutComment = code.replace(//g, ''); + + const [...script] = codeWithoutComment.matchAll(ScriptStartRegExp) + if (script.length) { if (script.length > 1) { descriptor.script = true @@ -26,7 +29,6 @@ export function parse(code: string) { } else { const [input = ""] = script[0] ?? [] descriptor.scriptSetup = !!~input.indexOf('setup') - descriptor.code = input } } @@ -37,8 +39,11 @@ export function parse(code: string) { export function compileScript(descriptor: Descriptor) { const { code } = descriptor - const [...attrs] = code.matchAll(/(?\w[^\r\n\s]+?)=["'](?\w[^\r\n\s]+?)["']/g) ?? [] - + // Eliminate comment interference + const codeWithoutComment = code.replace(//g, ''); + + const [...attrs] = codeWithoutComment.matchAll(/(?\w[^\r\n\s]+?)=["'](?\w[^\r\n\s]+?)["']/g) ?? [] + return { attrs: attrs.reduce((acc: Attrs, attr) => { const { key, value } = attr.groups ?? {} diff --git a/test/compiler.test.ts b/test/compiler.test.ts index ada3739..17ee5d4 100644 --- a/test/compiler.test.ts +++ b/test/compiler.test.ts @@ -50,4 +50,24 @@ describe('compiler', () => { lang: "ts" }) }) + + it('Eliminate comment interference', async () => { + const code = ` + + + ` + const descriptor = parse(code).descriptor + + expect(descriptor).toEqual({ + scriptSetup: true, + script: false, + code + }) + + expect(compileScript(descriptor).attrs).toEqual({ + name: "App", + lang: "ts" + }) + + }) })