Flow: Flow is trying to parse JSON files as JS

Created on 19 Feb 2016  Â·  16Comments  Â·  Source: facebook/flow

A small repo to demonstrate: https://github.com/grncdr/flowtest, running flow status produces the following output:

src/wat.json:1
  1: []
     ^ Unexpected token [


Found 1 error

This happens on the current master: 079271416e89552d23f903ede11c93592ab5f149, I don't know when it was introduced. It currently breaks flow for most any project using a non-trivial amount of npm packages, as these often have static JSON files for test fixtures and whatnot that flow will now find and break on.

Other than that, the intersection & union type fixes in master are awesome! Keep up the great work on Flow! :heart: :heart:

bug

Most helpful comment

@jedwards1211, it indeed is not an ideal solution, but they are working on a fix. You can tweak the regex a little to make it work for you. This should solve your issue:

.*/node_modules/.*/\(lib\|test\).*\.json$

The extra slash will make sure that lib or test are directory names within the package.

All 16 comments

Still happening with the 0.22 release.

0.21 was okay i only got this problem on 0.22

A temporary solution is to ignore the JSON files:

[ignore]
.*/node_modules/.*\.json$

@jamiter unfortunately that seems to cause flow to ignore package.json files, which then causes any modules that make use of the "main" field in package.json to be unresolvable.

for reference this was the regex I ended up with in my [ignore]:

.*\(lib\|test\|builtin-modules\|binary-extensions\|faker\|spdx\).*\.json

(as far as I can tell Ocaml regexps don't support negative look-behind)

Yep. 0.21 worked fine. 0.22 is b0rked. The only work-around I can find is what @grncdr suggests: explicitly ignoring all of the json files in node_modules/ except package.json files.

@grncdr, thank you! That explained those unresolvable errors! (Duh) Finally have it all working again. This bug of course still needs to be fixed.

I shortened it for myself:

.*/node_modules/.*[^e]\.json$
.*/node_modules/.*\(lib\|test\).*\.json$

This found all the json files I was having issues with. The first line prevents package.json files to be ignored.

FYI: I just landed https://github.com/facebook/flow/pull/1436 -- which lets Flow parse JSON files that have toplevel array structures (rather than just toplevel object structures).

Based on the error in the OP here, I think that should fix the error that caused that issue!

so we're now compliant with RFC 4627 (the original JSON RFC), thanks!

but we're not with RFC 7159. the latter allows false / null / true / object / array / number / string.

we don't support false, true, null, number or string yet but I think we need to. gonna reopen to track for myself.

@jamiter unfortunately none of these absurd workarounds work if you use a module with a name like owasp-password-strength-test

@jedwards1211, it indeed is not an ideal solution, but they are working on a fix. You can tweak the regex a little to make it work for you. This should solve your issue:

.*/node_modules/.*/\(lib\|test\).*\.json$

The extra slash will make sure that lib or test are directory names within the package.

With #1436, it will still fail when a module imports an invalid JSON file (e.g. for a test).

@eyko Yeah, in that case your best bet is to add a pattern targeting those files to the [ignore] section of your .flowconfig. We're looking into fixes that would require zero user intervention as well.

You might also consider opening an issue with those module authors, informing them that they are including tests in their npm package, which they probably didn't intend to do.

I am getting a related error for malformed JSON, e.g.

➜  isomorphic-webpack git:(master) ✗ flow version
Flow, a static type checker for JavaScript, version 0.34.0
➜  isomorphic-webpack git:(master) ✗ flow
node_modules/conventional-changelog-core/test/fixtures/_malformation.json:1
  1:
     ^ Unexpected end of input


Found 1 error

In this case, node_modules/conventional-changelog-core/test/fixtures/_malformation.json is just an empty file.

@gajus An empty string is invalid JSON, so that error message is accurate and not completely relevant to this issue. Though there is the separate question of whether it's useful for Flow to report that specific case. You might be interested in this issue: https://github.com/facebook/flow/issues/2364

Was this page helpful?
0 / 5 - 0 ratings