Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable TypeScript strictNullChecks #2755

Merged
merged 6 commits into from May 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 55 additions & 43 deletions src/Chunk.ts
Expand Up @@ -112,32 +112,32 @@ export default class Chunk {
facadeModule: Module | null = null;
graph: Graph;
hasDynamicImport: boolean = false;
id: string = undefined;
indentString: string = undefined;
id: string = undefined as any;
indentString: string = undefined as any;
isEmpty: boolean;
isManualChunk: boolean = false;
orderedModules: Module[];
renderedModules: {
[moduleId: string]: RenderedModule;
};
usedModules: Module[] = undefined;
usedModules: Module[] = undefined as any;
variableName: string;

private chunkName: string | void;
private dependencies: (ExternalModule | Chunk)[] = undefined;
private dynamicDependencies: (ExternalModule | Chunk)[] = undefined;
private dependencies: (ExternalModule | Chunk)[] = undefined as any;
private dynamicDependencies: (ExternalModule | Chunk)[] = undefined as any;
private exportNames: { [name: string]: Variable } = Object.create(null);
private exports = new Set<Variable>();
private imports = new Set<Variable>();
private needsExportsShim: boolean = false;
private renderedDeclarations: {
dependencies: ChunkDependencies;
exports: ChunkExports;
} = undefined;
private renderedHash: string = undefined;
private renderedModuleSources: MagicString[] = undefined;
} = undefined as any;
private renderedHash: string = undefined as any;
private renderedModuleSources: MagicString[] = undefined as any;
private renderedSource: MagicStringBundle | null = null;
private renderedSourceLength: number = undefined;
private renderedSourceLength: number = undefined as any;

