flow-runtime icon indicating copy to clipboard operation
flow-runtime copied to clipboard

flow-runtime-cli doesn't output type definition

Open harrykao opened this issue 8 years ago • 0 comments

This is a:

  • [X] Bug Report
  • [ ] Feature Request
  • [ ] Question
  • [ ] Other

Which concerns:

  • [X] flow-runtime
  • [ ] babel-plugin-flow-runtime
  • [ ] flow-runtime-validators
  • [ ] flow-runtime-mobx
  • [ ] flow-config-parser
  • [ ] The documentation website

What is the current behaviour?

The flow-runtime command doesn't output a type definition. Here's an example:

// src/test.js:

// @flow

const bar: Foo = 'string';
// flow-typed/defs.js:

declare type Foo = number;

Flow detects the error:

$ ./node_modules/.bin/flow 
src/test.js:3
  3: const bar: Foo = 'string';
                      ^^^^^^^^ string. This type is incompatible with
  3: const bar: Foo = 'string';
                ^^^ number


Found 1 error

But flow-runtime generate src produces no output.

I noticed that flow-parser and babylon output different ASTs:

> flowParser.parse('declare type Foo = number;').body
[ { type: 'TypeAlias',
    loc: { source: null, start: [Object], end: [Object] },
    range: [ 8, 26 ],
    id: 
     { type: 'Identifier',
       loc: [Object],
       range: [Object],
       name: 'Foo',
       typeAnnotation: null,
       optional: false },
    typeParameters: null,
    right: { type: 'NumberTypeAnnotation', loc: [Object], range: [Object] } } ]
> 
> babylon.parse('declare type Foo = number;', {plugins: ['flow']}).program.body
[ Node {
    type: 'DeclareTypeAlias',
    start: 0,
    end: 26,
    loc: SourceLocation { start: [Object], end: [Object] },
    id: 
     Node {
       type: 'Identifier',
       start: 13,
       end: 16,
       loc: [Object],
       name: 'Foo' },
    typeParameters: null,
    right: 
     Node {
       type: 'NumberTypeAnnotation',
       start: 19,
       end: 25,
       loc: [Object] } } ]

Note that flow-parser gives us a TypeAlias while babylon yields a DeclareTypeAlias. The latter seems more like what we want. What's the reason for using flow-parser instead of babylon?


What is the expected behaviour?

I'd expect something like this to be output:

import t from 'flow-runtime';
t.declare(t.type("Foo", t.number()));

Which package versions are you using?

I'm using the HEAD revision of flow-runtime-cli.

harrykao avatar Jun 02 '17 05:06 harrykao