From 01cd31d2c735cbecba34b824be9f537d098d1c0b Mon Sep 17 00:00:00 2001 From: Ben Kramer Date: Thu, 12 Jul 2018 10:21:48 -0500 Subject: [PATCH 1/2] fix: Handle exception on loading invalid source maps (#65) --- index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 5f9fe91..74ee432 100644 --- a/index.js +++ b/index.js @@ -50,7 +50,14 @@ module.exports = function(input, inputMap) { emitWarning("Cannot open SourceMap '" + result + "': " + err); return untouched(); } - processMap(JSON.parse(content), path.dirname(result), callback); + var map; + try { + map = JSON.parse(content) + } catch (e) { + emitWarning("Cannot parse SourceMap '" + result + "': " + e); + return untouched(); + } + processMap(map, path.dirname(result), callback); }); }.bind(this)); return; From 96322a92f9f8540550d13a796943e331776ca749 Mon Sep 17 00:00:00 2001 From: Ben Kramer Date: Thu, 12 Jul 2018 11:13:09 -0500 Subject: [PATCH 2/2] test: now has test for warning when invalid source map --- index.js | 4 ++-- test/fixtures/invalid-source-map.js | 3 +++ test/fixtures/invalid-source-map.map | 1 + test/index.test.js | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/invalid-source-map.js create mode 100644 test/fixtures/invalid-source-map.map diff --git a/index.js b/index.js index 74ee432..5419ac3 100644 --- a/index.js +++ b/index.js @@ -52,9 +52,9 @@ module.exports = function(input, inputMap) { } var map; try { - map = JSON.parse(content) + map = JSON.parse(content); } catch (e) { - emitWarning("Cannot parse SourceMap '" + result + "': " + e); + emitWarning("Cannot parse SourceMap '" + url + "': " + e); return untouched(); } processMap(map, path.dirname(result), callback); diff --git a/test/fixtures/invalid-source-map.js b/test/fixtures/invalid-source-map.js new file mode 100644 index 0000000..bd24048 --- /dev/null +++ b/test/fixtures/invalid-source-map.js @@ -0,0 +1,3 @@ +with SourceMap +//#sourceMappingURL=invalid-source-map.map +// comment \ No newline at end of file diff --git a/test/fixtures/invalid-source-map.map b/test/fixtures/invalid-source-map.map new file mode 100644 index 0000000..cdcf017 --- /dev/null +++ b/test/fixtures/invalid-source-map.map @@ -0,0 +1 @@ +{"version":3,"file":"invalid-source-map.js","sources":["../invalid-source-map.txt"],"mappings":"AAAA"}"} \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 0504647..819f036 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -149,6 +149,20 @@ describe("source-map-loader", function() { done(); }); }); + it("should warn on invalid SourceMap", function (done) { + execLoader(path.join(__dirname, "fixtures", "invalid-source-map.js"), function (err, res, map, deps, warns) { + should.equal(err, null); + warns.should.matchEach( + new RegExp("Cannot parse SourceMap 'invalid-source-map.map': SyntaxError: Unexpected string in JSON at position 102") + ); + should.equal(res, "with SourceMap\n//#sourceMappingURL=invalid-source-map.map\n// comment"); + should.equal(map, null); + deps.should.be.eql([ + path.join(__dirname, "fixtures", "invalid-source-map.map") + ]); + done(); + }); + }); it("should warn on missing SourceMap", function(done) { execLoader(path.join(__dirname, "fixtures", "missing-source-map.js"), function(err, res, map, deps, warns) { should.equal(err, null);