constructor(graph: Graph, orderedModules: Module[]) {
this.graph = graph;
Expand Down Expand Up @@ -217,6 +217,7 @@ export default class Chunk {
case 'name':
return this.getChunkName();
}
return undefined as any;
}),
existingNames
);
Expand Down Expand Up @@ -291,7 +292,7 @@ export default class Chunk {
Object.keys(this.exportNames)
.map(exportName => {
const variable = this.exportNames[exportName];
return `${relativeId(variable.module.id).replace(/\\/g, '/')}:${
return `${relativeId((variable.module as Module).id).replace(/\\/g, '/')}:${
variable.name
}:${exportName}`;
})
Expand All @@ -302,7 +303,7 @@ export default class Chunk {

getRenderedSourceLength() {
if (this.renderedSourceLength !== undefined) return this.renderedSourceLength;
return (this.renderedSourceLength = this.renderedSource.length());
return (this.renderedSourceLength = (this.renderedSource as MagicStringBundle).length());
}

getVariableExportName(variable: Variable) {
Expand Down Expand Up @@ -341,7 +342,7 @@ export default class Chunk {
}

for (const variable of Array.from(chunk.imports)) {
if (!this.imports.has(variable) && variable.module.chunk !== this) {
if (!this.imports.has(variable) && (variable.module as Module).chunk !== this) {
this.imports.add(variable);
}
}
Expand All @@ -365,12 +366,14 @@ export default class Chunk {
) => {
if (dep.imports) {
for (const impt of dep.imports) {
impt.imported = this.getVariableExportName(oldExportNames[impt.imported]);
impt.imported = this.getVariableExportName(oldExportNames[impt.imported]) as string;
}
}
if (dep.reexports) {
for (const reexport of dep.reexports) {
reexport.imported = this.getVariableExportName(oldExportNames[reexport.imported]);
reexport.imported = this.getVariableExportName(
oldExportNames[reexport.imported]
) as string;
}
}
};
Expand Down Expand Up @@ -405,7 +408,7 @@ export default class Chunk {
// go through the other chunks and update their dependencies
// also update their import and reexport names in the process
for (const c of chunkList) {
let includedDeclaration: ModuleDeclarationDependency;
let includedDeclaration: ModuleDeclarationDependency = undefined as any;
for (let i = 0; i < c.dependencies.length; i++) {
const dep = c.dependencies[i];
if ((dep === chunk || dep === this) && includedDeclaration) {
Expand Down Expand Up @@ -444,9 +447,9 @@ export default class Chunk {
const _ = options.compact ? '' : ' ';

const renderOptions: RenderOptions = {
compact: options.compact,
dynamicImportFunction: options.dynamicImportFunction,
format: options.format,
compact: options.compact as boolean,
dynamicImportFunction: options.dynamicImportFunction as string,
format: options.format as string,
freeze: options.freeze !== false,
indent: this.indentString,
namespaceToStringTag: options.namespaceToStringTag === true,
Expand All @@ -455,7 +458,9 @@ export default class Chunk {

// Make sure the direct dependencies of a chunk are present to maintain execution order
for (const { module } of Array.from(this.imports)) {
const chunkOrExternal = module instanceof Module ? module.chunk : module;
const chunkOrExternal = (module instanceof Module ? module.chunk : module) as
| Chunk
| ExternalModule;
if (this.dependencies.indexOf(chunkOrExternal) === -1) {
this.dependencies.push(chunkOrExternal);
}
Expand Down Expand Up @@ -526,8 +531,8 @@ export default class Chunk {
this.renderedSource = magicString.trim();
}

this.renderedSourceLength = undefined;
this.renderedHash = undefined;
this.renderedSourceLength = undefined as any;
this.renderedHash = undefined as any;

if (this.getExportNames().length === 0 && this.getImportIds().length === 0 && this.isEmpty) {
this.graph.warn({
Expand All @@ -550,7 +555,7 @@ export default class Chunk {
if (!this.renderedSource)
throw new Error('Internal error: Chunk render called before preRender');

const finalise = finalisers[options.format];
const finalise = finalisers[options.format as string];
if (!finalise) {
error({
code: 'INVALID_OPTION',
Expand Down Expand Up @@ -588,7 +593,7 @@ export default class Chunk {
const hasExports =
this.renderedDeclarations.exports.length !== 0 ||
this.renderedDeclarations.dependencies.some(
dep => dep.reexports && dep.reexports.length !== 0
dep => (dep.reexports && dep.reexports.length !== 0) as boolean
);

const usesTopLevelAwait = this.orderedModules.some(module => module.usesTopLevelAwait);
Expand All @@ -609,11 +614,11 @@ export default class Chunk {
exports: this.renderedDeclarations.exports,
hasExports,
indentString: this.indentString,
intro: addons.intro,
intro: addons.intro as string,
isEntryModuleFacade: this.facadeModule !== null && this.facadeModule.isEntryPoint,
namedExportsMode: this.exportMode !== 'default',
needsAmdModule,
outro: addons.outro,
outro: addons.outro as string,
usesTopLevelAwait,
varOrConst: options.preferConst ? 'const' : 'var',
warn: this.graph.warn.bind(this.graph)
Expand All @@ -626,7 +631,7 @@ export default class Chunk {

timeEnd('render format', 3);

let map: SourceMap = null;
let map: SourceMap = null as any;
const chunkSourcemapChain: RawSourceMap[] = [];

return renderChunk({
Expand All @@ -653,7 +658,7 @@ export default class Chunk {
decodedMap,
this.usedModules,
chunkSourcemapChain,
options.sourcemapExcludeSources
options.sourcemapExcludeSources as boolean
);
} else {
map = magicString.generateMap({ file, includeContent: !options.sourcemapExcludeSources });
Expand All @@ -680,7 +685,7 @@ export default class Chunk {
this.facadeModule = facadedModule;
facadedModule.facadeChunk = this;
for (const exportName of facadedModule.getAllExports()) {
const tracedVariable = facadedModule.getVariableForExportName(exportName);
const tracedVariable = facadedModule.getVariableForExportName(exportName) as Variable;
this.exports.add(tracedVariable);
this.exportNames[exportName] = tracedVariable;
}
Expand All @@ -706,7 +711,7 @@ export default class Chunk {
): boolean {
const seen = new Set<Chunk | ExternalModule>();
function visitDep(dep: Chunk | ExternalModule): boolean {
if (seen.has(dep)) return;
if (seen.has(dep)) return false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic change. Shouldn't change any behavior.

seen.add(dep);
if (dep instanceof Chunk) {
for (const subDep of dep.dependencies) {
Expand Down Expand Up @@ -792,7 +797,14 @@ export default class Chunk {
const module = this.orderedModules[i];
const code = this.renderedModuleSources[i];
for (const importMeta of module.importMetas) {
if (importMeta.renderFinalMechanism(code, this.id, options.format, options.compact))
if (
importMeta.renderFinalMechanism(
code,
this.id,
options.format as string,
options.compact as boolean
)
)
usesMechanism = true;
}
}
Expand All @@ -818,7 +830,7 @@ export default class Chunk {
if (!module || module.chunk === this) continue;
if (module instanceof Module) {
exportModule = module.chunk;
importName = module.chunk.getVariableExportName(variable);
importName = module.chunk.getVariableExportName(variable) as string;
} else {
exportModule = module;
importName = variable.name;
Expand All @@ -838,7 +850,7 @@ export default class Chunk {
for (const variable of importsAsArray) {
const renderedVariable =
variable instanceof ExportDefaultVariable && variable.referencesOriginal()
? variable.getOriginalVariable()
? (variable.getOriginalVariable() as Variable)
: variable;
if (
(variable.module instanceof Module
Expand All @@ -851,8 +863,8 @@ export default class Chunk {
const imported =
variable.module instanceof ExternalModule
? variable.name
: variable.module.chunk.getVariableExportName(variable);
imports.push({ local, imported });
: (variable.module as Module).chunk.getVariableExportName(variable);
imports.push({ local, imported: imported as string });
}
}

Expand All @@ -869,17 +881,17 @@ export default class Chunk {
namedExportsMode = dep.exportMode !== 'default';
}

let id: string;
let globalName: string;
let id: string = undefined as any;
let globalName: string = undefined as any;
if (dep instanceof ExternalModule) {
id = dep.setRenderPath(options, inputBase);
if (options.format === 'umd' || options.format === 'iife') {
globalName = getGlobalName(
dep,
options.globals,
options.globals as GlobalsOption,
this.graph,
exportsNames || exportsDefault
);
) as string;
}
}

Expand All @@ -888,7 +900,7 @@ export default class Chunk {
exportsNames,
globalName,
id, // chunk id updated on render
imports: imports.length > 0 ? imports : null,
imports: imports.length > 0 ? imports : (null as any),
isChunk: !(<ExternalModule>dep).isExternal,
name: dep.variableName,
namedExportsMode,
Expand Down Expand Up @@ -1022,15 +1034,15 @@ export default class Chunk {
this.dependencies,
this.imports,
usedNames,
options.format,
options.format as string,
options.interop !== false,
this.graph.preserveModules
);
}

private setUpModuleImports(module: Module) {
for (const variable of Array.from(module.imports)) {
if (variable.module.chunk !== this) {
if ((variable.module as Module).chunk !== this) {
this.imports.add(variable);
if (variable.module instanceof Module) {
variable.module.chunk.exports.add(variable);
Expand All @@ -1040,8 +1052,8 @@ export default class Chunk {
if (module.getOrCreateNamespace().included) {
for (const reexportName of Object.keys(module.reexports)) {
const reexport = module.reexports[reexportName];
const variable = reexport.module.getVariableForExportName(reexport.localName);
if (variable.module.chunk !== this) {
const variable = reexport.module.getVariableForExportName(reexport.localName) as Variable;
if ((variable.module as Module).chunk !== this) {
this.imports.add(variable);
if (variable.module instanceof Module) {
variable.module.chunk.exports.add(variable);
Expand Down
4 changes: 2 additions & 2 deletions src/ExternalModule.ts
Expand Up @@ -17,7 +17,7 @@ export default class ExternalModule {
mostCommonSuggestion: number = 0;
nameSuggestions: { [name: string]: number };
reexported: boolean = false;
renderPath: string = undefined;
renderPath: string = undefined as any;
renormalizeRenderPath = false;
used = false;
variableName: string;
Expand All @@ -30,7 +30,7 @@ export default class ExternalModule {
this.execIndex = Infinity;

const parts = id.split(/[\\/]/);
this.variableName = makeLegal(parts.pop());
this.variableName = makeLegal(parts.pop() as string);

this.nameSuggestions = Object.create(null);
this.declarations = Object.create(null);
Expand Down