or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel-plugin-transform-flow-comments

Babel plugin that transforms Flow type annotations into JavaScript comments while preserving type information

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-plugin-transform-flow-comments@6.22.x

To install, run

npx @tessl/cli install tessl/npm-babel-plugin-transform-flow-comments@6.22.0

index.mddocs/

babel-plugin-transform-flow-comments

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.

Package Information

  • Package Name: babel-plugin-transform-flow-comments
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install --save-dev babel-plugin-transform-flow-comments

Core Imports

The 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.js

Via Node API:

const babel = require("babel-core");

const result = babel.transform(code, {
  plugins: ["transform-flow-comments"]
});

Basic Usage

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';*/

Capabilities

Plugin Function

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
}

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;
}

Transformation Patterns

The plugin handles the following Flow syntax transformations:

Optional Parameters

Transforms optional function parameters into Flow comment syntax:

  • Input: function foo(bar?) {}
  • Output: function foo(bar /*:: ?*/) {}

Type Annotations

Converts Flow type annotations to trailing comments:

  • Input: function foo(x: number): string {}
  • Output: function foo(x /*: number*/) /*: string*/ {}

Type Declarations

Wraps Flow type declarations in block comments:

  • Input: type User = { name: string };
  • Output: /*:: type User = { name: string }; */

Import/Export Types

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; */

Type Casting

Converts Flow type cast expressions:

  • Input: (value: string)
  • Output: (value /*: string*/)

Class Properties

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 }

Dependencies

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;
}

Error Handling

The plugin handles various Flow syntax patterns and automatically:

  • Escapes existing comment delimiters in Flow syntax to prevent conflicts
  • Removes optional flags from function parameters and assignment patterns to prevent runtime errors
  • Handles edge cases like class properties without values and complex export declarations

Notes

  • This plugin preserves the /* @flow */ directive, allowing continued use of Flow tooling
  • Can be used as a drop-in replacement for babel-plugin-flow-strip-types when type information preservation is needed
  • All transformations maintain the original semantic meaning while making code compatible with JavaScript environments
  • The plugin inherits Flow syntax parsing capabilities from babel-plugin-syntax-flow