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

Fix TypeScript Enum self-references #14093

Merged
merged 6 commits into from Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
44 changes: 40 additions & 4 deletions packages/babel-plugin-transform-typescript/src/enum.ts
@@ -1,7 +1,7 @@
import assert from "assert";
import { template } from "@babel/core";
import type * as t from "@babel/types";
import type { NodePath } from "@babel/traverse";
import * as t from "@babel/types";
import assert from "assert";

export default function transpileEnum(path, t) {
const { node } = path;
Expand Down Expand Up @@ -100,6 +100,28 @@ function enumFill(path, t, id) {
*/
type PreviousEnumMembers = Map<string, number | string>;

type EnumSelfReferenceVisitorState = {
seen: PreviousEnumMembers;
path: NodePath<t.TSEnumDeclaration>;
};

function ReferencedIdentifier(
expr: NodePath<t.Identifier>,
state: EnumSelfReferenceVisitorState,
) {
const { seen, path } = state;
if (expr.isIdentifier() && seen.has(expr.node.name)) {
expr.replaceWith(
t.memberExpression(t.cloneNode(path.node.id), t.cloneNode(expr.node)),
);
expr.skip();
}
}

const enumSelfReferenceVisitor = {
ReferencedIdentifier,
};

export function translateEnumValues(
path: NodePath<t.TSEnumDeclaration>,
t: typeof import("@babel/types"),
Expand All @@ -109,7 +131,8 @@ export function translateEnumValues(
let constValue: number | string | undefined = -1;
let lastName: string;

return path.node.members.map(member => {
return path.get("members").map(memberPath => {
const member = memberPath.node;
const name = t.isIdentifier(member.id) ? member.id.name : member.id.value;
const initializer = member.initializer;
let value: t.Expression;
Expand All @@ -124,7 +147,19 @@ export function translateEnumValues(
value = t.stringLiteral(constValue);
}
} else {
value = initializer;
const initializerPath = memberPath.get("initializer");

if (initializerPath.isReferencedIdentifier()) {
ReferencedIdentifier(initializerPath, {
seen,
path,
});
} else {
initializerPath.traverse(enumSelfReferenceVisitor, { seen, path });
}

value = initializerPath.node;
seen.set(name, undefined);
}
} else if (typeof constValue === "number") {
constValue += 1;
Expand All @@ -140,6 +175,7 @@ export function translateEnumValues(
true,
);
value = t.binaryExpression("+", t.numericLiteral(1), lastRef);
seen.set(name, undefined);
}

lastName = name;
Expand Down
@@ -0,0 +1,21 @@
var x = 10;

enum Foo {
a = 10,
b = a,
c = b + x,
}

enum Bar {
D = Foo.a,
E = D,
F = Math.E,
G = E + Foo.c,
}

enum Baz {
a = 0,
b = 1,
// @ts-ignore
x = a.toString(),
}
@@ -0,0 +1,25 @@
var x = 10;
var Foo;

(function (Foo) {
Foo[Foo["a"] = 10] = "a";
Foo[Foo["b"] = 10] = "b";
Foo[Foo["c"] = Foo.b + x] = "c";
})(Foo || (Foo = {}));

var Bar;

(function (Bar) {
Bar[Bar["D"] = Foo.a] = "D";
Bar[Bar["E"] = Bar.D] = "E";
Bar[Bar["F"] = Math.E] = "F";
Bar[Bar["G"] = Bar.E + Foo.c] = "G";
})(Bar || (Bar = {}));

var Baz;

(function (Baz) {
Baz[Baz["a"] = 0] = "a";
Baz[Baz["b"] = 1] = "b";
Baz[Baz["x"] = Baz.a.toString()] = "x";
})(Baz || (Baz = {}));