Skip to content

Commit 6f72243

Browse files
authored
Merge pull request webpack#7700 from webpack/bugfix/loc-in-single-entry-and-loader-plugin
add types and fix incorrect loc type
2 parents 3366421 + b93225a commit 6f72243

File tree

4 files changed

+81
-45
lines changed

4 files changed

+81
-45
lines changed

lib/Dependency.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ const DependencyReference = require("./dependencies/DependencyReference");
1818
*/
1919

2020
/** @typedef {Object} SourcePosition
21-
* @property {number} column
2221
* @property {number} line
22+
* @property {number=} column
2323
*/
2424

2525
/** @typedef {Object} RealDependencyLocation
2626
* @property {SourcePosition} start
27-
* @property {SourcePosition} end
27+
* @property {SourcePosition=} end
2828
* @property {number=} index
2929
*/
3030

lib/SingleEntryPlugin.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@ class SingleEntryPlugin {
4848
);
4949
}
5050

51+
/**
52+
* @param {string} entry entry request
53+
* @param {string} name entry name
54+
* @returns {SingleEntryDependency} the dependency
55+
*/
5156
static createDependency(entry, name) {
5257
const dep = new SingleEntryDependency(entry);
53-
dep.loc = name;
58+
dep.loc = { name };
5459
return dep;
5560
}
5661
}

lib/dependencies/LoaderPlugin.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
const LoaderDependency = require("./LoaderDependency");
88
const NormalModule = require("../NormalModule");
99

10+
/** @typedef {import("../Module")} Module */
11+
12+
/**
13+
* @callback LoadModuleCallback
14+
* @param {Error=} err error object
15+
* @param {string=} source source code
16+
* @param {object=} map source map
17+
* @param {Module=} module loaded module if successful
18+
*/
19+
1020
class LoaderPlugin {
1121
apply(compiler) {
1222
compiler.hooks.compilation.tap(
@@ -23,9 +33,16 @@ class LoaderPlugin {
2333
compilation.hooks.normalModuleLoader.tap(
2434
"LoaderPlugin",
2535
(loaderContext, module) => {
36+
/**
37+
* @param {string} request the request string to load the module from
38+
* @param {LoadModuleCallback} callback callback returning the loaded module or error
39+
* @returns {void}
40+
*/
2641
loaderContext.loadModule = (request, callback) => {
2742
const dep = new LoaderDependency(request);
28-
dep.loc = request;
43+
dep.loc = {
44+
name: request
45+
};
2946
const factory = compilation.dependencyFactories.get(
3047
dep.constructor
3148
);

lib/formatLocation.js

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,71 @@
55

66
"use strict";
77

8+
/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
9+
/** @typedef {import("./Dependency").SourcePosition} SourcePosition */
10+
11+
// TODO webpack 5: pos must be SourcePosition
12+
/**
13+
* @param {SourcePosition|DependencyLocation|string} pos position
14+
* @returns {string} formatted position
15+
*/
816
const formatPosition = pos => {
917
if (pos === null) return "";
10-
const typeOfPos = typeof pos;
11-
switch (typeOfPos) {
12-
case "string":
13-
return pos;
14-
case "number":
15-
return `${pos}`;
16-
case "object":
17-
if (typeof pos.line === "number" && typeof pos.column === "number") {
18-
return `${pos.line}:${pos.column}`;
19-
} else if (typeof pos.line === "number") {
20-
return `${pos.line}:?`;
21-
} else if (typeof pos.index === "number") {
22-
return `+${pos.index}`;
23-
} else {
24-
return "";
25-
}
26-
default:
18+
// TODO webpack 5: Simplify this
19+
if (typeof pos === "string") return pos;
20+
if (typeof pos === "number") return `${pos}`;
21+
if (typeof pos === "object") {
22+
if ("line" in pos && "column" in pos) {
23+
return `${pos.line}:${pos.column}`;
24+
} else if ("line" in pos) {
25+
return `${pos.line}:?`;
26+
} else if ("index" in pos) {
27+
// TODO webpack 5 remove this case
28+
return `+${pos.index}`;
29+
} else {
2730
return "";
31+
}
2832
}
33+
return "";
2934
};
3035

36+
// TODO webpack 5: loc must be DependencyLocation
37+
/**
38+
* @param {DependencyLocation|SourcePosition|string} loc ___location
39+
* @returns {string} formatted ___location
40+
*/
3141
const formatLocation = loc => {
3242
if (loc === null) return "";
33-
const typeOfLoc = typeof loc;
34-
switch (typeOfLoc) {
35-
case "string":
36-
return loc;
37-
case "number":
38-
return `${loc}`;
39-
case "object":
40-
if (loc.start && loc.end) {
41-
if (
42-
typeof loc.start.line === "number" &&
43-
typeof loc.end.line === "number" &&
44-
typeof loc.end.column === "number" &&
45-
loc.start.line === loc.end.line
46-
) {
47-
return `${formatPosition(loc.start)}-${loc.end.column}`;
48-
} else {
49-
return `${formatPosition(loc.start)}-${formatPosition(loc.end)}`;
50-
}
51-
}
52-
if (loc.start) {
53-
return formatPosition(loc.start);
43+
// TODO webpack 5: Simplify this
44+
if (typeof loc === "string") return loc;
45+
if (typeof loc === "number") return `${loc}`;
46+
if (typeof loc === "object") {
47+
if ("start" in loc && loc.start && "end" in loc && loc.end) {
48+
if (
49+
typeof loc.start === "object" &&
50+
typeof loc.start.line === "number" &&
51+
typeof loc.end === "object" &&
52+
typeof loc.end.line === "number" &&
53+
typeof loc.end.column === "number" &&
54+
loc.start.line === loc.end.line
55+
) {
56+
return `${formatPosition(loc.start)}-${loc.end.column}`;
57+
} else {
58+
return `${formatPosition(loc.start)}-${formatPosition(loc.end)}`;
5459
}
55-
return formatPosition(loc);
56-
default:
57-
return "";
60+
}
61+
if ("start" in loc && loc.start) {
62+
return formatPosition(loc.start);
63+
}
64+
if ("name" in loc && "index" in loc) {
65+
return `${loc.name}[${loc.index}]`;
66+
}
67+
if ("name" in loc) {
68+
return loc.name;
69+
}
70+
return formatPosition(loc);
5871
}
72+
return "";
5973
};
6074

6175
module.exports = formatLocation;

0 commit comments

Comments
 (0)