Skip to content

Commit 840d640

Browse files
authored
Merge pull request #126 from webpack/bugfix/null-source
improve handling of null sources and sourcesContent
2 parents a44f36a + 8330ba6 commit 840d640

File tree

7 files changed

+6931
-6785
lines changed

7 files changed

+6931
-6785
lines changed

lib/ConcatSource.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -142,31 +142,41 @@ class ConcatSource extends Source {
142142
needToCloseMapping = false;
143143
}
144144
const resultSourceIndex =
145-
sourceIndex < 0 ? -1 : sourceIndexMapping[sourceIndex];
145+
sourceIndex < 0 || sourceIndex >= sourceIndexMapping.length
146+
? -1
147+
: sourceIndexMapping[sourceIndex];
146148
const resultNameIndex =
147-
nameIndex < 0 ? -1 : nameIndexMapping[nameIndex];
149+
nameIndex < 0 || nameIndex >= nameIndexMapping.length
150+
? -1
151+
: nameIndexMapping[nameIndex];
148152
lastMappingLine = resultSourceIndex < 0 ? 0 : generatedLine;
149-
if (finalSource && chunk !== undefined) {
150-
code += chunk;
151-
onChunk(
152-
undefined,
153-
line,
154-
column,
155-
resultSourceIndex,
156-
originalLine,
157-
originalColumn,
158-
resultNameIndex
159-
);
153+
if (finalSource) {
154+
if (chunk !== undefined) code += chunk;
155+
if (resultSourceIndex >= 0) {
156+
onChunk(
157+
undefined,
158+
line,
159+
column,
160+
resultSourceIndex,
161+
originalLine,
162+
originalColumn,
163+
resultNameIndex
164+
);
165+
}
160166
} else {
161-
onChunk(
162-
chunk,
163-
line,
164-
column,
165-
resultSourceIndex,
166-
originalLine,
167-
originalColumn,
168-
resultNameIndex
169-
);
167+
if (resultSourceIndex < 0) {
168+
onChunk(chunk, line, column, -1, -1, -1, -1);
169+
} else {
170+
onChunk(
171+
chunk,
172+
line,
173+
column,
174+
resultSourceIndex,
175+
originalLine,
176+
originalColumn,
177+
resultNameIndex
178+
);
179+
}
170180
}
171181
},
172182
(i, source, sourceContent) => {

lib/ReplaceSource.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ class ReplaceSource extends Source {
250250
sourceIndex,
251251
originalLine,
252252
originalColumn,
253-
nameIndex < 0 ? -1 : nameIndexMapping[nameIndex]
253+
nameIndex < 0 || nameIndex >= nameIndexMapping.length
254+
? -1
255+
: nameIndexMapping[nameIndex]
254256
);
255257
generatedColumn += offset;
256258
chunkPos += offset;

lib/helpers/streamChunksOfCombinedSourceMap.js

Lines changed: 77 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ const streamChunksOfCombinedSourceMap = (
8484
const locationInChunk = originalColumn - innerGeneratedColumn;
8585
if (locationInChunk > 0) {
8686
let originalSourceLines =
87-
innerSourceContentLines[innerSourceIndex];
87+
innerSourceIndex < innerSourceContentLines.length
88+
? innerSourceContentLines[innerSourceIndex]
89+
: null;
8890
if (originalSourceLines === undefined) {
8991
const originalSource = innerSourceContents[innerSourceIndex];
9092
originalSourceLines = originalSource
@@ -110,11 +112,15 @@ const streamChunksOfCombinedSourceMap = (
110112
// We have a inner mapping to original source
111113

112114
// emit source when needed and compute global source index
113-
let sourceIndex = innerSourceIndexMapping[innerSourceIndex];
114-
if (sourceIndex < 0) {
115-
const [source, sourceContent] = innerSourceIndexValueMapping[
116-
innerSourceIndex
117-
];
115+
let sourceIndex =
116+
innerSourceIndex < innerSourceIndexMapping.length
117+
? innerSourceIndexMapping[innerSourceIndex]
118+
: -2;
119+
if (sourceIndex === -2) {
120+
const [source, sourceContent] =
121+
innerSourceIndex < innerSourceIndexValueMapping.length
122+
? innerSourceIndexValueMapping[innerSourceIndex]
123+
: [null, undefined];
118124
let globalIndex = sourceMapping.get(source);
119125
if (globalIndex === undefined) {
120126
sourceMapping.set(source, (globalIndex = sourceMapping.size));
@@ -128,15 +134,25 @@ const streamChunksOfCombinedSourceMap = (
128134
let finalNameIndex = -1;
129135
if (innerNameIndex >= 0) {
130136
// when we have a inner name
131-
finalNameIndex = innerNameIndexMapping[innerNameIndex];
132-
if (finalNameIndex < 0) {
133-
const name = innerNameIndexValueMapping[innerNameIndex];
134-
let globalIndex = nameMapping.get(name);
135-
if (globalIndex === undefined) {
136-
nameMapping.set(name, (globalIndex = nameMapping.size));
137-
onName(globalIndex, name);
137+
finalNameIndex =
138+
innerNameIndex < innerNameIndexMapping.length
139+
? innerNameIndexMapping[innerNameIndex]
140+
: -2;
141+
if (finalNameIndex === -2) {
142+
const name =
143+
innerNameIndex < innerNameIndexValueMapping.length
144+
? innerNameIndexValueMapping[innerNameIndex]
145+
: undefined;
146+
if (name) {
147+
let globalIndex = nameMapping.get(name);
148+
if (globalIndex === undefined) {
149+
nameMapping.set(name, (globalIndex = nameMapping.size));
150+
onName(globalIndex, name);
151+
}
152+
finalNameIndex = globalIndex;
153+
} else {
154+
finalNameIndex = -1;
138155
}
139-
finalNameIndex = globalIndex;
140156
innerNameIndexMapping[innerNameIndex] = finalNameIndex;
141157
}
142158
} else if (nameIndex >= 0) {
@@ -161,15 +177,22 @@ const streamChunksOfCombinedSourceMap = (
161177
)
162178
: "";
163179
if (name === originalName) {
164-
finalNameIndex = nameIndexMapping[nameIndex];
165-
if (finalNameIndex < 0) {
180+
finalNameIndex =
181+
nameIndex < nameIndexMapping.length
182+
? nameIndexMapping[nameIndex]
183+
: -2;
184+
if (finalNameIndex === -2) {
166185
const name = nameIndexValueMapping[nameIndex];
167-
let globalIndex = nameMapping.get(name);
168-
if (globalIndex === undefined) {
169-
nameMapping.set(name, (globalIndex = nameMapping.size));
170-
onName(globalIndex, name);
186+
if (name) {
187+
let globalIndex = nameMapping.get(name);
188+
if (globalIndex === undefined) {
189+
nameMapping.set(name, (globalIndex = nameMapping.size));
190+
onName(globalIndex, name);
191+
}
192+
finalNameIndex = globalIndex;
193+
} else {
194+
finalNameIndex = -1;
171195
}
172-
finalNameIndex = globalIndex;
173196
nameIndexMapping[nameIndex] = finalNameIndex;
174197
}
175198
}
@@ -194,30 +217,39 @@ const streamChunksOfCombinedSourceMap = (
194217
}
195218
}
196219

197-
// Pass through the chunk with mapping
198-
let finalNameIndex = -1;
199-
if (nameIndex >= 0) {
200-
finalNameIndex = nameIndexMapping[nameIndex];
201-
if (finalNameIndex < 0) {
202-
const name = nameIndexValueMapping[nameIndex];
203-
let globalIndex = nameMapping.get(name);
204-
if (globalIndex === undefined) {
205-
nameMapping.set(name, (globalIndex = nameMapping.size));
206-
onName(globalIndex, name);
220+
const finalSourceIndex =
221+
sourceIndex < 0 || sourceIndex >= sourceIndexMapping.length
222+
? -1
223+
: sourceIndexMapping[sourceIndex];
224+
if (finalSourceIndex < 0) {
225+
// no source, so we make it a generated chunk
226+
onChunk(chunk, generatedLine, generatedColumn, -1, -1, -1, -1);
227+
} else {
228+
// Pass through the chunk with mapping
229+
let finalNameIndex = -1;
230+
if (nameIndex >= 0 && nameIndex < nameIndexMapping.length) {
231+
finalNameIndex = nameIndexMapping[nameIndex];
232+
if (finalNameIndex === -2) {
233+
const name = nameIndexValueMapping[nameIndex];
234+
let globalIndex = nameMapping.get(name);
235+
if (globalIndex === undefined) {
236+
nameMapping.set(name, (globalIndex = nameMapping.size));
237+
onName(globalIndex, name);
238+
}
239+
finalNameIndex = globalIndex;
240+
nameIndexMapping[nameIndex] = finalNameIndex;
207241
}
208-
finalNameIndex = globalIndex;
209-
nameIndexMapping[nameIndex] = finalNameIndex;
210242
}
243+
onChunk(
244+
chunk,
245+
generatedLine,
246+
generatedColumn,
247+
finalSourceIndex,
248+
originalLine,
249+
originalColumn,
250+
finalNameIndex
251+
);
211252
}
212-
onChunk(
213-
chunk,
214-
generatedLine,
215-
generatedColumn,
216-
sourceIndex < 0 ? -1 : sourceIndexMapping[sourceIndex],
217-
originalLine,
218-
originalColumn,
219-
nameIndex < 0 ? -1 : nameIndexMapping[nameIndex]
220-
);
221253
},
222254
(i, source, sourceContent) => {
223255
if (source === innerSourceName) {
@@ -254,11 +286,11 @@ const streamChunksOfCombinedSourceMap = (
254286
(i, source, sourceContent) => {
255287
innerSourceContents[i] = sourceContent;
256288
innerSourceContentLines[i] = undefined;
257-
innerSourceIndexMapping[i] = -1;
289+
innerSourceIndexMapping[i] = -2;
258290
innerSourceIndexValueMapping[i] = [source, sourceContent];
259291
},
260292
(i, name) => {
261-
innerNameIndexMapping[i] = -1;
293+
innerNameIndexMapping[i] = -2;
262294
innerNameIndexValueMapping[i] = name;
263295
},
264296
false,
@@ -274,7 +306,7 @@ const streamChunksOfCombinedSourceMap = (
274306
sourceIndexMapping[i] = globalIndex;
275307
},
276308
(i, name) => {
277-
nameIndexMapping[i] = -1;
309+
nameIndexMapping[i] = -2;
278310
nameIndexValueMapping[i] = name;
279311
},
280312
finalSource,

lib/helpers/streamChunksOfSourceMap.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ const streamChunksOfSourceMapFull = (
2727
}
2828
const { sources, sourcesContent, names, mappings } = sourceMap;
2929
for (let i = 0; i < sources.length; i++) {
30-
onSource(i, getSource(sourceMap, i), sourcesContent && sourcesContent[i]);
30+
onSource(
31+
i,
32+
getSource(sourceMap, i),
33+
(sourcesContent && sourcesContent[i]) || undefined
34+
);
3135
}
3236
if (names) {
3337
for (let i = 0; i < names.length; i++) {
@@ -167,7 +171,11 @@ const streamChunksOfSourceMapLinesFull = (
167171
}
168172
const { sources, sourcesContent, mappings } = sourceMap;
169173
for (let i = 0; i < sources.length; i++) {
170-
onSource(i, getSource(sourceMap, i), sourcesContent && sourcesContent[i]);
174+
onSource(
175+
i,
176+
getSource(sourceMap, i),
177+
(sourcesContent && sourcesContent[i]) || undefined
178+
);
171179
}
172180

173181
let currentGeneratedLine = 1;
@@ -242,7 +250,11 @@ const streamChunksOfSourceMapFinal = (
242250
) => {
243251
const { sources, sourcesContent, names, mappings } = sourceMap;
244252
for (let i = 0; i < sources.length; i++) {
245-
onSource(i, getSource(sourceMap, i), sourcesContent && sourcesContent[i]);
253+
onSource(
254+
i,
255+
getSource(sourceMap, i),
256+
(sourcesContent && sourcesContent[i]) || undefined
257+
);
246258
}
247259
if (names) {
248260
for (let i = 0; i < names.length; i++) {
@@ -298,7 +310,11 @@ const streamChunksOfSourceMapLinesFinal = (
298310

299311
const { sources, sourcesContent, mappings } = sourceMap;
300312
for (let i = 0; i < sources.length; i++) {
301-
onSource(i, getSource(sourceMap, i), sourcesContent && sourcesContent[i]);
313+
onSource(
314+
i,
315+
getSource(sourceMap, i),
316+
(sourcesContent && sourcesContent[i]) || undefined
317+
);
302318
}
303319

304320
let currentGeneratedLine = 1;

test/Fuzzy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ describe("Fuzzy", () => {
117117
throw e;
118118
}
119119
};
120-
const rawSourceFn = list.reduce(
120+
const rawSourceFn = list.reduceRight(
121121
(result, fn) => () => fn(result()),
122122
() => new RawSource(input)
123123
);
124-
const originalSourceFn = list.reduce(
124+
const originalSourceFn = list.reduceRight(
125125
(result, fn) => () => fn(result()),
126126
() => new OriginalSource(input, "lorem.txt")
127127
);

0 commit comments

Comments
 (0)