Skip to content

Commit

Permalink
fix(compiler-core): handle slot argument parsing edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 27, 2023
1 parent a778034 commit b434d12
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
31 changes: 30 additions & 1 deletion packages/compiler-core/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
Position,
TextNode,
InterpolationNode,
ConstantTypes
ConstantTypes,
DirectiveNode
} from '../src/ast'

describe('compiler: parse', () => {
Expand Down Expand Up @@ -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]
Expand Down
5 changes: 4 additions & 1 deletion packages/compiler-core/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit b434d12

Please sign in to comment.