Skip to content

Commit

Permalink
chore(types): add Compiler and Compilation type support
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLarkInn authored and sokra committed May 15, 2018
1 parent 687c038 commit 761d73b
Show file tree
Hide file tree
Showing 28 changed files with 1,378 additions and 633 deletions.
93 changes: 93 additions & 0 deletions declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,99 @@ declare namespace NodeJS {
}
}

declare module "neo-async" {
export interface Dictionary<T> {
[key: string]: T;
}
export type IterableCollection<T> = T[] | IterableIterator<T> | Dictionary<T>;

export interface ErrorCallback<T> {
(err?: T): void;
}
export interface AsyncBooleanResultCallback<E> {
(err?: E, truthValue?: boolean): void;
}
export interface AsyncResultCallback<T, E> {
(err?: E, result?: T): void;
}
export interface AsyncResultArrayCallback<T, E> {
(err?: E, results?: Array<T | undefined>): void;
}
export interface AsyncResultObjectCallback<T, E> {
(err: E | undefined, results: Dictionary<T | undefined>): void;
}

export interface AsyncFunction<T, E> {
(callback: (err?: E, result?: T) => void): void;
}
export interface AsyncFunctionEx<T, E> {
(callback: (err?: E, ...results: T[]) => void): void;
}
export interface AsyncIterator<T, E> {
(item: T, callback: ErrorCallback<E>): void;
}
export interface AsyncForEachOfIterator<T, E> {
(item: T, key: number | string, callback: ErrorCallback<E>): void;
}
export interface AsyncResultIterator<T, R, E> {
(item: T, callback: AsyncResultCallback<R, E>): void;
}
export interface AsyncMemoIterator<T, R, E> {
(memo: R | undefined, item: T, callback: AsyncResultCallback<R, E>): void;
}
export interface AsyncBooleanIterator<T, E> {
(item: T, callback: AsyncBooleanResultCallback<E>): void;
}

export interface AsyncWorker<T, E> {
(task: T, callback: ErrorCallback<E>): void;
}
export interface AsyncVoidFunction<E> {
(callback: ErrorCallback<E>): void;
}

export type AsyncAutoTasks<R extends Dictionary<any>, E> = {
[K in keyof R]: AsyncAutoTask<R[K], R, E>
};
export type AsyncAutoTask<R1, R extends Dictionary<any>, E> =
| AsyncAutoTaskFunctionWithoutDependencies<R1, E>
| (keyof R | AsyncAutoTaskFunction<R1, R, E>)[];
export interface AsyncAutoTaskFunctionWithoutDependencies<R1, E> {
(cb: AsyncResultCallback<R1, E> | ErrorCallback<E>): void;
}
export interface AsyncAutoTaskFunction<R1, R extends Dictionary<any>, E> {
(results: R, cb: AsyncResultCallback<R1, E> | ErrorCallback<E>): void;
}

export function each<T, E>(
arr: IterableCollection<T>,
iterator: AsyncIterator<T, E>,
callback?: ErrorCallback<E>
): void;

export function map<T, R, E>(
arr: T[] | IterableIterator<T>,
iterator: AsyncResultIterator<T, R, E>,
callback?: AsyncResultArrayCallback<R, E>
): void;
export function map<T, R, E>(
arr: Dictionary<T>,
iterator: AsyncResultIterator<T, R, E>,
callback?: AsyncResultArrayCallback<R, E>
): void;

export function parallel<T, E>(
tasks: Array<AsyncFunction<T, E>>,
callback?: AsyncResultArrayCallback<T, E>
): void;
export function parallel<T, E>(
tasks: Dictionary<AsyncFunction<T, E>>,
callback?: AsyncResultObjectCallback<T, E>
): void;

export const forEach: typeof each;
}

