Skip to content

Commit

Permalink
fix(compiler-sfc): expose correct range for empty blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 19, 2021
1 parent d810a1a commit b274b08
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/compiler-sfc/__tests__/parse.spec.ts
Expand Up @@ -111,10 +111,26 @@ h1 { color: red }
)
})

test('should keep template nodes with no content', () => {
test('should parse correct range for blocks with no content (self closing)', () => {
const { descriptor } = parse(`<template/>`)
expect(descriptor.template).toBeTruthy()
expect(descriptor.template!.content).toBeFalsy()
expect(descriptor.template!.loc).toMatchObject({
start: { line: 1, column: 1, offset: 0 },
end: { line: 1, column: 1, offset: 0 },
source: ''
})
})

test('should parse correct range for blocks with no content (explicit)', () => {
const { descriptor } = parse(`<template></template>`)
expect(descriptor.template).toBeTruthy()
expect(descriptor.template!.content).toBeFalsy()
expect(descriptor.template!.loc).toMatchObject({
start: { line: 1, column: 11, offset: 10 },
end: { line: 1, column: 11, offset: 10 },
source: ''
})
})

test('should ignore other nodes with no content', () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/compiler-sfc/src/parse.ts
Expand Up @@ -305,6 +305,16 @@ function createBlock(
start = node.children[0].loc.start
end = node.children[node.children.length - 1].loc.end
content = source.slice(start.offset, end.offset)
} else {
const offset = node.loc.source.indexOf(`</`)
if (offset > -1) {
start = {
line: start.line,
column: start.column + offset,
offset: start.offset + offset
}
}
end = { ...start }
}
const loc = {
source: content,
Expand Down

0 comments on commit b274b08

Please sign in to comment.