From 26dd51221a256ae48faf2dfbd56ca7ed9fa8613b Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Tue, 27 Aug 2019 19:25:15 +0200 Subject: [PATCH] style: collapse if statements (#15449) --- .../angular/cli/commands/generate-impl.ts | 6 ++--- .../angular/cli/models/architect-command.ts | 6 ++--- packages/angular/cli/models/parser.ts | 12 +++------ .../angular/cli/models/schematic-command.ts | 6 ++--- .../plugins/bundle-budget.ts | 20 ++++++-------- .../build_angular/src/dev-server/index.ts | 27 ++++++++++--------- .../core/src/json/schema/registry.ts | 22 +++++++-------- .../schematics/src/utility/update-buffer.ts | 13 ++++----- 8 files changed, 48 insertions(+), 64 deletions(-) diff --git a/packages/angular/cli/commands/generate-impl.ts b/packages/angular/cli/commands/generate-impl.ts index 98a64c7c98..3fa015da84 100644 --- a/packages/angular/cli/commands/generate-impl.ts +++ b/packages/angular/cli/commands/generate-impl.ts @@ -98,10 +98,8 @@ export class GenerateCommand extends SchematicCommand { let schematicName = options.schematic; - if (schematicName) { - if (schematicName.includes(':')) { - [collectionName, schematicName] = schematicName.split(':', 2); - } + if (schematicName && schematicName.includes(':')) { + [collectionName, schematicName] = schematicName.split(':', 2); } return [collectionName, schematicName]; diff --git a/packages/angular/cli/models/architect-command.ts b/packages/angular/cli/models/architect-command.ts index 3677e68c64..34bf24a76c 100644 --- a/packages/angular/cli/models/architect-command.ts +++ b/packages/angular/cli/models/architect-command.ts @@ -179,10 +179,8 @@ export abstract class ArchitectCommand< // Update options to remove analytics from options if the builder isn't safelisted. for (const o of this.description.options) { - if (o.userAnalytics) { - if (!isPackageNameSafeForAnalytics(builderConf)) { - o.userAnalytics = undefined; - } + if (o.userAnalytics && !isPackageNameSafeForAnalytics(builderConf)) { + o.userAnalytics = undefined; } } } diff --git a/packages/angular/cli/models/parser.ts b/packages/angular/cli/models/parser.ts index 2d5dbf7495..f8c65ecd14 100644 --- a/packages/angular/cli/models/parser.ts +++ b/packages/angular/cli/models/parser.ts @@ -84,10 +84,8 @@ function _coerce(str: string | undefined, o: Option | null, v?: Value): Value | // enum. If there's no enum, just return the first one that matches. for (const type of types) { const maybeResult = _coerceType(str, type, v); - if (maybeResult !== undefined) { - if (!o.enum || o.enum.includes(maybeResult)) { - return maybeResult; - } + if (maybeResult !== undefined && (!o.enum || o.enum.includes(maybeResult))) { + return maybeResult; } } @@ -159,11 +157,9 @@ function _assignOption( value = nextArg; let shouldShift = true; - if (value && value.startsWith('-')) { + if (value && value.startsWith('-') && _coerce(undefined, maybeOption) !== undefined) { // Verify if not having a value results in a correct parse, if so don't shift. - if (_coerce(undefined, maybeOption) !== undefined) { - shouldShift = false; - } + shouldShift = false; } // Only absorb it if it leads to a better value. diff --git a/packages/angular/cli/models/schematic-command.ts b/packages/angular/cli/models/schematic-command.ts index 6daecb163b..3047d7f167 100644 --- a/packages/angular/cli/models/schematic-command.ts +++ b/packages/angular/cli/models/schematic-command.ts @@ -103,10 +103,8 @@ export abstract class SchematicCommand< // Remove any user analytics from schematics that are NOT part of our safelist. for (const o of this.description.options) { - if (o.userAnalytics) { - if (!isPackageNameSafeForAnalytics(this.collectionName)) { - o.userAnalytics = undefined; - } + if (o.userAnalytics && !isPackageNameSafeForAnalytics(this.collectionName)) { + o.userAnalytics = undefined; } } } diff --git a/packages/angular_devkit/build_angular/src/angular-cli-files/plugins/bundle-budget.ts b/packages/angular_devkit/build_angular/src/angular-cli-files/plugins/bundle-budget.ts index 911e4c55d8..ca2284b35a 100644 --- a/packages/angular_devkit/build_angular/src/angular-cli-files/plugins/bundle-budget.ts +++ b/packages/angular_devkit/build_angular/src/angular-cli-files/plugins/bundle-budget.ts @@ -56,22 +56,18 @@ export class BundleBudgetPlugin { } private checkMinimum(threshold: number | undefined, size: Size, messages: string[]) { - if (threshold) { - if (threshold > size.size) { - const sizeDifference = formatSize(threshold - size.size); - messages.push(`budgets, minimum exceeded for ${size.label}. ` - + `Budget ${formatSize(threshold)} was not reached by ${sizeDifference}.`); - } + if (threshold && threshold > size.size) { + const sizeDifference = formatSize(threshold - size.size); + messages.push(`budgets, minimum exceeded for ${size.label}. ` + + `Budget ${formatSize(threshold)} was not reached by ${sizeDifference}.`); } } private checkMaximum(threshold: number | undefined, size: Size, messages: string[]) { - if (threshold) { - if (threshold < size.size) { - const sizeDifference = formatSize(size.size - threshold); - messages.push(`budgets, maximum exceeded for ${size.label}. ` - + `Budget ${formatSize(threshold)} was exceeded by ${sizeDifference}.`); - } + if (threshold && threshold < size.size) { + const sizeDifference = formatSize(size.size - threshold); + messages.push(`budgets, maximum exceeded for ${size.label}. ` + + `Budget ${formatSize(threshold)} was exceeded by ${sizeDifference}.`); } } diff --git a/packages/angular_devkit/build_angular/src/dev-server/index.ts b/packages/angular_devkit/build_angular/src/dev-server/index.ts index c6d8cf9f6c..6b15ab96eb 100644 --- a/packages/angular_devkit/build_angular/src/dev-server/index.ts +++ b/packages/angular_devkit/build_angular/src/dev-server/index.ts @@ -286,20 +286,23 @@ export function buildServerConfig( browserOptions: BrowserBuilderSchema, logger: logging.LoggerApi, ): WebpackDevServer.Configuration { - if (serverOptions.host) { - // Check that the host is either localhost or prints out a message. - if (!/^127\.\d+\.\d+\.\d+/g.test(serverOptions.host) && serverOptions.host !== 'localhost') { - logger.warn(tags.stripIndent` - WARNING: This is a simple server for use in testing or debugging Angular applications - locally. It hasn't been reviewed for security issues. + // Check that the host is either localhost or prints out a message. + if ( + serverOptions.host + && !/^127\.\d+\.\d+\.\d+/g.test(serverOptions.host) + && serverOptions.host !== 'localhost' + ) { + logger.warn(tags.stripIndent` + WARNING: This is a simple server for use in testing or debugging Angular applications + locally. It hasn't been reviewed for security issues. - Binding this server to an open connection can result in compromising your application or - computer. Using a different host than the one passed to the "--host" flag might result in - websocket connection issues. You might need to use "--disableHostCheck" if that's the - case. - `); - } + Binding this server to an open connection can result in compromising your application or + computer. Using a different host than the one passed to the "--host" flag might result in + websocket connection issues. You might need to use "--disableHostCheck" if that's the + case. + `); } + if (serverOptions.disableHostCheck) { logger.warn(tags.oneLine` WARNING: Running a server with --disable-host-check is a security risk. diff --git a/packages/angular_devkit/core/src/json/schema/registry.ts b/packages/angular_devkit/core/src/json/schema/registry.ts index 6245bdd2f4..146347c381 100644 --- a/packages/angular_devkit/core/src/json/schema/registry.ts +++ b/packages/angular_devkit/core/src/json/schema/registry.ts @@ -549,18 +549,16 @@ export class CoreSchemaRegistry implements SchemaRegistry { } } - if (type === 'list' && !items) { - if (Array.isArray(parentSchema.enum)) { - type = 'list'; - items = []; - for (const value of parentSchema.enum) { - if (typeof value == 'string') { - items.push(value); - } else if (typeof value == 'object') { - // Invalid - } else { - items.push({ label: value.toString(), value }); - } + if (type === 'list' && !items && Array.isArray(parentSchema.enum)) { + type = 'list'; + items = []; + for (const value of parentSchema.enum) { + if (typeof value == 'string') { + items.push(value); + } else if (typeof value == 'object') { + // Invalid + } else { + items.push({ label: value.toString(), value }); } } } diff --git a/packages/angular_devkit/schematics/src/utility/update-buffer.ts b/packages/angular_devkit/schematics/src/utility/update-buffer.ts index 165305ddab..4389f8ab24 100644 --- a/packages/angular_devkit/schematics/src/utility/update-buffer.ts +++ b/packages/angular_devkit/schematics/src/utility/update-buffer.ts @@ -128,15 +128,12 @@ export class Chunk { } assert(left: boolean, _content: boolean, right: boolean) { - if (left) { - if (this._assertLeft) { - throw new ContentCannotBeRemovedException(); - } + if (left && this._assertLeft) { + throw new ContentCannotBeRemovedException(); } - if (right) { - if (this._assertRight) { - throw new ContentCannotBeRemovedException(); - } + + if (right && this._assertRight) { + throw new ContentCannotBeRemovedException(); } }