A Babel syntax plugin that enables parsing of Flow type annotations in JavaScript code
npx @tessl/cli install tessl/npm-babel--plugin-syntax-flow@7.27.0@babel/plugin-syntax-flow is a Babel syntax plugin that enables parsing of Flow type annotations in JavaScript code. It is a lightweight plugin that adds Flow syntax support to Babel's parser without performing any transformations, allowing developers to use Flow type syntax in their code by configuring Babel to recognize Flow-specific language constructs.
Important: This plugin only enables parsing of Flow syntax - it does not transform or remove Flow type annotations. For transformation, use @babel/plugin-transform-flow-strip-types or @babel/preset-flow.
npm install --save-dev @babel/plugin-syntax-flow or yarn add @babel/plugin-syntax-flow --devimport plugin from "@babel/plugin-syntax-flow";
import type { Options } from "@babel/plugin-syntax-flow";For CommonJS:
const plugin = require("@babel/plugin-syntax-flow");
// TypeScript users can import the Options type:
// import type { Options } from "@babel/plugin-syntax-flow";Add the plugin to your Babel configuration:
// babel.config.js
module.exports = {
plugins: ["@babel/plugin-syntax-flow"]
};With options:
// babel.config.js
module.exports = {
plugins: [
["@babel/plugin-syntax-flow", { all: true }]
]
};Example Flow code that can be parsed:
// @flow
function add(x: number, y: number): number {
return x + y;
}
type User = {
id: number,
name: string,
email?: string
};
const user: User = {
id: 1,
name: "John Doe"
};The main plugin function created using the declare helper from @babel/helper-plugin-utils.
/**
* Flow syntax plugin created with declare() helper
* Configures Babel to parse Flow type annotations
*/
declare const plugin: (
api: PluginAPI,
options: Options,
dirname: string
) => {
name: "syntax-flow";
manipulateOptions(opts: any, parserOpts: { plugins: Array<string | [string, any]> }): void;
};
export default plugin;
interface PluginAPI {
version: string;
assertVersion(range: number | string): void;
}Configuration options interface that is also exported as a named export.
/**
* Configuration options for the Flow syntax plugin
*/
export interface Options {
/** When enabled, treats all files as if they have the @flow pragma */
all?: boolean;
/** @deprecated Removed in Babel 8. Previously controlled Flow enums parsing (now always enabled) */
enums?: boolean;
}Usage Example:
// Enable Flow parsing for all files
{
"plugins": [
["@babel/plugin-syntax-flow", { "all": true }]
]
}The plugin works by manipulating Babel's parser options to enable Flow syntax recognition:
["flow", { all, enums }] to parserOpts.plugins arrayall: true, treats all files as if they contain the @flow pragmaall and enums options to Flow parserall option (enums always enabled)api.assertVersion(REQUIRED_VERSION(7)) to ensure Babel 7+ compatibilityTechnical Details:
manipulateOptions hook which modifies parser options before parsing beginsThe plugin validates configuration options and throws descriptive errors:
// Error thrown for invalid .all option
throw new Error(".all must be a boolean, or undefined");
// Error thrown in Babel 8 for deprecated .enums option
throw new Error("The .enums option has been removed and it's now always enabled. Please remove it from your config.");
// Warning in Babel 7 for deprecated .enums option
console.warn("The .enums option has been removed and it's now always enabled.");Error Conditions:
.all option is not a boolean or undefined.enums option is provided (any value).enums option is set to falseassertVersion(REQUIRED_VERSION(7))./lib/index.js./lib/index.d.ts./package.json