push(`)`)
}
- // declare variables: let _foo, _bar
+ // declare variables: let _foo, __foo
if (declareNames.size) {
frag.splice(1, 0, `let ${[...declareNames].join(', ')}`, NEWLINE)
}
allDeclareNames.add([...preAccessNames].join(', '))
}
+ // pre access: __foo = _ctx.foo
const accessExps: CodeFragment[] =
preAccessExps.size > 0 ? [[...preAccessExps].join(';'), NEWLINE] : []
- // const newlineCount = operationsExps.filter(frag => frag === NEWLINE).length
- // if (newlineCount > 1) {
- // multiline check expression: if (_foo !== _ctx.foo || _bar !== _ctx.bar) {
- const checkExpsStart: CodeFragment[] =
+ // check start: if (_foo !== __foo) {
+ const checkStartExps: CodeFragment[] =
earlyCheckExps.length > 0
? [`if(`, ...earlyCheckExps.join(' || '), `) {`, INDENT_START]
: []
- const checkExpsEnd: CodeFragment[] =
+ // check end: }
+ const checkEndExps: CodeFragment[] =
earlyCheckExps.length > 0 ? [INDENT_END, NEWLINE, '}'] : []
- // assignment: _foo = _ctx.foo; _bar = _ctx.bar
+ // assignment: _foo = __foo
const assignmentExps: CodeFragment[] =
earlyCheckExps.length > 0
? [NEWLINE, ...earlyCheckExps.map(c => c.replace('!==', '=')).join(';')]
: []
push(
...accessExps,
- ...checkExpsStart,
+ ...checkStartExps,
...operationsExps,
...assignmentExps,
- ...checkExpsEnd,
+ ...checkEndExps,
)
- // } else {
- // single line check expression: (_foo !== _ctx.foo || _bar !== _ctx.bar) &&
- // const multiple = earlyCheckExps.length > 1
- // const checkExps: CodeFragment[] =
- // earlyCheckExps.length > 0
- // ? [
- // multiple ? `(` : undefined,
- // ...earlyCheckExps.join(' || '),
- // multiple ? `)` : undefined,
- // ' && ',
- // ]
- // : []
- // push(...checkExps, ...operationsExps.filter(frag => frag !== NEWLINE))
- // }
-
return frag
}
} = oper
const { helperName, omitKey } = getRuntimeHelper(tag, key.content, modifier)
const propValue = genPropValue(values, context)
- const { prevValueName, shouldWrapInParentheses } = processPropValues(
- context,
- helperName,
- [propValue],
- )
+ const { prevValueName } = processPropValues(context, helperName, [propValue])
return [
NEWLINE,
- ...(prevValueName
- ? [shouldWrapInParentheses ? `(` : undefined, `${prevValueName} = `]
- : []),
+ ...(prevValueName ? [`${prevValueName} = `] : []),
...genCall(
[helper(helperName), null],
`n${oper.element}`,
? 'true'
: undefined,
),
- ...(prevValueName && shouldWrapInParentheses ? [`)`] : []),
]
}
? genLiteralObjectProps([props], context) // dynamic arg props
: genExpression(props.value, context),
) // v-bind=""
- const { prevValueName, shouldWrapInParentheses } = processPropValues(
+ const { prevValueName } = processPropValues(
context,
'setDynamicProps',
values,
)
return [
NEWLINE,
- ...(prevValueName
- ? [shouldWrapInParentheses ? `(` : undefined, `${prevValueName} = `]
- : []),
+ ...(prevValueName ? [`${prevValueName} = `] : []),
...genCall(
helper('setDynamicProps'),
`n${oper.element}`,
genMulti(DELIMITERS_ARRAY, ...values),
oper.root && 'true',
),
- ...(prevValueName && shouldWrapInParentheses ? [`)`] : []),
]
}
context: CodegenContext,
helperName: string,
values: CodeFragment[][],
-): { prevValueName: string | undefined; shouldWrapInParentheses: boolean } {
+): { prevValueName: string | undefined } {
const { shouldCacheRenderEffectDeps, processingRenderEffect } = context
- // single-line render effect and the operation needs cache return a value,
- // the expression needs to be wrapped in parentheses.
- // e.g. _foo === _ctx.foo && (_prev_foo = _setStyle(...))
- let shouldWrapInParentheses: boolean = false
let prevValueName
if (shouldCacheRenderEffectDeps()) {
const { declareNames, preAccessNames, preAccessExps } =
}
declareNames.add(prevValueName)
}
- shouldWrapInParentheses = false //operations.length === 1
}
- return { prevValueName, shouldWrapInParentheses }
+ return { prevValueName }
}
export function processValues(
const { declareNames, earlyCheckExps, preAccessNames, preAccessExps } =
processingRenderEffect!
- // const isSingleLine = operations.length === 1
for (const frag of values) {
if (!isArray(frag)) continue
- // [code, newlineIndex, loc, name] -> [(_name = code), newlineIndex, loc, name]
+ // [code, newlineIndex, loc, name] -> [(__name), newlineIndex, loc, name]
const [newName, , , rawName] = frag
if (rawName) {
let name = rawName.replace(/[^\w]/g, '_')
- // if (rewrittenNames.has(name)) continue
- // rewrittenNames.add(name)
name = `_${name}`
if (!declareNames.has(name)) {
preAccessExps.add(`${preAccessName} = ${newName}`)
earlyCheckExps.push(`${name} !== ${preAccessName}`)
if (needRewrite) {
+ // replace the original name with the preAccessName
frag[0] = `${preAccessName}`
}
}