This commit is contained in:
Kim Biesbjerg 2020-05-31 14:46:41 +02:00
parent b928ca7555
commit 0d74fb4f55

View File

@ -1,21 +1,20 @@
import { import {
AST, AST,
ASTWithSource,
TmplAstNode as Node, TmplAstNode as Node,
TmplAstBoundText as BoundText, TmplAstBoundText as BoundText,
TmplAstElement as Element, TmplAstElement as Element,
TmplAstTemplate as Template, TmplAstTemplate as Template,
TmplAstBoundAttribute as BoundAttribute, TmplAstBoundAttribute as BoundAttribute,
TmplAstBoundEvent as BoundEvent,
parseTemplate, parseTemplate,
BindingPipe, BindingPipe,
LiteralPrimitive, LiteralPrimitive,
Conditional, Conditional,
TmplAstTextAttribute,
Binary, Binary,
LiteralMap, LiteralMap,
LiteralArray, LiteralArray,
Interpolation, Interpolation
ASTWithSource,
TmplAstBoundEvent as BoundEvent
} from '@angular/compiler'; } from '@angular/compiler';
import { ParserInterface } from './parser.interface'; import { ParserInterface } from './parser.interface';
@ -33,16 +32,12 @@ export class PipeParser implements ParserInterface {
source = extractComponentInlineTemplate(source); source = extractComponentInlineTemplate(source);
} }
const nodes: Node[] = this.parseTemplate(source, filePath); const nodes: Node[] = this.parseTemplate(source, filePath);
const pipes = this.getBindingPipes(nodes, TRANSLATE_PIPE_NAME); const pipes = this.getBindingPipes(nodes, TRANSLATE_PIPE_NAME).filter((pipe) => !this.pipeHasConcatenatedString(pipe));
pipes.forEach((pipe) => { pipes.forEach((pipe) => {
// Skip concatenated strings this.visitEachChild(pipe, (child) => {
if (pipe.exp instanceof Binary && pipe.exp.operation === '+') { if (child instanceof LiteralPrimitive) {
return; collection = collection.add(child.value);
}
this.visitEachChild(pipe, (exp) => {
if (exp instanceof LiteralPrimitive) {
collection = collection.add(exp.value);
} }
}); });
}); });
@ -72,33 +67,33 @@ export class PipeParser implements ParserInterface {
return pipes; return pipes;
} }
protected visitEachChild(exp: AST, visitor: (exp: AST) => void): void { protected visitEachChild(exp: AST, visitor: (child: AST) => void): void {
visitor(exp); visitor(exp);
let childExp: AST[] = []; let children: AST[] = [];
if (exp instanceof BoundText) { if (exp instanceof BoundText) {
childExp = [exp.value]; children = [exp.value];
} else if (exp instanceof BoundAttribute) { } else if (exp instanceof BoundAttribute) {
childExp = [exp.value]; children = [exp.value];
} else if (exp instanceof BoundEvent) { } else if (exp instanceof BoundEvent) {
childExp = [exp.handler]; children = [exp.handler];
} else if (exp instanceof Interpolation) { } else if (exp instanceof Interpolation) {
childExp = exp.expressions; children = exp.expressions;
} else if (exp instanceof LiteralArray) { } else if (exp instanceof LiteralArray) {
childExp = exp.expressions; children = exp.expressions;
} else if (exp instanceof LiteralMap) { } else if (exp instanceof LiteralMap) {
childExp = exp.values; children = exp.values;
} else if (exp instanceof BindingPipe) { } else if (exp instanceof BindingPipe) {
childExp = [exp.exp, ...exp.args]; children = [exp.exp, ...exp.args];
} else if (exp instanceof Conditional) { } else if (exp instanceof Conditional) {
childExp = [exp.trueExp, exp.falseExp]; children = [exp.trueExp, exp.falseExp];
} else if (exp instanceof Binary) { } else if (exp instanceof Binary) {
childExp = [exp.left, exp.right]; children = [exp.left, exp.right];
} else if (exp instanceof ASTWithSource) { } else if (exp instanceof ASTWithSource) {
childExp = [exp.ast]; children = [exp.ast];
} }
childExp.forEach((child) => { children.forEach((child) => {
this.visitEachChild(child, visitor); this.visitEachChild(child, visitor);
}); });
} }
@ -111,6 +106,14 @@ export class PipeParser implements ParserInterface {
return node instanceof Element || node instanceof Template; return node instanceof Element || node instanceof Template;
} }
/**
* Check if pipe concatenates string (in that case we don't want to extract it)
* @param pipe
*/
protected pipeHasConcatenatedString(pipe: BindingPipe): boolean {
return pipe?.exp instanceof Binary && pipe.exp.operation === '+';
}
protected parseTemplate(template: string, path: string): Node[] { protected parseTemplate(template: string, path: string): Node[] {
return parseTemplate(template, path).nodes; return parseTemplate(template, path).nodes;
} }