(chore) refactor to use safe navigation operator

This commit is contained in:
Kim Biesbjerg 2020-03-08 10:07:27 +01:00
parent a17ad9c373
commit 131713d9db
2 changed files with 14 additions and 11 deletions

View File

@ -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 {

View File

@ -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) => {
// <element [attrib]="'identifier' | translate">
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);
}
// <element attrib="{{'identifier' | translate}}>"
if (input.value && input.value.ast && input.value.ast.expressions) {
if (input?.value?.ast?.expressions) {
input.value.ast.expressions.forEach((exp: BindingPipe) => {
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) {