From 131713d9dbd0f746d713077b45a376de4bb79ced Mon Sep 17 00:00:00 2001 From: Kim Biesbjerg Date: Sun, 8 Mar 2020 10:07:27 +0100 Subject: [PATCH] (chore) refactor to use safe navigation operator --- src/parsers/directive.parser.ts | 4 ++-- src/parsers/pipe.parser.ts | 21 ++++++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/parsers/directive.parser.ts b/src/parsers/directive.parser.ts index 8e1773f..59493cc 100644 --- a/src/parsers/directive.parser.ts +++ b/src/parsers/directive.parser.ts @@ -58,7 +58,7 @@ export class DirectiveParser implements ParserInterface { } protected isElement(node: any): node is TmplAstElement { - return node && node.attributes !== undefined && node.children !== undefined; + return node?.attributes && node?.children; } protected isTranslatable(node: TmplAstNode): boolean { @@ -70,7 +70,7 @@ export class DirectiveParser implements ParserInterface { protected getElementTranslateAttrValue(element: TmplAstElement): string { const attr: TmplAstTextAttribute = element.attributes.find(attribute => attribute.name === 'translate'); - return (attr && attr.value) || ''; + return attr?.value ?? ''; } protected getElementContents(element: TmplAstElement): string { diff --git a/src/parsers/pipe.parser.ts b/src/parsers/pipe.parser.ts index e894fec..c27b1e7 100644 --- a/src/parsers/pipe.parser.ts +++ b/src/parsers/pipe.parser.ts @@ -1,7 +1,10 @@ +import { TmplAstNode, parseTemplate, BindingPipe, LiteralPrimitive, Conditional, TmplAstTextAttribute } from '@angular/compiler'; + import { ParserInterface } from './parser.interface'; import { TranslationCollection } from '../utils/translation.collection'; import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils'; -import { TmplAstNode, parseTemplate, BindingPipe, LiteralPrimitive, Conditional, TmplAstTextAttribute } from '@angular/compiler'; + +const TRANSLATE_PIPE_NAME = 'translate'; export class PipeParser implements ParserInterface { public extract(source: string, filePath: string): TranslationCollection | null { @@ -23,7 +26,7 @@ export class PipeParser implements ParserInterface { protected findPipesInNode(node: any): BindingPipe[] { let ret: BindingPipe[] = []; - if (node.children) { + if (node?.children) { ret = node.children.reduce( (result: BindingPipe[], childNode: TmplAstNode) => { const children = this.findPipesInNode(childNode); @@ -33,27 +36,27 @@ export class PipeParser implements ParserInterface { ); } - if (node.value && node.value.ast && node.value.ast.expressions) { + if (node?.value?.ast?.expressions) { const translateables = node.value.ast.expressions.filter((exp: any) => this.expressionIsOrHasBindingPipe(exp)); ret.push(...translateables); } - if (node.attributes) { + if (node?.attributes) { const translateableAttributes = node.attributes.filter((attr: TmplAstTextAttribute) => { - return attr.name === 'translate'; + return attr.name === TRANSLATE_PIPE_NAME; }); ret = [...ret, ...translateableAttributes]; } - if (node.inputs) { + if (node?.inputs) { node.inputs.forEach((input: any) => { // - if (input.value && input.value.ast && this.expressionIsOrHasBindingPipe(input.value.ast)) { + if (input?.value?.ast && this.expressionIsOrHasBindingPipe(input.value.ast)) { ret.push(input.value.ast); } // { if (this.expressionIsOrHasBindingPipe(exp)) { ret.push(exp); @@ -82,7 +85,7 @@ export class PipeParser implements ParserInterface { } protected expressionIsOrHasBindingPipe(exp: any): boolean { - if (exp.name && exp.name === 'translate') { + if (exp.name && exp.name === TRANSLATE_PIPE_NAME) { return true; } if (exp.exp && exp.exp instanceof BindingPipe) {