Skip to content

Commit d35794e

Browse files
[Babel 8] Create TSEnumBody for TSEnumDeclaration (#16979)
* update babel-types definitions * breaking: create TSEnumBody for Babel 8 * update test fixtures * breaking: remove unused initializer field * fix typings * print TSEnumBody * support Babel 8 AST in typescript transform * fix: inherit enum member's comments * add TSEnumBody printer method * polish: adjust inner comments for TSEnumDeclaration * fix printer test * define TSEnumBody for Babel 7 * update artifacts * fix: adapt to Babel 8 AST * purge failing prettier test due to the AST change * Update packages/babel-generator/src/generators/typescript.ts Co-authored-by: Nicolò Ribaudo <[email protected]> * fix typings * print trailing comma only according to original source * update test fixtures * modify according to the changed interface --------- Co-authored-by: Nicolò Ribaudo <[email protected]>
1 parent 9c4e3c4 commit d35794e

File tree

231 files changed

+3358
-248
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

231 files changed

+3358
-248
lines changed

packages/babel-generator/src/generators/typescript.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ export function TSInstantiationExpression(
604604
}
605605

606606
export function TSEnumDeclaration(this: Printer, node: t.TSEnumDeclaration) {
607-
const { declare, const: isConst, id, members } = node;
607+
const { declare, const: isConst, id } = node;
608608
if (declare) {
609609
this.word("declare");
610610
this.space();
@@ -618,11 +618,21 @@ export function TSEnumDeclaration(this: Printer, node: t.TSEnumDeclaration) {
618618
this.print(id);
619619
this.space();
620620

621+
if (process.env.BABEL_8_BREAKING) {
622+
// @ts-ignore(Babel 7 vs Babel 8) Babel 8 AST
623+
this.print(node.body);
624+
} else {
625+
// cast to TSEnumBody for Babel 7 AST
626+
TSEnumBody.call(this, node as unknown as t.TSEnumBody);
627+
}
628+
}
629+
630+
export function TSEnumBody(this: Printer, node: t.TSEnumBody) {
621631
printBraced(this, node, () =>
622632
this.printList(
623-
members,
624-
// TODO: Default to false for consistency with everything else
625-
this.shouldPrintTrailingComma("}") ?? true,
633+
node.members,
634+
this.shouldPrintTrailingComma("}") ??
635+
(process.env.BABEL_8_BREAKING ? false : true),
626636
true,
627637
true,
628638
),

packages/babel-generator/src/printer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
isStatement,
99
isClassBody,
1010
isTSInterfaceBody,
11-
isTSEnumDeclaration,
11+
isTSEnumMember,
1212
} from "@babel/types";
1313
import type { Opts as jsescOptions } from "jsesc";
1414

@@ -1360,7 +1360,7 @@ class Printer {
13601360
!isStatement(node) &&
13611361
!isClassBody(parent) &&
13621362
!isTSInterfaceBody(parent) &&
1363-
!isTSEnumDeclaration(parent);
1363+
!isTSEnumMember(node);
13641364

13651365
if (type === COMMENT_TYPE.LEADING) {
13661366
this._printComment(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum E {
2+
A,
3+
B = 0
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"BABEL_8_BREAKING": false
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum E {
2+
A,
3+
B = 0,
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum E {
2+
const,
3+
default
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"BABEL_8_BREAKING": false
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum E {
2+
const,
3+
default,
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"BABEL_8_BREAKING": true
3+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
enum E {
22
const,
3-
default,
3+
default
44
}

0 commit comments

Comments
 (0)