replace DirectiveParser with new version that uses Angular compiler

This commit is contained in:
Kim Biesbjerg
2019-07-08 15:12:54 +02:00
parent 7d1bcd2a80
commit 6e161c83f8
2 changed files with 29 additions and 107 deletions

View File

@@ -1,85 +0,0 @@
import { ParserInterface } from './parser.interface';
import { TranslationCollection } from '../utils/translation.collection';
import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils';
import { parseTemplate, TmplAstNode, TmplAstElement, TmplAstTextAttribute } from '@angular/compiler';
export class DirectiveAstParser implements ParserInterface {
public extract(template: string, path: string): TranslationCollection {
if (path && isPathAngularComponent(path)) {
template = extractComponentInlineTemplate(template);
}
let collection: TranslationCollection = new TranslationCollection();
throw new Error('noåpe');
const nodes: TmplAstNode[] = this.parseTemplate(template, path);
this.getTranslatableElements(nodes).forEach(element => {
const key = this.getElementTranslateAttrValue(element) || this.getElementContents(element);
collection = collection.add(key);
});
console.log(collection);
return collection;
}
protected getTranslatableElements(nodes: TmplAstNode[]): TmplAstElement[] {
return nodes
.filter(element => this.isElement(element))
.reduce((result: TmplAstElement[], element: TmplAstElement) => {
return result.concat(this.findChildrenElements(element));
}, [])
.filter(element => this.isTranslatable(element));
}
protected findChildrenElements(node: TmplAstNode): TmplAstElement[] {
if (!this.isElement(node)) {
return [];
}
// If element has translate attribute all its contents is translatable
// so we don't need to traverse any deeper
if (this.isTranslatable(node)) {
return [node];
}
return node.children.reduce((result: TmplAstElement[], childNode: TmplAstNode) => {
if (this.isElement(childNode)) {
const children = this.findChildrenElements(childNode);
return result.concat(children);
}
return result;
}, [node]);
}
protected parseTemplate(template: string, path: string): TmplAstNode[] {
return parseTemplate(template, path).nodes;
}
protected isElement(node: any): node is TmplAstElement {
return node
&& node.attributes !== undefined
&& node.children !== undefined;
}
protected isTranslatable(node: TmplAstNode): boolean {
if (this.isElement(node) && node.attributes.some(attribute => attribute.name === 'translate')) {
return true;
}
return false;
}
protected getElementTranslateAttrValue(element: TmplAstElement): string {
const attr: TmplAstTextAttribute = element.attributes.find(attribute => attribute.name === 'translate');
return attr && attr.value || '';
}
protected getElementContents(element: TmplAstElement): string {
const contents = element.sourceSpan.start.file.content;
const start = element.startSourceSpan.end.offset;
const end = element.endSourceSpan.start.offset;
return contents.substring(start, end).trim();
}
}

View File

@@ -12,10 +12,10 @@ export class DirectiveParser implements ParserInterface {
} }
let collection: TranslationCollection = new TranslationCollection(); let collection: TranslationCollection = new TranslationCollection();
const nodes: TmplAstNode[] = this.parseTemplate(template, path); const nodes: TmplAstNode[] = this.parseTemplate(template, path);
this.getTranslatableElements(nodes).forEach(element => { this.getTranslatableElements(nodes).forEach(element => {
const translateAttr = this.getTranslateAttribute(element); const key = this.getElementTranslateAttrValue(element) || this.getElementContents(element);
const key = translateAttr.value || this.getContents(element);
collection = collection.add(key); collection = collection.add(key);
}); });
@@ -28,26 +28,26 @@ export class DirectiveParser implements ParserInterface {
.reduce((result: TmplAstElement[], element: TmplAstElement) => { .reduce((result: TmplAstElement[], element: TmplAstElement) => {
return result.concat(this.findChildrenElements(element)); return result.concat(this.findChildrenElements(element));
}, []) }, [])
.filter(element => this.hasTranslateAttribute(element)); .filter(element => this.isTranslatable(element));
} }
protected findChildrenElements(node: TmplAstNode): TmplAstElement[] { protected findChildrenElements(node: TmplAstNode): TmplAstElement[] {
// Safe guard, since only elements have children
if (!this.isElement(node)) { if (!this.isElement(node)) {
return []; return [];
} }
// If element has translate attribute it means all of its contents // If element has translate attribute all its contents is translatable
// is translatable, so we don't need to go any deeper // so we don't need to traverse any deeper
if (this.hasTranslateAttribute(node)) { if (this.isTranslatable(node)) {
return [node]; return [node];
} }
return node.children.reduce((result: TmplAstElement[], childNode: TmplAstNode) => { return node.children.reduce((result: TmplAstElement[], childNode: TmplAstNode) => {
if (!this.isElement(childNode)) { if (this.isElement(childNode)) {
return result; const children = this.findChildrenElements(childNode);
return result.concat(children);
} }
return result.concat(this.findChildrenElements(childNode)); return result;
}, [node]); }, [node]);
} }
@@ -55,22 +55,29 @@ export class DirectiveParser implements ParserInterface {
return parseTemplate(template, path).nodes; return parseTemplate(template, path).nodes;
} }
protected isElement(node: TmplAstNode): node is TmplAstElement { protected isElement(node: any): node is TmplAstElement {
return node instanceof TmplAstElement; return node
&& node.attributes !== undefined
&& node.children !== undefined;
} }
protected getContents(element: TmplAstElement): string { protected isTranslatable(node: TmplAstNode): boolean {
if (this.isElement(node) && node.attributes.some(attribute => attribute.name === 'translate')) {
return true;
}
return false;
}
protected getElementTranslateAttrValue(element: TmplAstElement): string {
const attr: TmplAstTextAttribute = element.attributes.find(attribute => attribute.name === 'translate');
return attr && attr.value || '';
}
protected getElementContents(element: TmplAstElement): string {
const contents = element.sourceSpan.start.file.content;
const start = element.startSourceSpan.end.offset; const start = element.startSourceSpan.end.offset;
const end = element.endSourceSpan.start.offset; const end = element.endSourceSpan.start.offset;
return element.sourceSpan.start.file.content.substring(start, end).trim(); return contents.substring(start, end).trim();
}
protected hasTranslateAttribute(element: TmplAstElement): boolean {
return !!this.getTranslateAttribute(element);
}
protected getTranslateAttribute(element: TmplAstElement): TmplAstTextAttribute {
return element.attributes.find(attribute => attribute.name === 'translate');
} }
} }