Skip to content

fix(compiler-core): ensure mapping is added only if node source is available #13285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/compiler-core/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ function createCodegenContext(
name = content
}
}
addMapping(node.loc.start, name)
if (node.loc.source) {
addMapping(node.loc.start, name)
}
}
if (newlineIndex === NewlineType.Unknown) {
// multiple newlines, full iteration
Expand Down Expand Up @@ -225,7 +227,7 @@ function createCodegenContext(
context.column = code.length - newlineIndex
}
}
if (node && node.loc !== locStub) {
if (node && node.loc !== locStub && node.loc.source) {
addMapping(node.loc.end)
}
}
Expand Down
29 changes: 29 additions & 0 deletions packages/compiler-sfc/__tests__/compileTemplate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,35 @@ test('source map', () => {
).toMatchObject(getPositionInCode(template.content, `foobar`))
})

test('source map: v-if generated comment should not have original position', () => {
const template = parse(
`
<template>
<div v-if="true"></div>
</template>
`,
{ filename: 'example.vue', sourceMap: true },
).descriptor.template!

const { code, map } = compile({
filename: 'example.vue',
source: template.content,
})

expect(map!.sources).toEqual([`example.vue`])
expect(map!.sourcesContent).toEqual([template.content])

const consumer = new SourceMapConsumer(map as RawSourceMap)
const commentNode = code.match(/_createCommentVNode\("v-if", true\)/)
expect(commentNode).not.toBeNull()
const commentPosition = getPositionInCode(code, commentNode![0])
const originalPosition = consumer.originalPositionFor(commentPosition)
// the comment node should not be mapped to the original source
expect(originalPosition.column).toBeNull()
expect(originalPosition.line).toBeNull()
expect(originalPosition.source).toBeNull()
})

test('should work w/ AST from descriptor', () => {
const source = `
<template>
Expand Down