Skip to content

Commit

Permalink
Merge pull request #7700 from webpack/bugfix/loc-in-single-entry-and-…
Browse files Browse the repository at this point in the history
…loader-plugin

add types and fix incorrect loc type
  • Loading branch information
sokra committed Jul 11, 2018
2 parents 3366421 + b93225a commit 6f72243
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 45 deletions.
4 changes: 2 additions & 2 deletions lib/Dependency.js
Expand Up @@ -18,13 +18,13 @@ const DependencyReference = require("./dependencies/DependencyReference");
*/

/** @typedef {Object} SourcePosition
* @property {number} column
* @property {number} line
* @property {number=} column
*/

/** @typedef {Object} RealDependencyLocation
* @property {SourcePosition} start
* @property {SourcePosition} end
* @property {SourcePosition=} end
* @property {number=} index
*/

Expand Down
7 changes: 6 additions & 1 deletion lib/SingleEntryPlugin.js
Expand Up @@ -48,9 +48,14 @@ class SingleEntryPlugin {
);
}

/**
* @param {string} entry entry request
* @param {string} name entry name
* @returns {SingleEntryDependency} the dependency
*/
static createDependency(entry, name) {
const dep = new SingleEntryDependency(entry);
dep.loc = name;
dep.loc = { name };
return dep;
}
}
Expand Down
19 changes: 18 additions & 1 deletion lib/dependencies/LoaderPlugin.js
Expand Up @@ -7,6 +7,16 @@
const LoaderDependency = require("./LoaderDependency");
const NormalModule = require("../NormalModule");

/** @typedef {import("../Module")} Module */

/**
* @callback LoadModuleCallback
* @param {Error=} err error object
* @param {string=} source source code
* @param {object=} map source map
* @param {Module=} module loaded module if successful
*/

class LoaderPlugin {
apply(compiler) {
compiler.hooks.compilation.tap(
Expand All @@ -23,9 +33,16 @@ class LoaderPlugin {
compilation.hooks.normalModuleLoader.tap(
"LoaderPlugin",
(loaderContext, module) => {
/**
* @param {string} request the request string to load the module from
* @param {LoadModuleCallback} callback callback returning the loaded module or error
* @returns {void}
*/
loaderContext.loadModule = (request, callback) => {
const dep = new LoaderDependency(request);
dep.loc = request;
dep.loc = {
name: request
};
const factory = compilation.dependencyFactories.get(
dep.constructor
);
Expand Down
96 changes: 55 additions & 41 deletions lib/formatLocation.js
Expand Up @@ -5,57 +5,71 @@

"use strict";

/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
/** @typedef {import("./Dependency").SourcePosition} SourcePosition */

// TODO webpack 5: pos must be SourcePosition
/**
* @param {SourcePosition|DependencyLocation|string} pos position
* @returns {string} formatted position
*/
const formatPosition = pos => {
if (pos === null) return "";
const typeOfPos = typeof pos;
switch (typeOfPos) {
case "string":
return pos;
case "number":
return `${pos}`;
case "object":
if (typeof pos.line === "number" && typeof pos.column === "number") {
return `${pos.line}:${pos.column}`;
} else if (typeof pos.line === "number") {
return `${pos.line}:?`;
} else if (typeof pos.index === "number") {
return `+${pos.index}`;
} else {
return "";
}
default:
// TODO webpack 5: Simplify this
if (typeof pos === "string") return pos;
if (typeof pos === "number") return `${pos}`;
if (typeof pos === "object") {
if ("line" in pos && "column" in pos) {
return `${pos.line}:${pos.column}`;
} else if ("line" in pos) {
return `${pos.line}:?`;
} else if ("index" in pos) {
// TODO webpack 5 remove this case
return `+${pos.index}`;
} else {
return "";
}
}
return "";
};

// TODO webpack 5: loc must be DependencyLocation
/**
* @param {DependencyLocation|SourcePosition|string} loc location
* @returns {string} formatted location
*/
const formatLocation = loc => {
if (loc === null) return "";
const typeOfLoc = typeof loc;
switch (typeOfLoc) {
case "string":
return loc;
case "number":
return `${loc}`;
case "object":
if (loc.start && loc.end) {
if (
typeof loc.start.line === "number" &&
typeof loc.end.line === "number" &&
typeof loc.end.column === "number" &&
loc.start.line === loc.end.line
) {
return `${formatPosition(loc.start)}-${loc.end.column}`;
} else {
return `${formatPosition(loc.start)}-${formatPosition(loc.end)}`;
}
}
if (loc.start) {
return formatPosition(loc.start);
// TODO webpack 5: Simplify this
if (typeof loc === "string") return loc;
if (typeof loc === "number") return `${loc}`;
if (typeof loc === "object") {
if ("start" in loc && loc.start && "end" in loc && loc.end) {
if (
typeof loc.start === "object" &&
typeof loc.start.line === "number" &&
typeof loc.end === "object" &&
typeof loc.end.line === "number" &&
typeof loc.end.column === "number" &&
loc.start.line === loc.end.line
) {
return `${formatPosition(loc.start)}-${loc.end.column}`;
} else {
return `${formatPosition(loc.start)}-${formatPosition(loc.end)}`;
}
return formatPosition(loc);
default:
return "";
}
if ("start" in loc && loc.start) {
return formatPosition(loc.start);
}
if ("name" in loc && "index" in loc) {
return `${loc.name}[${loc.index}]`;
}
if ("name" in loc) {
return loc.name;
}
return formatPosition(loc);
}
return "";
};

module.exports = formatLocation;

0 comments on commit 6f72243

Please sign in to comment.