From 9e5dad362c7ad5a9b2a00d246ec78648708036fe Mon Sep 17 00:00:00 2001 From: Kim Biesbjerg Date: Mon, 8 Jul 2019 14:36:24 +0200 Subject: [PATCH] removed test parsers --- src/parsers/new-pipe.parser.ts | 181 --------------------------------- src/parsers/pipe-v2.parser.ts | 114 --------------------- src/parsers/test.parser.ts | 68 ------------- 3 files changed, 363 deletions(-) delete mode 100644 src/parsers/new-pipe.parser.ts delete mode 100644 src/parsers/pipe-v2.parser.ts delete mode 100644 src/parsers/test.parser.ts diff --git a/src/parsers/new-pipe.parser.ts b/src/parsers/new-pipe.parser.ts deleted file mode 100644 index 8aa29e7..0000000 --- a/src/parsers/new-pipe.parser.ts +++ /dev/null @@ -1,181 +0,0 @@ -import { ParserInterface } from './parser.interface'; -import { TranslationCollection } from '../utils/translation.collection'; -import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils'; - -import { parseTemplate, TmplAstNode, TmplAstElement, TmplAstBoundText, ASTWithSource, Interpolation, BindingPipe, LiteralPrimitive, flatten, TmplAstTemplate, TmplAstBoundAttribute } from '@angular/compiler'; - - -export class NewPipeParser implements ParserInterface { - - public getTranslatableString(node: TmplAstBoundAttribute) { - const bindingPipes = (node.value as ASTWithSource).ast; - - console.log({astWithSrc: bindingPipes}); -return; - const expressions: BindingPipe[] = interpolation.expressions.filter(expression => { - return expression instanceof BindingPipe - && expression.name === 'translate' - && expression.exp instanceof LiteralPrimitive; - }); - - console.log({node, astWithSrc: bindingPipes, interpolation, expressions}); - -/* - .map((node: TmplAstBoundText) => node.value) - .map((value: ASTWithSource) => value.ast) - .map((ast: Interpolation) => ast.expressions) - .map((expressions: any[]) => - expressions.filter(expression => - expression instanceof BindingPipe - && expression.name === 'translate' - && expression.exp instanceof LiteralPrimitive - ) - ) - .map((expressions: BindingPipe[]) => - expressions.map(expression => (expression.exp as LiteralPrimitive).value as string) - );*/ - } - - public extract(template: string, path: string): TranslationCollection { - if (path && isPathAngularComponent(path)) { - template = extractComponentInlineTemplate(template); - } - - // template = '

{{ "Hello World" | translate }} {{ "Another string" | translate }}

Hello {{ "World (translatable)" | translate }}'; - - template = ` - -
- {{ 'tag content' | translate }} -
- `; - - template = `{{ 'hello' | translate | upper }}`; - - const rootNodes: TmplAstNode[] = this.parseTemplate(template, path); - - const pipes: BindingPipe[] = rootNodes.reduce((result: BindingPipe[], rootNode: TmplAstNode) => { - return result.concat(this.findBindingPipeNodes(rootNode)); - }, []); - - - - const flat = flatten(pipes.map(pipe => this.flattenBindingPipeNodes(pipe))); - - console.log(flat); - - return new TranslationCollection(); - - const boundTextNodes: TmplAstBoundText[] = rootNodes.reduce((result: TmplAstBoundText[], rootNode: TmplAstNode) => { - return result.concat(this.findBoundTextNodes(rootNode)); - }, []); - - console.log(boundTextNodes); - - const result = boundTextNodes - .map((node: TmplAstBoundText) => node.value) - .map((value: ASTWithSource) => value.ast) - .map((ast: Interpolation) => ast.expressions) - .map((expressions: any[]) => - expressions.filter(expression => - expression instanceof BindingPipe - && expression.name === 'translate' - && expression.exp instanceof LiteralPrimitive - ) - ) - .map((expressions: BindingPipe[]) => - expressions.map(expression => (expression.exp as LiteralPrimitive).value as string) - ); - - let collection: TranslationCollection = new TranslationCollection(); - flatten(result).forEach(key => collection = collection.add(key)); - - return collection; - } - - protected flattenBindingPipeNodes(node: BindingPipe): BindingPipe[] { - let ret: BindingPipe[] = []; - ret = [node]; - if (node.exp instanceof BindingPipe) { - return ret.concat(this.flattenBindingPipeNodes(node.exp)); - } - return ret; - } - - protected findBindingPipeNodes(node: TmplAstNode): BindingPipe[] { - let bindingPipes: BindingPipe[] = []; - - if (node instanceof BindingPipe) { - bindingPipes.push(node); -/* let expression = node.exp; - console.log('YOYOOYOYOY', expression); - while (expression instanceof BindingPipe) { - bindingPipes.push(expression); - expression = expression.exp; - console.log('puhs'); - }*/ - } - - //

{{ 'message' | translate }}

- //

- if (node instanceof TmplAstBoundText || node instanceof TmplAstBoundAttribute) { - const expressions = ((node.value as ASTWithSource).ast as Interpolation).expressions; - if (expressions) { - bindingPipes = bindingPipes.concat(expressions.filter(expression => expression instanceof BindingPipe)); - } - } - - /*console.log(''); - console.log(node); - console.log('');*/ - - // Visit element/template attributes - if (node instanceof TmplAstElement || node instanceof TmplAstTemplate) { - const childBuffer = node.inputs.reduce((result: BindingPipe[], childNode: TmplAstNode) => { - return this.findBindingPipeNodes(childNode); - }, []); - - bindingPipes = bindingPipes.concat(childBuffer); - } - - // Visit element/template children - if (node instanceof TmplAstElement || node instanceof TmplAstTemplate) { - const childBuffer = node.children.reduce((result: BindingPipe[], childNode: TmplAstNode) => { - return this.findBindingPipeNodes(childNode); - }, []); - - bindingPipes = bindingPipes.concat(childBuffer); - } - - return bindingPipes; - } - - protected findBoundTextNodes(node: TmplAstNode): TmplAstBoundText[] { - if (node instanceof TmplAstBoundText) { - return [node]; - } - if (node instanceof TmplAstElement || node instanceof TmplAstTemplate) { - return node.children.reduce((result: TmplAstBoundText[], childNode: TmplAstNode) => { - return this.findBoundTextNodes(childNode); - }, []); - } - return []; - } - - protected findBoundAttrNodes(node: TmplAstNode): TmplAstBoundAttribute[] { - if (node instanceof TmplAstBoundAttribute) { - return [node]; - } - if (node instanceof TmplAstElement || node instanceof TmplAstTemplate) { - return node.inputs.reduce((result: TmplAstBoundAttribute[], boundAttribute: TmplAstBoundAttribute) => { - return this.findBoundAttrNodes(boundAttribute); - }, []); - } - return []; - } - - protected parseTemplate(template: string, path: string): TmplAstNode[] { - return parseTemplate(template, path).nodes; - } - -} diff --git a/src/parsers/pipe-v2.parser.ts b/src/parsers/pipe-v2.parser.ts deleted file mode 100644 index 2167611..0000000 --- a/src/parsers/pipe-v2.parser.ts +++ /dev/null @@ -1,114 +0,0 @@ -import { ParserInterface } from './parser.interface'; -import { TranslationCollection } from '../utils/translation.collection'; -import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils'; - -import { parseTemplate, TmplAstNode, TmplAstBoundAttribute, TmplAstBoundText, TmplAstText, TmplAstTextAttribute, TmplAstBoundEvent } from '@angular/compiler'; - -import { visitAll, NullVisitor, RecursiveVisitor } from '@angular/compiler/src/render3/r3_ast'; - -/*export class PipeCollector extends RecursiveAstVisitor { - public pipes = new Map(); - public visitPipe(ast: BindingPipe, context: any): any { - console.log('VISIT PIPE'); - this.pipes.set(ast.name, ast); - ast.exp.visit(this); - this.visitAll(ast.args, context); - return null; - } -}*/ - -export class PipeV2Parser implements ParserInterface { - - public extract(template: string, path: string): TranslationCollection { - if (path && isPathAngularComponent(path)) { - template = extractComponentInlineTemplate(template); - } - - // template = '

{{ "Hello World" | translate }} {{ "Another string" | translate }}

Hello {{ "World (translatable)" | translate }}'; - - template = ` - {{ 'Some text' }} - -
- {{ 'tag content' | translate }} -
- `; - - template = ` - {{ 'hello' | translate }} - -
-

