Skip to content

Commit b13376b

Browse files
Only evaluate own String/Number/Math methods (#16033)
1 parent aa65ac2 commit b13376b

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

packages/babel-traverse/src/path/evaluation.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,11 @@ function _evaluate(path: NodePath, state: State): any {
444444
!isInvalidMethod(property.node.name)
445445
) {
446446
context = global[object.node.name];
447-
// @ts-expect-error property may not exist in context object
448-
func = context[property.node.name];
447+
const key = property.node.name;
448+
// TODO(Babel 8): Use Object.hasOwn
449+
if (Object.hasOwnProperty.call(context, key)) {
450+
func = context[key as keyof typeof context];
451+
}
449452
}
450453

451454
// "abc".charCodeAt(4)

packages/babel-traverse/test/evaluation.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ describe("evaluation", function () {
152152
expect(eval_invalid_call.confident).toBe(false);
153153
});
154154

155+
it("should not evaluate inherited methods", function () {
156+
const path = getPath("Math.hasOwnProperty('min')");
157+
const evalResult = path.get("body.0.expression").evaluate();
158+
expect(evalResult.confident).toBe(false);
159+
});
160+
155161
it("should evaluate global call expressions", function () {
156162
expect(
157163
getPath("isFinite(1);").get("body.0.expression").evaluate().value,

0 commit comments

Comments
 (0)