Skip to content

Commit 191db78

Browse files
committed
refactor: use global whitelist for render proxy has check
1 parent d87255c commit 191db78

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

packages/compiler-core/src/transforms/transformExpression.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
parseJS,
2424
walkJS
2525
} from '../utils'
26+
import { globalsWhitelist } from '@vue/shared'
2627

2728
export const transformExpression: NodeTransform = (node, context) => {
2829
if (node.type === NodeTypes.INTERPOLATION) {
@@ -215,17 +216,6 @@ const isPropertyShorthand = (node: Node, parent: Node) =>
215216
const isStaticPropertyKey = (node: Node, parent: Node) =>
216217
isPropertyKey(node, parent) && (parent as Property).value !== node
217218

218-
const globals = new Set(
219-
(
220-
'Infinity,undefined,NaN,isFinite,isNaN,' +
221-
'parseFloat,parseInt,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' +
222-
'Math,Number,Date,Array,Object,Boolean,String,RegExp,Map,Set,JSON,Intl,' +
223-
'require,' + // for webpack
224-
'arguments,'
225-
) // parsed as identifier but is a special keyword...
226-
.split(',')
227-
)
228-
229219
function shouldPrefix(identifier: Identifier, parent: Node) {
230220
if (
231221
!(
@@ -245,8 +235,12 @@ function shouldPrefix(identifier: Identifier, parent: Node) {
245235
) &&
246236
// not in an Array destructure pattern
247237
!(parent.type === 'ArrayPattern') &&
248-
// skip globals + commonly used shorthands
249-
!globals.has(identifier.name)
238+
// skip whitelisted globals
239+
!globalsWhitelist.has(identifier.name) &&
240+
// special case for webpack compilation
241+
identifier.name !== `require` &&
242+
// is a special keyword but parsed as identifier
243+
identifier.name !== `arguments`
250244
) {
251245
return true
252246
}

packages/runtime-core/src/componentProxy.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ComponentInternalInstance, Data } from './component'
22
import { nextTick } from './scheduler'
33
import { instanceWatch } from './apiWatch'
4-
import { EMPTY_OBJ, hasOwn } from '@vue/shared'
4+
import { EMPTY_OBJ, hasOwn, globalsWhitelist } from '@vue/shared'
55
import { ExtracComputedReturns } from './apiOptions'
66
import { UnwrapRef } from '@vue/reactivity'
77

@@ -78,15 +78,10 @@ export const PublicInstanceProxyHandlers = {
7878
}
7979
}
8080
},
81-
has(target: ComponentInternalInstance, key: string): boolean {
82-
const { renderContext, data, props } = target
83-
// TODO handle $xxx properties
84-
return (
85-
key[0] !== '_' &&
86-
((data !== EMPTY_OBJ && hasOwn(data, key)) ||
87-
hasOwn(renderContext, key) ||
88-
hasOwn(props, key))
89-
)
81+
// this trap is only called in browser-compiled render functions that use
82+
// `with (this) {}`
83+
has(_: any, key: string): boolean {
84+
return key[0] !== '_' && !globalsWhitelist.has(key)
9085
},
9186
set(target: ComponentInternalInstance, key: string, value: any): boolean {
9287
const { data, renderContext } = target
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const globalsWhitelist = new Set(
2+
(
3+
'Infinity,undefined,NaN,isFinite,isNaN,parseFloat,parseInt,decodeURI,' +
4+
'decodeURIComponent,encodeURI,encodeURIComponent,Math,Number,Date,Array,' +
5+
'Object,Boolean,String,RegExp,Map,Set,JSON,Intl'
6+
).split(',')
7+
)

packages/shared/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './patchFlags'
2+
export { globalsWhitelist } from './globalsWhitelist'
23

34
export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
45
? Object.freeze({})

0 commit comments

Comments
 (0)