aboutsummaryrefslogtreecommitdiff
path: root/test/mocha/input-sourcemaps.js
blob: 30ee5b92e700a511b7d351dfeeffcb12b1ac0cae (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var Uglify = require('../../');
var assert = require("assert");
var SourceMapConsumer = require("source-map").SourceMapConsumer;

describe("input sourcemaps", function() {
    var transpiled = '"use strict";\n\n' +
        'var foo = function foo(x) {\n  return "foo " + x;\n};\n' +
        'console.log(foo("bar"));\n\n' +
        '//# sourceMappingURL=bundle.js.map';

    var transpilemap = {
        "version": 3,
        "sources": ["index.js"],
        "names": [],
        "mappings": ";;AAAA,IAAI,MAAM,SAAN,GAAM;AAAA,SAAK,SAAS,CAAd;AAAA,CAAV;AACA,QAAQ,GAAR,CAAY,IAAI,KAAJ,CAAZ",
        "file": "bundle.js",
        "sourcesContent": ["let foo = x => \"foo \" + x;\nconsole.log(foo(\"bar\"));"]
    };

    var result = Uglify.minify(transpiled, {
        fromString: true,
        inSourceMap: transpilemap,
        outSourceMap: true
    });
    var map = new SourceMapConsumer(result.map);

    it("Should copy over original sourcesContent", function() {
        assert.equal(map.sourceContentFor("index.js"), transpilemap.sourcesContent[0]);
    });

    it("Final sourcemap should not have invalid mappings from inputSourceMap (issue #882) ", function() {
        // The original source has only 2 lines, check that mappings don't have more lines

        var msg = "Mapping should not have higher line number than the original file had";
        map.eachMapping(function(mapping) {
            assert.ok(mapping.originalLine <= 2, msg)
        });

        map.allGeneratedPositionsFor({source: "index.js", line: 1, column: 1}).forEach(function(pos) {
            assert.ok(pos.line <= 2, msg);
        })
    });
});