Skip to content

Commit

Permalink
Use more restrictive types (#4782)
Browse files Browse the repository at this point in the history
* Use more readonly

* Simplify, remove non-null assertion operator

* Add generic type parameters to Map

* Use nullish coalescing

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
dnalborczyk and lukastaegert committed Jan 2, 2023
1 parent 023ed0f commit c6c8844
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Bundle.ts
Expand Up @@ -267,7 +267,7 @@ function getIncludedModules(modulesById: ReadonlyMap<string, Module | ExternalMo
}

function getAbsoluteEntryModulePaths(
includedModules: Module[],
includedModules: readonly Module[],
preserveModules: boolean
): string[] {
const absoluteEntryModulePaths: string[] = [];
Expand Down
4 changes: 2 additions & 2 deletions src/ast/scopes/ClassBodyScope.ts
Expand Up @@ -6,8 +6,8 @@ import ChildScope from './ChildScope';
import type Scope from './Scope';

export default class ClassBodyScope extends ChildScope {
instanceScope: ChildScope;
thisVariable: LocalVariable;
readonly instanceScope: ChildScope;
readonly thisVariable: LocalVariable;

constructor(parent: Scope, classNode: ClassNode, context: AstContext) {
super(parent);
Expand Down
2 changes: 1 addition & 1 deletion src/ast/scopes/ModuleScope.ts
Expand Up @@ -10,7 +10,7 @@ import ChildScope from './ChildScope';
import type GlobalScope from './GlobalScope';

export default class ModuleScope extends ChildScope {
context: AstContext;
readonly context: AstContext;
declare parent: GlobalScope;

constructor(parent: GlobalScope, context: AstContext) {
Expand Down
3 changes: 1 addition & 2 deletions src/ast/scopes/ParameterScope.ts
Expand Up @@ -10,9 +10,8 @@ import type Scope from './Scope';

export default class ParameterScope extends ChildScope {
readonly hoistedBodyVarScope: ChildScope;

protected parameters: readonly LocalVariable[][] = [];
private context: AstContext;
private readonly context: AstContext;
private hasRest = false;

constructor(parent: Scope, context: AstContext) {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/scopes/ReturnValueScope.ts
Expand Up @@ -4,7 +4,7 @@ import ParameterScope from './ParameterScope';

export default class ReturnValueScope extends ParameterScope {
private returnExpression: ExpressionEntity | null = null;
private returnExpressions: ExpressionEntity[] = [];
private readonly returnExpressions: ExpressionEntity[] = [];

addReturnExpression(expression: ExpressionEntity): void {
this.returnExpressions.push(expression);
Expand Down
4 changes: 2 additions & 2 deletions src/ast/scopes/Scope.ts
Expand Up @@ -7,8 +7,8 @@ import type Variable from '../variables/Variable';
import type ChildScope from './ChildScope';

export default class Scope {
children: ChildScope[] = [];
variables = new Map<string, Variable>();
readonly children: ChildScope[] = [];
readonly variables = new Map<string, Variable>();

addDeclaration(
identifier: Identifier,
Expand Down
2 changes: 1 addition & 1 deletion src/ast/scopes/TrackingScope.ts
Expand Up @@ -5,7 +5,7 @@ import type LocalVariable from '../variables/LocalVariable';
import BlockScope from './BlockScope';

export default class TrackingScope extends BlockScope {
hoistedDeclarations: Identifier[] = [];
readonly hoistedDeclarations: Identifier[] = [];

addDeclaration(
identifier: Identifier,
Expand Down
2 changes: 1 addition & 1 deletion src/ast/variables/ExportShimVariable.ts
Expand Up @@ -3,7 +3,7 @@ import { MISSING_EXPORT_SHIM_VARIABLE } from '../../utils/variableNames';
import Variable from './Variable';

export default class ExportShimVariable extends Variable {
module: Module;
readonly module: Module;

constructor(module: Module) {
super(MISSING_EXPORT_SHIM_VARIABLE);
Expand Down
4 changes: 2 additions & 2 deletions src/ast/variables/ExternalVariable.ts
Expand Up @@ -6,8 +6,8 @@ import type { ObjectPath } from '../utils/PathTracker';
import Variable from './Variable';

export default class ExternalVariable extends Variable {
isNamespace: boolean;
module: ExternalModule;
readonly isNamespace: boolean;
readonly module: ExternalModule;
referenced = false;

constructor(module: ExternalModule, name: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/ast/variables/NamespaceVariable.ts
Expand Up @@ -15,9 +15,9 @@ import { SymbolToStringTag, UNKNOWN_PATH } from '../utils/PathTracker';
import Variable from './Variable';

export default class NamespaceVariable extends Variable {
context: AstContext;
readonly context: AstContext;
declare isNamespace: true;
module: Module;
readonly module: Module;

private memberVariables: { [name: string]: Variable } | null = null;
private mergedNamespaces: readonly Variable[] = [];
Expand Down
6 changes: 3 additions & 3 deletions src/ast/variables/SyntheticNamedExportVariable.ts
Expand Up @@ -4,9 +4,9 @@ import ExportDefaultVariable from './ExportDefaultVariable';
import Variable from './Variable';

export default class SyntheticNamedExportVariable extends Variable {
context: AstContext;
module: Module;
syntheticNamespace: Variable;
readonly context: AstContext;
readonly module: Module;
readonly syntheticNamespace: Variable;

private baseVariable: Variable | null = null;

Expand Down
2 changes: 1 addition & 1 deletion src/finalisers/cjs.ts
Expand Up @@ -73,7 +73,7 @@ export default function cjs(
}

function getImportBlock(
dependencies: ChunkDependency[],
dependencies: readonly ChunkDependency[],
{ _, cnst, n }: GenerateCodeSnippets,
compact: boolean
): string {
Expand Down
5 changes: 4 additions & 1 deletion src/finalisers/es.ts
Expand Up @@ -32,7 +32,10 @@ export default function es(
magicString.trim();
}

function getImportBlock(dependencies: ChunkDependency[], { _ }: GenerateCodeSnippets): string[] {
function getImportBlock(
dependencies: readonly ChunkDependency[],
{ _ }: GenerateCodeSnippets
): string[] {
const importBlock: string[] = [];
for (const { importPath, reexports, imports, name, assertions } of dependencies) {
const assertion = assertions ? `${_}assert${_}${assertions}` : '';
Expand Down
2 changes: 1 addition & 1 deletion src/finalisers/shared/getCompleteAmdId.ts
Expand Up @@ -9,5 +9,5 @@ export default function getCompleteAmdId(
return `${options.basePath ? options.basePath + '/' : ''}${removeJsExtension(chunkId)}`;
}

return options.id || '';
return options.id ?? '';
}
4 changes: 2 additions & 2 deletions src/finalisers/shared/getExportBlock.ts
Expand Up @@ -10,7 +10,7 @@ import {

export function getExportBlock(
exports: ChunkExports,
dependencies: ChunkDependency[],
dependencies: readonly ChunkDependency[],
namedExportsMode: boolean,
interop: GetInterop,
snippets: GenerateCodeSnippets,
Expand Down Expand Up @@ -117,7 +117,7 @@ export function getExportBlock(

function getSingleDefaultExport(
exports: ChunkExports,
dependencies: ChunkDependency[],
dependencies: readonly ChunkDependency[],
interop: GetInterop,
externalLiveBindings: boolean,
getPropertyAccess: (name: string) => string
Expand Down
16 changes: 8 additions & 8 deletions src/utils/FileEmitter.ts
Expand Up @@ -246,8 +246,11 @@ export class FileEmitter {
bundle: OutputBundleWithPlaceholders,
outputOptions: NormalizedOutputOptions
): void => {
const fileNamesBySource = new Map();
const output = (this.output = { bundle, fileNamesBySource, outputOptions });
const output = (this.output = {
bundle,
fileNamesBySource: new Map<string, string>(),
outputOptions
});
for (const emittedFile of this.filesByReferenceId.values()) {
if (emittedFile.fileName) {
reserveFileNameInBundle(emittedFile.fileName, output, this.options.onwarn);
Expand All @@ -265,16 +268,13 @@ export class FileEmitter {
}

private assignReferenceId(file: ConsumedFile, idBase: string): string {
let referenceId: string | undefined;
let referenceId = idBase;

do {
referenceId = createHash()
.update(referenceId || idBase)
.digest('hex')
.slice(0, 8);
referenceId = createHash().update(referenceId).digest('hex').slice(0, 8);
} while (
this.filesByReferenceId.has(referenceId) ||
this.outputFileEmitters.some(({ filesByReferenceId }) => filesByReferenceId.has(referenceId!))
this.outputFileEmitters.some(({ filesByReferenceId }) => filesByReferenceId.has(referenceId))
);
this.filesByReferenceId.set(referenceId, file);
for (const { filesByReferenceId } of this.outputFileEmitters) {
Expand Down

0 comments on commit c6c8844

Please sign in to comment.