George Zahariev ec3345bb57 Flow enums parsing (#10344)
* Flow enums parsing

* Parse exporting enums

* Enums parsing remove lookahead, other improvements

* Enums: add EnumBody and EnumMember aliases, change boolean members to use BooleaLiteral value

* Fix enum member init flow type, now that boolean members have a BooleanLiteral value

* Flow enums: use contextual utils, change members length checks to use logic operators, remove reserved word logic modification

* Flow enums: remove unnecessary code in generator, fix error message
2019-10-29 22:55:12 +01:00

36 lines
943 B
JavaScript

import { declare } from "@babel/helper-plugin-utils";
export default declare((api, options) => {
api.assertVersion(7);
// When enabled and plugins includes flow, all files should be parsed as if
// the @flow pragma was provided.
const { all, enums } = options;
if (typeof all !== "boolean" && typeof all !== "undefined") {
throw new Error(".all must be a boolean, or undefined");
}
if (typeof enums !== "boolean" && typeof enums !== "undefined") {
throw new Error(".enums must be a boolean, or undefined");
}
return {
name: "syntax-flow",
manipulateOptions(opts, parserOpts) {
// If the file has already enabled TS, assume that this is not a
// valid Flowtype file.
if (
parserOpts.plugins.some(
p => (Array.isArray(p) ? p[0] : p) === "typescript",
)
) {
return;
}
parserOpts.plugins.push(["flow", { all, enums }]);
},
};
});