Skip to content

Commit

Permalink
Only perform path.evaluate() on pure nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 11, 2023
1 parent cbca3eb commit e9f0aa8
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/babel-helper-define-polyfill-provider/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ function resolveId(path): string {
return path.node.name;
}

const { deopt } = path.evaluate();
if (deopt && deopt.isIdentifier()) {
return deopt.node.name;
if (path.isPure()) {
const { deopt } = path.evaluate();
if (deopt && deopt.isIdentifier()) {
return deopt.node.name;
}
}
}

Expand Down Expand Up @@ -55,7 +57,11 @@ export function resolveKey(
if (sym) return "Symbol." + sym;
}

if (!isIdentifier || scope.hasBinding(path.node.name, /* noGlobals */ true)) {
if (
isIdentifier
? scope.hasBinding(path.node.name, /* noGlobals */ true)
: path.isPure()
) {
const { value } = path.evaluate();
if (typeof value === "string") return value;
}
Expand All @@ -82,13 +88,15 @@ export function resolveSource(obj: NodePath): {
return { id, placement: "static" };
}

const { value } = obj.evaluate();
if (value !== undefined) {
return { id: getType(value), placement: "prototype" };
} else if (obj.isRegExpLiteral()) {
if (obj.isRegExpLiteral()) {
return { id: "RegExp", placement: "prototype" };
} else if (obj.isFunction()) {
return { id: "Function", placement: "prototype" };
} else if (obj.isPure()) {
const { value } = obj.evaluate();
if (value !== undefined) {
return { id: getType(value), placement: "prototype" };
}
}

return { id: null, placement: null };
Expand Down

0 comments on commit e9f0aa8

Please sign in to comment.