Flow Types for JavaScript AST

0.0.8 · abandoned · verified Sun Apr 19

ast-types-flow provides Flow type definitions specifically tailored for JavaScript Abstract Syntax Trees (ASTs), designed to integrate with AST structures produced by parsers compatible with `benjamn/ast-types`. Published as version 0.0.8, the project appears to be abandoned, with its last update several years ago, reflecting the broader shift in the JavaScript ecosystem's preference from Flow to TypeScript for static typing. The library employs a distinctive method, utilizing special comments like `// extends Node` to define type hierarchies, which are then processed by a custom transform into disjoint union types. This approach aims to facilitate robust type refinement for diverse AST nodes, though it often requires duplicating complete type definitions and introducing unique `_Foo: void` fields to satisfy Flow's structural uniqueness requirements for disjoint unions. It was intended for static analysis of ASTs within Flow-typed projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing Flow types for AST nodes and using them in a type-checked function to safely extract names, handling potential nulls and missing properties according to Flow's rules.

/* @flow */

import type { Node, Identifier, ClassDeclaration, FunctionDeclaration, FunctionExpression, Literal } from 'ast-types-flow';

/**
 * Safely extracts the name from various AST node types.
 * Handles cases where IDs might be null or properties might not exist.
 * @param {Node} node - The AST node to inspect.
 * @returns {string} The name of the node or 'Unknown'.
 */
function getName(node: Node): string {
  switch (node.type) {
    case 'Identifier':
      return node.name; // 'Identifier' nodes reliably have a 'name' property.

    case 'ClassDeclaration':
      // Flow would flag 'node.id' as potentially null. Refinement is needed.
      if (node.id) {
        return node.id.name;
      }
      return 'AnonymousClass'; // Handle the nullable case explicitly.

    case 'FunctionDeclaration':
      // Assuming 'id' is always present for 'FunctionDeclaration' in this context.
      return node.id.name;

    case 'FunctionExpression':
      // 'FunctionExpression' can be anonymous; 'id' might be null.
      if (node.id) {
        return node.id.name;
      } else {
        return 'AnonymousFunction';
      }

    case 'Literal':
      // 'Literal' nodes do not have a 'name' property. Flow would error here.
      if (typeof node.value === 'string' || typeof node.value === 'number') {
        return String(node.value); // Return the literal's value instead.
      }
      return 'LiteralValue';

    default:
      return 'UnknownNode';
  }
}

// Example usage (not runnable without an actual AST node)
const myIdentifier: Identifier = { type: 'Identifier', name: 'foo' };
const myClassDecl: ClassDeclaration = { type: 'ClassDeclaration', id: { type: 'Identifier', name: 'MyClass' }, body: { type: 'BlockStatement', body: [] } };
const myLiteral: Literal = { type: 'Literal', value: 123, raw: '123' };

console.log(getName(myIdentifier)); // Expected: foo
console.log(getName(myClassDecl));  // Expected: MyClass
console.log(getName(myLiteral));    // Expected: 123

view raw JSON →