Skip to content

Commit 7669065

Browse files
committed
refactor: seen enum members Object -> Map
1 parent 1efd019 commit 7669065

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

  • packages/babel-plugin-transform-typescript/src

packages/babel-plugin-transform-typescript/src/enum.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,13 @@ function enumFill(path, t, id) {
9898
* Z = X | Y,
9999
* }
100100
*/
101-
type PreviousEnumMembers = {
102-
[name: string]: number | string;
103-
};
101+
type PreviousEnumMembers = Map<string, number | string>;
104102

105103
export function translateEnumValues(
106104
path: NodePath<t.TSEnumDeclaration>,
107105
t: typeof import("@babel/types"),
108106
): Array<[name: string, value: t.Expression]> {
109-
const seen: PreviousEnumMembers = Object.create(null);
107+
const seen: PreviousEnumMembers = new Map();
110108
// Start at -1 so the first enum member is its increment, 0.
111109
let constValue: number | string | undefined = -1;
112110
let lastName: string;
@@ -118,7 +116,7 @@ export function translateEnumValues(
118116
if (initializer) {
119117
constValue = evaluate(initializer, seen);
120118
if (constValue !== undefined) {
121-
seen[name] = constValue;
119+
seen.set(name, constValue);
122120
if (typeof constValue === "number") {
123121
value = t.numericLiteral(constValue);
124122
} else {
@@ -131,7 +129,7 @@ export function translateEnumValues(
131129
} else if (typeof constValue === "number") {
132130
constValue += 1;
133131
value = t.numericLiteral(constValue);
134-
seen[name] = constValue;
132+
seen.set(name, constValue);
135133
} else if (typeof constValue === "string") {
136134
throw path.buildCodeFrameError("Enum member must have initializer.");
137135
} else {
@@ -169,7 +167,7 @@ function evaluate(
169167
case "ParenthesizedExpression":
170168
return evalConstant(expr.expression);
171169
case "Identifier":
172-
return seen[expr.name];
170+
return seen.get(expr.name);
173171
case "TemplateLiteral":
174172
if (expr.quasis.length === 1) {
175173
return expr.quasis[0].value.cooked;

0 commit comments

Comments
 (0)