remove console log
This commit is contained in:
		
							
								
								
									
										181
									
								
								src/parsers/new-pipe.parser.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										181
									
								
								src/parsers/new-pipe.parser.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,181 @@ | |||||||
|  | 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 = '<h1>{{ "Hello World" | translate }} {{ "Another string" | translate }}</h1><body><strong>Hello</strong> {{ "World (translatable)" | translate }}</body>'; | ||||||
|  |  | ||||||
|  | 		template = ` | ||||||
|  | 			<span [attr]="'Hello World' | translate"></span> | ||||||
|  | 			<div key="{{ 'message' | translate | upper }}"> | ||||||
|  | 				<strong subattr="{{ 'hello last' | translate }}">{{ 'tag content' | translate }}</strong> | ||||||
|  | 			</div> | ||||||
|  | 		`; | ||||||
|  |  | ||||||
|  | 		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'); | ||||||
|  | 			}*/ | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		// <p>{{ 'message' | translate }}</p> | ||||||
|  | 		// <p key="{{ 'message' | translate }}"></p> | ||||||
|  | 		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; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
							
								
								
									
										114
									
								
								src/parsers/pipe-v2.parser.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										114
									
								
								src/parsers/pipe-v2.parser.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,114 @@ | |||||||
|  | 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<string, BindingPipe>(); | ||||||
|  | 	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 = '<h1>{{ "Hello World" | translate }} {{ "Another string" | translate }}</h1><body><strong>Hello</strong> {{ "World (translatable)" | translate }}</body>'; | ||||||
|  |  | ||||||
|  | 		template = ` | ||||||
|  | 			{{ 'Some text' }} | ||||||
|  | 			<span [attr]="'Hello World' | translate"></span> | ||||||
|  | 			<div hello key="{{ 'message' | translate | upper }}"> | ||||||
|  | 				<strong subattr="{{ 'hello last' | translate }}">{{ 'tag content' | translate }}</strong> | ||||||
|  | 			</div> | ||||||
|  | 		`; | ||||||
|  |  | ||||||
|  | 		template = ` | ||||||
|  | 			{{ 'hello' | translate }} | ||||||
|  | 			<span [testing]="'bound attr?'"></span> | ||||||
|  | 			<div attr="this attribute has text"></div> | ||||||
|  | 			<h1>{{ 'HEADER' }}<h1> | ||||||
|  | 			<header (click)="dosomething()"></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'); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
|  | */ | ||||||
| @@ -21,8 +21,6 @@ export class PipeParser implements ParserInterface { | |||||||
| 			collection = collection.add(matches[2].split('\\\'').join('\'')); | 			collection = collection.add(matches[2].split('\\\'').join('\'')); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		console.log(collection); |  | ||||||
|  |  | ||||||
| 		return collection; | 		return collection; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										68
									
								
								src/parsers/test.parser.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								src/parsers/test.parser.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,68 @@ | |||||||
|  | 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 = '<h2>Hello world</h2>'; | ||||||
|  |  | ||||||
|  | 		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; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user