0.0.8 • Published 1 year ago

ast-types-flow v0.0.8

Weekly downloads
6,336,576
License
MIT
Repository
github
Last release
1 year ago

ast-types-flow

Flow types for the Javascript AST. Based off of benjamn/ast-types.

Usage

First install ast-types-flow via npm, then you can import any of the types that are exported.

/* @flow */

import type {Node} from 'ast-types-flow';

function getName(node: Node): string {
  switch (node.type) {
    case 'Identifier':
      return node.name;

    case 'ClassDeclaration':
      return node.id.name; // Error, id could be null.

    case 'FunctionDeclaration':
      return node.id.name; // Fine if it's always there.

    case 'FunctionExpression':
      if (node.id) {
        return node.id.name; // Can refine id to make sure it exists.
      } else {
        return 'Unknown';
      }

    case 'Literal':
      return node.name; // Error, Literals don't have names, don't be silly.
  }
  return 'Unknown';
}

How it works

A notion of "extends" is added to the Flow syntax via comments. A transform is included that will compile the source code into useful disjoint union types based on how the different types extend each other. For example:

type Node = {
  common: string,
};

type Foo = {
  // extends Node
  foo: string,
};

type Bar = {
  // extends Node
  bar: number,
};

Will be transformed into:

type Node = {
  type: 'Foo',
  _Foo: void,
  common: string,
  foo: string,
} | {
  type: 'Bar',
  _Bar: void,
  common: string,
  bar: number,
};

type Foo = {
  type: 'Foo',
  _Foo: void,
  common: string,
  foo: string,
};

type Bar = {
  type: 'Bar',
  _Foo: void,
  common: string,
  bar: number,
};

A few things to note:

  1. The type Node would more ideally be compiled into Foo | Bar but then the disjoint union cannot be properly refined. For now we have to duplicate the complete definitions.
  2. Each entry in a disjoint union has to be structurally unique or Flow will have an error on the definition. That is why the private _Foo: void fields appear in the types.
eslint-plugin-jsx-a11ykilli8n-react-native-fast-image@arisageha/react-lazyload@arisageha/react-lazyload-fixeslint-plugin-jsx-a11y-div-fourtythreeeslint-plugin-jsx-a11y-div-thirtyseveneslint-plugin-jsx-a11y-div-thirtysixeslint-plugin-jsx-a11y-div-thirtythreeeslint-plugin-jsx-a11y-div-thirtytwoeslint-plugin-jsx-a11y-div-threeeslint-plugin-jsx-a11y-div-twelveeslint-plugin-jsx-a11y-div-twentyeslint-plugin-jsx-a11y-div-twentyeighteslint-plugin-jsx-a11y-div-twentyfiveeslint-plugin-jsx-a11y-div-twentyfoureslint-plugin-jsx-a11y-div-twentynineeslint-plugin-jsx-a11y-div-twentyoneeslint-plugin-jsx-a11y-div-fifteeneslint-plugin-jsx-a11y-div-fiftyeslint-plugin-jsx-a11y-div-fiftyeighteslint-plugin-jsx-a11y-div-fiftyfiveeslint-plugin-jsx-a11y-div-fiftynineeslint-plugin-jsx-a11y-div-fiftyoneeslint-plugin-jsx-a11y-div-fiftyseveneslint-plugin-jsx-a11y-div-fiftysixeslint-plugin-jsx-a11y-div-fiftythreeeslint-plugin-jsx-a11y-div-fiftytwoeslint-plugin-jsx-a11y-div-fiveeslint-plugin-jsx-a11y-div-foureslint-plugin-jsx-a11y-div-fourteeneslint-plugin-jsx-a11y-div-fourtyeslint-plugin-jsx-a11y-div-fourtyeighteslint-plugin-jsx-a11y-div-fourtyfiveeslint-plugin-jsx-a11y-div-fourtyfoureslint-plugin-jsx-a11y-div-fourtynineeslint-plugin-jsx-a11y-div-fourtyoneeslint-plugin-jsx-a11y-div-fourtyseveneslint-plugin-jsx-a11y-div-fourtysixeslint-plugin-jsx-a11y-diveslint-plugin-jsx-a11y-div-eighteslint-plugin-jsx-a11y-div-eighteeneslint-plugin-jsx-a11y-div-eleveneslint-plugin-jsx-a11y-div-sixtynineeslint-plugin-jsx-a11y-div-sixtyoneeslint-plugin-jsx-a11y-div-sixtyseveneslint-plugin-jsx-a11y-div-sixtysixeslint-plugin-jsx-a11y-div-sixtythreeeslint-plugin-jsx-a11y-div-sixtytwoeslint-plugin-jsx-a11y-div-teneslint-plugin-jsx-a11y-div-thirteeneslint-plugin-jsx-a11y-div-thirtyeslint-plugin-jsx-a11y-div-seventeeneslint-plugin-jsx-a11y-div-seventyeslint-plugin-jsx-a11y-div-seventyoneeslint-plugin-jsx-a11y-div-sixeslint-plugin-jsx-a11y-div-sixteeneslint-plugin-jsx-a11y-div-sixtyeslint-plugin-jsx-a11y-div-sixtyeigtheslint-plugin-jsx-a11y-div-sixtyfiveeslint-plugin-jsx-a11y-div-sixtyfoureslint-plugin-jsx-a11y-div-fourtytwoeslint-plugin-jsx-a11y-div-nineeslint-plugin-jsx-a11y-div-nineteeneslint-plugin-jsx-a11y-div-thirtyeighteslint-plugin-jsx-a11y-div-thirtyfiveeslint-plugin-jsx-a11y-div-thirtyfoureslint-plugin-jsx-a11y-div-thirtynineeslint-plugin-jsx-a11y-div-seveneslint-plugin-jsx-a11y-div-thirtyoneeslint-plugin-jsx-a11y-div-twentyseveneslint-plugin-jsx-a11y-div-twentysixeslint-plugin-jsx-a11y-div-twentythreeeslint-plugin-jsx-a11y-div-twentytwoeslint-plugin-jsx-a11y-div-twogql_din_modmutasi-bcacogoportutilsexpand-react-bridgesklif-ui-kitsklif-api@everything-registry/sub-chunk-1173@deepakorg/test@deepak757/testp149-tablesklif-uimachinebeemrcapskardussomplakdavidbeckahm45julien-easy-modaldegeamunited28jualwkascopexxtrudemdemsirjusmelon204rusferdevdwi0712sabugratiskintilidin15dohun
0.0.8

1 year ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago