From 6e161c83f83fdc5054773590097a643e87207809 Mon Sep 17 00:00:00 2001 From: Kim Biesbjerg Date: Mon, 8 Jul 2019 15:12:54 +0200 Subject: [PATCH] replace DirectiveParser with new version that uses Angular compiler --- src/parsers/directive-ast.parser.ts | 85 ----------------------------- src/parsers/directive.parser.ts | 51 +++++++++-------- 2 files changed, 29 insertions(+), 107 deletions(-) delete mode 100644 src/parsers/directive-ast.parser.ts diff --git a/src/parsers/directive-ast.parser.ts b/src/parsers/directive-ast.parser.ts deleted file mode 100644 index 028b6e1..0000000 --- a/src/parsers/directive-ast.parser.ts +++ /dev/null @@ -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(); - } - -} diff --git a/src/parsers/directive.parser.ts b/src/parsers/directive.parser.ts index 4584c05..7b7032d 100644 --- a/src/parsers/directive.parser.ts +++ b/src/parsers/directive.parser.ts @@ -12,10 +12,10 @@ export class DirectiveParser implements ParserInterface { } let collection: TranslationCollection = new TranslationCollection(); + const nodes: TmplAstNode[] = this.parseTemplate(template, path); this.getTranslatableElements(nodes).forEach(element => { - const translateAttr = this.getTranslateAttribute(element); - const key = translateAttr.value || this.getContents(element); + const key = this.getElementTranslateAttrValue(element) || this.getElementContents(element); collection = collection.add(key); }); @@ -28,26 +28,26 @@ export class DirectiveParser implements ParserInterface { .reduce((result: TmplAstElement[], element: TmplAstElement) => { return result.concat(this.findChildrenElements(element)); }, []) - .filter(element => this.hasTranslateAttribute(element)); + .filter(element => this.isTranslatable(element)); } protected findChildrenElements(node: TmplAstNode): TmplAstElement[] { - // Safe guard, since only elements have children if (!this.isElement(node)) { return []; } - // If element has translate attribute it means all of its contents - // is translatable, so we don't need to go any deeper - if (this.hasTranslateAttribute(node)) { + // 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)) { - return result; + if (this.isElement(childNode)) { + const children = this.findChildrenElements(childNode); + return result.concat(children); } - return result.concat(this.findChildrenElements(childNode)); + return result; }, [node]); } @@ -55,22 +55,29 @@ export class DirectiveParser implements ParserInterface { return parseTemplate(template, path).nodes; } - protected isElement(node: TmplAstNode): node is TmplAstElement { - return node instanceof TmplAstElement; + protected isElement(node: any): node is 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 end = element.endSourceSpan.start.offset; - return element.sourceSpan.start.file.content.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'); + return contents.substring(start, end).trim(); } }