]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: avoid tracking multiple times
authordaiwei <daiwei521@126.com>
Wed, 11 Dec 2024 14:40:14 +0000 (22:40 +0800)
committerdaiwei <daiwei521@126.com>
Wed, 11 Dec 2024 14:40:14 +0000 (22:40 +0800)
packages/compiler-vapor/src/generators/operation.ts
packages/compiler-vapor/src/generators/prop.ts
packages/compiler-vapor/src/ir/index.ts
packages/compiler-vapor/src/transform.ts

index 2435340dba38f41b6ddc77198b19d6fff3b6cd6d..b9402ffa0ee0dd662736ccdcb793f4b4602f5b50 100644 (file)
@@ -116,45 +116,49 @@ export function genEffect(
   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
 }
index 35716537dd8479f94062800f27f407ed00bb1745..f8c2e0e37c47ebaab58f672cab2f87cbacc2d19a 100644 (file)
@@ -295,32 +295,32 @@ function processValue(
   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}`
       }
     }
   }
index 7dabd0dc2e0727902beb6966b57990e720a19834..46365eecd3a0e1a3b1a792ff7ac7df0452185820 100644 (file)
@@ -265,6 +265,7 @@ export interface IREffect {
   declareNames: Set<string>
   rewrittenNames: Set<string>
   earlyCheckExps: string[]
+  preAccessExps: Set<string>
   inVFor: boolean
 }
 
index 0b4f71f8d7e1c671f9c72bada0f56197b287f627..a1609a92870f26ac250c5b2a6599a094a76d2939 100644 (file)
@@ -163,6 +163,7 @@ export class TransformContext<T extends AllNode = AllNode> {
         expressions,
         operations,
         earlyCheckExps: [],
+        preAccessExps: new Set<string>(),
         declareNames: new Set<string>(),
         rewrittenNames: new Set<string>(),
         inVFor: this.inVFor > 0,