]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): custom blocks sourcemap (#1812)
authorkazuya kawaguchi <kawakazu80@gmail.com>
Fri, 14 Aug 2020 21:47:28 +0000 (06:47 +0900)
committerGitHub <noreply@github.com>
Fri, 14 Aug 2020 21:47:28 +0000 (17:47 -0400)
packages/compiler-sfc/__tests__/parse.spec.ts
packages/compiler-sfc/src/parse.ts

index d81ce60ce7aef1ea5a92ea9a32c7f0c0b78ccf5d..a782bc837793cecc65ec1dfe8267e15fcbdf5637 100644 (file)
@@ -33,6 +33,20 @@ describe('compiler:sfc', () => {
         expect(mapping.originalLine - mapping.generatedLine).toBe(padding)
       })
     })
+
+    test('custom block', () => {
+      const padding = Math.round(Math.random() * 10)
+      const custom = parse(
+        `${'\n'.repeat(padding)}<i18n>\n{\n  "greeting": "hello"\n}\n</i18n>\n`
+      ).descriptor.customBlocks[0]
+
+      expect(custom!.map).not.toBeUndefined()
+
+      const consumer = new SourceMapConsumer(custom!.map!)
+      consumer.eachMapping(mapping => {
+        expect(mapping.originalLine - mapping.generatedLine).toBe(padding)
+      })
+    })
   })
 
   test('pad content', () => {
@@ -45,11 +59,16 @@ export default {}
 </script>
 <style>
 h1 { color: red }
-</style>`
+</style>
+<i18n>
+{ "greeting": "hello" }
+</i18n>
+`
     const padFalse = parse(content.trim(), { pad: false }).descriptor
     expect(padFalse.template!.content).toBe('\n<div></div>\n')
     expect(padFalse.script!.content).toBe('\nexport default {}\n')
     expect(padFalse.styles[0].content).toBe('\nh1 { color: red }\n')
+    expect(padFalse.customBlocks[0].content).toBe('\n{ "greeting": "hello" }\n')
 
     const padTrue = parse(content.trim(), { pad: true }).descriptor
     expect(padTrue.script!.content).toBe(
@@ -58,6 +77,9 @@ h1 { color: red }
     expect(padTrue.styles[0].content).toBe(
       Array(6 + 1).join('\n') + '\nh1 { color: red }\n'
     )
+    expect(padTrue.customBlocks[0].content).toBe(
+      Array(9 + 1).join('\n') + '\n{ "greeting": "hello" }\n'
+    )
 
     const padLine = parse(content.trim(), { pad: 'line' }).descriptor
     expect(padLine.script!.content).toBe(
@@ -66,6 +88,9 @@ h1 { color: red }
     expect(padLine.styles[0].content).toBe(
       Array(6 + 1).join('\n') + '\nh1 { color: red }\n'
     )
+    expect(padLine.customBlocks[0].content).toBe(
+      Array(9 + 1).join('\n') + '\n{ "greeting": "hello" }\n'
+    )
 
     const padSpace = parse(content.trim(), { pad: 'space' }).descriptor
     expect(padSpace.script!.content).toBe(
@@ -78,6 +103,12 @@ h1 { color: red }
         ' '
       ) + '\nh1 { color: red }\n'
     )
+    expect(padSpace.customBlocks[0].content).toBe(
+      `<template>\n<div></div>\n</template>\n<script>\nexport default {}\n</script>\n<style>\nh1 { color: red }\n</style>\n<i18n>`.replace(
+        /./g,
+        ' '
+      ) + '\n{ "greeting": "hello" }\n'
+    )
   })
 
   test('should ignore nodes with no content', () => {
index eaeb2bcd6cece9d9596c2ec5c23774a5a7c9c266..34d6715010e05626f91a31946b868d3c2d81b79e 100644 (file)
@@ -204,6 +204,7 @@ export function parse(
     genMap(descriptor.template)
     genMap(descriptor.script)
     descriptor.styles.forEach(genMap)
+    descriptor.customBlocks.forEach(genMap)
   }
 
   const result = {