{{ 'HEADER' }}

-
- `; - - const templateNodes: TmplAstNode[] = this.parseTemplate(template, path); - const visitor = new MyVisitor2(); - - visitAll(visitor, templateNodes); - - // const rootNodes: TmplAstNode[] = this.parseTemplate(template, path); - - // const visitor = new MyVisitor2(); - -/* - rootNodes.forEach(rootNode => { - rootNode.visit(visitor); - }); -*/ - return new TranslationCollection(); - } - - protected parseTemplate(template: string, path: string): TmplAstNode[] { - return parseTemplate(template, path).nodes; - } - -} - -class MyVisitor2 extends RecursiveVisitor { - visitBoundText(text: TmplAstBoundText): void { - console.log('boundText\n', text); - } - - visitBoundAttribute(attribute: TmplAstBoundAttribute): void { - console.log('boundAttr\n', attribute); - } - - visitTextAttribute(attribute: TmplAstTextAttribute): void { - console.log('text attr\n', attribute); - } - - visitText(text: TmplAstText): void { - console.log('text\n', text); - } - - visitBoundEvent(attribute: TmplAstBoundEvent): void { - console.log('YAAAS'); - } -} -/* -class MyVisitor extends RecursiveVisitor { - constructor() { super(); } - - visitBoundText(text: TmplAstBoundText): void { - console.log('boundText\n', text); - } - - visitBoundAttribute(attribute: TmplAstBoundAttribute): void { - console.log('boundAttr\n', attribute); - } - - visitTextAttribute(attribute: TmplAstTextAttribute): void { - console.log('text attr\n', attribute); - } - - visitText(text: TmplAstText): void { - console.log('text\n', text); - } - - visitBoundEvent(attribute: TmplAstBoundEvent): void { - console.log('YAAAS'); - } - -} -*/ diff --git a/src/parsers/test.parser.ts b/src/parsers/test.parser.ts deleted file mode 100644 index 7dde3e2..0000000 --- a/src/parsers/test.parser.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { ParserInterface } from './parser.interface'; -import { TranslationCollection } from '../utils/translation.collection'; -import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils'; - -import { parseTemplate, ElementAst, TmplAstNode, HtmlParser, Node } from '@angular/compiler'; -import { Visitor, RecursiveVisitor } from '@angular/compiler/src/ml_parser/ast'; - -class TemplateVisitor extends RecursiveVisitor { - -} - -export class TestParser implements ParserInterface { - - public extract(template: string, path: string): TranslationCollection { - if (path && isPathAngularComponent(path)) { - template = extractComponentInlineTemplate(template); - } - - template = '

Hello world

'; - - let collection: TranslationCollection = new TranslationCollection(); - - const rootNodes = this.parseHtml(template, path); - const visitor = new TemplateVisitor(); - - const result = this.visitAll(visitor, rootNodes); - - console.log(rootNodes); - - return collection; - } - - protected visitAll(visitor: Visitor, nodes: Node[], context: any = null) { - const result: any[] = []; - - const visit = visitor.visit ? - (ast: Node) => visitor.visit !(ast, context) || ast.visit(visitor, context) : - (ast: Node) => ast.visit(visitor, context); - nodes.forEach(ast => { - const astResult = visit(ast); - if (astResult) { - result.push(astResult); - } - }); - return result; - - - /*const result: any[] = []; - - nodes.forEach(node => { - node.tmpl - if (visitor.visit) { - visitor.visit(node.ast) - } - });*/ - } - - protected parseHtml(template: string, templateUrl: string): Node[] { - const htmlParser = new HtmlParser(); - const parseResult = htmlParser.parse(template, templateUrl); - return parseResult.rootNodes; - } - - protected parseTemplate(template: string, path: string): TmplAstNode[] { - return parseTemplate(template, path).nodes; - } - -}