Skip to content

feat(shared): add makeMap method and replace globalWhilelist set #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/compiler-core/src/transforms/transformExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function processExpression(
if (
!asParams &&
!context.identifiers[rawExp] &&
!globalsWhitelist.has(rawExp) &&
!globalsWhitelist(rawExp) &&
!literalsWhitelist.has(rawExp)
) {
node.content = `_ctx.${rawExp}`
Expand Down Expand Up @@ -246,7 +246,7 @@ function shouldPrefix(identifier: Identifier, parent: Node) {
// not in an Array destructure pattern
!(parent.type === 'ArrayPattern') &&
// skip whitelisted globals
!globalsWhitelist.has(identifier.name) &&
!globalsWhitelist(identifier.name) &&
// special case for webpack compilation
identifier.name !== `require` &&
// is a special keyword but parsed as identifier
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/componentProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ if (__RUNTIME_COMPILE__) {
// this trap is only called in browser-compiled render functions that use
// `with (this) {}`
PublicInstanceProxyHandlers.has = (_: any, key: string): boolean => {
return key[0] !== '_' && !globalsWhitelist.has(key)
return key[0] !== '_' && !globalsWhitelist(key)
}
}
4 changes: 3 additions & 1 deletion packages/shared/src/globalsWhitelist.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const globalsWhitelist = new Set([
import { makeMap } from "./makeMap";

export const globalsWhitelist = makeMap([
'Infinity',
'undefined',
'NaN',
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './patchFlags'
export * from './element'
export { globalsWhitelist } from './globalsWhitelist'
export { makeMap } from './makeMap'

export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
? Object.freeze({})
Expand Down
15 changes: 15 additions & 0 deletions packages/shared/src/makeMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Make a map and return a function for checking if a key
* is in that map.
*/
export const makeMap = (
str: string | string[],
expectsLowerCase?: boolean
): ((key: string) => true | void) => {
const map: Record<string, true> = {}
const list = typeof str === 'string' ? str.split(',') : str
for (let i = 0; i < list.length; i++) {
map[list[i]] = true
}
return expectsLowerCase ? val => map[val.toLowerCase()] : val => map[val]
}