Skip to content

Commit

Permalink
fix: more types (#4361)
Browse files Browse the repository at this point in the history
* remove type assertion

* use type imports

* simplify creating Set

* use ternary to assign type (remove any)

* use readonly, add set types, use type imports

* use more readonly

* use type inference

* stricter types, use more readonly

* remove type assertion + comment

* use const + new var

* add return types

* make plugincontexts readonly, simplify construction

* more readonly, type imports

* add return types, fix return values

* combine if/else

* consistency: use arrow functions to pass 'this' context

* consistency: use more arrow functions to pass this context

* Revert "consistency: use more arrow functions to pass this context"

This reverts commit 437e90b.

* Revert "consistency: use arrow functions to pass 'this' context"

This reverts commit e331522.

* Revert "add return types, fix return values"

This reverts commit b841c99.

* use type imports

* use more readonly

* replace any with unknown

* use as const

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
dnalborczyk and lukastaegert committed Jan 28, 2022
1 parent 4e124d8 commit 7194b88
Show file tree
Hide file tree
Showing 55 changed files with 238 additions and 230 deletions.
30 changes: 16 additions & 14 deletions build-plugins/generate-license-file.ts
@@ -1,24 +1,24 @@
import { readFileSync, writeFileSync } from 'fs';
import { PluginImpl } from 'rollup';
import license, { Dependency, Person } from 'rollup-plugin-license';
import type { PluginImpl } from 'rollup';
import license, { type Dependency, type Person } from 'rollup-plugin-license';

function generateLicenseFile(dependencies: Dependency[]) {
function generateLicenseFile(dependencies: readonly Dependency[]) {
const coreLicense = readFileSync('LICENSE-CORE.md');
const licenses = new Set();
const dependencyLicenseTexts = dependencies
const licenses = new Set<string>();
const dependencyLicenseTexts = Array.from(dependencies)
.sort(({ name: nameA }, { name: nameB }) => (nameA! > nameB! ? 1 : -1))
.map(({ name, license, licenseText, author, maintainers, contributors, repository }) => {
let text = `## ${name}\n`;
if (license) {
text += `License: ${license}\n`;
}
const names = new Set();
if (author && author.name) {
const names = new Set<string>();
if (author?.name) {
names.add(author.name);
}
// TODO there is an inconsistency in the rollup-plugin-license types
for (const person of contributors.concat(maintainers as unknown as Person[])) {
if (person && person.name) {
if (person?.name) {
names.add(person.name);
}
}
Expand All @@ -39,7 +39,7 @@ function generateLicenseFile(dependencies: Dependency[]) {
.join('\n') +
'\n';
}
licenses.add(license);
licenses.add(license!);
return text;
})
.join('\n---------------------------------------\n\n');
Expand All @@ -59,16 +59,18 @@ function generateLicenseFile(dependencies: Dependency[]) {
}
}

export default function getLicenseHandler(): {
interface LicenseHandler {
collectLicenses: PluginImpl;
writeLicense: PluginImpl;
} {
const licenses = new Map();
}

export default function getLicenseHandler(): LicenseHandler {
const licenses = new Map<string, Dependency>();
return {
collectLicenses() {
function addLicenses(dependencies: Dependency[]) {
function addLicenses(dependencies: readonly Dependency[]) {
for (const dependency of dependencies) {
licenses.set(dependency.name, dependency);
licenses.set(dependency.name!, dependency);
}
}

Expand Down
6 changes: 3 additions & 3 deletions cli/run/loadConfigFile.ts
Expand Up @@ -2,14 +2,14 @@ import { realpathSync } from 'fs';
import { extname, isAbsolute } from 'path';
import { pathToFileURL } from 'url';
import * as rollup from '../../src/node-entry';
import { MergedRollupOptions } from '../../src/rollup/types';
import type { MergedRollupOptions } from '../../src/rollup/types';
import { bold } from '../../src/utils/colors';
import { error } from '../../src/utils/error';
import { mergeOptions } from '../../src/utils/options/mergeOptions';
import { GenericConfigObject } from '../../src/utils/options/options';
import type { GenericConfigObject } from '../../src/utils/options/options';
import relativeId from '../../src/utils/relativeId';
import { stderr } from '../logging';
import batchWarnings, { BatchWarnings } from './batchWarnings';
import batchWarnings, { type BatchWarnings } from './batchWarnings';
import { addCommandPluginsToInputOptions, addPluginsFromCommandOption } from './commandPlugins';

function supportsNativeESM(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion cli/run/loadConfigFromCommand.ts
Expand Up @@ -4,7 +4,7 @@ import batchWarnings, { BatchWarnings } from './batchWarnings';
import { addCommandPluginsToInputOptions } from './commandPlugins';
import { stdinName } from './stdin';

export default async function loadConfigFromCommand(command: Record<string, any>): Promise<{
export default async function loadConfigFromCommand(command: Record<string, unknown>): Promise<{
options: MergedRollupOptions[];
warnings: BatchWarnings;
}> {
Expand Down
2 changes: 1 addition & 1 deletion cli/run/resetScreen.ts
@@ -1,4 +1,4 @@
import { MergedRollupOptions } from '../../src/rollup/types';
import type { MergedRollupOptions } from '../../src/rollup/types';
import { stderr } from '../logging';

const CLEAR_SCREEN = '\u001Bc';
Expand Down
2 changes: 1 addition & 1 deletion cli/run/stdin.ts
@@ -1,4 +1,4 @@
import { Plugin } from '../../src/rollup/types';
import type { Plugin } from '../../src/rollup/types';

export const stdinName = '-';

Expand Down
8 changes: 4 additions & 4 deletions cli/run/waitForInput.ts
@@ -1,5 +1,5 @@
import { PluginContext } from 'rollup';
import { NormalizedInputOptions, Plugin } from '../../src/rollup/types';
import type { PluginContext } from 'rollup';
import type { NormalizedInputOptions, Plugin } from '../../src/rollup/types';
import { bold } from '../../src/utils/colors';
import { stderr } from '../logging';

Expand All @@ -8,9 +8,9 @@ export function waitForInputPlugin(): Plugin {
async buildStart(this: PluginContext, options: NormalizedInputOptions) {
const inputSpecifiers = Array.isArray(options.input)
? options.input
: Object.keys(options.input as { [entryAlias: string]: string });
: Object.keys(options.input);

let lastAwaitedSpecifier = null;
let lastAwaitedSpecifier: string | null = null;
checkSpecifiers: while (true) {
for (const specifier of inputSpecifiers) {
if ((await this.resolve(specifier)) === null) {
Expand Down
5 changes: 3 additions & 2 deletions cli/run/watch-cli.ts
@@ -1,14 +1,15 @@
import { type FSWatcher, readFileSync } from 'fs';
import process from 'process';
import chokidar from 'chokidar';
import dateTime from 'date-time';
import ms from 'pretty-ms';
import onExit from 'signal-exit';
import * as rollup from '../../src/node-entry';
import { MergedRollupOptions, RollupWatcher } from '../../src/rollup/types';
import type { MergedRollupOptions, RollupWatcher } from '../../src/rollup/types';
import { bold, cyan, green, underline } from '../../src/utils/colors';
import relativeId from '../../src/utils/relativeId';
import { handleError, stderr } from '../logging';
import { BatchWarnings } from './batchWarnings';
import type { BatchWarnings } from './batchWarnings';
import { getConfigPath } from './getConfigPath';
import loadAndParseConfigFile from './loadConfigFile';
import loadConfigFromCommand from './loadConfigFromCommand';
Expand Down
18 changes: 9 additions & 9 deletions src/Bundle.ts
@@ -1,8 +1,8 @@
import Chunk from './Chunk';
import ExternalModule from './ExternalModule';
import Graph from './Graph';
import type ExternalModule from './ExternalModule';
import type Graph from './Graph';
import Module from './Module';
import {
import type {
GetManualChunk,
NormalizedInputOptions,
NormalizedOutputOptions,
Expand All @@ -13,8 +13,8 @@ import {
WarningHandler
} from './rollup/types';
import { FILE_PLACEHOLDER } from './utils/FileEmitter';
import { PluginDriver } from './utils/PluginDriver';
import { Addons, createAddons } from './utils/addons';
import type { PluginDriver } from './utils/PluginDriver';
import { type Addons, createAddons } from './utils/addons';
import { getChunkAssignments } from './utils/chunkAssignment';
import commondir from './utils/commondir';
import {
Expand All @@ -25,7 +25,7 @@ import {
warnDeprecation
} from './utils/error';
import { sortByExecutionOrder } from './utils/executionOrder';
import { GenerateCodeSnippets, getGenerateCodeSnippets } from './utils/generateCodeSnippets';
import { type GenerateCodeSnippets, getGenerateCodeSnippets } from './utils/generateCodeSnippets';
import { basename, isAbsolute } from './utils/path';
import { timeEnd, timeStart } from './utils/timers';

Expand Down Expand Up @@ -104,7 +104,7 @@ export default class Bundle {
}

private async addManualChunks(
manualChunks: Record<string, string[]>
manualChunks: Record<string, readonly string[]>
): Promise<Map<Module, string>> {
const manualChunkAliasByEntry = new Map<Module, string>();
const chunkEntries = await Promise.all(
Expand Down Expand Up @@ -305,7 +305,7 @@ function validateOptionsForMultiChunkOutput(
);
}

function getIncludedModules(modulesById: Map<string, Module | ExternalModule>): Module[] {
function getIncludedModules(modulesById: ReadonlyMap<string, Module | ExternalModule>): Module[] {
return [...modulesById.values()].filter(
(module): module is Module =>
module instanceof Module &&
Expand All @@ -317,7 +317,7 @@ function addModuleToManualChunk(
alias: string,
module: Module,
manualChunkAliasByEntry: Map<Module, string>
) {
): void {
const existingAlias = manualChunkAliasByEntry.get(module);
if (typeof existingAlias === 'string' && existingAlias !== alias) {
return error(errCannotAssignModuleToChunk(module.id, alias, existingAlias));
Expand Down
34 changes: 17 additions & 17 deletions src/Chunk.ts
@@ -1,17 +1,17 @@
import MagicString, { Bundle as MagicStringBundle, SourceMap } from 'magic-string';
import MagicString, { Bundle as MagicStringBundle, type SourceMap } from 'magic-string';
import { relative } from '../browser/path';
import ExternalModule from './ExternalModule';
import Module from './Module';
import ExportDefaultDeclaration from './ast/nodes/ExportDefaultDeclaration';
import FunctionDeclaration from './ast/nodes/FunctionDeclaration';
import ChildScope from './ast/scopes/ChildScope';
import type ChildScope from './ast/scopes/ChildScope';
import ExportDefaultVariable from './ast/variables/ExportDefaultVariable';
import LocalVariable from './ast/variables/LocalVariable';
import NamespaceVariable from './ast/variables/NamespaceVariable';
import SyntheticNamedExportVariable from './ast/variables/SyntheticNamedExportVariable';
import Variable from './ast/variables/Variable';
import type Variable from './ast/variables/Variable';
import finalisers from './finalisers/index';
import {
import type {
DecodedSourceMapOrMissing,
GetInterop,
GlobalsOption,
Expand All @@ -23,11 +23,11 @@ import {
RenderedModule,
WarningHandler
} from './rollup/types';
import { PluginDriver } from './utils/PluginDriver';
import { Addons } from './utils/addons';
import type { PluginDriver } from './utils/PluginDriver';
import type { Addons } from './utils/addons';
import { collapseSourcemaps } from './utils/collapseSourcemaps';
import { createHash } from './utils/crypto';
import { deconflictChunk, DependenciesToBeDeconflicted } from './utils/deconflictChunk';
import { deconflictChunk, type DependenciesToBeDeconflicted } from './utils/deconflictChunk';
import {
errCyclicCrossChunkReexport,
errFailedValidation,
Expand All @@ -38,7 +38,7 @@ import {
} from './utils/error';
import { escapeId } from './utils/escapeId';
import { assignExportsToMangledNames, assignExportsToNames } from './utils/exportNames';
import { GenerateCodeSnippets } from './utils/generateCodeSnippets';
import type { GenerateCodeSnippets } from './utils/generateCodeSnippets';
import getExportMode from './utils/getExportMode';
import { getId } from './utils/getId';
import getIndentString from './utils/getIndentString';
Expand All @@ -54,7 +54,7 @@ import {
import { basename, dirname, extname, isAbsolute, normalize, resolve } from './utils/path';
import relativeId, { getAliasName } from './utils/relativeId';
import renderChunk from './utils/renderChunk';
import { RenderOptions } from './utils/renderHelpers';
import type { RenderOptions } from './utils/renderHelpers';
import { makeUnique, renderNamePattern } from './utils/renderNamePattern';
import { timeEnd, timeStart } from './utils/timers';
import { MISSING_EXPORT_SHIM_VARIABLE } from './utils/variableNames';
Expand Down Expand Up @@ -165,13 +165,13 @@ export default class Chunk {
private strictFacade = false;
private usedModules: Module[] = undefined as never;
constructor(
private readonly orderedModules: Module[],
private readonly orderedModules: readonly Module[],
private readonly inputOptions: NormalizedInputOptions,
private readonly outputOptions: NormalizedOutputOptions,
private readonly unsetOptions: Set<string>,
private readonly unsetOptions: ReadonlySet<string>,
private readonly pluginDriver: PluginDriver,
private readonly modulesById: Map<string, Module | ExternalModule>,
private readonly chunkByModule: Map<Module, Chunk>,
private readonly modulesById: ReadonlyMap<string, Module | ExternalModule>,
private readonly chunkByModule: ReadonlyMap<Module, Chunk>,
private readonly facadeChunkByModule: Map<Module, Chunk>,
private readonly includedNamespaces: Set<Module>,
private readonly manualChunkAlias: string | null
Expand Down Expand Up @@ -209,10 +209,10 @@ export default class Chunk {
private static generateFacade(
inputOptions: NormalizedInputOptions,
outputOptions: NormalizedOutputOptions,
unsetOptions: Set<string>,
unsetOptions: ReadonlySet<string>,
pluginDriver: PluginDriver,
modulesById: Map<string, Module | ExternalModule>,
chunkByModule: Map<Module, Chunk>,
modulesById: ReadonlyMap<string, Module | ExternalModule>,
chunkByModule: ReadonlyMap<Module, Chunk>,
facadeChunkByModule: Map<Module, Chunk>,
includedNamespaces: Set<Module>,
facadedModule: Module,
Expand Down Expand Up @@ -833,7 +833,7 @@ export default class Chunk {
}
}

private checkCircularDependencyImport(variable: Variable, importingModule: Module) {
private checkCircularDependencyImport(variable: Variable, importingModule: Module): void {
const variableModule = variable.module;
if (variableModule instanceof Module) {
const exportChunk = this.chunkByModule.get(variableModule);
Expand Down
2 changes: 1 addition & 1 deletion src/ExternalModule.ts
@@ -1,5 +1,5 @@
import ExternalVariable from './ast/variables/ExternalVariable';
import {
import type {
CustomPluginOptions,
ModuleInfo,
NormalizedInputOptions,
Expand Down
6 changes: 3 additions & 3 deletions src/Graph.ts
@@ -1,10 +1,10 @@
import * as acorn from 'acorn';
import ExternalModule from './ExternalModule';
import type ExternalModule from './ExternalModule';
import Module from './Module';
import { ModuleLoader, UnresolvedModule } from './ModuleLoader';
import { ModuleLoader, type UnresolvedModule } from './ModuleLoader';
import GlobalScope from './ast/scopes/GlobalScope';
import { PathTracker } from './ast/utils/PathTracker';
import {
import type {
ModuleInfo,
ModuleJSON,
NormalizedInputOptions,
Expand Down
30 changes: 16 additions & 14 deletions src/Module.ts
Expand Up @@ -3,31 +3,31 @@ import * as acorn from 'acorn';
import { locate } from 'locate-character';
import MagicString from 'magic-string';
import ExternalModule from './ExternalModule';
import Graph from './Graph';
import type Graph from './Graph';
import { createHasEffectsContext, createInclusionContext } from './ast/ExecutionContext';
import ExportAllDeclaration from './ast/nodes/ExportAllDeclaration';
import ExportDefaultDeclaration from './ast/nodes/ExportDefaultDeclaration';
import ExportNamedDeclaration from './ast/nodes/ExportNamedDeclaration';
import Identifier from './ast/nodes/Identifier';
import ImportDeclaration from './ast/nodes/ImportDeclaration';
import ImportExpression from './ast/nodes/ImportExpression';
import type ExportNamedDeclaration from './ast/nodes/ExportNamedDeclaration';
import type Identifier from './ast/nodes/Identifier';
import type ImportDeclaration from './ast/nodes/ImportDeclaration';
import type ImportExpression from './ast/nodes/ImportExpression';
import Literal from './ast/nodes/Literal';
import MetaProperty from './ast/nodes/MetaProperty';
import type MetaProperty from './ast/nodes/MetaProperty';
import * as NodeType from './ast/nodes/NodeType';
import Program from './ast/nodes/Program';
import TemplateLiteral from './ast/nodes/TemplateLiteral';
import VariableDeclaration from './ast/nodes/VariableDeclaration';
import { nodeConstructors } from './ast/nodes/index';
import { ExpressionNode, NodeBase } from './ast/nodes/shared/Node';
import type { ExpressionNode, NodeBase } from './ast/nodes/shared/Node';
import ModuleScope from './ast/scopes/ModuleScope';
import { PathTracker, UNKNOWN_PATH } from './ast/utils/PathTracker';
import { type PathTracker, UNKNOWN_PATH } from './ast/utils/PathTracker';
import ExportDefaultVariable from './ast/variables/ExportDefaultVariable';
import ExportShimVariable from './ast/variables/ExportShimVariable';
import ExternalVariable from './ast/variables/ExternalVariable';
import NamespaceVariable from './ast/variables/NamespaceVariable';
import SyntheticNamedExportVariable from './ast/variables/SyntheticNamedExportVariable';
import Variable from './ast/variables/Variable';
import {
import type Variable from './ast/variables/Variable';
import type {
CustomPluginOptions,
DecodedSourceMapOrMissing,
EmittedFile,
Expand Down Expand Up @@ -62,7 +62,7 @@ import { getOriginalLocation } from './utils/getOriginalLocation';
import { makeLegal } from './utils/identifierHelpers';
import { basename, extname } from './utils/path';
import relativeId from './utils/relativeId';
import { RenderOptions } from './utils/renderHelpers';
import type { RenderOptions } from './utils/renderHelpers';
import { timeEnd, timeStart } from './utils/timers';
import { markModuleAndImpureDependenciesAsExecuted } from './utils/traverseStaticDependencies';
import { MISSING_EXPORT_SHIM_VARIABLE } from './utils/variableNames';
Expand Down Expand Up @@ -991,12 +991,14 @@ export default class Module {

private addRelevantSideEffectDependencies(
relevantDependencies: Set<Module | ExternalModule>,
necessaryDependencies: Set<Module | ExternalModule>,
alwaysCheckedDependencies: Set<Module | ExternalModule>
necessaryDependencies: ReadonlySet<Module | ExternalModule>,
alwaysCheckedDependencies: ReadonlySet<Module | ExternalModule>
): void {
const handledDependencies = new Set<Module | ExternalModule>();

const addSideEffectDependencies = (possibleDependencies: Set<Module | ExternalModule>) => {
const addSideEffectDependencies = (
possibleDependencies: ReadonlySet<Module | ExternalModule>
) => {
for (const dependency of possibleDependencies) {
if (handledDependencies.has(dependency)) {
continue;
Expand Down

0 comments on commit 7194b88

Please sign in to comment.