feat(compiler): correct source map generation

This commit is contained in:
Evan You
2019-09-26 14:55:53 -04:00
parent 2e3a1ff3c3
commit 63b6902bdb
8 changed files with 120 additions and 111 deletions

View File

@@ -66,7 +66,7 @@ export interface CodegenContext extends Required<CodegenOptions> {
indentLevel: number
map?: SourceMapGenerator
helper(name: string): string
push(code: string, node?: CodegenNode): void
push(code: string, node?: CodegenNode, openOnly?: boolean): void
indent(): void
deindent(withoutNewLine?: boolean): void
newline(): void
@@ -102,7 +102,7 @@ function createCodegenContext(
helper(name) {
return prefixIdentifiers ? name : `_${name}`
},
push(code, node?: CodegenNode) {
push(code, node, openOnly) {
context.code += code
if (context.map) {
if (node) {
@@ -131,6 +131,19 @@ function createCodegenContext(
})
}
advancePositionWithMutation(context, code)
if (node && !openOnly) {
context.map.addMapping({
source: context.filename,
original: {
line: node.loc.end.line,
column: node.loc.end.column - 1
},
generated: {
line: context.line,
column: context.column - 1
}
})
}
}
},
indent() {
@@ -453,7 +466,7 @@ function genIfBranch(
function genFor(node: ForNode, context: CodegenContext) {
const { push, helper, indent, deindent } = context
const { source, keyAlias, valueAlias, objectIndexAlias, children } = node
push(`${helper(RENDER_LIST)}(`, node)
push(`${helper(RENDER_LIST)}(`, node, true)
genExpression(source, context)
push(`, (`)
if (valueAlias) {
@@ -491,7 +504,7 @@ function genCallExpression(
context: CodegenContext,
multilines = node.arguments.length > 2
) {
context.push(node.callee + `(`, node)
context.push(node.callee + `(`, node, true)
multilines && context.indent()
genNodeList(node.arguments, context, multilines)
multilines && context.deindent()
@@ -502,7 +515,7 @@ function genObjectExpression(node: ObjectExpression, context: CodegenContext) {
const { push, indent, deindent, newline } = context
const { properties } = node
const multilines = properties.length > 1
push(multilines ? `{` : `{ `, node)
push(multilines ? `{` : `{ `)
multilines && indent()
for (let i = 0; i < properties.length; i++) {
const { key, value } = properties[i]

View File

@@ -542,6 +542,7 @@ function parseAttribute(
valueLoc.start.offset++
valueLoc.start.column++
valueLoc.end = advancePositionWithClone(valueLoc.start, value.content)
valueLoc.source = valueLoc.source.slice(1, -1)
}
return {
@@ -642,15 +643,26 @@ function parseInterpolation(
return undefined
}
const start = getCursor(context)
advanceBy(context, open.length)
const content = parseTextData(context, closeIndex - open.length, mode).trim()
const start = getCursor(context)
const end = getCursor(context)
const rawContentLength = closeIndex - open.length
const rawContent = context.source.slice(0, rawContentLength)
const preTrimContent = parseTextData(context, rawContentLength, mode)
const content = preTrimContent.trim()
const startOffset = preTrimContent.indexOf(content)
if (startOffset > 0) {
advancePositionWithMutation(start, rawContent, startOffset)
}
const endOffset =
rawContentLength - (preTrimContent.length - content.length - startOffset)
advancePositionWithMutation(end, rawContent, endOffset)
advanceBy(context, close.length)
return {
type: NodeTypes.EXPRESSION,
content,
loc: getSelection(context, start),
loc: getSelection(context, start, end),
isStatic: content === '',
isInterpolation: true
}

View File

@@ -157,8 +157,8 @@ export function processExpression(
children.push(
createExpression(id.name, false, {
source,
start: advancePositionWithClone(node.loc.start, source, id.start + 2),
end: advancePositionWithClone(node.loc.start, source, id.end + 2)
start: advancePositionWithClone(node.loc.start, source, id.start - 1),
end: advancePositionWithClone(node.loc.start, source, id.end - 1)
})
)
if (i === ids.length - 1 && id.end - 1 < full.length) {