Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

whitespace trimming approach to fix #81 #179

Closed
wants to merge 3 commits into from

Conversation

glromeo
Copy link
Contributor

@glromeo glromeo commented Jan 13, 2022

This PR aims at improving the precision of the v8 range mapping trimming the whitespaces.

e.g. #81 is caused by:

if (startCol <= line.startCol && endCol >= line.endCol && !line.ignore) {
line.count = range.count
}

because the v8 startOffset is 50 while the line startCol is 48.

I introduced minCol and maxCol which take into account the gaps caused by whitespaces and now the matching
seems more accurate.

With the trimming in place c8 tests are all green except from the following 7 cases:

    source-maps
      ✔ does not attempt to load source map URLs that aren't (195ms)
      TypeScript
        ✔ remaps branches (219ms)
        1) remaps classes
      UglifyJS
        ✔ remaps branches (221ms)
        2) remaps classes
      nyc
        ✔ remaps branches (217ms)
        3) remaps classes
      rollup
        ✔ remaps branches (216ms)
        4) remaps classes
      ts-node
        5) reads source-map from cache, and applies to coverage
    --all
      ✔ reports coverage for unloaded js files as 0 for line, branch and function (311ms)
      6) reports coverage for unloaded transpiled ts files as 0 for line, branch and function
      7) reports coverage for unloaded ts files as 0 for line, branch and function when using ts-node
      ✔ should allow for --all to be used in conjunction with --check-coverage (298ms)
      ✔ should allow for --all to be used with the check-coverage command (2 invocations) (544ms)

These differences look promising (the new range cover the method signatures which in the past were missing):

  1) AssertionError: expected value to match snapshot c8 source-maps TypeScript remaps classes 1
      + expected - actual
       -----------------------|---------|----------|---------|---------|-------------------
       File                   | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
       -----------------------|---------|----------|---------|---------|-------------------
      -All files              |   75.75 |    85.71 |      60 |   75.75 |                   
      - classes.typescript.ts |   75.75 |    85.71 |      60 |   75.75 | 12-13,20-22,26-28 
      +All files              |   81.81 |    85.71 |      60 |   81.81 |                   
      + classes.typescript.ts |   81.81 |    85.71 |      60 |   81.81 | 12-13,21-22,27-28 
       -----------------------|---------|----------|---------|---------|-------------------

  2) AssertionError: expected value to match snapshot c8 source-maps UglifyJS remaps classes 1
      + expected - actual
       ------------|---------|----------|---------|---------|-------------------
       File        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
       ------------|---------|----------|---------|---------|-------------------
      -All files   |   77.77 |       80 |      60 |   77.77 |                   
      - classes.js |   77.77 |       80 |      60 |   77.77 | 6-7,14-15,20-21   
      +All files   |   85.18 |       80 |      60 |   85.18 |                   
      + classes.js |   85.18 |       80 |      60 |   85.18 | 6-7,15,21         
       ------------|---------|----------|---------|---------|-------------------

  3) AssertionError: expected value to match snapshot c8 source-maps nyc remaps classes 1
      + expected - actual
       ------------|---------|----------|---------|---------|-------------------
       File        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
       ------------|---------|----------|---------|---------|-------------------
      -All files   |   70.37 |    83.33 |      60 |   70.37 |                   
      - classes.js |   70.37 |    83.33 |      60 |   70.37 | 7-8,14-16,20-22   
      +All files   |   77.77 |    83.33 |      60 |   77.77 |                   
      + classes.js |   77.77 |    83.33 |      60 |   77.77 | 7-8,15-16,21-22   
       ------------|---------|----------|---------|---------|-------------------
  4) AssertionError: expected value to match snapshot c8 source-maps rollup remaps classes 1
      + expected - actual
       ------------|---------|----------|---------|---------|-------------------
       File        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
       ------------|---------|----------|---------|---------|-------------------
      -All files   |   71.42 |    83.33 |      60 |   71.42 |                   
      +All files   |   78.57 |    83.33 |      60 |   78.57 |                   
        class-1.js |     100 |      100 |     100 |     100 |                   
      - class-2.js |   65.21 |    83.33 |      60 |   65.21 | 7-8,14-16,20-22   
      + class-2.js |   73.91 |    83.33 |      60 |   73.91 | 7-8,15-16,21-22   
       ------------|---------|----------|---------|---------|-------------------
  5) AssertionError: expected value to match snapshot c8 source-maps ts-node reads source-map from cache, and applies to coverage 1
      + expected - actual
       ------------------|---------|----------|---------|---------|-------------------
       File              | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
       ------------------|---------|----------|---------|---------|-------------------
      -All files         |   85.29 |    83.33 |      80 |   85.29 |                   
      - ts-node-basic.ts |   85.29 |    83.33 |      80 |   85.29 | 12-13,27-29       
      +All files         |   88.23 |    83.33 |      80 |   88.23 |                   
      + ts-node-basic.ts |   88.23 |    83.33 |      80 |   88.23 | 12-13,28-29       
       ------------------|---------|----------|---------|---------|-------------------

