Skip to content

Commit

Permalink
run inherit types tool on existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jul 21, 2018
1 parent 0e6d505 commit 8614c75
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/DelegatedModule.js
Expand Up @@ -12,6 +12,7 @@ const DelegatedSourceDependency = require("./dependencies/DelegatedSourceDepende
const DelegatedExportsDependency = require("./dependencies/DelegatedExportsDependency");

/** @typedef {import("./dependencies/ModuleDependency")} ModuleDependency */
/** @typedef {import("./util/createHash").Hash} Hash */

class DelegatedModule extends Module {
constructor(sourceRequest, data, type, userRequest, originalRequest) {
Expand Down Expand Up @@ -99,6 +100,10 @@ class DelegatedModule extends Module {
return 42;
}

/**
* @param {Hash} hash the hash used to track dependencies
* @returns {void}
*/
updateHash(hash) {
hash.update(this.type);
hash.update(JSON.stringify(this.request));
Expand Down
2 changes: 1 addition & 1 deletion lib/DependenciesBlock.js
Expand Up @@ -73,7 +73,7 @@ class DependenciesBlock {
}

/**
* @param {Hash} hash the hash used to track dependencies, from "crypto" module
* @param {Hash} hash the hash used to track dependencies
* @returns {void}
*/
updateHash(hash) {
Expand Down
6 changes: 6 additions & 0 deletions lib/DllModule.js
Expand Up @@ -7,6 +7,8 @@
const { RawSource } = require("webpack-sources");
const Module = require("./Module");

/** @typedef {import("./util/createHash").Hash} Hash */

class DllModule extends Module {
constructor(context, dependencies, name, type) {
super("javascript/dynamic", context);
Expand Down Expand Up @@ -44,6 +46,10 @@ class DllModule extends Module {
return 12;
}

/**
* @param {Hash} hash the hash used to track dependencies
* @returns {void}
*/
updateHash(hash) {
hash.update("dll module");
hash.update(this.name || "");
Expand Down
6 changes: 6 additions & 0 deletions lib/ExternalModule.js
Expand Up @@ -9,6 +9,8 @@ const Module = require("./Module");
const WebpackMissingModule = require("./dependencies/WebpackMissingModule");
const Template = require("./Template");

/** @typedef {import("./util/createHash").Hash} Hash */

class ExternalModule extends Module {
constructor(request, type, userRequest) {
super("javascript/dynamic", null);
Expand Down Expand Up @@ -148,6 +150,10 @@ class ExternalModule extends Module {
return 42;
}

/**
* @param {Hash} hash the hash used to track dependencies
* @returns {void}
*/
updateHash(hash) {
hash.update(this.externalType);
hash.update(JSON.stringify(this.request));
Expand Down
14 changes: 11 additions & 3 deletions lib/Generator.js
Expand Up @@ -4,9 +4,10 @@
*/
"use strict";

/** @typedef {import("./Module")} Module */
/** @typedef {import("./NormalModule")} NormalModule */
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./Dependency").DependencyTemplate} DependencyTemplate */

/**
*
Expand All @@ -18,8 +19,8 @@ class Generator {

/**
* @abstract
* @param {Module} module module for which the code should be generated
* @param {Map<Function, TODO>} dependencyTemplates mapping from dependencies to templates
* @param {NormalModule} module module for which the code should be generated
* @param {Map<Function, DependencyTemplate>} dependencyTemplates mapping from dependencies to templates
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @param {string} type which kind of code should be generated
* @returns {Source} generated code
Expand All @@ -35,6 +36,13 @@ class ByTypeGenerator extends Generator {
this.map = map;
}

/**
* @param {NormalModule} module module for which the code should be generated
* @param {Map<Function, DependencyTemplate>} dependencyTemplates mapping from dependencies to templates
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @param {string} type which kind of code should be generated
* @returns {Source} generated code
*/
generate(module, dependencyTemplates, runtimeTemplate, type) {
const generator = this.map[type];
if (!generator) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Module.js
Expand Up @@ -14,6 +14,7 @@ const Template = require("./Template");
/** @typedef {import("./Chunk")} Chunk */
/** @typedef {import("./RequestShortener")} RequestShortener */
/** @typedef {import("./WebpackError")} WebpackError */
/** @typedef {import("./util/createHash").Hash} Hash */

const EMPTY_RESOLVE_OPTIONS = {};

Expand Down Expand Up @@ -298,6 +299,10 @@ class Module extends DependenciesBlock {
return true;
}

/**
* @param {Hash} hash the hash used to track dependencies
* @returns {void}
*/
updateHash(hash) {
hash.update(`${this.id}`);
hash.update(JSON.stringify(this.usedExports));
Expand Down
6 changes: 6 additions & 0 deletions lib/MultiModule.js
Expand Up @@ -8,6 +8,8 @@ const Module = require("./Module");
const Template = require("./Template");
const { RawSource } = require("webpack-sources");

/** @typedef {import("./util/createHash").Hash} Hash */

class MultiModule extends Module {
constructor(context, dependencies, name) {
super("javascript/dynamic", context);
Expand Down Expand Up @@ -45,6 +47,10 @@ class MultiModule extends Module {
return 16 + this.dependencies.length * 12;
}

/**
* @param {Hash} hash the hash used to track dependencies
* @returns {void}
*/
updateHash(hash) {
hash.update("multi module");
hash.update(this.name || "");
Expand Down
6 changes: 6 additions & 0 deletions lib/NormalModule.js
Expand Up @@ -24,6 +24,8 @@ const ModuleWarning = require("./ModuleWarning");
const createHash = require("./util/createHash");
const contextify = require("./util/identifier").contextify;

/** @typedef {import("./util/createHash").Hash} Hash */

const asString = buf => {
if (Buffer.isBuffer(buf)) {
return buf.toString("utf-8");
Expand Down Expand Up @@ -527,6 +529,10 @@ class NormalModule extends Module {
return this._source ? this._source.size() : -1;
}

/**
* @param {Hash} hash the hash used to track dependencies
* @returns {void}
*/
updateHash(hash) {
hash.update(this._buildHash);
super.updateHash(hash);
Expand Down
12 changes: 12 additions & 0 deletions lib/RuntimeTemplate.js
Expand Up @@ -6,6 +6,8 @@

const Template = require("./Template");

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

module.exports = class RuntimeTemplate {
constructor(outputOptions, requestShortener) {
this.outputOptions = outputOptions || {};
Expand Down Expand Up @@ -188,6 +190,16 @@ module.exports = class RuntimeTemplate {
return `${promise || "Promise.resolve()"}.then(${getModuleFunction})`;
}

/**
*
* @param {Object} options options object
* @param {boolean=} options.update whether a new variable should be created or the existing one updated
* @param {Module} options.module the module
* @param {string} options.request the request that should be printed as comment
* @param {string} options.importVar name of the import variable
* @param {Module} options.originModule module in which the statement is emitted
* @returns {string} the import statement
*/
importStatement({ update, module, request, importVar, originModule }) {
if (!module) {
return this.missingModuleStatement({
Expand Down
5 changes: 5 additions & 0 deletions lib/optimize/ConcatenatedModule.js
Expand Up @@ -21,6 +21,7 @@ const createHash = require("../util/createHash");

/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../util/createHash").Hash} Hash */

/**
* @typedef {Object} ConcatenationEntry
Expand Down Expand Up @@ -1180,6 +1181,10 @@ class ConcatenatedModule extends Module {
return nameWithNumber;
}

/**
* @param {Hash} hash the hash used to track dependencies
* @returns {void}
*/
updateHash(hash) {
for (const info of this._orderedConcatenationList) {
switch (info.type) {
Expand Down
18 changes: 16 additions & 2 deletions lib/wasm/WebAssemblyGenerator.js
Expand Up @@ -21,6 +21,10 @@ const WebAssemblyExportImportedDependency = require("../dependencies/WebAssembly

/** @typedef {import("../Module")} Module */
/** @typedef {import("./WebAssemblyUtils").UsedWasmDependency} UsedWasmDependency */
/** @typedef {import("../NormalModule")} NormalModule */
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../Dependency").DependencyTemplate} DependencyTemplate */

/**
* @typedef {(ArrayBuffer) => ArrayBuffer} ArrayBufferTransform
Expand Down Expand Up @@ -366,7 +370,14 @@ class WebAssemblyGenerator extends Generator {
this.options = options;
}

generate(module) {
/**
* @param {NormalModule} module module for which the code should be generated
* @param {Map<Function, DependencyTemplate>} dependencyTemplates mapping from dependencies to templates
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @param {string} type which kind of code should be generated
* @returns {Source} generated code
*/
generate(module, dependencyTemplates, runtimeTemplate, type) {
let bin = module.originalSource().source();
bin = preprocess(bin);

Expand Down Expand Up @@ -398,7 +409,10 @@ class WebAssemblyGenerator extends Generator {
const externalExports = new Set(
module.dependencies
.filter(d => d instanceof WebAssemblyExportImportedDependency)
.map(d => d.exportName)
.map(d => {
const wasmDep = /** @type {WebAssemblyExportImportedDependency} */ (d);
return wasmDep.exportName;
})
);

/** @type {t.Instruction[]} */
Expand Down
18 changes: 16 additions & 2 deletions lib/wasm/WebAssemblyJavascriptGenerator.js
Expand Up @@ -10,8 +10,20 @@ const { RawSource } = require("webpack-sources");
const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");

/** @typedef {import("../NormalModule")} NormalModule */
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../Dependency").DependencyTemplate} DependencyTemplate */

class WebAssemblyJavascriptGenerator extends Generator {
generate(module, dependencyTemplates, runtimeTemplate) {
/**
* @param {NormalModule} module module for which the code should be generated
* @param {Map<Function, DependencyTemplate>} dependencyTemplates mapping from dependencies to templates
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @param {string} type which kind of code should be generated
* @returns {Source} generated code
*/
generate(module, dependencyTemplates, runtimeTemplate, type) {
const initIdentifer = Array.isArray(module.usedExports)
? Template.numberToIdentifer(module.usedExports.length)
: "__webpack_init__";
Expand All @@ -21,6 +33,7 @@ class WebAssemblyJavascriptGenerator extends Generator {
const initParams = [];
let index = 0;
for (const dep of module.dependencies) {
const depAsAny = /** @type {any} */ (dep);
if (dep.module) {
let importData = importedModules.get(dep.module);
if (importData === undefined) {
Expand All @@ -29,7 +42,8 @@ class WebAssemblyJavascriptGenerator extends Generator {
(importData = {
importVar: `m${index}`,
index,
request: dep.userRequest,
request:
"userRequest" in depAsAny ? depAsAny.userRequest : undefined,
names: new Set(),
reexports: []
})
Expand Down

0 comments on commit 8614c75

Please sign in to comment.