Skip to content

Commit

Permalink
Add ignoreInvalidMapping option (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
7rulnik authored Mar 17, 2024
1 parent 8c9b10e commit 9d1d81f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Change Log

## 1.1.0

Add `ignoreInvalidMapping` option to `SourceMapGenerator`. If enabled, source-map-js will not throw an error on the incorrect previous source map. Instead, it will print warnings and ignore broken mappings.

```js
var generator = new sourceMap.SourceMapGenerator({
file: "my-generated-javascript-file.js",
sourceRoot: "http://example.com/app/js/",
ignoreInvalidMapping: true,
});
```

* Do not throw an error since broken prev map is popular issue #20 ([#20](https://github.com/7rulnik/source-map-js/pull/20)) [@ai](https://github.com/ai)
* Add ignoreInvalidMapping option ([#21](https://github.com/7rulnik/source-map-js/pull/21)) [@7rulnik](https://github.com/7rulnik)

## 1.0.3

* Use sourceContents when non-null, even if it's an empty string ([#17](https://github.com/7rulnik/source-map-js/pull/17)) [@bshepherdson](https://github.com/bshepherdson)
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ You may pass an object with the following properties:
discretion, as a last resort. Even then, one should avoid using this flag when
running tests, if possible.

* `ignoreInvalidMapping`: Optional. When `true`, instead of throwing error on
invalid mapping, it will be ignored.

```js
var generator = new sourceMap.SourceMapGenerator({
file: "my-generated-javascript-file.js",
Expand Down
41 changes: 26 additions & 15 deletions lib/source-map-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function SourceMapGenerator(aArgs) {
this._file = util.getArg(aArgs, 'file', null);
this._sourceRoot = util.getArg(aArgs, 'sourceRoot', null);
this._skipValidation = util.getArg(aArgs, 'skipValidation', false);
this._ignoreInvalidMapping = util.getArg(aArgs, 'ignoreInvalidMapping', false);
this._sources = new ArraySet();
this._names = new ArraySet();
this._mappings = new MappingList();
Expand Down Expand Up @@ -275,14 +276,18 @@ SourceMapGenerator.prototype._validateMapping =
// specific error message to try to guide them the right way.
// For example: https://github.com/Polymer/polymer-bundler/pull/519
if (aOriginal && typeof aOriginal.line !== 'number' && typeof aOriginal.column !== 'number') {
if (typeof console !== 'undefined' && console.warn) {
console.warn(
'original.line and original.column are not numbers -- you probably meant to omit ' +
'the original mapping entirely and only map the generated position. If so, pass ' +
'null for the original mapping instead of an object with empty or null values.'
);
var message = 'original.line and original.column are not numbers -- you probably meant to omit ' +
'the original mapping entirely and only map the generated position. If so, pass ' +
'null for the original mapping instead of an object with empty or null values.'

if (this._ignoreInvalidMapping) {
if (typeof console !== 'undefined' && console.warn) {
console.warn(message);
}
return false;
} else {
throw new Error(message);
}
return false;
}

if (aGenerated && 'line' in aGenerated && 'column' in aGenerated
Expand All @@ -300,15 +305,21 @@ SourceMapGenerator.prototype._validateMapping =
return;
}
else {
if (typeof console !== 'undefined' && console.warn) {
console.warn('Invalid mapping: ' + JSON.stringify({
generated: aGenerated,
source: aSource,
original: aOriginal,
name: aName
}));
var message = 'Invalid mapping: ' + JSON.stringify({
generated: aGenerated,
source: aSource,
original: aOriginal,
name: aName
});

if (this._ignoreInvalidMapping) {
if (typeof console !== 'undefined' && console.warn) {
console.warn(message);
}
return false;
} else {
throw new Error(message)
}
return false;
}
};

Expand Down

0 comments on commit 9d1d81f

Please sign in to comment.