// There are no typings for chrome-trace-event
declare module "chrome-trace-event" {
interface Event {
Expand Down
58 changes: 58 additions & 0 deletions lib/AsyncDependenciesBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,34 @@
"use strict";
const DependenciesBlock = require("./DependenciesBlock");

/**
* @typedef {import("./ChunkGroup")} ChunkGroup
* @typedef {import("./Module")} Module
* @typedef {import("crypto").Hash} Hash
* @typedef {TODO} GroupOptions
*
*/

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

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

module.exports = class AsyncDependenciesBlock extends DependenciesBlock {
/**
* @param {GroupOptions} groupOptions options for the group
* @param {Module} module the Module object
* @param {SourceLocation=} loc the line of code
* @param {TODO=} request the request
*/
constructor(groupOptions, module, loc, request) {
super();
if (typeof groupOptions === "string") {
Expand All @@ -14,28 +41,50 @@ module.exports = class AsyncDependenciesBlock extends DependenciesBlock {
groupOptions = { name: undefined };
}
this.groupOptions = groupOptions;
/** @type {ChunkGroup=} */
this.chunkGroup = undefined;
this.module = module;
this.loc = loc;
this.request = request;
/** @type {DependenciesBlock} */
this.parent = undefined;
}

/**
* @returns {string} The name of the chunk
*/
get chunkName() {
return this.groupOptions.name;
}

/**
* @param {string} value The new chunk name
* @returns {void}
*/
set chunkName(value) {
this.groupOptions.name = value;
}

/**
* @returns {never} this throws and should never be called
*/
get chunks() {
throw new Error("Moved to AsyncDependenciesBlock.chunkGroup");
}

/**
* @param {never} value setter value
* @returns {never} this is going to throw therefore we should throw type
* assertions by returning never
*/
set chunks(value) {
throw new Error("Moved to AsyncDependenciesBlock.chunkGroup");
}

/**
* @param {Hash} hash the hash used to track block changes, from "crypto" module
* @returns {void}
*/
updateHash(hash) {
hash.update(JSON.stringify(this.groupOptions));
hash.update(
Expand All @@ -50,16 +99,25 @@ module.exports = class AsyncDependenciesBlock extends DependenciesBlock {
super.updateHash(hash);
}

/**
* @returns {void}
*/
disconnect() {
this.chunkGroup = undefined;
super.disconnect();
}

/**
* @returns {void}
*/
unseal() {
this.chunkGroup = undefined;
super.unseal();
}

/**
* @returns {void}
*/
sortItems() {
super.sortItems();
}
Expand Down
22 changes: 22 additions & 0 deletions lib/Chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const ERR_CHUNK_INITIAL =
/** @typedef {import("./ChunkGroup")} ChunkGroup */
/** @typedef {import("./ModuleReason.js")} ModuleReason */
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("crypto").Hash} Hash */

/**
* @typedef {Object} Identifiable an object who contains an identifier function property
Expand Down Expand Up @@ -471,6 +472,11 @@ class Chunk {
return this.addMultiplierAndOverhead(this.modulesSize(), options);
}

/**
* @param {Chunk} otherChunk the other chunk
* @param {TODO} options the options for this function
* @returns {number | false} the size, or false if it can't be integrated
*/
integratedSize(otherChunk, options) {
// Chunk if it's possible to integrate this chunk
if (!this.canBeIntegrated(otherChunk)) {
Expand Down Expand Up @@ -500,6 +506,9 @@ class Chunk {
this.sortModules();
}

/**
* @returns {Set<Chunk>} a set of all the async chunks
*/
getAllAsyncChunks() {
const queue = new Set();
const chunks = new Set();
Expand All @@ -522,6 +531,10 @@ class Chunk {
return chunks;
}

/**
* @param {Hash} realHash the hash for the chunk maps
* @returns {{ hash: TODO, contentHash: TODO, name: TODO }} the chunk map information
*/
getChunkMaps(realHash) {
const chunkHashMap = Object.create(null);
const chunkContentHashMap = Object.create(null);
Expand All @@ -544,6 +557,9 @@ class Chunk {
};
}

/**
* @returns {Record<string, Array<Set<TODO>>>} a record object of names to lists of child ids(?)
*/
getChildIdsByOrders() {
const lists = new Map();
for (const group of this.groupsIterable) {
Expand Down Expand Up @@ -600,6 +616,12 @@ class Chunk {
return chunkMaps;
}

/** @typedef {(module: Module) => true} FilterFn */

/**
* @param {FilterFn} filterFn function used to filter modules
* @returns {TODO} module map information
*/
getChunkModuleMaps(filterFn) {
const chunkModuleIdMap = Object.create(null);
const chunkModuleHashMap = Object.create(null);
Expand Down
Loading

0 comments on commit 761d73b

Please sign in to comment.