Skip to content

Commit

Permalink
+ stricter validation for source and generated line/column: when they…
Browse files Browse the repository at this point in the history
… exist, they MUST be numbers. Related to mozilla#385, though I choose to stick with the exception being thrown as that better ensures code/mapping quality AFAIAC.

+ `addMapping()`: ensure that `originalLine` and `originalColumn` are `null` when `original` argument was undefined/`null`.
  • Loading branch information
GerHobbelt committed Jun 18, 2019
1 parent 8866e47 commit 151593b
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions lib/source-map-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ class SourceMapGenerator {
this._mappings.add({
generatedLine: generated.line,
generatedColumn: generated.column,
originalLine: original != null && original.line,
originalColumn: original != null && original.column,
originalLine: original && original.line,
originalColumn: original && original.column,
source,
name
});
Expand Down Expand Up @@ -261,50 +261,66 @@ class SourceMapGenerator {
* in to one of these categories.
*/
_validateMapping(aGenerated, aOriginal, aSource, aName) {
// When aOriginal is truthy but has empty values for .line and .column,
// it is most likely a programmer error. In this case we throw a very
// 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"
) {
throw new Error(
"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 (
aGenerated &&
"line" in aGenerated &&
"column" in aGenerated &&
typeof aGenerated.line === "number" &&
typeof aGenerated.column === "number" &&
aGenerated.line > 0 &&
aGenerated.column >= 0 &&
!aOriginal &&
!aSource &&
!aName
) {
// Case 1.
return;
} else if (
aGenerated &&
"line" in aGenerated &&
"column" in aGenerated &&
typeof aGenerated.line === "number" &&
typeof aGenerated.column === "number" &&
aOriginal &&
"line" in aOriginal &&
"column" in aOriginal &&
typeof aOriginal.line === "number" &&
typeof aOriginal.column === "number" &&
aGenerated.line > 0 &&
aGenerated.column >= 0 &&
aOriginal.line > 0 &&
aOriginal.column >= 0 &&
aSource
) {
// Cases 2 and 3.
return;
} else {
let errMsg;

// When `aOriginal` is truthy but has empty values for .line and .column,
// it is most likely a programmer error. In this case we throw a very
// 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")
) {
errMsg =
"original.line and/or 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.";
}

// When `aGenerated` is truthy but has empty values for .line and .column,
// it is most likely a programmer error. In this case we throw a very
// specific error message to try to guide them the right way.
else if (
aGenerated &&
(typeof aGenerated.line !== "number" ||
typeof aGenerated.column !== "number")
) {
errMsg =
"generated.line and/or generated.column are not numbers -- you MUST specify " +
"a valid generated position.";
}

throw new Error(
"Invalid mapping: " +
(errMsg ? errMsg + " " : "") + "Invalid mapping: " +
JSON.stringify({
generated: aGenerated,
source: aSource,
Expand Down

0 comments on commit 151593b

Please sign in to comment.