fix: fix source map by fixing advancePositionWithMutation

This commit is contained in:
Evan You
2019-09-25 19:17:45 -04:00
parent ff2313e43a
commit 6c8f226a79
20 changed files with 533 additions and 255 deletions

View File

@@ -42,12 +42,11 @@ export const transformElement: NodeTransform = (node, context) => {
let componentIdentifier: string | undefined
if (isComponent) {
context.imports.add(RESOLVE_COMPONENT)
componentIdentifier = `_component_${toValidId(node.tag)}`
context.statements.push(
`const ${componentIdentifier} = ${RESOLVE_COMPONENT}(${JSON.stringify(
node.tag
)})`
`const ${componentIdentifier} = ${context.helper(
RESOLVE_COMPONENT
)}(${JSON.stringify(node.tag)})`
)
}
@@ -70,13 +69,15 @@ export const transformElement: NodeTransform = (node, context) => {
}
const { loc } = node
context.imports.add(CREATE_VNODE)
const vnode = createCallExpression(CREATE_VNODE, args, loc)
const vnode = createCallExpression(
context.helper(CREATE_VNODE),
args,
loc
)
if (runtimeDirectives && runtimeDirectives.length) {
context.imports.add(APPLY_DIRECTIVES)
node.codegenNode = createCallExpression(
APPLY_DIRECTIVES,
context.helper(APPLY_DIRECTIVES),
[
vnode,
createArrayExpression(
@@ -147,11 +148,10 @@ function buildProps(
mergeArgs.push(exp)
} else {
// v-on="obj" -> toHandlers(obj)
context.imports.add(TO_HANDLERS)
mergeArgs.push({
type: NodeTypes.JS_CALL_EXPRESSION,
loc,
callee: TO_HANDLERS,
callee: context.helper(TO_HANDLERS),
arguments: [exp]
})
}
@@ -197,8 +197,11 @@ function buildProps(
)
}
if (mergeArgs.length > 1) {
context.imports.add(MERGE_PROPS)
propsExpression = createCallExpression(MERGE_PROPS, mergeArgs, elementLoc)
propsExpression = createCallExpression(
context.helper(MERGE_PROPS),
mergeArgs,
elementLoc
)
} else {
// single v-bind with nothing else - no need for a mergeProps call
propsExpression = mergeArgs[0]
@@ -285,12 +288,12 @@ function createDirectiveArgs(
dir: DirectiveNode,
context: TransformContext
): ArrayExpression {
// inject import for `resolveDirective`
context.imports.add(RESOLVE_DIRECTIVE)
// inject statement for resolving directive
const dirIdentifier = `_directive_${toValidId(dir.name)}`
context.statements.push(
`const ${dirIdentifier} = ${RESOLVE_DIRECTIVE}(${JSON.stringify(dir.name)})`
`const ${dirIdentifier} = ${context.helper(
RESOLVE_DIRECTIVE
)}(${JSON.stringify(dir.name)})`
)
const dirArgs: ArrayExpression['elements'] = [dirIdentifier]
const { loc } = dir

View File

@@ -24,7 +24,7 @@ export const transformFor = createStructuralDirectiveTransform(
const parseResult = parseForExpression(dir.exp, context)
if (parseResult) {
context.imports.add(RENDER_LIST)
context.helper(RENDER_LIST)
const { source, value, key, index } = parseResult
context.replaceNode({

View File

@@ -19,14 +19,10 @@ export const transformIf = createStructuralDirectiveTransform(
processExpression(dir.exp, context)
}
if (dir.name === 'if') {
// check if this v-if is root - so that in codegen we can avoid generating
// arrays for each branch
const isRoot = context.parent === context.root
context.replaceNode({
type: NodeTypes.IF,
loc: node.loc,
branches: [createIfBranch(node, dir, isRoot)],
isRoot
branches: [createIfBranch(node, dir)]
})
} else {
// locate the adjacent v-if
@@ -43,7 +39,7 @@ export const transformIf = createStructuralDirectiveTransform(
if (sibling && sibling.type === NodeTypes.IF) {
// move the node to the if node's branches
context.removeNode()
const branch = createIfBranch(node, dir, sibling.isRoot)
const branch = createIfBranch(node, dir)
if (__DEV__ && comments.length) {
branch.children = [...comments, ...branch.children]
}
@@ -67,16 +63,11 @@ export const transformIf = createStructuralDirectiveTransform(
}
)
function createIfBranch(
node: ElementNode,
dir: DirectiveNode,
isRoot: boolean
): IfBranchNode {
function createIfBranch(node: ElementNode, dir: DirectiveNode): IfBranchNode {
return {
type: NodeTypes.IF_BRANCH,
loc: node.loc,
condition: dir.name === 'else' ? undefined : dir.exp,
children: node.tagType === ElementTypes.TEMPLATE ? node.children : [node],
isRoot
children: node.tagType === ElementTypes.TEMPLATE ? node.children : [node]
}
}