diff --git a/packages/compiler-core/src/codegen.ts b/packages/compiler-core/src/codegen.ts index 70116cfb61a..6b4559fabb2 100644 --- a/packages/compiler-core/src/codegen.ts +++ b/packages/compiler-core/src/codegen.ts @@ -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 @@ -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) } } diff --git a/packages/compiler-sfc/__tests__/compileTemplate.spec.ts b/packages/compiler-sfc/__tests__/compileTemplate.spec.ts index 2ea1eb9d378..22623299a02 100644 --- a/packages/compiler-sfc/__tests__/compileTemplate.spec.ts +++ b/packages/compiler-sfc/__tests__/compileTemplate.spec.ts @@ -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( + ` + + `, + { 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 = `