refactor(ivy): ngcc - use `.has()` to check Map membership (#25445)

Previously we were relying upon the `.get()` method to return `undefined`
but it is clearer and safer to always check with `.has()` first.

PR Close #25445
This commit is contained in:
Pete Bacon Darwin 2019-05-14 08:36:57 +01:00 committed by Jason Aden
parent edd775eabc
commit 757d4c33df
4 changed files with 25 additions and 16 deletions

View File

@ -63,7 +63,7 @@ export class ModuleWithProvidersAnalyzer {
ngModule = {node: dtsNgModule, viaModule: null}; ngModule = {node: dtsNgModule, viaModule: null};
} }
const dtsFile = dtsFn.getSourceFile(); const dtsFile = dtsFn.getSourceFile();
const analysis = analyses.get(dtsFile) || []; const analysis = analyses.has(dtsFile) ? analyses.get(dtsFile) : [];
analysis.push({declaration: dtsFn, ngModule}); analysis.push({declaration: dtsFn, ngModule});
analyses.set(dtsFile, analysis); analyses.set(dtsFile, analysis);
} }

View File

@ -49,8 +49,8 @@ export class PrivateDeclarationsAnalyzer {
if (exports) { if (exports) {
exports.forEach((declaration, exportedName) => { exports.forEach((declaration, exportedName) => {
if (hasNameIdentifier(declaration.node)) { if (hasNameIdentifier(declaration.node)) {
const privateDeclaration = privateDeclarations.get(declaration.node.name); if (privateDeclarations.has(declaration.node.name)) {
if (privateDeclaration) { const privateDeclaration = privateDeclarations.get(declaration.node.name) !;
if (privateDeclaration.node !== declaration.node) { if (privateDeclaration.node !== declaration.node) {
throw new Error(`${declaration.node.name.text} is declared multiple times.`); throw new Error(`${declaration.node.name.text} is declared multiple times.`);
} }
@ -96,7 +96,7 @@ export class PrivateDeclarationsAnalyzer {
return Array.from(privateDeclarations.keys()).map(id => { return Array.from(privateDeclarations.keys()).map(id => {
const from = AbsoluteFsPath.fromSourceFile(id.getSourceFile()); const from = AbsoluteFsPath.fromSourceFile(id.getSourceFile());
const declaration = privateDeclarations.get(id) !; const declaration = privateDeclarations.get(id) !;
const alias = exportAliasDeclarations.get(id) || null; const alias = exportAliasDeclarations.has(id) ? exportAliasDeclarations.get(id) ! : null;
const dtsDeclaration = this.host.getDtsDeclaration(declaration.node); const dtsDeclaration = this.host.getDtsDeclaration(declaration.node);
const dtsFrom = const dtsFrom =
dtsDeclaration && AbsoluteFsPath.fromSourceFile(dtsDeclaration.getSourceFile()); dtsDeclaration && AbsoluteFsPath.fromSourceFile(dtsDeclaration.getSourceFile());

View File

@ -366,7 +366,9 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
throw new Error( throw new Error(
`Cannot get the dts file for a declaration that has no name: ${declaration.getText()} in ${declaration.getSourceFile().fileName}`); `Cannot get the dts file for a declaration that has no name: ${declaration.getText()} in ${declaration.getSourceFile().fileName}`);
} }
return this.dtsDeclarationMap.get(declaration.name.text) || null; return this.dtsDeclarationMap.has(declaration.name.text) ?
this.dtsDeclarationMap.get(declaration.name.text) ! :
null;
} }
/** /**
@ -419,7 +421,9 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
*/ */
protected resolveAliasedClassIdentifier(declaration: ts.Declaration): ts.Identifier|null { protected resolveAliasedClassIdentifier(declaration: ts.Declaration): ts.Identifier|null {
this.ensurePreprocessed(declaration.getSourceFile()); this.ensurePreprocessed(declaration.getSourceFile());
return this.aliasedClassDeclarations.get(declaration) || null; return this.aliasedClassDeclarations.has(declaration) ?
this.aliasedClassDeclarations.get(declaration) ! :
null;
} }
/** /**
@ -738,7 +742,8 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
helperCall, makeMemberTargetFilter(classSymbol.name)); helperCall, makeMemberTargetFilter(classSymbol.name));
memberDecorators.forEach((decorators, memberName) => { memberDecorators.forEach((decorators, memberName) => {
if (memberName) { if (memberName) {
const memberDecorators = memberDecoratorMap.get(memberName) || []; const memberDecorators =
memberDecoratorMap.has(memberName) ? memberDecoratorMap.get(memberName) ! : [];
const coreDecorators = decorators.filter(decorator => this.isFromCore(decorator)); const coreDecorators = decorators.filter(decorator => this.isFromCore(decorator));
memberDecoratorMap.set(memberName, memberDecorators.concat(coreDecorators)); memberDecoratorMap.set(memberName, memberDecorators.concat(coreDecorators));
} }
@ -775,7 +780,8 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
if (keyName === undefined) { if (keyName === undefined) {
classDecorators.push(decorator); classDecorators.push(decorator);
} else { } else {
const decorators = memberDecorators.get(keyName) || []; const decorators =
memberDecorators.has(keyName) ? memberDecorators.get(keyName) ! : [];
decorators.push(decorator); decorators.push(decorator);
memberDecorators.set(keyName, decorators); memberDecorators.set(keyName, decorators);
} }
@ -874,8 +880,8 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
const decorator = reflectObjectLiteral(node); const decorator = reflectObjectLiteral(node);
// Is the value of the `type` property an identifier? // Is the value of the `type` property an identifier?
let typeIdentifier = decorator.get('type'); if (decorator.has('type')) {
if (typeIdentifier) { let typeIdentifier = decorator.get('type') !;
if (ts.isPropertyAccessExpression(typeIdentifier)) { if (ts.isPropertyAccessExpression(typeIdentifier)) {
// the type is in a namespace, e.g. `core.Directive` // the type is in a namespace, e.g. `core.Directive`
typeIdentifier = typeIdentifier.name; typeIdentifier = typeIdentifier.name;
@ -1036,8 +1042,8 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
*/ */
protected getConstructorParameterDeclarations(classSymbol: ClassSymbol): protected getConstructorParameterDeclarations(classSymbol: ClassSymbol):
ts.ParameterDeclaration[]|null { ts.ParameterDeclaration[]|null {
const constructorSymbol = classSymbol.members && classSymbol.members.get(CONSTRUCTOR); if (classSymbol.members && classSymbol.members.has(CONSTRUCTOR)) {
if (constructorSymbol) { const constructorSymbol = classSymbol.members.get(CONSTRUCTOR) !;
// For some reason the constructor does not have a `valueDeclaration` ?!? // For some reason the constructor does not have a `valueDeclaration` ?!?
const constructor = constructorSymbol.declarations && const constructor = constructorSymbol.declarations &&
constructorSymbol.declarations[0] as ts.ConstructorDeclaration | undefined; constructorSymbol.declarations[0] as ts.ConstructorDeclaration | undefined;
@ -1113,8 +1119,10 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
element => element =>
ts.isObjectLiteralExpression(element) ? reflectObjectLiteral(element) : null) ts.isObjectLiteralExpression(element) ? reflectObjectLiteral(element) : null)
.map(paramInfo => { .map(paramInfo => {
const typeExpression = paramInfo && paramInfo.get('type') || null; const typeExpression =
const decoratorInfo = paramInfo && paramInfo.get('decorators') || null; paramInfo && paramInfo.has('type') ? paramInfo.get('type') ! : null;
const decoratorInfo =
paramInfo && paramInfo.has('decorators') ? paramInfo.get('decorators') ! : null;
const decorators = decoratorInfo && const decorators = decoratorInfo &&
this.reflectDecorators(decoratorInfo) this.reflectDecorators(decoratorInfo)
.filter(decorator => this.isFromCore(decorator)); .filter(decorator => this.isFromCore(decorator));

View File

@ -339,8 +339,9 @@ export class Esm5ReflectionHost extends Esm2015ReflectionHost {
if (expression && ts.isArrayLiteralExpression(expression)) { if (expression && ts.isArrayLiteralExpression(expression)) {
const elements = expression.elements; const elements = expression.elements;
return elements.map(reflectArrayElement).map(paramInfo => { return elements.map(reflectArrayElement).map(paramInfo => {
const typeExpression = paramInfo && paramInfo.get('type') || null; const typeExpression = paramInfo && paramInfo.has('type') ? paramInfo.get('type') ! : null;
const decoratorInfo = paramInfo && paramInfo.get('decorators') || null; const decoratorInfo =
paramInfo && paramInfo.has('decorators') ? paramInfo.get('decorators') ! : null;
const decorators = decoratorInfo && this.reflectDecorators(decoratorInfo); const decorators = decoratorInfo && this.reflectDecorators(decoratorInfo);
return {typeExpression, decorators}; return {typeExpression, decorators};
}); });