aboutsummaryrefslogtreecommitdiff
path: root/test/mocha/input-sourcemaps.js
blob: 22c4a232dbfdf09b513181f76626c930f3f310dc (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var assert = require("assert");
var Uglify = require("../../");
var SourceMapConsumer = require("source-map").SourceMapConsumer;

function getMap() {
    return {
        "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\"));"]
    };
}

function prepareMap(sourceMap) {
    var code = [
        '"use strict";',
        "",
        "var foo = function foo(x) {",
        '  return "foo " + x;',
        "};",
        'console.log(foo("bar"));',
        "",
        "//# sourceMappingURL=bundle.js.map",
    ].join("\n");
    var result = Uglify.minify(code, {
        sourceMap: {
            content: sourceMap,
            includeSources: true,
        }
    });
    if (result.error) throw result.error;
    return new SourceMapConsumer(result.map);
}

describe("input sourcemaps", function() {
    it("Should copy over original sourcesContent", function() {
        var orig = getMap();
        var map = prepareMap(orig);
        assert.equal(map.sourceContentFor("index.js"), orig.sourcesContent[0]);
    });

    it("Should copy sourcesContent if sources are relative", function() {
        var relativeMap = getMap();
        relativeMap.sources = ['./index.js'];
        var map = prepareMap(relativeMap);
        assert.notEqual(map.sourcesContent, null);
        assert.equal(map.sourcesContent.length, 1);
        assert.equal(map.sourceContentFor("index.js"), relativeMap.sourcesContent[0]);
    });

    it("Should not have invalid mappings from inputSourceMap (issue #882)", function() {
        var map = prepareMap(getMap());
        // 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);
        });
    });
});