]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): fix line/column tracking when fast forwarding
authorEvan You <yyx990803@gmail.com>
Mon, 27 Nov 2023 06:49:13 +0000 (14:49 +0800)
committerEvan You <yyx990803@gmail.com>
Mon, 27 Nov 2023 06:49:13 +0000 (14:49 +0800)
packages/compiler-core/src/tokenizer.ts
packages/compiler-sfc/__tests__/parse.spec.ts

index 39b64622dc04bd9381c72dec63e9aa88350bdc5b..22ff504dbe365e86858397959e87d8e3b83cf48e 100644 (file)
@@ -463,7 +463,11 @@ export default class Tokenizer {
    */
   private fastForwardTo(c: number): boolean {
     while (++this.index < this.buffer.length) {
-      if (this.buffer.charCodeAt(this.index) === c) {
+      const cc = this.buffer.charCodeAt(this.index)
+      if (cc === CharCodes.NewLine) {
+        this.newlines.push(this.index)
+      }
+      if (cc === c) {
         return true
       }
     }
index 7c8efdfc40f3ea1379570d0d33b0237d0fada477..c19232a7034e79fec701b4a6818e91995dd1c39a 100644 (file)
@@ -7,15 +7,61 @@ describe('compiler:sfc', () => {
     test('style block', () => {
       // Padding determines how many blank lines will there be before the style block
       const padding = Math.round(Math.random() * 10)
-      const style = parse(
-        `${'\n'.repeat(padding)}<style>\n.color {\n color: red;\n }\n</style>\n`
-      ).descriptor.styles[0]
+      const src =
+        `${'\n'.repeat(padding)}` +
+        `<style>
+.css {
+color: red;
+}
+</style>
 
-      expect(style.map).not.toBeUndefined()
+<style module>
+.css-module {
+color: red;
+}
+</style>
 
-      const consumer = new SourceMapConsumer(style.map!)
+<style scoped>
+.css-scoped {
+color: red;
+}
+</style>
+
+<style scoped>
+.css-scoped-nested {
+color: red;
+.dummy {
+color: green;
+}
+font-weight: bold;
+}
+</style>`
+      const {
+        descriptor: { styles }
+      } = parse(src)
+
+      expect(styles[0].map).not.toBeUndefined()
+      const consumer = new SourceMapConsumer(styles[0].map!)
+      const lineOffset =
+        src.slice(0, src.indexOf(`<style>`)).split('\n').length - 1
       consumer.eachMapping(mapping => {
-        expect(mapping.originalLine - mapping.generatedLine).toBe(padding)
+        expect(mapping.generatedLine + lineOffset).toBe(mapping.originalLine)
+      })
+
+      expect(styles[1].map).not.toBeUndefined()
+      const consumer1 = new SourceMapConsumer(styles[1].map!)
+      const lineOffset1 =
+        src.slice(0, src.indexOf(`<style module>`)).split('\n').length - 1
+      consumer1.eachMapping(mapping => {
+        expect(mapping.generatedLine + lineOffset1).toBe(mapping.originalLine)
+      })
+
+      expect(styles[2].map).not.toBeUndefined()
+      const consumer2 = new SourceMapConsumer(styles[2].map!)
+      const lineOffset2 =
+        src.slice(0, src.indexOf(`<style scoped>`)).split('\n').length - 1
+      consumer2.eachMapping(mapping => {
+        expect(mapping.generatedLine + lineOffset2).toBe(mapping.originalLine)
       })
     })