]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(compiler-sfc): simplify props destructure arguments
authorEvan You <yyx990803@gmail.com>
Tue, 11 Apr 2023 06:37:57 +0000 (14:37 +0800)
committerEvan You <yyx990803@gmail.com>
Tue, 11 Apr 2023 08:05:00 +0000 (16:05 +0800)
packages/compiler-sfc/src/compileScript.ts
packages/compiler-sfc/src/script/definePropsDestructure.ts

index 7a93693badc57cd8f1d66498617df296b8bb18b8..538366ac173fa43f669673eaf3f8e9365f31bbb7 100644 (file)
@@ -918,14 +918,7 @@ export function compileScript(
 
   // 3 props destructure transform
   if (ctx.propsDestructureDecl) {
-    transformDestructuredProps(
-      scriptSetupAst,
-      ctx.s,
-      startOffset,
-      ctx.propsDestructuredBindings,
-      error,
-      vueImportAliases
-    )
+    transformDestructuredProps(ctx, vueImportAliases)
   }
 
   // 4. Apply reactivity transform
index 2b1d229a739bb46fcc21f6ca4234b46e3cb5ec79..3759f0c662ebb20c0f4db3c7b4c08b32f730f96c 100644 (file)
@@ -5,7 +5,6 @@ import {
   Program,
   VariableDeclaration
 } from '@babel/types'
-import MagicString from 'magic-string'
 import { walk } from 'estree-walker'
 import {
   extractIdentifiers,
@@ -16,8 +15,8 @@ import {
   walkFunctionParams
 } from '@vue/compiler-core'
 import { genPropsAccessExp } from '@vue/shared'
-import { PropsDestructureBindings } from './defineProps'
 import { isCallOf, unwrapTSNode } from './utils'
+import { ScriptCompileContext } from './context'
 
 /**
  * true -> prop binding
@@ -26,11 +25,7 @@ import { isCallOf, unwrapTSNode } from './utils'
 type Scope = Record<string, boolean>
 
 export function transformDestructuredProps(
-  ast: Program,
-  s: MagicString,
-  offset = 0,
-  knownProps: PropsDestructureBindings,
-  error: (msg: string, node: Node, end?: number) => never,
+  ctx: ScriptCompileContext,
   vueImportAliases: Record<string, string>
 ) {
   const rootScope: Scope = {}
@@ -40,8 +35,8 @@ export function transformDestructuredProps(
   const parentStack: Node[] = []
   const propsLocalToPublicMap: Record<string, string> = Object.create(null)
 
-  for (const key in knownProps) {
-    const { local } = knownProps[key]
+  for (const key in ctx.propsDestructuredBindings) {
+    const { local } = ctx.propsDestructuredBindings[key]
     rootScope[local] = true
     propsLocalToPublicMap[local] = key
   }
@@ -60,7 +55,7 @@ export function transformDestructuredProps(
     if (currentScope) {
       currentScope[id.name] = false
     } else {
-      error(
+      ctx.error(
         'registerBinding called without active scope, something is wrong.',
         id
       )
@@ -121,7 +116,7 @@ export function transformDestructuredProps(
       (parent.type === 'AssignmentExpression' && id === parent.left) ||
       parent.type === 'UpdateExpression'
     ) {
-      error(`Cannot assign to destructured props as they are readonly.`, id)
+      ctx.error(`Cannot assign to destructured props as they are readonly.`, id)
     }
 
     if (isStaticProperty(parent) && parent.shorthand) {
@@ -132,16 +127,16 @@ export function transformDestructuredProps(
         isInDestructureAssignment(parent, parentStack)
       ) {
         // { prop } -> { prop: __props.prop }
-        s.appendLeft(
-          id.end! + offset,
+        ctx.s.appendLeft(
+          id.end! + ctx.startOffset!,
           `: ${genPropsAccessExp(propsLocalToPublicMap[id.name])}`
         )
       }
     } else {
       // x --> __props.x
-      s.overwrite(
-        id.start! + offset,
-        id.end! + offset,
+      ctx.s.overwrite(
+        id.start! + ctx.startOffset!,
+        id.end! + ctx.startOffset!,
         genPropsAccessExp(propsLocalToPublicMap[id.name])
       )
     }
@@ -151,7 +146,7 @@ export function transformDestructuredProps(
     if (isCallOf(node, alias)) {
       const arg = unwrapTSNode(node.arguments[0])
       if (arg.type === 'Identifier' && currentScope[arg.name]) {
-        error(
+        ctx.error(
           `"${arg.name}" is a destructured prop and should not be passed directly to ${method}(). ` +
             `Pass a getter () => ${arg.name} instead.`,
           arg
@@ -161,6 +156,7 @@ export function transformDestructuredProps(
   }
 
   // check root scope first
+  const ast = ctx.scriptSetupAst!
   walkScope(ast, true)
   ;(walk as any)(ast, {
     enter(node: Node, parent?: Node) {