Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignoreInvalidMapping option #21

Merged
merged 4 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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