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

fix crash when checking name but no sourcesContent is provided #128

Merged
merged 3 commits into from
Aug 2, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 44 additions & 31 deletions lib/helpers/streamChunksOfCombinedSourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,32 +168,34 @@ const streamChunksOfCombinedSourceMap = (
: null;
innerSourceContentLines[innerSourceIndex] = originalSourceLines;
}
const name = nameIndexValueMapping[nameIndex];
const originalName =
innerOriginalLine <= originalSourceLines.length
? originalSourceLines[innerOriginalLine - 1].slice(
innerOriginalColumn,
innerOriginalColumn + name.length
)
: "";
if (name === originalName) {
finalNameIndex =
nameIndex < nameIndexMapping.length
? nameIndexMapping[nameIndex]
: -2;
if (finalNameIndex === -2) {
const name = nameIndexValueMapping[nameIndex];
if (name) {
let globalIndex = nameMapping.get(name);
if (globalIndex === undefined) {
nameMapping.set(name, (globalIndex = nameMapping.size));
onName(globalIndex, name);
if (originalSourceLines !== null) {
const name = nameIndexValueMapping[nameIndex];
const originalName =
innerOriginalLine <= originalSourceLines.length
? originalSourceLines[innerOriginalLine - 1].slice(
innerOriginalColumn,
innerOriginalColumn + name.length
)
: "";
if (name === originalName) {
finalNameIndex =
nameIndex < nameIndexMapping.length
? nameIndexMapping[nameIndex]
: -2;
if (finalNameIndex === -2) {
const name = nameIndexValueMapping[nameIndex];
if (name) {
let globalIndex = nameMapping.get(name);
if (globalIndex === undefined) {
nameMapping.set(name, (globalIndex = nameMapping.size));
onName(globalIndex, name);
}
finalNameIndex = globalIndex;
} else {
finalNameIndex = -1;
}
finalNameIndex = globalIndex;
} else {
finalNameIndex = -1;
nameIndexMapping[nameIndex] = finalNameIndex;
}
nameIndexMapping[nameIndex] = finalNameIndex;
}
}
}
Expand All @@ -214,6 +216,15 @@ const streamChunksOfCombinedSourceMap = (
if (removeInnerSource) {
onChunk(chunk, generatedLine, generatedColumn, -1, -1, -1, -1);
return;
} else {
if (sourceIndexMapping[sourceIndex] === -2) {
let globalIndex = sourceMapping.get(innerSourceName);
if (globalIndex === undefined) {
sourceMapping.set(source, (globalIndex = sourceMapping.size));
onSource(globalIndex, innerSourceName, innerSource);
}
sourceIndexMapping[sourceIndex] = globalIndex;
}
}
}

Expand Down Expand Up @@ -255,6 +266,8 @@ const streamChunksOfCombinedSourceMap = (
if (source === innerSourceName) {
innerSourceIndex = i;
if (innerSource !== undefined) sourceContent = innerSource;
else innerSource = sourceContent;
sourceIndexMapping[i] = -2;
streamChunksOfSourceMap(
sourceContent,
innerSourceMap,
Expand Down Expand Up @@ -296,14 +309,14 @@ const streamChunksOfCombinedSourceMap = (
false,
columns
);
if (removeInnerSource) return;
}
let globalIndex = sourceMapping.get(source);
if (globalIndex === undefined) {
sourceMapping.set(source, (globalIndex = sourceMapping.size));
onSource(globalIndex, source, sourceContent);
} else {
let globalIndex = sourceMapping.get(source);
if (globalIndex === undefined) {
sourceMapping.set(source, (globalIndex = sourceMapping.size));
onSource(globalIndex, source, sourceContent);
}
sourceIndexMapping[i] = globalIndex;
}
sourceIndexMapping[i] = globalIndex;
},
(i, name) => {
nameIndexMapping[i] = -2;
Expand Down
98 changes: 94 additions & 4 deletions test/SourceMapSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ConcatSource = require("../").ConcatSource;
const PrefixSource = require("../").PrefixSource;
const ReplaceSource = require("../").ReplaceSource;
const CachedSource = require("../").CachedSource;
const createMappingsSerializer = require("../lib/helpers/createMappingsSerializer");
const SourceNode = require("source-map").SourceNode;
const fs = require("fs");
const path = require("path");
Expand Down Expand Up @@ -68,22 +69,22 @@ describe("SourceMapSource", () => {
2:0 -> [hello-world.txt] 2:0, :7, :13 -> [hello-world.txt] 2:10
3:0 -> [text] 3:11, :7, :8 -> [text] 3:17",
"file": "x",
"mappings": "YCAAA,K,CAAMC;AACN,O,MAAU;ADCC,O,CAAM",
"mappings": "YAAAA,K,CAAMC;AACN,O,MAAU;ACCC,O,CAAM",
"names": Array [
"Hello",
"World",
],
"sources": Array [
"text",
"hello-world.txt",
"text",
],
"sourcesContent": Array [
"Hello World
is a test string
Translate: Other text",
",
"Hello World
is a test string
",
Translate: Other text",
],
"version": 3,
}
Expand Down Expand Up @@ -336,4 +337,93 @@ describe("SourceMapSource", () => {
testCached(source, s => withPrefix(s).sourceAndMap({ columns: false }));
testCached(source, s => withPrefix(s).map({ columns: false }));
});

it("should not crash without original source when mapping names", () => {
const source = new SourceMapSource(
"h",
"hello.txt",
{
version: 3,
sources: ["hello.txt"],
mappings: "AAAAA",
names: ["hello"]
},
"hello",
{
version: 3,
sources: ["hello world.txt"],
mappings: "AAAA"
},
false
);
expect(withReadableMappings(source.map())).toMatchInlineSnapshot(`
Object {
"_mappings": "1:0 -> [hello world.txt] 1:0",
"file": "x",
"mappings": "AAAA",
"names": Array [],
"sources": Array [
"hello world.txt",
],
"sourcesContent": undefined,
"version": 3,
}
`);
});

it("should map generated lines to the inner source", () => {
const m = createMappingsSerializer();
const m2 = createMappingsSerializer();
const source = new SourceMapSource(
"Message: H W!",
"HELLO_WORLD.txt",
{
version: 3,
sources: ["messages.txt", "HELLO_WORLD.txt"],
mappings: [
m(1, 0, 0, 1, 0, 0),
m(1, 9, 1, 1, 0, 1),
m(1, 11, 1, 1, 6, 2),
m(1, 12, -1, -1, -1, -1)
].join(""),
names: ["Message", "hello", "world"]
},
"HELLO WORLD",
{
version: 3,
sources: ["hello world.txt"],
mappings: [m2(1, 0, 0, 1, 0, 0), m2(1, 6, -1, -1, -1, -1)].join(""),
sourcesContent: ["hello world"]
},
false
);
expect(withReadableMappings(source.sourceAndMap())).toMatchInlineSnapshot(`
Object {
"_mappings": "1:0 -> [messages.txt] 1:0 (Message), :9 -> [hello world.txt] 1:0, :11 -> [HELLO_WORLD.txt] 1:6 (world), :12
Message: H W!
^________^_^.
",
"map": Object {
"file": "x",
"mappings": "AAAAA,SCAA,ECAMC,C",
"names": Array [
"Message",
"world",
],
"sources": Array [
"messages.txt",
"hello world.txt",
"HELLO_WORLD.txt",
],
"sourcesContent": Array [
null,
"hello world",
"HELLO WORLD",
],
"version": 3,
},
"source": "Message: H W!",
}
`);
});
});
19 changes: 14 additions & 5 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,26 @@ exports.readableMappings = (mappings, sources, names, generatedCode) => {
};

exports.withReadableMappings = (sourceMap, generatedCode) => {
return (
sourceMap &&
Object.assign({}, sourceMap, {
if (!sourceMap) return sourceMap;
if (sourceMap.map) {
return Object.assign({}, sourceMap, {
_mappings: exports.readableMappings(
sourceMap.map.mappings,
sourceMap.map.sources,
sourceMap.map.names,
sourceMap.source
)
});
} else {
return Object.assign({}, sourceMap, {
_mappings: exports.readableMappings(
sourceMap.mappings,
sourceMap.sources,
sourceMap.names,
generatedCode
)
})
);
});
}
};

describe("helpers", () => {
Expand Down