Skip to content

Commit d0285ee

Browse files
authored
fix: Use resolve instead of evaluate (#225)
* fix * improve
1 parent 786a49e commit d0285ee

File tree

1 file changed

+20
-18
lines changed
  • packages/babel-helper-define-polyfill-provider/src

1 file changed

+20
-18
lines changed

packages/babel-helper-define-polyfill-provider/src/utils.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ export function has(object: any, key: string) {
1313
return Object.prototype.hasOwnProperty.call(object, key);
1414
}
1515

16-
function getType(target: any): string {
17-
return Object.prototype.toString.call(target).slice(8, -1);
18-
}
19-
2016
function resolveId(path): string {
2117
if (
2218
path.isIdentifier() &&
@@ -25,11 +21,9 @@ function resolveId(path): string {
2521
return path.node.name;
2622
}
2723

28-
if (path.isPure()) {
29-
const { deopt } = path.evaluate();
30-
if (deopt && deopt.isIdentifier()) {
31-
return deopt.node.name;
32-
}
24+
const resolved = path.resolve();
25+
if (resolved.isIdentifier()) {
26+
return resolved.node.name;
3327
}
3428
}
3529

@@ -88,15 +82,23 @@ export function resolveSource(obj: NodePath): {
8882
return { id, placement: "static" };
8983
}
9084

91-
if (obj.isRegExpLiteral()) {
92-
return { id: "RegExp", placement: "prototype" };
93-
} else if (obj.isFunction()) {
94-
return { id: "Function", placement: "prototype" };
95-
} else if (obj.isPure()) {
96-
const { value } = obj.evaluate();
97-
if (value !== undefined) {
98-
return { id: getType(value), placement: "prototype" };
99-
}
85+
// @ts-expect-error no type for resolve
86+
const path = obj.resolve() as NodePath;
87+
switch (path.type) {
88+
case "RegExpLiteral":
89+
return { id: "RegExp", placement: "prototype" };
90+
case "FunctionExpression":
91+
return { id: "Function", placement: "prototype" };
92+
case "StringLiteral":
93+
return { id: "String", placement: "prototype" };
94+
case "NumberLiteral":
95+
return { id: "Number", placement: "prototype" };
96+
case "BooleanLiteral":
97+
return { id: "Boolean", placement: "prototype" };
98+
case "ObjectExpression":
99+
return { id: "Object", placement: "prototype" };
100+
case "ArrayExpression":
101+
return { id: "Array", placement: "prototype" };
100102
}
101103

102104
return { id: null, placement: null };

0 commit comments

Comments
 (0)