Adds static typing to JavaScript to improve developer productivity and code quality.
Clone or download
samwgoldman and facebook-github-bot Remove weird mixed case from LookupT
Summary:
A while back I added a TODO comment asking why we would ever hit MixedT in a
LookupT failure. I'm finally coming back to figure out why and clean up this
code.

The lower bound of a LookupT is supposed to be something on the prototype
chain of an object or an instance, where a GetPropT or SetPropT has already
failed. Unlike GetPropT/SetPropT, LookupT constraints are not syntax-directed.
The significance of thise is that NullT as a lower bound of LookupT always
means a null prototype, rather than a null value.

This also means that a MixedT lower bound is generally unexpected, as a
prototype chain should only include object-like things and null.

It turns out that this case is only significant in the TestPropT case, which
forwards all non-null lower bounds to LookupT. This means that the lower bound
of a LookupT can be mixed. For example, in the program `if (o && o.foo) {}`
given that `o` is mixed.

It is simpler to handle this case in TestPropT directly, which is what this
diff does. I have also added a comment explaining why this logic is safe.

Reviewed By: panagosg7

Differential Revision: D13036543

fbshipit-source-id: 9e9f2db5137508958fc0ba262f2bf3221a39958b
Latest commit 1f8320b Nov 13, 2018
Permalink
Failed to load latest commit information.
.circleci [PR] [circle] use actual opam version in cache key Sep 27, 2018
.github [PR] ISSUE_TEMPLATE.md Jun 7, 2017
examples remove flux-chat example Feb 7, 2018
hack [hack] move opam mini repo in its own subdir Nov 13, 2018
js Make $Flow$DebugSleep cancelable Jul 20, 2018
lib [PR] Add React hooks (and tests) Nov 13, 2018
newtests Do not include prototype members when autocompleting JSX props Nov 9, 2018
packages Move flowtest out of flow to gather feedback Nov 8, 2018
prelude Use minimal prelude libdefs when no_flowlibs=true Aug 22, 2017
resources Remove Travis Apr 26, 2018
scripts allow configuring path to node during runtests.sh Nov 8, 2018
src Remove weird mixed case from LookupT Nov 14, 2018
testgen Add the location of function signatures to the AST Nov 9, 2018
tests [type-at-pos] Split tests in multiple folders Nov 13, 2018
website [PR] Remove misleading paragraph about other languages. Nov 8, 2018
.gitattributes [PR] Mark "tests/malformed_code/text.js" as binary Apr 27, 2017
.gitignore [deploy] add `make dist/flow.zip` Jan 24, 2018
.merlin [PR] Add lwt, js_of_ocaml to .merlin Mar 26, 2018
00_config.ocp Update build system and some fixes on the test-suite on Windows Dec 17, 2015
CODE_OF_CONDUCT.md [PR] Add Code of Conduct Jan 18, 2018
CONTRIBUTING.md [PR] Add Code of Conduct Jan 18, 2018
Changelog.md v0.86.0 Nov 8, 2018
LICENSE Update LICENSE from BSD3 to MIT Sep 26, 2017
Makefile remove `flow port` command Nov 1, 2018
README.md [PR] README: align https://flow.org/ format Sep 17, 2018
_tags Remove Hh_core from logging Sep 15, 2018
appveyor.yml [appveyor] use ocaml 4.05.0 Apr 13, 2018
opam v0.86.0 Nov 8, 2018
package.json [Yarn] Use new `yarn_workspace*` macros Apr 26, 2018
runtests.sh allow configuring path to node during runtests.sh Nov 8, 2018
tool Move tool into packages/ folder Jan 31, 2018
utils.bzl Format bzl files with new buildifier Sep 12, 2018
yarn.lock use yarn workspace Feb 8, 2018

README.md

Flow Build Status Windows Build Status

Flow is a static typechecker for JavaScript. To find out more about Flow, check out flow.org.

For a background on the project, please read this overview.

Requirements

Flow works with:

  • Mac OS X
  • Linux (64-bit)
  • Windows (64-bit, Windows 10 recommended)

There are binary distributions for each of these platforms and you can also build it from source on any of them as well.

Installing Flow

Flow is simple to install: all you need is the flow binary on your PATH and you're good to go.

Installing Flow Per Project

The recommended way to install Flow is via the flow-bin npm package. Adding flow-bin to your project's package.json:

  • provides a smoother upgrade experience, since the correct version of Flow is automatically used based on the revision you check out
  • installs Flow as part of your existing npm install workflow
  • lets you use different versions of Flow on different projects
