merge DependencyLocation and disallow string locations

This commit is contained in:
Tobias Koppers 2018-06-25 10:43:59 +02:00
parent 8ae803ba13
commit c774f030f1
8 changed files with 41 additions and 37 deletions

12
declarations.d.ts vendored
View File

@ -244,15 +244,3 @@ declare const $crossOriginLoading$;
declare const chunkId;
type TODO = any;
declare interface SourcePosition {
line: number;
column: number;
}
declare interface DependencyLocation {
name: string;
index: number;
start: SourcePosition;
end: SourcePosition;
}

View File

@ -8,6 +8,7 @@ const DependenciesBlock = require("./DependenciesBlock");
/** @typedef {import("./ChunkGroup")} ChunkGroup */
/** @typedef {import("./Module")} Module */
/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
/** @typedef {import("crypto").Hash} Hash */
/** @typedef {TODO} GroupOptions */

View File

@ -8,14 +8,14 @@ const WebpackError = require("./WebpackError");
/** @typedef {import("./Module.js")} Module */
/** @typedef {import("./Dependency.js").Loc} Loc */
/** @typedef {import("./Dependency.js").DependencyLocation} DependencyLocation */
class CommentCompilationWarning extends WebpackError {
/**
*
* @param {string} message warning message
* @param {Module} module affected module
* @param {Loc} loc affected lines of code
* @param {DependencyLocation} loc affected lines of code
*/
constructor(message, module, loc) {
super(message);

View File

@ -47,6 +47,7 @@ const ModuleDependency = require("./dependencies/ModuleDependency");
/** @typedef {import("./dependencies/DllEntryDependency")} DllEntryDependency */
/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
/** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
// TODO use @callback
/** @typedef {{[assetName: string]: Source}} CompilationAssets */

View File

@ -14,24 +14,36 @@ const DependencyReference = require("./dependencies/DependencyReference");
/**
* @typedef {Object} DependencyTemplate
* @property {function(Dependency, Source, RuntimeTemplate, Map<Function, DependencyTemplate>): void} apply
*/
/** @typedef {Object} Position
/** @typedef {Object} SourcePosition
* @property {number} column
* @property {number} line
*/
/** @typedef {Object} Loc
* @property {Position} start
* @property {Position} end
/** @typedef {Object} RealDependencyLocation
* @property {SourcePosition} start
* @property {SourcePosition} end
* @property {number=} index
*/
/** @typedef {Object} SynteticDependencyLocation
* @property {string} name
* @property {number} index
*/
/** @typedef {SynteticDependencyLocation|RealDependencyLocation} DependencyLocation */
class Dependency {
constructor() {
/** @type {Module|null} */
this.module = null;
// TODO remove in webpack 5
/** @type {boolean} */
this.weak = false;
/** @type {boolean} */
this.optional = false;
/** @type {DependencyLocation} */
this.loc = undefined;
}

View File

@ -36,7 +36,10 @@ class DllEntryPlugin {
new DllEntryDependency(
this.entries.map((e, idx) => {
const dep = new SingleEntryDependency(e);
dep.loc = `${this.name}:${idx}`;
dep.loc = {
name: this.name,
index: idx
};
return dep;
}),
this.name

View File

@ -66,7 +66,10 @@ class MultiEntryPlugin {
const dep = new SingleEntryDependency(e);
// Because entrypoints are not dependencies found in an
// existing module, we give it a synthetic id
dep.loc = `${name}:${100000 + idx}`;
dep.loc = {
name,
index: idx
};
return dep;
}),
name

View File

@ -4,23 +4,13 @@
*/
"use strict";
/**
* @typedef {Object} LineAndColumn
* @property {number=} line
* @property {number=} column
*/
/**
* @typedef {Object} NodeLocation
* @property {LineAndColumn=} start
* @property {LineAndColumn=} end
* @property {number=} index
*/
/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
// TODO webpack 5 remove string type from a and b
/**
* Compare two locations
* @param {string|NodeLocation} a A location node
* @param {string|NodeLocation} b A location node
* @param {string|DependencyLocation} a A location node
* @param {string|DependencyLocation} b A location node
* @returns {-1|0|1} sorting comparator value
*/
module.exports = (a, b) => {
@ -38,7 +28,7 @@ module.exports = (a, b) => {
if (typeof b === "string") {
return -1;
} else if (typeof b === "object") {
if (a.start && b.start) {
if ("start" in a && "start" in b) {
const ap = a.start;
const bp = b.start;
if (ap.line < bp.line) return -1;
@ -46,8 +36,14 @@ module.exports = (a, b) => {
if (ap.column < bp.column) return -1;
if (ap.column > bp.column) return 1;
}
if (a.index < b.index) return -1;
if (a.index > b.index) return 1;
if ("name" in a && "name" in b) {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
}
if ("index" in a && "index" in b) {
if (a.index < b.index) return -1;
if (a.index > b.index) return 1;
}
return 0;
} else {
return 0;