const operationsExps = genOperations(operations, context)
const { processingRenderEffect } = context
- const { declareNames, earlyCheckExps } = processingRenderEffect!
+ const { declareNames, earlyCheckExps, preAccessExps } =
+ processingRenderEffect!
if (declareNames.size) {
allDeclareNames.add([...declareNames].join(', '))
}
- const newlineCount = operationsExps.filter(frag => frag === NEWLINE).length
- if (newlineCount > 1) {
- // multiline check expression: if (_foo !== _ctx.foo || _bar !== _ctx.bar) {
- const checkExpsStart: CodeFragment[] =
- earlyCheckExps.length > 0
- ? [`if(`, ...earlyCheckExps.join(' || '), `) {`, INDENT_START]
- : []
- const checkExpsEnd: CodeFragment[] =
- earlyCheckExps.length > 0 ? [INDENT_END, NEWLINE, '}'] : []
- // assignment: _foo = _ctx.foo; _bar = _ctx.bar
- const assignmentExps: CodeFragment[] =
- earlyCheckExps.length > 0
- ? [NEWLINE, ...earlyCheckExps.map(c => c.replace('!==', '=')).join(';')]
- : []
- push(
- ...checkExpsStart,
- ...operationsExps,
- ...assignmentExps,
- ...checkExpsEnd,
- )
- } 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))
- }
+ 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[] =
+ earlyCheckExps.length > 0
+ ? [`if(`, ...earlyCheckExps.join(' || '), `) {`, INDENT_START]
+ : []
+ const checkExpsEnd: CodeFragment[] =
+ earlyCheckExps.length > 0 ? [INDENT_END, NEWLINE, '}'] : []
+ // assignment: _foo = _ctx.foo; _bar = _ctx.bar
+ const assignmentExps: CodeFragment[] =
+ earlyCheckExps.length > 0
+ ? [NEWLINE, ...earlyCheckExps.map(c => c.replace('!==', '=')).join(';')]
+ : []
+ push(
+ ...accessExps,
+ ...checkExpsStart,
+ ...operationsExps,
+ ...assignmentExps,
+ ...checkExpsEnd,
+ )
+ // } 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
}
needRewrite: boolean = true,
): string[] | undefined {
const { processingRenderEffect, allRenderEffectSeenNames } = context
- const { declareNames, rewrittenNames, earlyCheckExps, operations } =
+ const { declareNames, earlyCheckExps, preAccessExps } =
processingRenderEffect!
- const isSingleLine = operations.length === 1
+ // const isSingleLine = operations.length === 1
for (const frag of values) {
if (!isArray(frag)) continue
// [code, newlineIndex, loc, name] -> [(_name = code), newlineIndex, loc, name]
const [newName, , , rawName] = frag
if (rawName) {
let name = rawName.replace(/[^\w]/g, '_')
- if (rewrittenNames.has(name)) continue
- rewrittenNames.add(name)
+ // if (rewrittenNames.has(name)) continue
+ // rewrittenNames.add(name)
name = `_${name}`
- if (declareNames.has(name)) continue
-
- if (allRenderEffectSeenNames[name] === undefined)
- allRenderEffectSeenNames[name] = 0
- else name += ++allRenderEffectSeenNames[name]
-
- declareNames.add(name)
- earlyCheckExps.push(`${name} !== ${newName}`)
-
- if (needRewrite && isSingleLine) {
- // replace the original code fragment with the assignment expression
- frag[0] = `(${name} = ${newName})`
+ if (!declareNames.has(name)) {
+ if (allRenderEffectSeenNames[name] === undefined)
+ allRenderEffectSeenNames[name] = 0
+ else name += ++allRenderEffectSeenNames[name]
+ declareNames.add(name)
+ }
+ const preAccessName = `_${name}`
+ declareNames.add(`${preAccessName}`)
+ preAccessExps.add(`${preAccessName} = ${newName}`)
+ earlyCheckExps.push(`${name} !== ${preAccessName}`)
+ if (needRewrite) {
+ frag[0] = `${preAccessName}`
}
}
}