]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(compiler): extract isStaticExp util
authorEvan You <yyx990803@gmail.com>
Mon, 13 Jul 2020 20:48:16 +0000 (16:48 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 13 Jul 2020 20:48:24 +0000 (16:48 -0400)
packages/compiler-core/src/transforms/transformElement.ts
packages/compiler-core/src/transforms/vModel.ts
packages/compiler-core/src/transforms/vSlot.ts
packages/compiler-core/src/utils.ts
packages/compiler-dom/src/transforms/vOn.ts
packages/compiler-ssr/src/transforms/ssrTransformElement.ts

index 78f024818b30e4bb926e2676f050c24e5897a9ae..7b34a79827b04e44298a80c21f459936ea357430 100644 (file)
@@ -43,7 +43,8 @@ import {
   findProp,
   isCoreComponent,
   isBindKey,
-  findDir
+  findDir,
+  isStaticExp
 } from '../utils'
 import { buildSlots } from './vSlot'
 import { getStaticType } from './hoistStatic'
@@ -275,7 +276,7 @@ export function buildProps(
   const dynamicPropNames: string[] = []
 
   const analyzePatchFlag = ({ key, value }: Property) => {
-    if (key.type === NodeTypes.SIMPLE_EXPRESSION && key.isStatic) {
+    if (isStaticExp(key)) {
       const name = key.content
       if (
         !isComponent &&
index f4ec6ea34ab740d58f379c2c71109b401e5370e8..ee1a4bd8bc31e5c9d1102a97c0dee48f4534d163 100644 (file)
@@ -8,7 +8,12 @@ import {
   ElementTypes
 } from '../ast'
 import { createCompilerError, ErrorCodes } from '../errors'
-import { isMemberExpression, isSimpleIdentifier, hasScopeRef } from '../utils'
+import {
+  isMemberExpression,
+  isSimpleIdentifier,
+  hasScopeRef,
+  isStaticExp
+} from '../utils'
 
 export const transformModel: DirectiveTransform = (dir, node, context) => {
   const { exp, arg } = dir
@@ -43,7 +48,7 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
 
   const propName = arg ? arg : createSimpleExpression('modelValue', true)
   const eventName = arg
-    ? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
+    ? isStaticExp(arg)
       ? `onUpdate:${arg.content}`
       : createCompoundExpression(['"onUpdate:" + ', arg])
     : `onUpdate:modelValue`
@@ -74,7 +79,7 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
       .map(m => (isSimpleIdentifier(m) ? m : JSON.stringify(m)) + `: true`)
       .join(`, `)
     const modifiersKey = arg
-      ? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
+      ? isStaticExp(arg)
         ? `${arg.content}Modifiers`
         : createCompoundExpression([arg, ' + "Modifiers"'])
       : `modelModifiers`
index 5930bcedf4774ae857db5621433aabe4b6f63f0d..0cec12745b1974eec4b518fbc6ec6f8cf1ef08fd 100644 (file)
@@ -14,7 +14,6 @@ import {
   SourceLocation,
   createConditionalExpression,
   ConditionalExpression,
-  JSChildNode,
   SimpleExpressionNode,
   FunctionExpression,
   CallExpression,
@@ -24,13 +23,17 @@ import {
 } from '../ast'
 import { TransformContext, NodeTransform } from '../transform'
 import { createCompilerError, ErrorCodes } from '../errors'
-import { findDir, isTemplateNode, assert, isVSlot, hasScopeRef } from '../utils'
+import {
+  findDir,
+  isTemplateNode,
+  assert,
+  isVSlot,
+  hasScopeRef,
+  isStaticExp
+} from '../utils'
 import { CREATE_SLOTS, RENDER_LIST, WITH_CTX } from '../runtimeHelpers'
 import { parseForExpression, createForLoopParams } from './vFor'
 
-const isStaticExp = (p: JSChildNode): p is SimpleExpressionNode =>
-  p.type === NodeTypes.SIMPLE_EXPRESSION && p.isStatic
-
 const defaultFallback = createSimpleExpression(`undefined`, false)
 
 // A NodeTransform that:
index 0a3a9b3f9ba0913d70a1a85e9d28dc12fb56412d..fe0bc452a4027556446f2aba3fef33269e97c0c4 100644 (file)
@@ -20,7 +20,8 @@ import {
   IfBranchNode,
   TextNode,
   InterpolationNode,
-  VNodeCall
+  VNodeCall,
+  SimpleExpressionNode
 } from './ast'
 import { TransformContext } from './transform'
 import {
@@ -35,6 +36,9 @@ import { parse } from '@babel/parser'
 import { walk } from 'estree-walker'
 import { Node } from '@babel/types'
 
+export const isStaticExp = (p: JSChildNode): p is SimpleExpressionNode =>
+  p.type === NodeTypes.SIMPLE_EXPRESSION && p.isStatic
+
 export const isBuiltInType = (tag: string, expected: string): boolean =>
   tag === expected || tag === hyphenate(expected)
 
@@ -196,12 +200,7 @@ export function findProp(
 }
 
 export function isBindKey(arg: DirectiveNode['arg'], name: string): boolean {
-  return !!(
-    arg &&
-    arg.type === NodeTypes.SIMPLE_EXPRESSION &&
-    arg.isStatic &&
-    arg.content === name
-  )
+  return !!(arg && isStaticExp(arg) && arg.content === name)
 }
 
 export function hasDynamicKeyVBind(node: ElementNode): boolean {
index f527d282ca9bdd3e9605637cf1373a74df25d2fd..02fac64f7a616bf4d68593d5ef09bf2241655bc4 100644 (file)
@@ -8,7 +8,8 @@ import {
   NodeTypes,
   createCompoundExpression,
   ExpressionNode,
-  SimpleExpressionNode
+  SimpleExpressionNode,
+  isStaticExp
 } from '@vue/compiler-core'
 import { V_ON_WITH_MODIFIERS, V_ON_WITH_KEYS } from '../runtimeHelpers'
 import { makeMap } from '@vue/shared'
@@ -30,7 +31,6 @@ const isKeyboardEvent = /*#__PURE__*/ makeMap(
 )
 
 const resolveModifiers = (key: ExpressionNode, modifiers: string[]) => {
-  const isStaticKey = key.type === NodeTypes.SIMPLE_EXPRESSION && key.isStatic
   const keyModifiers = []
   const nonKeyModifiers = []
   const eventOptionModifiers = []
@@ -44,7 +44,7 @@ const resolveModifiers = (key: ExpressionNode, modifiers: string[]) => {
     } else {
       // runtimeModifiers: modifiers that needs runtime guards
       if (maybeKeyModifier(modifier)) {
-        if (isStaticKey) {
+        if (isStaticExp(key)) {
           if (isKeyboardEvent((key as SimpleExpressionNode).content)) {
             keyModifiers.push(modifier)
           } else {
@@ -73,9 +73,7 @@ const resolveModifiers = (key: ExpressionNode, modifiers: string[]) => {
 
 const transformClick = (key: ExpressionNode, event: string) => {
   const isStaticClick =
-    key.type === NodeTypes.SIMPLE_EXPRESSION &&
-    key.isStatic &&
-    key.content.toLowerCase() === 'onclick'
+    isStaticExp(key) && key.content.toLowerCase() === 'onclick'
   return isStaticClick
     ? createSimpleExpression(event, true)
     : key.type !== NodeTypes.SIMPLE_EXPRESSION
@@ -119,9 +117,7 @@ export const transformOn: DirectiveTransform = (dir, node, context) => {
     if (
       keyModifiers.length &&
       // if event name is dynamic, always wrap with keys guard
-      (key.type === NodeTypes.COMPOUND_EXPRESSION ||
-        !key.isStatic ||
-        isKeyboardEvent(key.content))
+      (!isStaticExp(key) || isKeyboardEvent(key.content))
     ) {
       handlerExp = createCallExpression(context.helper(V_ON_WITH_KEYS), [
         handlerExp,
index cb4201fbaf2c189e3aa66c346f22ab4e5a162c9e..e2f2ed8de847c7361b50faf67e1713417b1e21a5 100644 (file)
@@ -24,7 +24,8 @@ import {
   MERGE_PROPS,
   isBindKey,
   createSequenceExpression,
-  InterpolationNode
+  InterpolationNode,
+  isStaticExp
 } from '@vue/compiler-dom'
 import {
   escapeHtml,
@@ -194,7 +195,7 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
             }
             for (let j = 0; j < props.length; j++) {
               const { key, value } = props[j]
-              if (key.type === NodeTypes.SIMPLE_EXPRESSION && key.isStatic) {
+              if (isStaticExp(key)) {
                 let attrName = key.content
                 // static key attr
                 if (attrName === 'class') {