]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): handle slot argument parsing edge case
authorEvan You <yyx990803@gmail.com>
Thu, 27 Apr 2023 08:55:37 +0000 (16:55 +0800)
committerEvan You <yyx990803@gmail.com>
Thu, 27 Apr 2023 08:55:37 +0000 (16:55 +0800)
ref https://github.com/vuejs/language-tools/issues/2710

packages/compiler-core/__tests__/parse.spec.ts
packages/compiler-core/src/parse.ts

index 191be158fd77d33f7dec74240259513f7b3d1a62..7a32f21a00a114831808f505e88bc6d65eccf828 100644 (file)
@@ -11,7 +11,8 @@ import {
   Position,
   TextNode,
   InterpolationNode,
-  ConstantTypes
+  ConstantTypes,
+  DirectiveNode
 } from '../src/ast'
 
 describe('compiler: parse', () => {
@@ -1164,6 +1165,34 @@ describe('compiler: parse', () => {
       })
     })
 
+    // #3494
+    test('directive argument edge case', () => {
+      const ast = baseParse('<div v-slot:slot />')
+      const directive = (ast.children[0] as ElementNode)
+        .props[0] as DirectiveNode
+      expect(directive.arg).toMatchObject({
+        loc: {
+          start: { offset: 12, line: 1, column: 13 },
+          end: { offset: 16, line: 1, column: 17 },
+          source: 'slot'
+        }
+      })
+    })
+
+    // https://github.com/vuejs/language-tools/issues/2710
+    test('directive argument edge case (2)', () => {
+      const ast = baseParse('<div #item.item />')
+      const directive = (ast.children[0] as ElementNode)
+        .props[0] as DirectiveNode
+      expect(directive.arg).toMatchObject({
+        loc: {
+          start: { offset: 6, line: 1, column: 7 },
+          end: { offset: 15, line: 1, column: 16 },
+          source: 'item.item'
+        }
+      })
+    })
+
     test('directive with dynamic argument', () => {
       const ast = baseParse('<div v-on:[event]/>')
       const directive = (ast.children[0] as ElementNode).props[0]
index 73a1953348e841abc87aa603a28b259aee397e05..b72ad028b4bad1d4c6cc84e9753f166d1297d2f2 100644 (file)
@@ -817,7 +817,10 @@ function parseAttribute(
 
     if (match[2]) {
       const isSlot = dirName === 'slot'
-      const startOffset = name.lastIndexOf(match[2])
+      const startOffset = name.lastIndexOf(
+        match[2],
+        name.length - (match[3]?.length || 0)
+      )
       const loc = getSelection(
         context,
         getNewPosition(context, start, startOffset),