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

Types not resolving to `.js.flow` files

Open jamsea opened this issue 8 years ago • 5 comments

Issuehunt badges

I'm using an npm package that transpiles its files then uses flow-copy-source to copy its flow annotated files to the package's lib folder. The result is there's an index.js file and an index.js.flow file. index.js.flow contains custom types exported by the application, while index.js has these stripped.

At compile time, the flow-bin binary resolves to index.js.flow instead of index.js, allowing the flow compiler to pick out the custom types. However, the flow-runtime babel plugin doesn't resolve index.js.flowfirst, causing flow-runtime to throw the error Error: Could not reference the given type.

If you could point me in the right direction in the code base I might be able to help submit a PR. If you have another suggestion on how I could fix this though I'd appreciate it!


IssueHunt Summary

Backers (Total: $20.00)

Become a backer now!

Or submit a pull request to get the deposits!

Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

jamsea avatar Jan 13 '17 00:01 jamsea

We're not yet using flow's config file and we aren't yet importing types from external definition files. We have a lot of the machinery in place - there's a concept of modules and declarations, and we have a parser for the config files but it's not all tied together yet. The file discovery aspects should be quite straightforward, but there's some complexity or at least open questions around where we put the code we generate from those external files.

In an ideal world, when babel starts we'd visit external definition files, make babel pull them into the working set and process them like any other, importing them into the code where necessary. Everything would be great. Unfortunately babel has no concept of "starting" and it processes one file at a time - there is no concept of a working set. As a babel plugin we don't have the ability to tell babel to process other files as well, in fact even the concept of a "file" is tenuous in babel - it could be a script tag.

There are 2 core problems here:

  1. We need to be able to discover definition files, process them with babel and turn them into calls to t.declare(t.module('mocha', ...)) etc.

  2. The user code needs to be able to reference the definitions. In some cases that's via an explicit import type ..., in other cases it's an implicit reference to a global type, for which we currently generate t.ref("someGlobalTypeName").


I think this has to be done out of band - we will need an external tool which inspects the directory structure, discovers definition files, collates them together, processes them and produces two files:

  • flow-runtime-typedefs.js which can then be imported by the developer at the top of their entry script.
  • flow-runtime-type-data.json which contains information about the external typedefs and is used by the babel plugin.

Whenever you install a new module or edit a definition file, you'll have to run something like flow-runtime generate on the CLI to update these files. We'll probably want a webpack plugin to handle this too.

phpnode avatar Jan 13 '17 02:01 phpnode

@jamsea I just published v0.1.0 which should not throw in this case, just emit a warning. It should help in the interim. If you get sick of the warnings you could override t.emitWarningMessage() until the issue is fixed properly

phpnode avatar Jan 13 '17 02:01 phpnode

in 0.3.0 this is mostly done, there's now flow-runtime-cli which can generate definitions from flow files, next step is to discover .flow files too

phpnode avatar Feb 08 '17 15:02 phpnode

:+1: for this feature as I would like to be able to do: t.validate(Type from .flow file, runtime object); and I get: flow-runtime: Failed to reveal unknown type in Temporal Dead Zone.

kagrela avatar May 05 '17 13:05 kagrela

@issuehunt has funded $20.00 to this issue.


issuehunt-oss[bot] avatar Jul 24 '19 13:07 issuehunt-oss[bot]