gajus / gajus/flow-runtime

flow-runtime-cli doesn't output type definition

Open
#116 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
797
Forks
49
PR merge metrics
No merged PRs in 30d

Description

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`.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.