Skip to content

Commit

Permalink
Enable full TypeScript strict mode (#2888)
Browse files Browse the repository at this point in the history
* Enable strict function types

* Enable strict property initialization
  • Loading branch information
lukastaegert committed Jun 1, 2019
1 parent df11753 commit dc87d58
Show file tree
Hide file tree
Showing 83 changed files with 349 additions and 440 deletions.
4 changes: 2 additions & 2 deletions bin/src/run/loadConfigFile.ts
Expand Up @@ -40,9 +40,9 @@ export default function loadConfigFile(
.then(({ output: [{ code }] }: RollupOutput) => {
// temporarily override require
const defaultLoader = require.extensions['.js'];
require.extensions['.js'] = (module: NodeModuleWithCompile, filename: string) => {
require.extensions['.js'] = (module: NodeModule, filename: string) => {
if (filename === configFile) {
module._compile(code, filename);
(module as NodeModuleWithCompile)._compile(code, filename);
} else {
defaultLoader(module, filename);
}
Expand Down
28 changes: 14 additions & 14 deletions src/Chunk.ts
Expand Up @@ -113,7 +113,7 @@ export function isChunkRendered(chunk: Chunk): boolean {
export default class Chunk {
static generateFacade(graph: Graph, facadedModule: Module): Chunk {
const chunk = new Chunk(graph, []);
chunk.dependencies = [facadedModule.chunk];
chunk.dependencies = [facadedModule.chunk as Chunk];
chunk.dynamicDependencies = [];
chunk.facadeModule = facadedModule;
facadedModule.facadeChunk = chunk;
Expand All @@ -135,13 +135,13 @@ export default class Chunk {
isEmpty: boolean;
manualChunkAlias: string | null = null;
orderedModules: Module[];
renderedModules: {
renderedModules?: {
[moduleId: string]: RenderedModule;
};
usedModules: Module[] = undefined as any;

variableName: string;
private chunkName: string | void;
private chunkName?: string;
private dependencies: (ExternalModule | Chunk)[] = undefined as any;
private dynamicDependencies: (ExternalModule | Chunk)[] = undefined as any;
private exportNames: { [name: string]: Variable } = Object.create(null);
Expand Down Expand Up @@ -524,7 +524,7 @@ export default class Chunk {

let hoistedSource = '';

this.renderedModules = Object.create(null);
const renderedModules = (this.renderedModules = Object.create(null));
this.renderedModuleSources = [];

for (let i = 0; i < this.orderedModules.length; i++) {
Expand All @@ -535,7 +535,7 @@ export default class Chunk {
this.renderedModuleSources.push(source);

const { renderedExports, removedExports } = module.getRenderedExports();
this.renderedModules[module.id] = {
renderedModules[module.id] = {
originalLength: module.originalCode.length,
removedExports,
renderedExports,
Expand Down Expand Up @@ -774,7 +774,7 @@ export default class Chunk {
}
let dependency: Chunk | ExternalModule;
if (depModule instanceof Module) {
dependency = depModule.chunk;
dependency = depModule.chunk as Chunk;
} else {
if (!(depModule.used || depModule.moduleSideEffects)) {
continue;
Expand Down Expand Up @@ -820,8 +820,8 @@ export default class Chunk {
for (const { node, resolution } of module.dynamicImports) {
if (!resolution) continue;
if (resolution instanceof Module) {
if (resolution.chunk !== this && isChunkRendered(resolution.chunk)) {
const resolutionChunk = resolution.facadeChunk || resolution.chunk;
if (resolution.chunk !== this && isChunkRendered(resolution.chunk as Chunk)) {
const resolutionChunk = resolution.facadeChunk || (resolution.chunk as Chunk);
let relPath = normalize(relative(dirname(this.id), resolutionChunk.id));
if (!relPath.startsWith('../')) relPath = './' + relPath;
node.renderFinalResolution(code, `'${relPath}'`, format);
Expand Down Expand Up @@ -866,8 +866,8 @@ export default class Chunk {
// skip local exports
if (!module || module.chunk === this) continue;
if (module instanceof Module) {
exportChunk = module.chunk;
importName = module.chunk.getVariableExportName(variable);
exportChunk = module.chunk as Chunk;
importName = exportChunk.getVariableExportName(variable);
needsLiveBinding = variable.isReassigned;
} else {
exportChunk = module;
Expand Down Expand Up @@ -899,7 +899,7 @@ export default class Chunk {
imported:
variable.module instanceof ExternalModule
? variable.name
: (variable.module as Module).chunk.getVariableExportName(variable),
: ((variable.module as Module).chunk as Chunk).getVariableExportName(variable),
local: variable.getName()
});
}
Expand Down Expand Up @@ -938,7 +938,7 @@ export default class Chunk {
globalName,
id, // chunk id updated on render
imports: imports.length > 0 ? imports : (null as any),
isChunk: !(dep as ExternalModule).isExternal,
isChunk: dep instanceof Chunk,
name: dep.variableName,
namedExportsMode,
reexports
Expand Down Expand Up @@ -1081,7 +1081,7 @@ export default class Chunk {
if ((variable.module as Module).chunk !== this) {
this.imports.add(variable);
if (variable.module instanceof Module) {
variable.module.chunk.exports.add(variable);
(variable.module.chunk as Chunk).exports.add(variable);
}
}
}
Expand All @@ -1102,7 +1102,7 @@ export default class Chunk {
if ((variable.module as Module).chunk !== this) {
this.imports.add(variable);
if (variable.module instanceof Module) {
variable.module.chunk.exports.add(variable);
(variable.module.chunk as Chunk).exports.add(variable);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/ExternalModule.ts
Expand Up @@ -12,7 +12,6 @@ export default class ExternalModule {
exportsNames = false;
exportsNamespace = false;
id: string;
isExternal = true;
moduleSideEffects: boolean;
mostCommonSuggestion = 0;
nameSuggestions: { [name: string]: number };
Expand Down
17 changes: 8 additions & 9 deletions src/Graph.ts
Expand Up @@ -79,17 +79,15 @@ export default class Graph {
preserveModules: boolean;
scope: GlobalScope;
shimMissingExports: boolean;
// deprecated
treeshake: boolean;
treeshakingOptions: TreeshakingOptions;
treeshakingOptions?: TreeshakingOptions;
watchFiles: Record<string, true> = Object.create(null);

private cacheExpiry: number;
private context: string;
private externalModules: ExternalModule[] = [];
private modules: Module[] = [];
private onwarn: WarningHandler;
private pluginCache: Record<string, SerializablePluginCache>;
private pluginCache?: Record<string, SerializablePluginCache>;

constructor(options: InputOptions, watcher?: RollupWatcher) {
this.curChunkIndex = 0;
Expand All @@ -112,8 +110,7 @@ export default class Graph {

this.cacheExpiry = options.experimentalCacheExpiry as number;

this.treeshake = options.treeshake !== false;
if (this.treeshake) {
if (options.treeshake !== false) {
this.treeshakingOptions = options.treeshake
? {
annotations: (options.treeshake as TreeshakingOptions).annotations !== false,
Expand Down Expand Up @@ -191,10 +188,12 @@ export default class Graph {
this.pluginDriver,
options.external as ExternalOption,
(typeof options.manualChunks === 'function' && options.manualChunks) as GetManualChunk | null,
(this.treeshake
(this.treeshakingOptions
? this.treeshakingOptions.moduleSideEffects
: null) as ModuleSideEffectsOption,
(this.treeshake ? this.treeshakingOptions.pureExternalModules : false) as PureModulesOption
(this.treeshakingOptions
? this.treeshakingOptions.pureExternalModules
: false) as PureModulesOption
);
}

Expand Down Expand Up @@ -344,7 +343,7 @@ export default class Graph {
}

includeMarked(modules: Module[]) {
if (this.treeshake) {
if (this.treeshakingOptions) {
let treeshakingPass = 1;
do {
timeStart(`treeshaking pass ${treeshakingPass}`, 3);
Expand Down
51 changes: 27 additions & 24 deletions src/Module.ts
Expand Up @@ -166,11 +166,11 @@ const MISSING_EXPORT_SHIM_DESCRIPTION: ExportDescription = {
};

export default class Module {
chunk: Chunk;
chunk?: Chunk;
chunkAlias: string = null as any;
code: string;
code!: string;
comments: CommentDescription[] = [];
customTransformCache: boolean;
customTransformCache!: boolean;
dependencies: (Module | ExternalModule)[] = [];
dynamicallyImportedBy: Module[] = [];
dynamicDependencies: (Module | ExternalModule)[] = [];
Expand All @@ -193,30 +193,29 @@ export default class Module {
imports = new Set<Variable>();
isEntryPoint: boolean;
isExecuted = false;
isExternal: false;
isUserDefinedEntryPoint = false;
manualChunkAlias: string = null as any;
moduleSideEffects: boolean;
originalCode: string;
originalSourcemap: RawSourceMap | void;
originalCode!: string;
originalSourcemap!: RawSourceMap | void;
reexports: { [name: string]: ReexportDescription } = Object.create(null);
resolvedIds: ResolvedIdMap;
scope: ModuleScope;
sourcemapChain: RawSourceMap[];
resolvedIds!: ResolvedIdMap;
scope!: ModuleScope;
sourcemapChain!: RawSourceMap[];
sources: string[] = [];
transformAssets: Asset[];
transformAssets?: Asset[];
usesTopLevelAwait = false;

private allExportNames?: Set<string>;
private ast: Program;
private astContext: AstContext;
private ast!: Program;
private astContext!: AstContext;
private context: string;
private esTreeAst: ESTree.Program;
private esTreeAst!: ESTree.Program;
private graph: Graph;
private magicString: MagicString;
private magicString!: MagicString;
private namespaceVariable: NamespaceVariable = undefined as any;
private transformDependencies: string[];
private transitiveReexports: string[];
private transformDependencies!: string[];
private transitiveReexports?: string[];

constructor(graph: Graph, id: string, moduleSideEffects: boolean, isEntry: boolean) {
this.id = id;
Expand Down Expand Up @@ -319,7 +318,10 @@ export default class Module {
const exportNamesByVariable: Map<Variable, string[]> = new Map();
for (const exportName of this.getAllExportNames()) {
const tracedVariable = this.getVariableForExportName(exportName);
if (!tracedVariable || !(tracedVariable.included || tracedVariable.isExternal)) {
if (
!tracedVariable ||
!(tracedVariable.included || tracedVariable instanceof ExternalVariable)
) {
continue;
}
const existingExportNames = exportNamesByVariable.get(tracedVariable);
Expand Down Expand Up @@ -464,8 +466,8 @@ export default class Module {
for (const name of this.getReexports()) {
const variable = this.getVariableForExportName(name);

if (variable.isExternal) {
variable.reexported = (variable as ExternalVariable).module.reexported = true;
if (variable instanceof ExternalVariable) {
variable.reexported = variable.module.reexported = true;
} else if (!variable.included) {
variable.include();
variable.deoptimizePath(UNKNOWN_PATH);
Expand Down Expand Up @@ -560,7 +562,8 @@ export default class Module {
addExport: this.addExport.bind(this),
addImport: this.addImport.bind(this),
addImportMeta: this.addImportMeta.bind(this),
annotations: (this.graph.treeshake && this.graph.treeshakingOptions.annotations) as boolean,
annotations: (this.graph.treeshakingOptions &&
this.graph.treeshakingOptions.annotations) as boolean,
code, // Only needed for debugging
deoptimizationTracker: this.graph.deoptimizationTracker,
error: this.error.bind(this),
Expand All @@ -581,11 +584,11 @@ export default class Module {
moduleContext: this.context,
nodeConstructors,
preserveModules: this.graph.preserveModules,
propertyReadSideEffects: (!this.graph.treeshake ||
propertyReadSideEffects: (!this.graph.treeshakingOptions ||
this.graph.treeshakingOptions.propertyReadSideEffects) as boolean,
traceExport: this.getVariableForExportName.bind(this),
traceVariable: this.traceVariable.bind(this),
treeshake: this.graph.treeshake,
treeshake: !!this.graph.treeshakingOptions,
usesTopLevelAwait: false,
warn: this.warn.bind(this)
};
Expand Down Expand Up @@ -627,8 +630,8 @@ export default class Module {
const importDeclaration = this.importDescriptions[name];
const otherModule = importDeclaration.module as Module | ExternalModule;

if (!otherModule.isExternal && importDeclaration.name === '*') {
return (otherModule as Module).getOrCreateNamespace();
if (otherModule instanceof Module && importDeclaration.name === '*') {
return (otherModule).getOrCreateNamespace();
}

const declaration = otherModule.getVariableForExportName(importDeclaration.name);
Expand Down

0 comments on commit dc87d58

Please sign in to comment.