@@ -3,11 +3,35 @@ import type * as t from "@babel/types";
3
3
4
4
// This file contains Babels metainterpreter that can evaluate static code.
5
5
6
- const VALID_CALLEES = [ "String" , "Number" , "Math" ] as const ;
6
+ const VALID_OBJECT_CALLEES = [ "Number" , "String" , "Math" ] as const ;
7
+ const VALID_IDENTIFIER_CALLEES = [
8
+ "isFinite" ,
9
+ "isNaN" ,
10
+ "parseFloat" ,
11
+ "parseInt" ,
12
+ "decodeURI" ,
13
+ "decodeURIComponent" ,
14
+ "encodeURI" ,
15
+ "encodeURIComponent" ,
16
+ process . env . BABEL_8_BREAKING ? "btoa" : null ,
17
+ process . env . BABEL_8_BREAKING ? "atob" : null ,
18
+ ] as const ;
19
+
7
20
const INVALID_METHODS = [ "random" ] as const ;
8
21
9
- function isValidCallee ( val : string ) : val is ( typeof VALID_CALLEES ) [ number ] {
10
- return VALID_CALLEES . includes (
22
+ function isValidObjectCallee (
23
+ val : string ,
24
+ ) : val is ( typeof VALID_OBJECT_CALLEES ) [ number ] {
25
+ return VALID_OBJECT_CALLEES . includes (
26
+ // @ts -expect-error val is a string
27
+ val ,
28
+ ) ;
29
+ }
30
+
31
+ function isValidIdentifierCallee (
32
+ val : string ,
33
+ ) : val is ( typeof VALID_IDENTIFIER_CALLEES ) [ number ] {
34
+ return VALID_IDENTIFIER_CALLEES . includes (
11
35
// @ts -expect-error val is a string
12
36
val ,
13
37
) ;
@@ -402,7 +426,8 @@ function _evaluate(path: NodePath, state: State): any {
402
426
if (
403
427
callee . isIdentifier ( ) &&
404
428
! path . scope . getBinding ( callee . node . name ) &&
405
- isValidCallee ( callee . node . name )
429
+ ( isValidObjectCallee ( callee . node . name ) ||
430
+ isValidIdentifierCallee ( callee . node . name ) )
406
431
) {
407
432
func = global [ callee . node . name ] ;
408
433
}
@@ -415,7 +440,7 @@ function _evaluate(path: NodePath, state: State): any {
415
440
if (
416
441
object . isIdentifier ( ) &&
417
442
property . isIdentifier ( ) &&
418
- isValidCallee ( object . node . name ) &&
443
+ isValidObjectCallee ( object . node . name ) &&
419
444
! isInvalidMethod ( property . node . name )
420
445
) {
421
446
context = global [ object . node . name ] ;
0 commit comments