]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: ignore non-ref const mutation cases in codegen
authorEvan You <yyx990803@gmail.com>
Thu, 19 Nov 2020 02:16:09 +0000 (21:16 -0500)
committerEvan You <yyx990803@gmail.com>
Thu, 19 Nov 2020 02:16:09 +0000 (21:16 -0500)
packages/compiler-core/src/codegen.ts
packages/compiler-core/src/options.ts
packages/compiler-core/src/transforms/transformExpression.ts
packages/compiler-core/src/transforms/vOn.ts
packages/runtime-dom/src/helpers/useCssVars.ts

index a481ddcf55a93c8461f624706c942d65fa77b723..3ca4b7def6525f216cea197ae12832ed2fcbca2b 100644 (file)
@@ -66,7 +66,10 @@ export interface CodegenResult {
 }
 
 export interface CodegenContext
-  extends Omit<Required<CodegenOptions>, 'bindingMetadata' | 'inline'> {
+  extends Omit<
+      Required<CodegenOptions>,
+      'bindingMetadata' | 'inline' | 'isTS'
+    > {
   source: string
   code: string
   line: number
@@ -214,29 +217,33 @@ export function generate(
     genFunctionPreamble(ast, preambleContext)
   }
 
-  // binding optimizations
-  const optimizeSources =
-    options.bindingMetadata && !options.inline
-      ? `, $props, $setup, $data, $options`
-      : ``
+  const args = ssr ? ['_ctx', '_push', '_parent', '_attrs'] : ['_ctx', '_cache']
+  if (!__BROWSER__ && options.bindingMetadata && !options.inline) {
+    // binding optimization args
+    args.push('$props', '$setup', '$data', '$options')
+  }
+  const signature =
+    !__BROWSER__ && options.isTS
+      ? args.map(arg => `${arg}: any`).join(',')
+      : args.join(',')
   // enter render function
   if (!ssr) {
     if (isSetupInlined) {
       if (genScopeId) {
         push(`${PURE_ANNOTATION}_withId(`)
       }
-      push(`(_ctx, _cache${optimizeSources}) => {`)
+      push(`(${signature}) => {`)
     } else {
       if (genScopeId) {
         push(`const render = ${PURE_ANNOTATION}_withId(`)
       }
-      push(`function render(_ctx, _cache${optimizeSources}) {`)
+      push(`function render(${signature}) {`)
     }
   } else {
     if (genScopeId) {
       push(`const ssrRender = ${PURE_ANNOTATION}_withId(`)
     }
-    push(`function ssrRender(_ctx, _push, _parent, _attrs${optimizeSources}) {`)
+    push(`function ssrRender(${signature}) {`)
   }
   indent()
 
index 43d019b753ecedf9b8efdbd98cbef2cb94b32791..02ee406838d3d1e1a9778476ddcae69df4599154 100644 (file)
@@ -124,6 +124,10 @@ interface SharedTransformCodegenOptions {
    * This allows the function to directly access setup() local bindings.
    */
   inline?: boolean
+  /**
+   * Indicates that transforms and codegen should try to output valid TS code
+   */
+  isTS?: boolean
 }
 
 export interface TransformOptions extends SharedTransformCodegenOptions {
@@ -195,10 +199,6 @@ export interface TransformOptions extends SharedTransformCodegenOptions {
    * needed to render inline CSS variables on component root
    */
   ssrCssVars?: string
-  /**
-   * Indicates that transforms should try to output valid TS code
-   */
-  isTS?: boolean
   onError?: (error: CompilerError) => void
 }
 
index edee2f52ec3c99d774a226529e6c98d5ede1ec34..4ad7d8884a6029a551b951c63f9ed0e9c48a8d3e 100644 (file)
@@ -118,38 +118,32 @@ export function processExpression(
       // setup inline mode
       if (type === BindingTypes.SETUP_CONST) {
         return raw
-      } else if (type === BindingTypes.SETUP_REF) {
+      } else if (
+        type === BindingTypes.SETUP_REF ||
+        type === BindingTypes.SETUP_MAYBE_REF
+      ) {
+        // const binding that may or may not be ref
+        // if it's not a ref, then assignments don't make sense -
+        // so we ignore the non-ref assignment case and generate code
+        // that assumes the value to be a ref for more efficiency
         return isAssignmentLVal || isUpdateArg
           ? `${raw}.value`
           : `${context.helperString(UNREF)}(${raw})`
-      } else if (
-        type === BindingTypes.SETUP_MAYBE_REF ||
-        type === BindingTypes.SETUP_LET
-      ) {
+      } else if (type === BindingTypes.SETUP_LET) {
         if (isAssignmentLVal) {
-          if (type === BindingTypes.SETUP_MAYBE_REF) {
-            // const binding that may or may not be ref
-            // if it's not a ref, then the assignment doesn't make sense so
-            // just no-op it
-            // x = y ---> !isRef(x) ? null : x.value = y
-            return `!${context.helperString(
-              IS_REF
-            )}(${raw}) ? null : ${raw}.value`
-          } else {
-            // let binding.
-            // this is a bit more tricky as we need to cover the case where
-            // let is a local non-ref value, and we need to replicate the
-            // right hand side value.
-            // x = y --> isRef(x) ? x.value = y : x = y
-            const rVal = (parent as AssignmentExpression).right
-            const rExp = rawExp.slice(rVal.start! - 1, rVal.end! - 1)
-            const rExpString = stringifyExpression(
-              processExpression(createSimpleExpression(rExp, false), context)
-            )
-            return `${context.helperString(IS_REF)}(${raw})${
-              context.isTS ? ` //@ts-ignore\n` : ``
-            } ? ${raw}.value = ${rExpString} : ${raw}`
-          }
+          // let binding.
+          // this is a bit more tricky as we need to cover the case where
+          // let is a local non-ref value, and we need to replicate the
+          // right hand side value.
+          // x = y --> isRef(x) ? x.value = y : x = y
+          const rVal = (parent as AssignmentExpression).right
+          const rExp = rawExp.slice(rVal.start! - 1, rVal.end! - 1)
+          const rExpString = stringifyExpression(
+            processExpression(createSimpleExpression(rExp, false), context)
+          )
+          return `${context.helperString(IS_REF)}(${raw})${
+            context.isTS ? ` //@ts-ignore\n` : ``
+          } ? ${raw}.value = ${rExpString} : ${raw}`
         } else if (isUpdateArg) {
           // make id replace parent in the code range so the raw update operator
           // is removed
@@ -158,21 +152,11 @@ export function processExpression(
           const { prefix: isPrefix, operator } = parent as UpdateExpression
           const prefix = isPrefix ? operator : ``
           const postfix = isPrefix ? `` : operator
-          if (type === BindingTypes.SETUP_MAYBE_REF) {
-            // const binding that may or may not be ref
-            // if it's not a ref, then the assignment doesn't make sense so
-            // just no-op it
-            // x++ ---> !isRef(x) ? null : x.value++
-            return `!${context.helperString(
-              IS_REF
-            )}(${raw}) ? null : ${prefix}${raw}.value${postfix}`
-          } else {
-            // let binding.
-            // x++ --> isRef(a) ? a.value++ : a++
-            return `${context.helperString(IS_REF)}(${raw})${
-              context.isTS ? ` //@ts-ignore\n` : ``
-            } ? ${prefix}${raw}.value${postfix} : ${prefix}${raw}${postfix}`
-          }
+          // let binding.
+          // x++ --> isRef(a) ? a.value++ : a++
+          return `${context.helperString(IS_REF)}(${raw})${
+            context.isTS ? ` //@ts-ignore\n` : ``
+          } ? ${prefix}${raw}.value${postfix} : ${prefix}${raw}${postfix}`
         } else {
           return `${context.helperString(UNREF)}(${raw})`
         }
index ce506aa3eb1ef8eb842f396d330161b9db743c80..00db326800790f6e21cc1be75526998d46c4c9e3 100644 (file)
@@ -124,7 +124,9 @@ export const transformOn: DirectiveTransform = (
       exp = createCompoundExpression([
         `${
           isInlineStatement
-            ? `$event`
+            ? !__BROWSER__ && context.isTS
+              ? `($event: any)`
+              : `$event`
             : `${
                 !__BROWSER__ && context.isTS ? `\n//@ts-ignore\n` : ``
               }(...args)`
index 6f137ac3be204e5143e46d0554385e142c9597f3..c05ddbacf1534df4ae084119a2584f59c48371c9 100644 (file)
@@ -1,5 +1,4 @@
 import {
-  ComponentPublicInstance,
   getCurrentInstance,
   onMounted,
   warn,
@@ -14,9 +13,7 @@ import { ShapeFlags } from '@vue/shared'
  * Runtime helper for SFC's CSS variable injection feature.
  * @private
  */
-export function useCssVars(
-  getter: (ctx: ComponentPublicInstance) => Record<string, string>
-) {
+export function useCssVars(getter: (ctx: any) => Record<string, string>) {
   const instance = getCurrentInstance()
   /* istanbul ignore next */
   if (!instance) {