Skip to content

Commit b274b08

Browse files
committed
fix(compiler-sfc): expose correct range for empty blocks
1 parent d810a1a commit b274b08

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,26 @@ h1 { color: red }
111111
)
112112
})
113113

114-
test('should keep template nodes with no content', () => {
114+
test('should parse correct range for blocks with no content (self closing)', () => {
115115
const { descriptor } = parse(`<template/>`)
116116
expect(descriptor.template).toBeTruthy()
117117
expect(descriptor.template!.content).toBeFalsy()
118+
expect(descriptor.template!.loc).toMatchObject({
119+
start: { line: 1, column: 1, offset: 0 },
120+
end: { line: 1, column: 1, offset: 0 },
121+
source: ''
122+
})
123+
})
124+
125+
test('should parse correct range for blocks with no content (explicit)', () => {
126+
const { descriptor } = parse(`<template></template>`)
127+
expect(descriptor.template).toBeTruthy()
128+
expect(descriptor.template!.content).toBeFalsy()
129+
expect(descriptor.template!.loc).toMatchObject({
130+
start: { line: 1, column: 11, offset: 10 },
131+
end: { line: 1, column: 11, offset: 10 },
132+
source: ''
133+
})
118134
})
119135

120136
test('should ignore other nodes with no content', () => {

packages/compiler-sfc/src/parse.ts

+10
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,16 @@ function createBlock(
305305
start = node.children[0].loc.start
306306
end = node.children[node.children.length - 1].loc.end
307307
content = source.slice(start.offset, end.offset)
308+
} else {
309+
const offset = node.loc.source.indexOf(`</`)
310+
if (offset > -1) {
311+
start = {
312+
line: start.line,
313+
column: start.column + offset,
314+
offset: start.offset + offset
315+
}
316+
}
317+
end = { ...start }
308318
}
309319
const loc = {
310320
source: content,

0 commit comments

Comments
 (0)