Big update!Introducing GitHub Bot Commands. Learn more
Socket
Log inBook a demo

ast-types-flow

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Issues
File Explorer

Advanced tools

ast-types-flow

Flow types for the Javascript AST

    0.0.7latest

Version published
Maintainers
1
Weekly downloads
9,411,991
decreased by-12.92%

Weekly downloads

Readme

Source

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.

Keywords

FAQs

What is ast-types-flow?

Flow types for the Javascript AST

Is ast-types-flow popular?

The npm package ast-types-flow receives a total of 7,594,444 weekly downloads. As such, ast-types-flow popularity was classified as popular.

Is ast-types-flow well maintained?

We found that ast-types-flow demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.It has 1 open source maintainer collaborating on the project.

Last updated on 30 Oct 2015

Did you know?

Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install Socket
Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc