Skip to content

Commit

Permalink
more type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Dec 29, 2021
1 parent dd95b3f commit 1a6b965
Show file tree
Hide file tree
Showing 23 changed files with 88 additions and 79 deletions.
16 changes: 8 additions & 8 deletions src/Bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import { basename, isAbsolute } from './utils/path';
import { timeEnd, timeStart } from './utils/timers';

export default class Bundle {
private facadeChunkByModule = new Map<Module, Chunk>();
private includedNamespaces = new Set<Module>();
private readonly facadeChunkByModule = new Map<Module, Chunk>();
private readonly includedNamespaces = new Set<Module>();

constructor(
private readonly outputOptions: NormalizedOutputOptions,
Expand Down Expand Up @@ -82,7 +82,7 @@ export default class Bundle {
}

private async addFinalizedChunksToBundle(
chunks: Chunk[],
chunks: readonly Chunk[],
inputBase: string,
addons: Addons,
outputBundle: OutputBundleWithPlaceholders,
Expand Down Expand Up @@ -122,11 +122,11 @@ export default class Bundle {
}

private assignChunkIds(
chunks: Chunk[],
chunks: readonly Chunk[],
inputBase: string,
addons: Addons,
bundle: OutputBundleWithPlaceholders
) {
): void {
const entryChunks: Chunk[] = [];
const otherChunks: Chunk[] = [];
for (const chunk of chunks) {
Expand All @@ -137,7 +137,7 @@ export default class Bundle {
}

// make sure entry chunk names take precedence with regard to deconflicting
const chunksForNaming: Chunk[] = entryChunks.concat(otherChunks);
const chunksForNaming = entryChunks.concat(otherChunks);
for (const chunk of chunksForNaming) {
if (this.outputOptions.file) {
chunk.id = basename(this.outputOptions.file);
Expand Down Expand Up @@ -241,7 +241,7 @@ export default class Bundle {
}

private prerenderChunks(
chunks: Chunk[],
chunks: readonly Chunk[],
inputBase: string,
snippets: GenerateCodeSnippets
): void {
Expand All @@ -254,7 +254,7 @@ export default class Bundle {
}
}

function getAbsoluteEntryModulePaths(chunks: Chunk[]): string[] {
function getAbsoluteEntryModulePaths(chunks: readonly Chunk[]): string[] {
const absoluteEntryModulePaths: string[] = [];
for (const chunk of chunks) {
for (const entryModule of chunk.entryModules) {
Expand Down
15 changes: 9 additions & 6 deletions src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ export default class Chunk {
return hash.digest('hex').substr(0, 8);
}

private ensureReexportsAreAvailableForModule(module: Module) {
private ensureReexportsAreAvailableForModule(module: Module): void {
const map = module.getExportNamesByVariable();
for (const exportedVariable of map.keys()) {
const isSynthetic = exportedVariable instanceof SyntheticNamedExportVariable;
Expand All @@ -910,7 +910,10 @@ export default class Chunk {
}
}

private finaliseDynamicImports(options: NormalizedOutputOptions, snippets: GenerateCodeSnippets) {
private finaliseDynamicImports(
options: NormalizedOutputOptions,
snippets: GenerateCodeSnippets
): void {
const stripKnownJsExtensions = options.format === 'amd';
for (const [module, code] of this.renderedModuleSources) {
for (const { node, resolution } of module.dynamicImports) {
Expand Down Expand Up @@ -1218,7 +1221,7 @@ export default class Chunk {
return relativePath.startsWith('../') ? relativePath : './' + relativePath;
}

private inlineChunkDependencies(chunk: Chunk) {
private inlineChunkDependencies(chunk: Chunk): void {
for (const dep of chunk.dependencies) {
if (this.dependencies.has(dep)) continue;
this.dependencies.add(dep);
Expand All @@ -1228,7 +1231,7 @@ export default class Chunk {
}
}

private prepareModulesForRendering(snippets: GenerateCodeSnippets) {
private prepareModulesForRendering(snippets: GenerateCodeSnippets): void {
const accessedGlobalsByScope = this.accessedGlobalsByScope;
for (const module of this.orderedModules) {
for (const { node, resolution } of module.dynamicImports) {
Expand Down Expand Up @@ -1268,7 +1271,7 @@ export default class Chunk {
}
}

private setExternalRenderPaths(options: NormalizedOutputOptions, inputBase: string) {
private setExternalRenderPaths(options: NormalizedOutputOptions, inputBase: string): void {
for (const dependency of [...this.dependencies, ...this.dynamicDependencies]) {
if (dependency instanceof ExternalModule) {
dependency.setRenderPath(options, inputBase);
Expand Down Expand Up @@ -1347,7 +1350,7 @@ export default class Chunk {
);
}

private setUpChunkImportsAndExportsForModule(module: Module) {
private setUpChunkImportsAndExportsForModule(module: Module): void {
const moduleImports = new Set(module.imports);
// when we are not preserving modules, we need to make all namespace variables available for
// rendering the namespace object
Expand Down
14 changes: 7 additions & 7 deletions src/ExternalModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { printQuotedStringList } from './utils/printStringList';
import relativeId from './utils/relativeId';

export default class ExternalModule {
declarations: { [name: string]: ExternalVariable } = Object.create(null);
readonly declarations: { [name: string]: ExternalVariable } = Object.create(null);
defaultVariableName = '';
dynamicImporters: string[] = [];
readonly dynamicImporters: string[] = [];
execIndex = Infinity;
exportedVariables = new Map<ExternalVariable, string>();
importers: string[] = [];
info: ModuleInfo;
readonly exportedVariables = new Map<ExternalVariable, string>();
readonly importers: string[] = [];
readonly info: ModuleInfo;
mostCommonSuggestion = 0;
nameSuggestions: { [name: string]: number } = Object.create(null);
readonly nameSuggestions: { [name: string]: number } = Object.create(null);
namespaceVariableName = '';
reexported = false;
renderPath: string = undefined as never;
Expand All @@ -33,7 +33,7 @@ export default class ExternalModule {
public readonly id: string,
hasModuleSideEffects: boolean | 'no-treeshake',
meta: CustomPluginOptions,
public renormalizeRenderPath: boolean
public readonly renormalizeRenderPath: boolean
) {
this.suggestedVariableName = makeLegal(id.split(/[\\/]/).pop()!);

Expand Down
10 changes: 5 additions & 5 deletions src/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { timeEnd, timeStart } from './utils/timers';
import { markModuleAndImpureDependenciesAsExecuted } from './utils/traverseStaticDependencies';

function normalizeEntryModules(
entryModules: string[] | Record<string, string>
entryModules: readonly string[] | Record<string, string>
): UnresolvedModule[] {
if (Array.isArray(entryModules)) {
return entryModules.map(id => ({
Expand Down Expand Up @@ -57,7 +57,7 @@ export default class Graph {
watchFiles: Record<string, true> = Object.create(null);
watchMode = false;

private externalModules: ExternalModule[] = [];
private readonly externalModules: ExternalModule[] = [];
private implicitEntryModules: Module[] = [];
private modules: Module[] = [];
private declare pluginCache?: Record<string, SerializablePluginCache>;
Expand Down Expand Up @@ -178,7 +178,7 @@ export default class Graph {
}
}

private includeStatements() {
private includeStatements(): void {
for (const module of [...this.entryModules, ...this.implicitEntryModules]) {
markModuleAndImpureDependenciesAsExecuted(module);
}
Expand Down Expand Up @@ -221,7 +221,7 @@ export default class Graph {
}
}

private sortModules() {
private sortModules(): void {
const { orderedModules, cyclePaths } = analyseModuleExecution(this.entryModules);
for (const cyclePath of cyclePaths) {
this.options.onwarn({
Expand All @@ -238,7 +238,7 @@ export default class Graph {
this.warnForMissingExports();
}

private warnForMissingExports() {
private warnForMissingExports(): void {
for (const module of this.modules) {
for (const importDescription of Object.values(module.importDescriptions)) {
if (
Expand Down
8 changes: 4 additions & 4 deletions src/ast/scopes/ChildScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export default class ChildScope extends Scope {
addUsedOutsideNames(
usedNames: Set<string>,
format: InternalModuleFormat,
exportNamesByVariable: Map<Variable, string[]>,
accessedGlobalsByScope: Map<ChildScope, Set<string>>
exportNamesByVariable: ReadonlyMap<Variable, string[]>,
accessedGlobalsByScope: ReadonlyMap<ChildScope, Set<string>>
): void {
for (const variable of this.accessedOutsideVariables.values()) {
if (variable.included) {
Expand All @@ -76,8 +76,8 @@ export default class ChildScope extends Scope {

deconflict(
format: InternalModuleFormat,
exportNamesByVariable: Map<Variable, string[]>,
accessedGlobalsByScope: Map<ChildScope, Set<string>>
exportNamesByVariable: ReadonlyMap<Variable, string[]>,
accessedGlobalsByScope: ReadonlyMap<ChildScope, Set<string>>
): void {
const usedNames = new Set<string>();
this.addUsedOutsideNames(usedNames, format, exportNamesByVariable, accessedGlobalsByScope);
Expand Down
4 changes: 2 additions & 2 deletions src/ast/scopes/ModuleScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export default class ModuleScope extends ChildScope {

deconflict(
format: InternalModuleFormat,
exportNamesByVariable: Map<Variable, string[]>,
accessedGlobalsByScope: Map<ChildScope, Set<string>>
exportNamesByVariable: ReadonlyMap<Variable, string[]>,
accessedGlobalsByScope: ReadonlyMap<ChildScope, Set<string>>
): void {
// all module level variables are already deconflicted when deconflicting the chunk
for (const scope of this.children)
Expand Down
4 changes: 2 additions & 2 deletions src/utils/PluginDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class PluginDriver {
constructor(
private readonly graph: Graph,
private readonly options: NormalizedInputOptions,
userPlugins: Plugin[],
userPlugins: readonly Plugin[],
pluginCache: Record<string, SerializablePluginCache> | undefined,
basePluginDriver?: PluginDriver
) {
Expand Down Expand Up @@ -120,7 +120,7 @@ export class PluginDriver {
}
}

public createOutputPluginDriver(plugins: Plugin[]): PluginDriver {
public createOutputPluginDriver(plugins: readonly Plugin[]): PluginDriver {
return new PluginDriver(this.graph, this.options, plugins, this.pluginCache, this);
}

Expand Down
18 changes: 9 additions & 9 deletions src/utils/collapseSourcemaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { error } from './error';
import { basename, dirname, relative, resolve } from './path';

class Source {
content: string;
filename: string;
readonly content: string;
readonly filename: string;
isOriginal = true;

constructor(filename: string, content: string) {
Expand All @@ -32,9 +32,9 @@ interface SourceMapSegmentObject {
}

class Link {
mappings: SourceMapSegment[][];
names: string[];
sources: (Source | Link)[];
readonly mappings: SourceMapSegment[][];
readonly names: string[];
readonly sources: (Source | Link)[];

constructor(
map: { mappings: SourceMapSegment[][]; names: string[] },
Expand Down Expand Up @@ -176,7 +176,7 @@ function getCollapsedSourcemap(
id: string,
originalCode: string,
originalSourcemap: ExistingDecodedSourceMap | null,
sourcemapChain: DecodedSourceMapOrMissing[],
sourcemapChain: readonly DecodedSourceMapOrMissing[],
linkMap: (source: Source | Link, map: DecodedSourceMapOrMissing) => Link
): Source | Link {
let source: Source | Link;
Expand All @@ -200,8 +200,8 @@ function getCollapsedSourcemap(
export function collapseSourcemaps(
file: string,
map: DecodedSourceMap,
modules: Module[],
bundleSourcemapChain: DecodedSourceMapOrMissing[],
modules: readonly Module[],
bundleSourcemapChain: readonly DecodedSourceMapOrMissing[],
excludeContent: boolean | undefined,
warn: WarningHandler
): SourceMap {
Expand Down Expand Up @@ -241,7 +241,7 @@ export function collapseSourcemap(
id: string,
originalCode: string,
originalSourcemap: ExistingDecodedSourceMap | null,
sourcemapChain: DecodedSourceMapOrMissing[],
sourcemapChain: readonly DecodedSourceMapOrMissing[],
warn: WarningHandler
): ExistingDecodedSourceMap | null {
if (!sourcemapChain.length) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/commondir.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from './path';

// ported from https://github.com/substack/node-commondir
export default function commondir(files: string[]): string {
export default function commondir(files: readonly string[]): string {
if (files.length === 0) return '/';
if (files.length === 1) return path.dirname(files[0]);
const commonSegments = files.slice(1).reduce((commonSegments, file) => {
Expand Down
18 changes: 9 additions & 9 deletions src/utils/deconflictChunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const DECONFLICT_IMPORTED_VARIABLES_BY_FORMAT: {
};

export function deconflictChunk(
modules: Module[],
modules: readonly Module[],
dependenciesToBeDeconflicted: DependenciesToBeDeconflicted,
imports: Set<Variable>,
usedNames: Set<string>,
Expand All @@ -51,9 +51,9 @@ export function deconflictChunk(
externalLiveBindings: boolean,
chunkByModule: Map<Module, Chunk>,
syntheticExports: Set<SyntheticNamedExportVariable>,
exportNamesByVariable: Map<Variable, string[]>,
accessedGlobalsByScope: Map<ChildScope, Set<string>>,
includedNamespaces: Set<Module>
exportNamesByVariable: ReadonlyMap<Variable, string[]>,
accessedGlobalsByScope: ReadonlyMap<ChildScope, Set<string>>,
includedNamespaces: ReadonlySet<Module>
): void {
const reversedModules = modules.slice().reverse();
for (const module of reversedModules) {
Expand Down Expand Up @@ -83,7 +83,7 @@ export function deconflictChunk(

function deconflictImportsEsmOrSystem(
usedNames: Set<string>,
imports: Set<Variable>,
imports: ReadonlySet<Variable>,
dependenciesToBeDeconflicted: DependenciesToBeDeconflicted,
_interop: GetInterop,
preserveModules: boolean,
Expand Down Expand Up @@ -134,7 +134,7 @@ function deconflictImportsOther(
preserveModules: boolean,
externalLiveBindings: boolean,
chunkByModule: Map<Module, Chunk>
) {
): void {
for (const chunkOrExternalModule of dependencies) {
chunkOrExternalModule.variableName = getSafeName(
chunkOrExternalModule.suggestedVariableName,
Expand Down Expand Up @@ -206,9 +206,9 @@ function deconflictImportsOther(

function deconflictTopLevelVariables(
usedNames: Set<string>,
modules: Module[],
includedNamespaces: Set<Module>
) {
modules: readonly Module[],
includedNamespaces: ReadonlySet<Module>
): void {
for (const module of modules) {
for (const variable of module.scope.variables.values()) {
if (
Expand Down
4 changes: 2 additions & 2 deletions src/utils/executionOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function sortByExecutionOrder(units: OrderedExecutionUnit[]): void {
units.sort(compareExecIndex);
}

export function analyseModuleExecution(entryModules: Module[]): {
export function analyseModuleExecution(entryModules: readonly Module[]): {
cyclePaths: string[][];
orderedModules: Module[];
} {
Expand Down Expand Up @@ -72,7 +72,7 @@ function getCyclePath(
module: Module,
parent: Module,
parents: Map<Module | ExternalModule, Module | null>
) {
): string[] {
const cycleSymbol = Symbol(module.id);
const path = [relativeId(module.id)];
let nextModule = parent;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/exportNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import RESERVED_NAMES from './RESERVED_NAMES';
import { toBase64 } from './base64';

export function assignExportsToMangledNames(
exports: Set<Variable>,
exports: ReadonlySet<Variable>,
exportsByName: Record<string, Variable>,
exportNamesByVariable: Map<Variable, string[]>
): void {
Expand All @@ -26,7 +26,7 @@ export function assignExportsToMangledNames(
}

export function assignExportsToNames(
exports: Set<Variable>,
exports: ReadonlySet<Variable>,
exportsByName: Record<string, Variable>,
exportNamesByVariable: Map<Variable, string[]>
): void {
Expand Down

0 comments on commit 1a6b965

Please sign in to comment.