Skip to content

Commit b434d12

Browse files
committed
fix(compiler-core): handle slot argument parsing edge case
ref vuejs/language-tools#2710
1 parent a778034 commit b434d12

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

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

+30-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
Position,
1212
TextNode,
1313
InterpolationNode,
14-
ConstantTypes
14+
ConstantTypes,
15+
DirectiveNode
1516
} from '../src/ast'
1617

1718
describe('compiler: parse', () => {
@@ -1164,6 +1165,34 @@ describe('compiler: parse', () => {
11641165
})
11651166
})
11661167

1168+
// #3494
1169+
test('directive argument edge case', () => {
1170+
const ast = baseParse('<div v-slot:slot />')
1171+
const directive = (ast.children[0] as ElementNode)
1172+
.props[0] as DirectiveNode
1173+
expect(directive.arg).toMatchObject({
1174+
loc: {
1175+
start: { offset: 12, line: 1, column: 13 },
1176+
end: { offset: 16, line: 1, column: 17 },
1177+
source: 'slot'
1178+
}
1179+
})
1180+
})
1181+
1182+
// https://github.com/vuejs/language-tools/issues/2710
1183+
test('directive argument edge case (2)', () => {
1184+
const ast = baseParse('<div #item.item />')
1185+
const directive = (ast.children[0] as ElementNode)
1186+
.props[0] as DirectiveNode
1187+
expect(directive.arg).toMatchObject({
1188+
loc: {
1189+
start: { offset: 6, line: 1, column: 7 },
1190+
end: { offset: 15, line: 1, column: 16 },
1191+
source: 'item.item'
1192+
}
1193+
})
1194+
})
1195+
11671196
test('directive with dynamic argument', () => {
11681197
const ast = baseParse('<div v-on:[event]/>')
11691198
const directive = (ast.children[0] as ElementNode).props[0]

packages/compiler-core/src/parse.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,10 @@ function parseAttribute(
817817

818818
if (match[2]) {
819819
const isSlot = dirName === 'slot'
820-
const startOffset = name.lastIndexOf(match[2])
820+
const startOffset = name.lastIndexOf(
821+
match[2],
822+
name.length - (match[3]?.length || 0)
823+
)
821824
const loc = getSelection(
822825
context,
823826
getNewPosition(context, start, startOffset),

0 commit comments

Comments
 (0)