Instead I am still investigating these two cases (the min/max ranges seem to be remapped correctly by source maps while somethins is off with this.rawSource which breaks trimRange):

  6) AssertionError: expected value to match snapshot c8 --all reports coverage for unloaded transpiled ts files as 0 for line, branch and function 1
      + expected - actual
       -----------------|---------|----------|---------|---------|-------------------
       File             | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
       -----------------|---------|----------|---------|---------|-------------------
      -All files        |   67.85 |    57.14 |      50 |   67.85 |                   
      - ts-compiled     |    82.6 |    66.66 |     100 |    82.6 |                   
      -  loaded.ts      |   78.94 |    66.66 |     100 |   78.94 | 4-5,17-18         
      +All files        |   64.28 |    57.14 |      50 |   64.28 |                   
      + ts-compiled     |   78.26 |    66.66 |     100 |   78.26 |                   
      +  loaded.ts      |   73.68 |    66.66 |     100 |   73.68 | 4-5,16-18         
         main.ts        |     100 |      100 |     100 |     100 |                   
        ts-compiled/dir |       0 |        0 |       0 |       0 |                   
         unloaded.ts    |       0 |        0 |       0 |       0 | 1-5               
       -----------------|---------|----------|---------|---------|-------------------

  7) AssertionError: expected value to match snapshot c8 --all reports coverage for unloaded ts files as 0 for line, branch and function when using ts-node 1
      + expected - actual
       --------------|---------|----------|---------|---------|-------------------
       File          | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
       --------------|---------|----------|---------|---------|-------------------
      -All files     |   67.85 |    57.14 |      50 |   67.85 |                   
      - ts-only      |    82.6 |    66.66 |     100 |    82.6 |                   
      -  loaded.ts   |   78.94 |    66.66 |     100 |   78.94 | 4-5,17-18         
      +All files     |   64.28 |    57.14 |      50 |   64.28 |                   
      + ts-only      |   78.26 |    66.66 |     100 |   78.26 |                   
      +  loaded.ts   |   73.68 |    66.66 |     100 |   73.68 | 4-5,16-18         
         main.ts     |     100 |      100 |     100 |     100 |                   
        ts-only/dir  |       0 |        0 |       0 |       0 |                   
         unloaded.ts |       0 |        0 |       0 |       0 | 1-5               
       --------------|---------|----------|---------|---------|-------------------

I preferred to open the PR early to start receiving feedback. I will try to keep the noise to the minimum though!

I am keen to optimise the code but that once we are sure that the changes in the coverage are all good.

@glromeo glromeo marked this pull request as draft January 13, 2022 18:44
@glromeo glromeo changed the title whitespace trimming approach to fix #170 and #81 whitespace trimming approach to fix #81 Jan 19, 2022
@glromeo glromeo marked this pull request as ready for review January 19, 2022 03:00
@glromeo
Copy link
Contributor Author

glromeo commented Jan 19, 2022

Here are some updated comparisons of the c8 html reports generated for node-fetch.
On the left side the results with the new code, on the right side using v8 v8.1.1
Screenshot 2022-01-19 at 03 17 38
Screenshot 2022-01-19 at 03 14 48
Screenshot 2022-01-19 at 03 20 25
In the last picture in particular it appears that the new reports are more accurate

@glromeo
Copy link
Contributor Author

glromeo commented Jan 19, 2022

@bcoe this version fixes #172 (comment)

@bcoe
Copy link
Member

bcoe commented Feb 5, 2022

@glromeo sorry for the slow turnaround, I don't have much time for OSS outside of work currently (so it goes).

the node-fetch output looks identical, unless I'm missing something?

Happen to have an example of the incorrect behavior in an HTML report, would love to see the fix in action visually.

@glromeo
Copy link
Contributor Author

glromeo commented Feb 5, 2022

@bcoe no worries I understand...if anything thank you for carving out the time to look at my PR

Indeed the coverage summaries are the same which is good but if you look carefully at the iteration count
you will notice the differences... the left hand side (new) has more accurate counts which reassures me that
the change is an improvement.

@bcoe
Copy link
Member

bcoe commented Feb 5, 2022

the left hand side (new) has more accurate counts which reassures me that
the change is an improvement.

gotcha.

@bcoe
Copy link
Member

bcoe commented Apr 21, 2022

@glromeo apologies for the delay on this, unfortunately it's hard to do much open source work with my day job.

Would still love to land this work, as it seems like this and your other PR unblock some issues on c8.

@glromeo
Copy link
Contributor Author

glromeo commented Apr 23, 2022

@bcoe I am getting my head around the discrepancies and errors I now see since latest changes... I hope to have a fix soon

@glromeo glromeo marked this pull request as draft April 23, 2022 21:32
@bcoe
Copy link
Member

bcoe commented Feb 14, 2023

@glromeo it looks like this work has stalled a bit, closing for now, but please feel free to pick up again.

@bcoe bcoe closed this Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants