Skip to content

Commit b23c6a2

Browse files
gkzlydell
authored andcommitted
Flow enums (prettier#6833)
1 parent 16f2c97 commit b23c6a2

19 files changed

Lines changed: 462 additions & 1 deletion

src/language-js/needs-parens.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ function isStatement(node) {
790790
node.type === "DeclareModuleExports" ||
791791
node.type === "DeclareVariable" ||
792792
node.type === "DoWhileStatement" ||
793+
node.type === "EnumDeclaration" ||
793794
node.type === "ExportAllDeclaration" ||
794795
node.type === "ExportDefaultDeclaration" ||
795796
node.type === "ExportNamedDeclaration" ||

src/language-js/parser-babylon.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function createParse(parseMethod, extraPlugins) {
9494
}
9595

9696
const parse = createParse("parse", ["flow"]);
97-
const parseFlow = createParse("parse", [["flow", { all: true }]]);
97+
const parseFlow = createParse("parse", [["flow", { all: true, enums: true }]]);
9898
const parseExpression = createParse("parseExpression");
9999

100100
function tryCombinations(fn, combinations) {

src/language-js/parser-flow.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function parse(text, parsers, opts) {
1313
const flowParser = require("flow-parser");
1414

1515
const ast = flowParser.parse(text, {
16+
enums: true,
1617
esproposal_class_instance_fields: true,
1718
esproposal_class_static_fields: true,
1819
esproposal_export_star_as: true,

src/language-js/printer-estree.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,6 +2606,82 @@ function printPathNoParens(path, options, print, args) {
26062606
return concat(parts);
26072607
}
26082608

2609+
case "EnumDeclaration":
2610+
return concat([
2611+
"enum ",
2612+
path.call(print, "id"),
2613+
" ",
2614+
path.call(print, "body")
2615+
]);
2616+
case "EnumBooleanBody":
2617+
case "EnumNumberBody":
2618+
case "EnumStringBody":
2619+
case "EnumSymbolBody": {
2620+
if (n.type === "EnumSymbolBody" || n.explicitType) {
2621+
let type = null;
2622+
switch (n.type) {
2623+
case "EnumBooleanBody":
2624+
type = "boolean";
2625+
break;
2626+
case "EnumNumberBody":
2627+
type = "number";
2628+
break;
2629+
case "EnumStringBody":
2630+
type = "string";
2631+
break;
2632+
case "EnumSymbolBody":
2633+
type = "symbol";
2634+
break;
2635+
}
2636+
parts.push("of ", type, " ");
2637+
}
2638+
if (n.members.length === 0) {
2639+
parts.push(
2640+
group(
2641+
concat([
2642+
"{",
2643+
comments.printDanglingComments(path, options),
2644+
softline,
2645+
"}"
2646+
])
2647+
)
2648+
);
2649+
} else {
2650+
parts.push(
2651+
group(
2652+
concat([
2653+
"{",
2654+
indent(
2655+
concat([
2656+
hardline,
2657+
printArrayItems(path, options, "members", print),
2658+
shouldPrintComma(options) ? "," : ""
2659+
])
2660+
),
2661+
comments.printDanglingComments(
2662+
path,
2663+
options,
2664+
/* sameIndent */ true
2665+
),
2666+
hardline,
2667+
"}"
2668+
])
2669+
)
2670+
);
2671+
}
2672+
return concat(parts);
2673+
}
2674+
case "EnumBooleanMember":
2675+
case "EnumNumberMember":
2676+
case "EnumStringMember":
2677+
return concat([
2678+
path.call(print, "id"),
2679+
" = ",
2680+
typeof n.init === "object" ? path.call(print, "init") : String(n.init)
2681+
]);
2682+
case "EnumDefaultedMember":
2683+
return path.call(print, "id");
2684+
26092685
case "FunctionTypeAnnotation":
26102686
case "TSFunctionType": {
26112687
// FunctionTypeAnnotation is ambiguous:

0 commit comments

Comments
 (0)