npm install --save-dev flow-bin
node_modules/.bin/flow

Installing Flow Globally

Although not recommended, you can also install Flow globally (for example, perhaps you don't use npm or package.json).

The best way to install globally is via flow-bin:

npm install -g flow-bin
flow # make sure `npm bin -g` is on your path

On Mac OS X, you can install Flow via the Homebrew package manager:

brew update
brew install flow

You can also build and install Flow via the OCaml OPAM package manager. Since Flow has some non-OCaml dependencies, you need to use the depext package like so:

opam install depext
opam depext --install flowtype

If you don't have a new enough version of OCaml to compile Flow, you can also use OPAM to bootstrap a modern version. Install OPAM via the binary packages for your operating system and run:

opam init --comp=4.05.0
opam install flowtype
eval `opam config env`
flow --help

Getting started

Getting started with flow is super easy.

  • Initialize Flow by running the following command in the root of your project
flow init
  • Add the following to the top of all the files you want to typecheck
/* @flow */
  • Run and see the magic happen
flow check

More thorough documentation and many examples can be found at flow.org.

Building Flow

Flow is written in OCaml (OCaml 4.05.0 or higher is required). You can install OCaml on Mac OS X and Linux by following the instructions at ocaml.org.

For example, on Ubuntu 16.04 and similar systems:

sudo apt-get install opam
opam init --comp 4.05.0

On OS X, using the brew package manager:

brew install opam
opam init --comp 4.05.0

Then, restart your shell and install these additional libraries:

opam update
opam pin add flowtype . -n
opam install --deps-only flowtype

Once you have these dependencies, building Flow just requires running

make

This produces a bin folder containing the flow binary.

In order to make the flow.js file, you first need to install js_of_ocaml:

opam install -y js_of_ocaml

After that, making flow.js is easy:

make js

The new flow.js file will also live in the bin folder.

Note: at this time, the OCaml dependency prevents us from adding Flow to npm. Try flow-bin if you need a npm binary wrapper.

Flow can also compile its parser to JavaScript. Read how here.

Building Flow on Windows

This is a little more complicated. Here is a process that works, though it probably can be simplified.

The general idea is that we build in Cygwin, targeting mingw. This gives us a binary that works even outside of Cygwin.

Install Cygwin

  1. Install Cygwin 64bit from https://cygwin.com/install.html
  2. In powershell, run iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/ocaml/ocaml-ci-scripts/master/appveyor-install.ps1")) which will likely run a cygwin setup installer with a bunch of cygwin packages and stuff. This helps make sure that every package that opam needs is available.

Install Opam

  1. Open the cygwin64 terminal
  2. Download opam with curl -fsSL -o opam64.tar.xz https://github.com/fdopen/opam-repository-mingw/releases/download/0.0.0.1/opam64.tar.xz
  3. tar -xf opam64.tar.xz
  4. cd opam64
  5. Install opam ./install.sh
  6. Initialize opam to point to a mingw fork: opam init -a default "https://github.com/fdopen/opam-repository-mingw.git" --comp "4.05.0+mingw64c" --switch "4.05.0+mingw64c"
  7. Make sure opam stuff is in your path: eval `opam config env`

Install Flow

  1. Clone flow: git clone https://github.com/facebook/flow.git
  2. cd flow
  3. Tell opam to use this directory as the flowtype project: opam pin add flowtype . -n
  4. Install system dependencies opam depext -u flowtype
  5. Install Flow's dependencies opam install flowtype --deps-only
  6. Finally, build Flow: make all

Using Flow's parser from JavaScript

While Flow is written in OCaml, its parser is available as a compiled-to-JavaScript module published to npm, named flow-parser. Most end users of Flow will not need to use this parser directly (and should install flow-bin from npm above), but JavaScript packages which make use of parsing Flow-typed JavaScript can use this to generate Flow's syntax tree with annotated types attached.

Running the tests

To run the tests, first compile flow using make. Then run bash ./runtests.sh bin/flow

There is a make test target that compiles and runs tests.

To run a subset of the tests you can pass a second argument to the runtests.sh file.

For example: bash runtests.sh bin/flow class | grep -v 'SKIP'

Join the Flow community

License

Flow is MIT-licensed (LICENSE). The website and documentation are licensed under the Creative Commons Attribution 4.0 license (website/LICENSE-DOCUMENTATION).