add types and fix incorrect loc type

fix formatLocation for types
This commit is contained in:
Tobias Koppers 2018-07-10 17:18:45 +02:00
parent 9f0056b10d
commit b93225a6a1
4 changed files with 82 additions and 46 deletions

View File

@ -18,19 +18,19 @@ 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
*/
/** @typedef {Object} SynteticDependencyLocation
* @property {string} name
* @property {number} index
* @property {number=} index
*/
/** @typedef {SynteticDependencyLocation|RealDependencyLocation} DependencyLocation */

View File

@ -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;
}
}

View File

@ -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(
@ -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
);

View File

@ -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)}`;
}
// 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)}`;
}
if (loc.start) {
return formatPosition(loc.start);
}
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;