Skip to content

Commit d37a2ac

Browse files
authored
fix(compiler-core): ensure mapping is added only if node source is available (#13285)
close #13261 close vitejs/vite-plugin-vue#368
1 parent 80055fd commit d37a2ac

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

packages/compiler-core/src/codegen.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ function createCodegenContext(
188188
name = content
189189
}
190190
}
191-
addMapping(node.loc.start, name)
191+
if (node.loc.source) {
192+
addMapping(node.loc.start, name)
193+
}
192194
}
193195
if (newlineIndex === NewlineType.Unknown) {
194196
// multiple newlines, full iteration
@@ -225,7 +227,7 @@ function createCodegenContext(
225227
context.column = code.length - newlineIndex
226228
}
227229
}
228-
if (node && node.loc !== locStub) {
230+
if (node && node.loc !== locStub && node.loc.source) {
229231
addMapping(node.loc.end)
230232
}
231233
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,35 @@ test('source map', () => {
157157
).toMatchObject(getPositionInCode(template.content, `foobar`))
158158
})
159159

160+
test('source map: v-if generated comment should not have original position', () => {
161+
const template = parse(
162+
`
163+
<template>
164+
<div v-if="true"></div>
165+
</template>
166+
`,
167+
{ filename: 'example.vue', sourceMap: true },
168+
).descriptor.template!
169+
170+
const { code, map } = compile({
171+
filename: 'example.vue',
172+
source: template.content,
173+
})
174+
175+
expect(map!.sources).toEqual([`example.vue`])
176+
expect(map!.sourcesContent).toEqual([template.content])
177+
178+
const consumer = new SourceMapConsumer(map as RawSourceMap)
179+
const commentNode = code.match(/_createCommentVNode\("v-if", true\)/)
180+
expect(commentNode).not.toBeNull()
181+
const commentPosition = getPositionInCode(code, commentNode![0])
182+
const originalPosition = consumer.originalPositionFor(commentPosition)
183+
// the comment node should not be mapped to the original source
184+
expect(originalPosition.column).toBeNull()
185+
expect(originalPosition.line).toBeNull()
186+
expect(originalPosition.source).toBeNull()
187+
})
188+
160189
test('should work w/ AST from descriptor', () => {
161190
const source = `
162191
<template>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy