[go: up one dir, main page]

Skip to content
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

Expand evaluation of global built-ins in @babel/traverse #15797

Merged
merged 15 commits into from
Sep 25, 2023
Prev Previous commit
Next Next commit
fix lint errors
  • Loading branch information
= committed Jul 20, 2023
commit 5868b179ad2be61dd06b98e05c7a4e2cfd9ecd86
22 changes: 16 additions & 6 deletions packages/babel-traverse/src/path/evaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@ import type * as t from "@babel/types";
// This file contains Babels metainterpreter that can evaluate static code.

const VALID_CALLEES = ["Number", "String", "Math"] as const;
const GLOBAL_VALID_CALLEES = ["isFinite", "isNaN", "parseFloat", "parseInt", "decodeURI", "decodeURIComponent", "encodeURI", "encodeURIComponent", "btoa", "atob"] as const;
const GLOBAL_VALID_CALLEES = [
"isFinite",
"isNaN",
"parseFloat",
"parseInt",
"decodeURI",
"decodeURIComponent",
"encodeURI",
"encodeURIComponent",
"btoa",
"atob",
] as const;
const INVALID_METHODS = ["random"] as const;

function isValidCallee(val: string): val is (typeof VALID_CALLEES)[number] {
Expand All @@ -14,7 +25,9 @@ function isValidCallee(val: string): val is (typeof VALID_CALLEES)[number] {
);
}

function isGlobalValidCallee(val: string): val is (typeof GLOBAL_VALID_CALLEES)[number] {
function isGlobalValidCallee(
val: string,
): val is (typeof GLOBAL_VALID_CALLEES)[number] {
return GLOBAL_VALID_CALLEES.includes(
// @ts-expect-error val is a string
val,
Expand Down Expand Up @@ -410,10 +423,7 @@ function _evaluate(path: NodePath, state: State): any {
if (
callee.isIdentifier() &&
!path.scope.getBinding(callee.node.name) &&
(
isValidCallee(callee.node.name) ||
isGlobalValidCallee(callee.node.name)
)
(isValidCallee(callee.node.name) || isGlobalValidCallee(callee.node.name))
) {
func = global[callee.node.name];
}
Expand Down