]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): expose correct range for empty blocks
authorEvan You <yyx990803@gmail.com>
Mon, 19 Jul 2021 21:05:37 +0000 (17:05 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 19 Jul 2021 21:05:37 +0000 (17:05 -0400)
packages/compiler-sfc/__tests__/parse.spec.ts
packages/compiler-sfc/src/parse.ts

index 2c640853b1abb941be1b52e23979992791ce8503..fff6cf3c29f90b15e45275dc23bdd1e284d24fcc 100644 (file)
@@ -111,10 +111,26 @@ h1 { color: red }
     )
   })
 
-  test('should keep template nodes with no content', () => {
+  test('should parse correct range for blocks with no content (self closing)', () => {
     const { descriptor } = parse(`<template/>`)
     expect(descriptor.template).toBeTruthy()
     expect(descriptor.template!.content).toBeFalsy()
+    expect(descriptor.template!.loc).toMatchObject({
+      start: { line: 1, column: 1, offset: 0 },
+      end: { line: 1, column: 1, offset: 0 },
+      source: ''
+    })
+  })
+
+  test('should parse correct range for blocks with no content (explicit)', () => {
+    const { descriptor } = parse(`<template></template>`)
+    expect(descriptor.template).toBeTruthy()
+    expect(descriptor.template!.content).toBeFalsy()
+    expect(descriptor.template!.loc).toMatchObject({
+      start: { line: 1, column: 11, offset: 10 },
+      end: { line: 1, column: 11, offset: 10 },
+      source: ''
+    })
   })
 
   test('should ignore other nodes with no content', () => {
index ad50baf5b95b8e0de4cf3a68d1c7d79c2975464b..7e9c2c256d8dd00264ed59a30ce8f8d46d7ed02d 100644 (file)
@@ -305,6 +305,16 @@ function createBlock(
     start = node.children[0].loc.start
     end = node.children[node.children.length - 1].loc.end
     content = source.slice(start.offset, end.offset)
+  } else {
+    const offset = node.loc.source.indexOf(`</`)
+    if (offset > -1) {
+      start = {
+        line: start.line,
+        column: start.column + offset,
+        offset: start.offset + offset
+      }
+    }
+    end = { ...start }
   }
   const loc = {
     source: content,