Skip to content

Commit

Permalink
Merge pull request #1700 from linoleum-js/master
Browse files Browse the repository at this point in the history
require: warn invalid json
  • Loading branch information
goto-bus-stop committed Nov 19, 2019
2 parents 94931b9 + d3379fc commit e52476b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
11 changes: 10 additions & 1 deletion index.js
Expand Up @@ -662,7 +662,16 @@ Browserify.prototype._recorder = function (opts) {
Browserify.prototype._json = function () {
return through.obj(function (row, enc, next) {
if (/\.json$/.test(row.file)) {
row.source = 'module.exports=' + sanitize(row.source);
var sanitizedString = sanitize(row.source);
try {
// check json validity
JSON.parse(sanitizedString);
row.source = 'module.exports=' + sanitizedString;
} catch (err) {
err.message = 'While parsing ' + (row.file || row.id) + ': ' + err.message
this.emit('error', err);
return;
}
}
this.push(row);
next();
Expand Down
9 changes: 9 additions & 0 deletions test/json.js
Expand Up @@ -46,3 +46,12 @@ test('evil json', function (t) {
vm.runInNewContext(src, c);
});
});

test('invalid json', function (t) {
var b = browserify();
t.plan(1);
b.add(__dirname + '/json/invalid.js');
b.bundle(function (err, src) {
t.ok(err);
});
});
2 changes: 2 additions & 0 deletions test/json/invalid.js
@@ -0,0 +1,2 @@
ex(require('./invalid.json'));
ex(require('./invalid'));
3 changes: 3 additions & 0 deletions test/json/invalid.json
@@ -0,0 +1,3 @@
{
key: "value"
}

0 comments on commit e52476b

Please sign in to comment.