babel-plugin-transform-flow-comments is a Babel plugin that transforms Flow type annotations into JavaScript comments, preserving Flow type information while generating JavaScript code that can run in environments without Flow support. It serves as an alternative to babel-plugin-flow-strip-types while maintaining type information for Flow tooling.
npm install --save-dev babel-plugin-transform-flow-commentsThe plugin is imported and used through Babel configuration rather than direct imports:
Via .babelrc (Recommended):
{
"plugins": ["transform-flow-comments"]
}Via CLI:
babel --plugins transform-flow-comments script.jsVia Node API:
const babel = require("babel-core");
const result = babel.transform(code, {
plugins: ["transform-flow-comments"]
});The plugin automatically transforms Flow type annotations when applied to code with Flow syntax:
Input:
function foo(bar?: string): number {
return bar ? bar.length : 0;
}
type User = {
name: string;
age: number;
};
import type { Config } from './config';Output:
function foo(bar /*:: ?: string*/) /*: number*/ {
return bar ? bar.length : 0;
}
/*:: type User = {
name: string;
age: number;
};*/
/*:: import type { Config } from './config';*/The main export of the plugin that integrates with Babel's transformation pipeline.
/**
* Main Babel plugin function that returns transformation configuration
* @param {Object} param - Babel plugin API object
* @param {Object} param.types - Babel types utility (t)
* @returns {Object} Babel plugin configuration with inherits and visitor
*/
function transformFlowCommentsPlugin({ types: t }) {
// Returns plugin configuration
}The plugin returns a configuration object that Babel uses for AST transformation:
interface PluginConfig {
/** Inherits Flow syntax parsing from babel-plugin-syntax-flow */
inherits: any;
/** AST visitor object containing transformation methods */
visitor: VisitorObject;
}
interface VisitorObject {
/** Transforms Flow type cast expressions */
TypeCastExpression(path: NodePath): void;
/** Handles optional function parameters */
Identifier(path: NodePath): void;
/** Cleans up assignment pattern optional flags */
AssignmentPattern: {
exit(path: NodePath): void;
};
/** Cleans up function parameter optional flags */
Function: {
exit(path: NodePath): void;
};
/** Transforms Flow class property annotations */
ClassProperty(path: NodePath): void;
/** Transforms Flow export type declarations and Flow nodes */
"ExportNamedDeclaration|Flow"(path: NodePath): void;
/** Transforms Flow import type and import typeof declarations */
ImportDeclaration(path: NodePath): void;
}The plugin handles the following Flow syntax transformations:
Transforms optional function parameters into Flow comment syntax:
function foo(bar?) {}function foo(bar /*:: ?*/) {}Converts Flow type annotations to trailing comments:
function foo(x: number): string {}function foo(x /*: number*/) /*: string*/ {}Wraps Flow type declarations in block comments:
type User = { name: string };/*:: type User = { name: string }; */Transforms Flow import and export type statements:
Input: import type A from './types';
Output: /*:: import type A from './types'; */
Input: export type GraphQLError = number;
Output: /*:: export type GraphQLError = number; */
Converts Flow type cast expressions:
(value: string)(value /*: string*/)Transforms Flow class property type annotations:
Input: class X { baz: ?string } (property without value)
Output: class X { /*:: baz: ?string */ }
Input: class X { bar: number = 3 } (property with value)
Output: class X { bar /*: number*/ = 3 }
The plugin requires the following dependencies:
interface Dependencies {
/** Runtime utilities for Babel transpilation */
"babel-runtime": string;
/** Flow syntax parsing plugin */
"babel-plugin-syntax-flow": string;
}The plugin handles various Flow syntax patterns and automatically:
/* @flow */ directive, allowing continued use of Flow toolingbabel-plugin-flow-strip-types when type information preservation is neededbabel-plugin-syntax-flow