refactor: simplify `isPresent(x) ? x : y` to `x || y` (#12166)

Closes #12166
This commit is contained in:
Victor Berchet 2016-10-07 18:11:37 -07:00 committed by Tobias Bosch
parent bdcf46f82e
commit d972d82354
18 changed files with 40 additions and 43 deletions

View File

@ -212,7 +212,7 @@ function createCountingValidator(
return new MockValidator(log, (completeSample: MeasureValues[]) => {
count--;
if (count === 0) {
return isPresent(validSample) ? validSample : completeSample;
return validSample || completeSample;
} else {
return null;
}

View File

@ -304,7 +304,7 @@ export class CompileTemplateMetadata {
this.styleUrls = _normalizeArray(styleUrls);
this.externalStylesheets = _normalizeArray(externalStylesheets);
this.animations = isPresent(animations) ? ListWrapper.flatten(animations) : [];
this.ngContentSelectors = isPresent(ngContentSelectors) ? ngContentSelectors : [];
this.ngContentSelectors = ngContentSelectors || [];
if (isPresent(interpolation) && interpolation.length != 2) {
throw new Error(`'interpolation' should have a start and an end symbol.`);
}
@ -590,7 +590,7 @@ export function removeIdentifierDuplicates<T extends CompileMetadataWithIdentifi
}
function _normalizeArray(obj: any[]): any[] {
return isPresent(obj) ? obj : [];
return obj || [];
}
export function isStaticSymbol(value: any): value is StaticSymbol {

View File

@ -476,7 +476,7 @@ export class CssScanner {
var index: number = this.index;
var column: number = this.column;
var line: number = this.line;
errorTokenValue = isPresent(errorTokenValue) ? errorTokenValue : String.fromCharCode(this.peek);
errorTokenValue = errorTokenValue || String.fromCharCode(this.peek);
var invalidToken = new CssToken(index, column, line, CssTokenType.Invalid, errorTokenValue);
var errorMessage =
generateErrorMessage(this.input, message, errorTokenValue, index, line, column);

View File

@ -163,11 +163,11 @@ class _InjectorBuilder {
if (isPresent(provider.useExisting)) {
result = this._getDependency(new CompileDiDependencyMetadata({token: provider.useExisting}));
} else if (isPresent(provider.useFactory)) {
var deps = isPresent(provider.deps) ? provider.deps : provider.useFactory.diDeps;
var deps = provider.deps || provider.useFactory.diDeps;
var depsExpr = deps.map((dep) => this._getDependency(dep));
result = o.importExpr(provider.useFactory).callFn(depsExpr);
} else if (isPresent(provider.useClass)) {
var deps = isPresent(provider.deps) ? provider.deps : provider.useClass.diDeps;
var deps = provider.deps || provider.useClass.diDeps;
var depsExpr = deps.map((dep) => this._getDependency(dep));
result =
o.importExpr(provider.useClass).instantiate(depsExpr, o.importType(provider.useClass));

View File

@ -215,7 +215,7 @@ export class ReadVarExpr extends Expression {
export class WriteVarExpr extends Expression {
public value: Expression;
constructor(public name: string, value: Expression, type: Type = null) {
super(isPresent(type) ? type : value.type);
super(type || value.type);
this.value = value;
}
@ -233,7 +233,7 @@ export class WriteKeyExpr extends Expression {
public value: Expression;
constructor(
public receiver: Expression, public index: Expression, value: Expression, type: Type = null) {
super(isPresent(type) ? type : value.type);
super(type || value.type);
this.value = value;
}
visitExpression(visitor: ExpressionVisitor, context: any): any {
@ -246,7 +246,7 @@ export class WritePropExpr extends Expression {
public value: Expression;
constructor(
public receiver: Expression, public name: string, value: Expression, type: Type = null) {
super(isPresent(type) ? type : value.type);
super(type || value.type);
this.value = value;
}
visitExpression(visitor: ExpressionVisitor, context: any): any {
@ -322,7 +322,7 @@ export class ConditionalExpr extends Expression {
constructor(
public condition: Expression, trueCase: Expression, public falseCase: Expression = null,
type: Type = null) {
super(isPresent(type) ? type : trueCase.type);
super(type || trueCase.type);
this.trueCase = trueCase;
}
visitExpression(visitor: ExpressionVisitor, context: any): any {
@ -369,7 +369,7 @@ export class BinaryOperatorExpr extends Expression {
public lhs: Expression;
constructor(
public operator: BinaryOperator, lhs: Expression, public rhs: Expression, type: Type = null) {
super(isPresent(type) ? type : lhs.type);
super(type || lhs.type);
this.lhs = lhs;
}
visitExpression(visitor: ExpressionVisitor, context: any): any {
@ -479,7 +479,7 @@ export class DeclareVarStmt extends Statement {
public name: string, public value: Expression, type: Type = null,
modifiers: StmtModifier[] = null) {
super(modifiers);
this.type = isPresent(type) ? type : value.type;
this.type = type || value.type;
}
visitStatement(visitor: StatementVisitor, context: any): any {
@ -625,7 +625,7 @@ export class ExpressionTransformer implements StatementVisitor, ExpressionVisito
expr.value.visitExpression(this, context));
}
visitInvokeMethodExpr(ast: InvokeMethodExpr, context: any): any {
var method = isPresent(ast.builtin) ? ast.builtin : ast.name;
var method = ast.builtin || ast.name;
return new InvokeMethodExpr(
ast.receiver.visitExpression(this, context), method,
this.visitAllExpressions(ast.args, context), ast.type);

View File

@ -102,7 +102,7 @@ export class ProviderElementContext {
private _addQueryReadsTo(token: CompileTokenMetadata, queryReadTokens: Map<any, boolean>) {
this._getQueriesFor(token).forEach((query) => {
const queryReadToken = isPresent(query.read) ? query.read : token;
const queryReadToken = query.read || token;
if (isBlank(queryReadTokens.get(queryReadToken.reference))) {
queryReadTokens.set(queryReadToken.reference, true);
}
@ -169,11 +169,11 @@ export class ProviderElementContext {
transformedUseValue = existingDiDep.value;
}
} else if (isPresent(provider.useFactory)) {
var deps = isPresent(provider.deps) ? provider.deps : provider.useFactory.diDeps;
var deps = provider.deps || provider.useFactory.diDeps;
transformedDeps =
deps.map((dep) => this._getDependency(resolvedProvider.providerType, dep, eager));
} else if (isPresent(provider.useClass)) {
var deps = isPresent(provider.deps) ? provider.deps : provider.useClass.diDeps;
var deps = provider.deps || provider.useClass.diDeps;
transformedDeps =
deps.map((dep) => this._getDependency(resolvedProvider.providerType, dep, eager));
}
@ -338,11 +338,11 @@ export class NgModuleProviderAnalyzer {
transformedUseValue = existingDiDep.value;
}
} else if (isPresent(provider.useFactory)) {
var deps = isPresent(provider.deps) ? provider.deps : provider.useFactory.diDeps;
var deps = provider.deps || provider.useFactory.diDeps;
transformedDeps =
deps.map((dep) => this._getDependency(dep, eager, resolvedProvider.sourceSpan));
} else if (isPresent(provider.useClass)) {
var deps = isPresent(provider.deps) ? provider.deps : provider.useClass.diDeps;
var deps = provider.deps || provider.useClass.diDeps;
transformedDeps =
deps.map((dep) => this._getDependency(dep, eager, resolvedProvider.sourceSpan));
}

View File

@ -31,7 +31,7 @@ export function extractStyleUrls(
resolver: UrlResolver, baseUrl: string, cssText: string): StyleWithImports {
var foundUrls: string[] = [];
var modifiedCssText = cssText.replace(_cssImportRe, function(...m: string[]) {
const url = isPresent(m[1]) ? m[1] : m[2];
const url = m[1] || m[2];
if (!isStyleUrlResolvable(url)) {
// Do not attempt to resolve non-package absolute URLs with URI scheme
return m[0];

View File

@ -161,11 +161,11 @@ export class CompileElement extends CompileNode {
resolvedProvider.providerType,
new CompileDiDependencyMetadata({token: provider.useExisting}));
} else if (isPresent(provider.useFactory)) {
var deps = isPresent(provider.deps) ? provider.deps : provider.useFactory.diDeps;
var deps = provider.deps || provider.useFactory.diDeps;
var depsExpr = deps.map((dep) => this._getDependency(resolvedProvider.providerType, dep));
return o.importExpr(provider.useFactory).callFn(depsExpr);
} else if (isPresent(provider.useClass)) {
var deps = isPresent(provider.deps) ? provider.deps : provider.useClass.diDeps;
var deps = provider.deps || provider.useClass.diDeps;
var depsExpr = deps.map((dep) => this._getDependency(resolvedProvider.providerType, dep));
return o.importExpr(provider.useClass)
.instantiate(depsExpr, o.importType(provider.useClass));
@ -435,6 +435,6 @@ function createProviderProperty(
class _QueryWithRead {
public read: CompileTokenMetadata;
constructor(public query: CompileQuery, match: CompileTokenMetadata) {
this.read = isPresent(query.meta.read) ? query.meta.read : match;
this.read = query.meta.read || match;
}
}

View File

@ -59,7 +59,7 @@ export class CompileMethod {
resetDebugInfoExpr(nodeIndex: number, templateAst: TemplateAst): o.Expression {
var res = this._updateDebugContext(new _DebugState(nodeIndex, templateAst));
return isPresent(res) ? res : o.NULL_EXPR;
return res || o.NULL_EXPR;
}
resetDebugInfo(nodeIndex: number, templateAst: TemplateAst) {

View File

@ -59,8 +59,7 @@ export class CompileEventListener {
this._hasComponentHostListener = true;
}
this._method.resetDebugInfo(this.compileElement.nodeIndex, hostEvent);
var context = isPresent(directiveInstance) ? directiveInstance :
this.compileElement.view.componentContext;
var context = directiveInstance || this.compileElement.view.componentContext;
var actionStmts = convertCdStatementToIr(
this.compileElement.view, context, hostEvent.handler, this.compileElement.nodeIndex);
var lastIndex = actionStmts.length - 1;

View File

@ -79,10 +79,10 @@ class ViewBuilderVisitor implements TemplateAstVisitor {
if (this._isRootNode(parent)) {
// store appElement as root node only for ViewContainers
if (this.view.viewType !== ViewType.COMPONENT) {
this.view.rootNodesOrAppElements.push(isPresent(vcAppEl) ? vcAppEl : node.renderNode);
this.view.rootNodesOrAppElements.push(vcAppEl || node.renderNode);
}
} else if (isPresent(parent.component) && isPresent(ngContentIndex)) {
parent.addContentNode(ngContentIndex, isPresent(vcAppEl) ? vcAppEl : node.renderNode);
parent.addContentNode(ngContentIndex, vcAppEl || node.renderNode);
}
}

View File

@ -26,7 +26,7 @@ class MyVisitor implements CssAstVisitor {
* @internal
*/
_capture(method: string, ast: CssAst, context: any) {
this.captures[method] = isPresent(this.captures[method]) ? this.captures[method] : [];
this.captures[method] = this.captures[method] || [];
this.captures[method].push([ast, context]);
}

View File

@ -46,16 +46,14 @@ export class MockDirectiveResolver extends DirectiveResolver {
let providers = metadata.providers;
if (isPresent(providerOverrides)) {
const originalViewProviders: Provider[] =
isPresent(metadata.providers) ? metadata.providers : [];
const originalViewProviders: Provider[] = metadata.providers || [];
providers = originalViewProviders.concat(providerOverrides);
}
if (metadata instanceof Component) {
let viewProviders = metadata.viewProviders;
if (isPresent(viewProviderOverrides)) {
const originalViewProviders: Provider[] =
isPresent(metadata.viewProviders) ? metadata.viewProviders : [];
const originalViewProviders: Provider[] = metadata.viewProviders || [];
viewProviders = originalViewProviders.concat(viewProviderOverrides);
}

View File

@ -47,7 +47,7 @@ export class DefaultIterableDiffer implements IterableDiffer {
private _identityChangesTail: CollectionChangeRecord = null;
constructor(private _trackByFn?: TrackByFn) {
this._trackByFn = isPresent(this._trackByFn) ? this._trackByFn : trackByIdentity;
this._trackByFn = this._trackByFn || trackByIdentity;
}
get collection() { return this._collection; }

View File

@ -159,7 +159,7 @@ export class ViewContainerRef_ implements ViewContainerRef {
componentFactory: ComponentFactory<C>, index: number = -1, injector: Injector = null,
projectableNodes: any[][] = null): ComponentRef<C> {
var s = this._createComponentInContainerScope();
var contextInjector = isPresent(injector) ? injector : this._element.parentInjector;
var contextInjector = injector || this._element.parentInjector;
var componentRef = componentFactory.create(contextInjector, projectableNodes);
this.insert(componentRef.hostView, index);
return wtfLeave(s, componentRef);

View File

@ -52,13 +52,14 @@ function _find(control: AbstractControl, path: Array<string|number>| string, del
return (<Array<string|number>>path).reduce((v, name) => {
if (v instanceof FormGroup) {
return isPresent(v.controls[name]) ? v.controls[name] : null;
} else if (v instanceof FormArray) {
var index = <number>name;
return isPresent(v.at(index)) ? v.at(index) : null;
} else {
return null;
return v.controls[name] || null;
}
if (v instanceof FormArray) {
return v.at(<number>name) || null;
}
return null;
}, control);
}

View File

@ -27,7 +27,7 @@ export class ResourceLoaderImpl extends ResourceLoader {
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
// response/responseType properties were introduced in ResourceLoader Level2 spec (supported
// by IE10)
var response = isPresent(xhr.response) ? xhr.response : xhr.responseText;
var response = xhr.response || xhr.responseText;
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
var status = xhr.status === 1223 ? 204 : xhr.status;

View File

@ -78,8 +78,7 @@ class MultiplyDirectiveResolver extends DirectiveResolver {
}
resolve(component: Type): ViewMetadata {
var result = this._cache.get(component);
return isPresent(result) ? result : super.resolve(component);
return this._cache.get(component) || super.resolve(component);
}
}