SitePen / remap-istanbul

A tool for remapping Istanbul coverage via Source Maps
Other
240 stars 79 forks source link

Sourcemaps Error - Line must be Greater than or Equal to 1, Got 0 #57

Closed steve-gray closed 8 years ago

steve-gray commented 8 years ago

Situation:

Stack trace:

TypeError: Line must be greater than or equal to 1, got 0 at SourceMapConsumer_findMapping as _findMapping at SourceMapConsumer_originalPositionFor as originalPositionFor at getMapping (/Users/steveg/Desktop/workspace/steve-standards/node_modules/remap-istanbul/lib/remap.js:66:25) at /Users/steveg/Desktop/workspace/steve-standards/node_modules/remap-istanbul/lib/remap.js:264:20 at Array.forEach (native) at /Users/steveg/Desktop/workspace/steve-standards/node_modules/remap-istanbul/lib/remap.js:262:44 at Array.forEach (native) at /Users/steveg/Desktop/workspace/steve-standards/node_modules/remap-istanbul/lib/remap.js:182:22 at Array.forEach (native) at remap (/Users/steveg/Desktop/workspace/steve-standards/node_modules/remap-istanbul/lib/remap.js:181:12)

Cover task:

    import * as isparta from 'isparta';
    import istanbul from 'gulp-istanbul';
    import gulp from 'gulp';

    export default () =>
    gulp.src(['./dist/**/*.js'])
        .pipe(istanbul({
        includeUntested: true,
        instrumenter: isparta.Instrumenter,
        }))
        .pipe(gulp.dest('./docs/coverage/instrumented'))
        .pipe(istanbul.hookRequire());

Test task:

    import istanbul from 'gulp-istanbul';
    import mocha from 'gulp-mocha';
    import gulp from 'gulp';
    import remapIstanbul from 'remap-istanbul/lib/gulpRemapIstanbul';

    export default () =>
    gulp.src(['./tests/**/*.spec.js'])
        .pipe(mocha({
        reporter: 'mochawesome',
        reporterOptions: {
            inlineAssets: true,
            reportTitle: 'Unit Test Report',
            reportDir: './docs/unit-tests',
        },
        }))
        .pipe(istanbul.writeReports({
        dir: './docs/coverage',
        reporters: ['json'],
        }))
        .on('end', () => gulp.src('./docs/coverage/coverage-final.json')
            .pipe(remapIstanbul({
            'json': './docs/coverage/coverage.json',
            'html': './docs/coverage/html-report'
            }))
            .pipe(gulp.dest('.')));

Instrumented code:

        const x = 5;

        function imNeverRun() {
        if (x >= 7) {
            return 2;
        } else {
            return 5;
        }
        }

        export default x;

Babel intermediate:

  "use strict";

  Object.defineProperty(exports, "__esModule", {
    value: true
  });

  var _sourceMapSupport2 = require("source-map-support");

  (0, _sourceMapSupport2.install)();
  var x = 5;

  function imNeverRun() {
    if (x >= 7) {
      return 2;
    } else {
      return 5;
    }
  }

  exports.default = x;
  module.exports = exports['default'];
  //# sourceMappingURL=index.js.map

The sourcemap:

    {"version":3,"sources":["index.js"],"names":[],"mappings":";;;;;;;;;AAAA,IAAM,IAAI,CAAV;;AAEA,SAAS,UAAT,GAAsB;AACpB,MAAI,KAAK,CAAT,EAAY;AACV,WAAO,CAAP;AACD,GAFD,MAEO;AACL,WAAO,CAAP;AACD;AACF;;kBAEc,C","file":"index.js","sourcesContent":["const x = 5;\n\nfunction imNeverRun() {\n  if (x >= 7) {\n    return 2;\n  } else {\n    return 5;\n  }\n}\n\nexport default x;\n"]}

And then the coverage-final that's generated:

    {
    "/Users/steveg/Desktop/workspace/steve-standards/dist/index.js":{"path":"/Users/steveg/Desktop/workspace/steve-standards/dist/index.js","s":{"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":1,"8":0,"9":0,"10":0,"11":0,"12":0},"b":{"1":[0,0]},"f":{"1":0},"fnMap":{"1":{"name":"imNeverRun","loc":{"start":{"line":12,"column":0},"end":{"line":12,"column":22}},"line":15}},"statementMap":{"1":{"start":{"line":0,"column":0},"end":{"line":0,"column":0},"skip":true},"2":{"start":{"line":0,"column":0},"end":{"line":0,"column":0},"skip":true},"3":{"end":{"line":3,"column":0},"start":{"line":3,"column":0}},"4":{"end":{"line":7,"column":0},"start":{"line":7,"column":0}},"5":{"end":{"line":9,"column":0},"start":{"line":9,"column":0}},"6":{"end":{"line":10,"column":0},"start":{"line":10,"column":0}},"7":{"end":{"line":18,"column":1},"start":{"line":12,"column":0}},"8":{"end":{"line":17,"column":3},"start":{"line":13,"column":2}},"9":{"end":{"line":14,"column":4},"start":{"line":14,"column":4}},"10":{"end":{"line":16,"column":4},"start":{"line":16,"column":4}},"11":{"end":{"line":20,"column":0},"start":{"line":20,"column":0}},"12":{"end":{"line":21,"column":0},"start":{"line":21,"column":0}}},"branchMap":{"1":{"line":16,"type":"if","locations":[{"end":{"line":13,"column":2},"start":{"line":13,"column":2}},{"end":{"line":13,"column":2},"start":{"line":13,"column":2}}]}},"l":{"0":1,"3":0,"7":0,"9":0,"10":0,"12":1,"13":0,"14":0,"16":0,"20":0,"21":0}}}
steve-gray commented 8 years ago

Fix located/submitted:

https://github.com/SitePen/remap-istanbul/pull/59

The issue is that the various permutations of babel transforms I'm dealing with don't always have originating lines in the source file that they belong to. These nodes come through the remap operation with the 'skip' flag, but the code ploughs on and just attempts to remap them anyway, causing the underlying soucemaps helper classes to fail with the error in my post.

-Steve

steve-gray commented 8 years ago

Hey @kitsonk, is there any chance of getting this published as a patch soon-ish? Otherwise I'm going to need to pump out a hacked-up version for a project :(

kitsonk commented 8 years ago

I will try to make time today... Though no guarantees. Thanks for all your input!

Cheetah0723 commented 3 years ago

I also have this error in svelte. But I haven't found the answer. Please contact me.

richfergus commented 2 years ago

I also have this error in svelte. But I haven't found the answer. Please contact me.

Same

Ju-bot-code commented 2 years ago

I also have this error in svelte. But I haven't found the answer. Please contact me.

me toooo