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

Only evaluate own String/Number/Math methods #16033

Merged
merged 1 commit into from Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/babel-traverse/src/path/evaluation.ts
Expand Up @@ -444,8 +444,11 @@ function _evaluate(path: NodePath, state: State): any {
!isInvalidMethod(property.node.name)
) {
context = global[object.node.name];
// @ts-expect-error property may not exist in context object
func = context[property.node.name];
const key = property.node.name;
// TODO(Babel 8): Use Object.hasOwn
if (Object.hasOwnProperty.call(context, key)) {
func = context[key as keyof typeof context];
}
}

// "abc".charCodeAt(4)
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-traverse/test/evaluation.js
Expand Up @@ -152,6 +152,12 @@ describe("evaluation", function () {
expect(eval_invalid_call.confident).toBe(false);
});

it("should not evaluate inherited methods", function () {
const path = getPath("Math.hasOwnProperty('min')");
const evalResult = path.get("body.0.expression").evaluate();
expect(evalResult.confident).toBe(false);
});

it("should evaluate global call expressions", function () {
expect(
getPath("isFinite(1);").get("body.0.expression").evaluate().value,
Expand Down