]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(compiler-sfc): add ignoreEmpty option for sfc parse method
authorEvan You <yyx990803@gmail.com>
Mon, 19 Jul 2021 23:02:46 +0000 (19:02 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 19 Jul 2021 23:02:46 +0000 (19:02 -0400)
packages/compiler-sfc/__tests__/parse.spec.ts
packages/compiler-sfc/src/parse.ts

index 8d1cad1a9c8e534a9977cf43d08639468ca27051..9db9bb7f3e6e57151b24b7d188e35457fad0f80c 100644 (file)
@@ -167,6 +167,28 @@ h1 { color: red }
     expect(descriptor.script!.attrs['src']).toBe('com')
   })
 
+  test('ignoreEmpty: false', () => {
+    const { descriptor } = parse(
+      `<script></script>\n<script setup>\n</script>`,
+      {
+        ignoreEmpty: false
+      }
+    )
+    expect(descriptor.script).toBeTruthy()
+    expect(descriptor.script!.loc).toMatchObject({
+      source: '',
+      start: { line: 1, column: 9, offset: 8 },
+      end: { line: 1, column: 9, offset: 8 }
+    })
+
+    expect(descriptor.scriptSetup).toBeTruthy()
+    expect(descriptor.scriptSetup!.loc).toMatchObject({
+      source: '\n',
+      start: { line: 2, column: 15, offset: 32 },
+      end: { line: 3, column: 1, offset: 33 }
+    })
+  })
+
   test('nested templates', () => {
     const content = `
     <template v-if="ok">ok</template>
index 2650d48064bcd97ee906c50bce9d5772519f83a3..019851efab3d867567019c974b60402067aee744 100644 (file)
@@ -18,6 +18,7 @@ export interface SFCParseOptions {
   sourceMap?: boolean
   sourceRoot?: string
   pad?: boolean | 'line' | 'space'
+  ignoreEmpty?: boolean
   compiler?: TemplateCompiler
 }
 
@@ -104,6 +105,7 @@ export function parse(
     filename = 'anonymous.vue',
     sourceRoot = '',
     pad = false,
+    ignoreEmpty = true,
     compiler = CompilerDOM
   }: SFCParseOptions = {}
 ): SFCParseResult {
@@ -163,7 +165,12 @@ export function parse(
       return
     }
     // we only want to keep the nodes that are not empty (when the tag is not a template)
-    if (node.tag !== 'template' && isEmpty(node) && !hasSrc(node)) {
+    if (
+      ignoreEmpty &&
+      node.tag !== 'template' &&
+      isEmpty(node) &&
+      !hasSrc(node)
+    ) {
       return
     }
     switch (node.tag) {