From 5222eec71d74f9f58a1c4542a885ebe3715e6bf2 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 2 Aug 2021 09:16:48 +0200 Subject: [PATCH] add test case for code coverage --- test/SourceMapSource.js | 57 +++++++++++++++++++++++++++++++++++++++++ test/helpers.js | 19 ++++++++++---- 2 files changed, 71 insertions(+), 5 deletions(-) diff --git a/test/SourceMapSource.js b/test/SourceMapSource.js index 0374618..0c5b878 100644 --- a/test/SourceMapSource.js +++ b/test/SourceMapSource.js @@ -5,6 +5,7 @@ const ConcatSource = require("../").ConcatSource; const PrefixSource = require("../").PrefixSource; const ReplaceSource = require("../").ReplaceSource; const CachedSource = require("../").CachedSource; +const createMappingsSerializer = require("../lib/helpers/createMappingsSerializer"); const SourceNode = require("source-map").SourceNode; const fs = require("fs"); const path = require("path"); @@ -369,4 +370,60 @@ describe("SourceMapSource", () => { } `); }); + + it("should map generated lines to the inner source", () => { + const m = createMappingsSerializer(); + const m2 = createMappingsSerializer(); + const source = new SourceMapSource( + "Message: H W!", + "HELLO_WORLD.txt", + { + version: 3, + sources: ["messages.txt", "HELLO_WORLD.txt"], + mappings: [ + m(1, 0, 0, 1, 0, 0), + m(1, 9, 1, 1, 0, 1), + m(1, 11, 1, 1, 6, 2), + m(1, 12, -1, -1, -1, -1) + ].join(""), + names: ["Message", "hello", "world"] + }, + "HELLO WORLD", + { + version: 3, + sources: ["hello world.txt"], + mappings: [m2(1, 0, 0, 1, 0, 0), m2(1, 6, -1, -1, -1, -1)].join(""), + sourcesContent: ["hello world"] + }, + false + ); + expect(withReadableMappings(source.sourceAndMap())).toMatchInlineSnapshot(` + Object { + "_mappings": "1:0 -> [messages.txt] 1:0 (Message), :9 -> [hello world.txt] 1:0, :11 -> [HELLO_WORLD.txt] 1:6 (world), :12 + Message: H W! + ^________^_^. + ", + "map": Object { + "file": "x", + "mappings": "AAAAA,SCAA,ECAMC,C", + "names": Array [ + "Message", + "world", + ], + "sources": Array [ + "messages.txt", + "hello world.txt", + "HELLO_WORLD.txt", + ], + "sourcesContent": Array [ + null, + "hello world", + "HELLO WORLD", + ], + "version": 3, + }, + "source": "Message: H W!", + } + `); + }); }); diff --git a/test/helpers.js b/test/helpers.js index 07a0ad0..f3dcaa3 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -83,17 +83,26 @@ exports.readableMappings = (mappings, sources, names, generatedCode) => { }; exports.withReadableMappings = (sourceMap, generatedCode) => { - return ( - sourceMap && - Object.assign({}, sourceMap, { + if (!sourceMap) return sourceMap; + if (sourceMap.map) { + return Object.assign({}, sourceMap, { + _mappings: exports.readableMappings( + sourceMap.map.mappings, + sourceMap.map.sources, + sourceMap.map.names, + sourceMap.source + ) + }); + } else { + return Object.assign({}, sourceMap, { _mappings: exports.readableMappings( sourceMap.mappings, sourceMap.sources, sourceMap.names, generatedCode ) - }) - ); + }); + } }; describe("helpers", () => {