fix(compiler): Update types for TypeScript nullability support

This commit is contained in:
Miško Hevery
2017-03-24 09:59:58 -07:00
committed by Hans
parent d8b73e4223
commit 09d9f5fe54
118 changed files with 2086 additions and 1859 deletions

View File

@ -31,14 +31,14 @@ export interface StaticSymbolResolverHost {
* @param modulePath is a string identifier for a module as an absolute path.
* @returns the metadata for the given module.
*/
getMetadataFor(modulePath: string): {[key: string]: any}[];
getMetadataFor(modulePath: string): {[key: string]: any}[]|undefined;
/**
* Converts a module name that is used in an `import` to a file path.
* I.e.
* `path/to/containingFile.ts` containing `import {...} from 'module-name'`.
*/
moduleNameToFileName(moduleName: string, containingFile: string): string /*|null*/;
moduleNameToFileName(moduleName: string, containingFile?: string): string|null;
}
const SUPPORTED_SCHEMA_VERSION = 3;
@ -64,17 +64,17 @@ export class StaticSymbolResolver {
constructor(
private host: StaticSymbolResolverHost, private staticSymbolCache: StaticSymbolCache,
private summaryResolver: SummaryResolver<StaticSymbol>,
private errorRecorder?: (error: any, fileName: string) => void) {}
private errorRecorder?: (error: any, fileName?: string) => void) {}
resolveSymbol(staticSymbol: StaticSymbol): ResolvedStaticSymbol {
if (staticSymbol.members.length > 0) {
return this._resolveSymbolMembers(staticSymbol);
return this._resolveSymbolMembers(staticSymbol) !;
}
let result = this.resolvedSymbols.get(staticSymbol);
if (result) {
return result;
}
result = this._resolveSymbolFromSummary(staticSymbol);
result = this._resolveSymbolFromSummary(staticSymbol) !;
if (result) {
return result;
}
@ -82,7 +82,7 @@ export class StaticSymbolResolver {
// have summaries, only .d.ts files. So we always need to check both, the summary
// and metadata.
this._createSymbolsOf(staticSymbol.filePath);
result = this.resolvedSymbols.get(staticSymbol);
result = this.resolvedSymbols.get(staticSymbol) !;
return result;
}
@ -95,7 +95,7 @@ export class StaticSymbolResolver {
*
* @param staticSymbol the symbol for which to generate a import symbol
*/
getImportAs(staticSymbol: StaticSymbol): StaticSymbol {
getImportAs(staticSymbol: StaticSymbol): StaticSymbol|null {
if (staticSymbol.members.length) {
const baseSymbol = this.getStaticSymbol(staticSymbol.filePath, staticSymbol.name);
const baseImportAs = this.getImportAs(baseSymbol);
@ -105,7 +105,7 @@ export class StaticSymbolResolver {
}
let result = this.summaryResolver.getImportAs(staticSymbol);
if (!result) {
result = this.importAs.get(staticSymbol);
result = this.importAs.get(staticSymbol) !;
}
return result;
}
@ -123,7 +123,7 @@ export class StaticSymbolResolver {
* getTypeArity returns the number of generic type parameters the given symbol
* has. If the symbol is not a type the result is null.
*/
getTypeArity(staticSymbol: StaticSymbol): number /*|null*/ {
getTypeArity(staticSymbol: StaticSymbol): number|null {
// If the file is a factory file, don't resolve the symbol as doing so would
// cause the metadata for an factory file to be loaded which doesn't exist.
// All references to generated classes must include the correct arity whenever
@ -163,7 +163,7 @@ export class StaticSymbolResolver {
}
}
private _resolveSymbolMembers(staticSymbol: StaticSymbol): ResolvedStaticSymbol {
private _resolveSymbolMembers(staticSymbol: StaticSymbol): ResolvedStaticSymbol|null {
const members = staticSymbol.members;
const baseResolvedSymbol =
this.resolveSymbol(this.getStaticSymbol(staticSymbol.filePath, staticSymbol.name));
@ -188,7 +188,7 @@ export class StaticSymbolResolver {
return null;
}
private _resolveSymbolFromSummary(staticSymbol: StaticSymbol): ResolvedStaticSymbol {
private _resolveSymbolFromSummary(staticSymbol: StaticSymbol): ResolvedStaticSymbol|null {
const summary = this.summaryResolver.resolveSummary(staticSymbol);
return summary ? new ResolvedStaticSymbol(staticSymbol, summary.metadata) : null;
}
@ -252,7 +252,7 @@ export class StaticSymbolResolver {
const originFilePath = this.resolveModule(origin, filePath);
if (!originFilePath) {
this.reportError(
new Error(`Couldn't resolve original symbol for ${origin} from ${filePath}`), null);
new Error(`Couldn't resolve original symbol for ${origin} from ${filePath}`));
} else {
this.symbolResourcePaths.set(symbol, originFilePath);
}
@ -337,7 +337,7 @@ export class StaticSymbolResolver {
}
let filePath: string;
if (module) {
filePath = self.resolveModule(module, sourceSymbol.filePath);
filePath = self.resolveModule(module, sourceSymbol.filePath) !;
if (!filePath) {
return {
__symbolic: 'error',
@ -381,7 +381,7 @@ export class StaticSymbolResolver {
return new ResolvedStaticSymbol(sourceSymbol, targetSymbol);
}
private reportError(error: Error, context: StaticSymbol, path?: string) {
private reportError(error: Error, context?: StaticSymbol, path?: string) {
if (this.errorRecorder) {
this.errorRecorder(error, (context && context.filePath) || path);
} else {
@ -413,7 +413,7 @@ export class StaticSymbolResolver {
const errorMessage = moduleMetadata['version'] == 2 ?
`Unsupported metadata version ${moduleMetadata['version']} for module ${module}. This module should be compiled with a newer version of ngc` :
`Metadata version mismatch for module ${module}, found version ${moduleMetadata['version']}, expected ${SUPPORTED_SCHEMA_VERSION}`;
this.reportError(new Error(errorMessage), null);
this.reportError(new Error(errorMessage));
}
this.metadataCache.set(module, moduleMetadata);
}
@ -426,20 +426,20 @@ export class StaticSymbolResolver {
this.reportError(
new Error(`Could not resolve module ${module}${containingFile ? ` relative to $ {
containingFile
} `: ''}`),
null);
} `: ''}`));
return this.getStaticSymbol(`ERROR:${module}`, symbolName);
}
return this.getStaticSymbol(filePath, symbolName);
}
private resolveModule(module: string, containingFile: string): string {
private resolveModule(module: string, containingFile?: string): string|null {
try {
return this.host.moduleNameToFileName(module, containingFile);
} catch (e) {
console.error(`Could not resolve module '${module}' relative to file ${containingFile}`);
this.reportError(e, null, containingFile);
this.reportError(e, undefined, containingFile);
}
return null;
}
}
@ -447,4 +447,4 @@ export class StaticSymbolResolver {
// See https://github.com/Microsoft/TypeScript/blob/master/src/compiler/utilities.ts
export function unescapeIdentifier(identifier: string): string {
return identifier.startsWith('___') ? identifier.substr(1) : identifier;
}
}