]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(experimental): shouldTransform for ref-transform
authorEvan You <yyx990803@gmail.com>
Mon, 23 Aug 2021 14:45:58 +0000 (10:45 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 23 Aug 2021 14:45:58 +0000 (10:45 -0400)
packages/compiler-core/src/babelUtils.ts
packages/compiler-sfc/src/compileScript.ts
packages/ref-transform/README.md
packages/ref-transform/src/index.ts

index 6f19f3057ef5e81b7aec2b23803e44844785ef0e..e3d141857f4f2cec91c64bbf592f6fd6e9cd77ce 100644 (file)
@@ -142,24 +142,9 @@ export function walkFunctionParams(
   onIdent: (id: Identifier) => void
 ) {
   for (const p of node.params) {
-    ;(walk as any)(p, {
-      enter(child: Node, parent: Node) {
-        if (
-          child.type === 'Identifier' &&
-          // do not record as scope variable if is a destructured key
-          !isStaticPropertyKey(child, parent) &&
-          // do not record if this is a default value
-          // assignment of a destructured variable
-          !(
-            parent &&
-            parent.type === 'AssignmentPattern' &&
-            parent.right === child
-          )
-        ) {
-          onIdent(child)
-        }
-      }
-    })
+    for (const id of extractIdentifiers(p)) {
+      onIdent(id)
+    }
   }
 }
 
index 9d1775407c925a2bdb255b620a53770ea2cc50ad..3829df92956a34814083a5ab9fa9eda8e234498e 100644 (file)
@@ -51,7 +51,10 @@ import { compileTemplate, SFCTemplateCompileOptions } from './compileTemplate'
 import { warnExperimental, warnOnce } from './warn'
 import { rewriteDefault } from './rewriteDefault'
 import { createCache } from './cache'
-import { transformAST as transformWithRefSugar } from '@vue/ref-transform'
+import {
+  shouldTransform,
+  transformAST as transformWithRefSugar
+} from '@vue/ref-transform'
 
 // Special compiler macros
 const DEFINE_PROPS = 'defineProps'
@@ -855,7 +858,7 @@ export function compileScript(
   }
 
   // 3. Apply ref sugar transform
-  if (enableRefSugar) {
+  if (enableRefSugar && shouldTransform(source)) {
     warnExperimental(
       `ref sugar`,
       `https://github.com/vuejs/rfcs/discussions/369`
index 2a4135478c3d9004fa34b9431546a71a8c48adc4..45422555563838be3d41d30286d0eef5c0f2ef18 100644 (file)
@@ -37,6 +37,17 @@ A few commonly used APIs have shorthands (which also removes the need to import
 
 This package is the lower-level transform that can be used standalone. Higher-level tooling (e.g. `@vitejs/plugin-vue` and `vue-loader`) will provide integration via options.
 
+### `shouldTransform`
+
+Can be used to do a cheap check to determine whether full transform should be performed.
+
+```js
+import { shouldTransform } from '@vue/ref-transform'
+
+shouldTransform(`let a = ref(0)`) // false
+shouldTransform(`let a = $ref(0)`) // true
+```
+
 ### `transform`
 
 ```js
@@ -66,6 +77,8 @@ interface RefTransformOptions {
 
 ### `transformAST`
 
+Transform with an existing Babel AST + MagicString instance. This is used internally by `@vue/compiler-sfc` to avoid double parse/transform cost.
+
 ```js
 import { transformAST } from '@vue/ref-transform'
 import { parse } from '@babel/parser'
index 1792fe70b77a8037fc889c1e5990032158b3ba47..218427466811befdfb2d2dba50bd2e8890a3f721 100644 (file)
@@ -24,6 +24,11 @@ import { babelParserDefaultPlugins } from '@vue/shared'
 const TO_VAR_SYMBOL = '$'
 const TO_REF_SYMBOL = '$$'
 const shorthands = ['ref', 'computed', 'shallowRef']
+const transformCheckRE = /[^\w]\$(?:\$|ref|computed|shallowRef)?\(/
+
+export function shouldTransform(src: string): boolean {
+  return transformCheckRE.test(src)
+}
 
 export interface ReactiveDeclarator {
   node: VariableDeclarator