Flow: VSCode Flow uncovered code

Created on 30 Aug 2017  路  4Comments  路  Source: facebook/flow

I'm trying to do a type casting for my custom type inside array functions

return ((Object.values(terms).sort((t1: mixed, t2: mixed) => {
                // Cast t1 and t2 from mixed to Term
        term1 = ((t1: any): Term)
        term2 = ((t2: any): Term)
        // Sort by date
        return new Date(term1.details.startDate) - new Date(term2.details.startDate)
    }): any): Array<Term>)

However, in VSCode, it shows me blue error saying "[flow coverage] uncovered code", and I believe the way of me doing this casting is correct based on this link

Is there any way I can get rid of this error? Or is my implementation not correct? Thanks

coverage question

Most helpful comment

@thousight I know this is pretty old and probably you already found out the solution, but you should click on flow % icon on bottom left side of VSCode editor:

screen shot 2018-02-06 at 16 19 55

All 4 comments

@thousight I know this is pretty old and probably you already found out the solution, but you should click on flow % icon on bottom left side of VSCode editor:

screen shot 2018-02-06 at 16 19 55

I end up here many time to find out how to fix false positive warning "uncovered code".

To show the [flow coverage] uncovered code warning, I had include_warnings=true in .flowconfig

I finally find out adding //@flow strict at the beginning of each file fix that:

EDIT: I just notice that adding only //@flow at the beginning of each file as the same result. I don't understand this behavior, I have all=true in the options .flowconfig section which should avoid the need to add //@flow at the beginning of each file.

Without flow strict:
without flow strict

With flow strict:
with flow strict

real uncovered code: (any instead of string)
real uncovered code

Notes:

  • At first, I still add false positive. I use flow-bin locally, restarting vscode did not help, I had to restart linux mint. (twice in my case)
  • next step should be adding features in [strict] section of .flowconfig https://flow.org/en/docs/strict/

Anyone knows/understand why there are false positive warning without "flow strict"?

Well, actually, it's not just VS Code, I use Atom and the Flow IDE plugin which calls flow coverage and shows the same error on this expression:

function cloneAndExtend<T: Object>(obj: T, extension: $Shape<T>): T {
  const clone = ((Object.create(obj): any): T);
  return Object.assign(clone, extension);
}

module.exports = cloneAndExtend;
  const clone = ((Object.create(obj): any): T);
//      ^^^^^      ^^^^^^^^^^^^^^^^^^^^^^^
//      Flow IDE Coverage: Uncovered code

Also the raw flow coverage call gives the same uncovered locations (that's where the IDEs get the ranges to highlight):

flow coverage --json /Users/sompylasar/__CENSORED__/cloneAndExtend.js | jq
{
  "expressions": {
    "covered_count": 13,
    "uncovered_count": 2,
    "uncovered_locs": [
      {
        "source": "/Users/sompylasar/__CENSORED__/cloneAndExtend.js",
        "type": "SourceFile",
        "start": {
          "line": 2,
          "column": 9,
          "offset": 78
        },
        "end": {
          "line": 2,
          "column": 13,
          "offset": 83
        }
      },
      {
        "source": "/Users/sompylasar/__CENSORED__/cloneAndExtend.js",
        "type": "SourceFile",
        "start": {
          "line": 2,
          "column": 19,
          "offset": 88
        },
        "end": {
          "line": 2,
          "column": 41,
          "offset": 111
        }
      }
    ]
  }
}

So the previously mentioned highly upvoted solution to click the Flow % icon doesn't quite apply.

screen shot 2018-12-02 at 5 49 06 pm

A sidenote question to those who upvoted this, why should it have fixed the Flow coverage?

Based on your original code snippet it would generate this try. The only parts that would be uncovered would be the areas where you are casting through any. any is unsound and therefore uncovered. Though that doesn't mean it's the wrong approach.

Screen Shot 2020-08-29 at 12 15 42 pm

The only way you could solve this is if you do type refinement through if statements, checking that it's an object that has certain properties instead of casting it to any and then back to Term.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vjpr picture vjpr  路  55Comments

gabro picture gabro  路  57Comments

NgoKnows picture NgoKnows  路  40Comments

jamesisaac picture jamesisaac  路  44Comments

opensrcery picture opensrcery  路  88Comments