feat: source map for expressions
This commit is contained in:
parent
2f029b659c
commit
13d0993cd3
|
@ -76,7 +76,7 @@ export function render(_ctx) {
|
||||||
return n0
|
return n0
|
||||||
}
|
}
|
||||||
|
|
||||||
import { template as _template, children as _children, effect as _effect, withModifiers as _withModifiers, on as _on } from 'vue/vapor'
|
import { template as _template, children as _children, effect as _effect, on as _on, withModifiers as _withModifiers } from 'vue/vapor'
|
||||||
"
|
"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
|
|
@ -33,8 +33,16 @@ export interface CodegenContext extends Required<CodegenOptions> {
|
||||||
indentLevel: number
|
indentLevel: number
|
||||||
map?: SourceMapGenerator
|
map?: SourceMapGenerator
|
||||||
|
|
||||||
push(code: string, newlineIndex?: number, node?: BaseIRNode): void
|
push(
|
||||||
pushWithNewline(code: string, newlineIndex?: number, node?: BaseIRNode): void
|
code: string,
|
||||||
|
newlineIndex?: number,
|
||||||
|
node?: Pick<BaseIRNode, 'loc'>,
|
||||||
|
): void
|
||||||
|
pushWithNewline(
|
||||||
|
code: string,
|
||||||
|
newlineIndex?: number,
|
||||||
|
node?: Pick<BaseIRNode, 'loc'>,
|
||||||
|
): void
|
||||||
indent(): void
|
indent(): void
|
||||||
deindent(): void
|
deindent(): void
|
||||||
newline(): void
|
newline(): void
|
||||||
|
@ -286,8 +294,6 @@ export function generate(
|
||||||
NewlineType.End,
|
NewlineType.End,
|
||||||
)
|
)
|
||||||
|
|
||||||
console.log(ctx.code)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
code: ctx.code,
|
code: ctx.code,
|
||||||
ast: ir as any,
|
ast: ir as any,
|
||||||
|
@ -297,60 +303,50 @@ export function generate(
|
||||||
}
|
}
|
||||||
|
|
||||||
function genOperation(oper: OperationNode, context: CodegenContext) {
|
function genOperation(oper: OperationNode, context: CodegenContext) {
|
||||||
const { vaporHelper, pushWithNewline } = context
|
const { vaporHelper, push, pushWithNewline } = context
|
||||||
// TODO: cache old value
|
// TODO: cache old value
|
||||||
switch (oper.type) {
|
switch (oper.type) {
|
||||||
case IRNodeTypes.SET_PROP: {
|
case IRNodeTypes.SET_PROP: {
|
||||||
pushWithNewline(
|
pushWithNewline(`${vaporHelper('setAttr')}(n${oper.element}, `)
|
||||||
`${vaporHelper('setAttr')}(n${oper.element}, ${processExpression(
|
genExpression(oper.name, context)
|
||||||
oper.name,
|
push(`, undefined, `)
|
||||||
context,
|
genExpression(oper.value, context)
|
||||||
)}, undefined, ${processExpression(oper.value, context)})`,
|
push(')')
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
case IRNodeTypes.SET_TEXT: {
|
case IRNodeTypes.SET_TEXT: {
|
||||||
pushWithNewline(
|
pushWithNewline(`${vaporHelper('setText')}(n${oper.element}, undefined, `)
|
||||||
`${vaporHelper('setText')}(n${
|
genExpression(oper.value, context)
|
||||||
oper.element
|
push(')')
|
||||||
}, undefined, ${processExpression(oper.value, context)})`,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
case IRNodeTypes.SET_EVENT: {
|
case IRNodeTypes.SET_EVENT: {
|
||||||
let value = oper.value
|
pushWithNewline(`${vaporHelper('on')}(n${oper.element}, `)
|
||||||
if (oper.modifiers.length) {
|
genExpression(oper.name, context)
|
||||||
value = `${vaporHelper('withModifiers')}(${processExpression(
|
push(', ')
|
||||||
value,
|
|
||||||
context,
|
const hasModifiers = oper.modifiers.length
|
||||||
)}, ${genArrayExpression(oper.modifiers)})`
|
hasModifiers && push(`${vaporHelper('withModifiers')}(`)
|
||||||
}
|
genExpression(oper.value, context)
|
||||||
pushWithNewline(
|
hasModifiers && push(`, ${genArrayExpression(oper.modifiers)})`)
|
||||||
`${vaporHelper('on')}(n${oper.element}, ${processExpression(
|
|
||||||
oper.name,
|
push(')')
|
||||||
context,
|
|
||||||
)}, ${processExpression(value, context)})`,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
case IRNodeTypes.SET_HTML: {
|
case IRNodeTypes.SET_HTML: {
|
||||||
pushWithNewline(
|
pushWithNewline(`${vaporHelper('setHtml')}(n${oper.element}, undefined, `)
|
||||||
`${vaporHelper('setHtml')}(n${
|
genExpression(oper.value, context)
|
||||||
oper.element
|
push(')')
|
||||||
}, undefined, ${processExpression(oper.value, context)})`,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
case IRNodeTypes.CREATE_TEXT_NODE: {
|
case IRNodeTypes.CREATE_TEXT_NODE: {
|
||||||
pushWithNewline(
|
pushWithNewline(`const n${oper.id} = ${vaporHelper('createTextNode')}(`)
|
||||||
`const n${oper.id} = ${vaporHelper(
|
genExpression(oper.value, context)
|
||||||
'createTextNode',
|
push(')')
|
||||||
)}(${processExpression(oper.value, context)})`,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -414,37 +410,41 @@ function genChildren(children: IRDynamicChildren) {
|
||||||
|
|
||||||
// TODO: other types (not only string)
|
// TODO: other types (not only string)
|
||||||
function genArrayExpression(elements: string[]) {
|
function genArrayExpression(elements: string[]) {
|
||||||
return `[${elements.map((it) => `"${it}"`).join(', ')}]`
|
return `[${elements.map((it) => JSON.stringify(it)).join(', ')}]`
|
||||||
}
|
}
|
||||||
|
|
||||||
function processExpression(
|
function genExpression(
|
||||||
exp: IRExpression,
|
exp: IRExpression,
|
||||||
{ inline, prefixIdentifiers, bindingMetadata, vaporHelper }: CodegenContext,
|
{
|
||||||
|
inline,
|
||||||
|
prefixIdentifiers,
|
||||||
|
bindingMetadata,
|
||||||
|
vaporHelper,
|
||||||
|
push,
|
||||||
|
}: CodegenContext,
|
||||||
) {
|
) {
|
||||||
if (isString(exp)) return exp
|
if (isString(exp)) return push(exp)
|
||||||
|
|
||||||
let content = ''
|
// TODO NodeTypes.COMPOUND_EXPRESSION
|
||||||
if (exp.type === NodeTypes.SIMPLE_EXPRESSION) {
|
if (exp.type === NodeTypes.COMPOUND_EXPRESSION) return
|
||||||
content = exp.content
|
|
||||||
if (exp.isStatic) {
|
let content = exp.content
|
||||||
return JSON.stringify(content)
|
|
||||||
}
|
if (exp.isStatic) {
|
||||||
|
content = JSON.stringify(content)
|
||||||
} else {
|
} else {
|
||||||
// TODO NodeTypes.COMPOUND_EXPRESSION
|
switch (bindingMetadata[content]) {
|
||||||
|
case BindingTypes.SETUP_REF:
|
||||||
|
content += '.value'
|
||||||
|
break
|
||||||
|
case BindingTypes.SETUP_MAYBE_REF:
|
||||||
|
content = `${vaporHelper('unref')}(${content})`
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if (prefixIdentifiers && !inline) {
|
||||||
|
content = `_ctx.${content}`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (bindingMetadata[content]) {
|
push(content, NewlineType.None, exp)
|
||||||
case BindingTypes.SETUP_REF:
|
|
||||||
content += '.value'
|
|
||||||
break
|
|
||||||
case BindingTypes.SETUP_MAYBE_REF:
|
|
||||||
content = `${vaporHelper('unref')}(${content})`
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if (prefixIdentifiers && !inline) {
|
|
||||||
content = `_ctx.${content}`
|
|
||||||
}
|
|
||||||
|
|
||||||
return content
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue