]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: use global whitelist for render proxy has check
authorEvan You <yyx990803@gmail.com>
Wed, 2 Oct 2019 14:37:06 +0000 (10:37 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 2 Oct 2019 14:37:06 +0000 (10:37 -0400)
packages/compiler-core/src/transforms/transformExpression.ts
packages/runtime-core/src/componentProxy.ts
packages/shared/src/globalsWhitelist.ts [new file with mode: 0644]
packages/shared/src/index.ts

index 6cbb596271f19e952682bae1a0573195685340b7..0658ecc56cc7b4e04bbb2566082bd18a3270b828 100644 (file)
@@ -23,6 +23,7 @@ import {
   parseJS,
   walkJS
 } from '../utils'
+import { globalsWhitelist } from '@vue/shared'
 
 export const transformExpression: NodeTransform = (node, context) => {
   if (node.type === NodeTypes.INTERPOLATION) {
@@ -215,17 +216,6 @@ const isPropertyShorthand = (node: Node, parent: Node) =>
 const isStaticPropertyKey = (node: Node, parent: Node) =>
   isPropertyKey(node, parent) && (parent as Property).value !== node
 
-const globals = new Set(
-  (
-    'Infinity,undefined,NaN,isFinite,isNaN,' +
-    'parseFloat,parseInt,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' +
-    'Math,Number,Date,Array,Object,Boolean,String,RegExp,Map,Set,JSON,Intl,' +
-    'require,' + // for webpack
-    'arguments,'
-  ) // parsed as identifier but is a special keyword...
-    .split(',')
-)
-
 function shouldPrefix(identifier: Identifier, parent: Node) {
   if (
     !(
@@ -245,8 +235,12 @@ function shouldPrefix(identifier: Identifier, parent: Node) {
     ) &&
     // not in an Array destructure pattern
     !(parent.type === 'ArrayPattern') &&
-    // skip globals + commonly used shorthands
-    !globals.has(identifier.name)
+    // skip whitelisted globals
+    !globalsWhitelist.has(identifier.name) &&
+    // special case for webpack compilation
+    identifier.name !== `require` &&
+    // is a special keyword but parsed as identifier
+    identifier.name !== `arguments`
   ) {
     return true
   }
index 05f784287164489ca2d15dfad7562d23ae9d49eb..39ee392c1e7a8da98026c9df72a859facc617bcd 100644 (file)
@@ -1,7 +1,7 @@
 import { ComponentInternalInstance, Data } from './component'
 import { nextTick } from './scheduler'
 import { instanceWatch } from './apiWatch'
-import { EMPTY_OBJ, hasOwn } from '@vue/shared'
+import { EMPTY_OBJ, hasOwn, globalsWhitelist } from '@vue/shared'
 import { ExtracComputedReturns } from './apiOptions'
 import { UnwrapRef } from '@vue/reactivity'
 
@@ -78,15 +78,10 @@ export const PublicInstanceProxyHandlers = {
       }
     }
   },
-  has(target: ComponentInternalInstance, key: string): boolean {
-    const { renderContext, data, props } = target
-    // TODO handle $xxx properties
-    return (
-      key[0] !== '_' &&
-      ((data !== EMPTY_OBJ && hasOwn(data, key)) ||
-        hasOwn(renderContext, key) ||
-        hasOwn(props, key))
-    )
+  // this trap is only called in browser-compiled render functions that use
+  // `with (this) {}`
+  has(_: any, key: string): boolean {
+    return key[0] !== '_' && !globalsWhitelist.has(key)
   },
   set(target: ComponentInternalInstance, key: string, value: any): boolean {
     const { data, renderContext } = target
diff --git a/packages/shared/src/globalsWhitelist.ts b/packages/shared/src/globalsWhitelist.ts
new file mode 100644 (file)
index 0000000..97e2583
--- /dev/null
@@ -0,0 +1,7 @@
+export const globalsWhitelist = new Set(
+  (
+    'Infinity,undefined,NaN,isFinite,isNaN,parseFloat,parseInt,decodeURI,' +
+    'decodeURIComponent,encodeURI,encodeURIComponent,Math,Number,Date,Array,' +
+    'Object,Boolean,String,RegExp,Map,Set,JSON,Intl'
+  ).split(',')
+)
index 93781a8d229454e495a765f32d52b68f48083c95..11a72c1dcc40a58072a8a1021e5720ad10d8d869 100644 (file)
@@ -1,4 +1,5 @@
 export * from './patchFlags'
+export { globalsWhitelist } from './globalsWhitelist'
 
 export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
   ? Object.freeze({})