- (feat) add concept of post processors
- (feat) add 'key as default value' post processor (closes #109) - (chore) move clean functionality to a post processor - (chore) move sort functionality to a post processor - (refactor) get rid of leading underscore on protected properties/methods
This commit is contained in:
		| @@ -24,10 +24,7 @@ | ||||
|   }, | ||||
|   "keywords": [ | ||||
|     "angular", | ||||
|     "angular2", | ||||
|     "ionic", | ||||
|     "ionic2", | ||||
|     "ng2-translate", | ||||
|     "ngx-translate", | ||||
|     "extract", | ||||
|     "extractor", | ||||
|   | ||||
| @@ -4,6 +4,10 @@ import { PipeParser } from '../parsers/pipe.parser'; | ||||
| import { DirectiveParser } from '../parsers/directive.parser'; | ||||
| import { ServiceParser } from '../parsers/service.parser'; | ||||
| import { FunctionParser } from '../parsers/function.parser'; | ||||
| import { PostProcessorInterface } from '../post-processors/post-processor.interface'; | ||||
| import { SortByKeyPostProcessor } from '../post-processors/sort-by-key.post-processor'; | ||||
| import { KeyAsDefaultValuePostProcessor } from '../post-processors/key-as-default-value.post-processor'; | ||||
| import { PurgeObsoleteKeysPostProcessor } from '../post-processors/purge-obsolete-keys.post-processor'; | ||||
| import { CompilerInterface } from '../compilers/compiler.interface'; | ||||
| import { CompilerFactory } from '../compilers/compiler.factory'; | ||||
|  | ||||
| @@ -82,28 +86,21 @@ export const cli = yargs | ||||
| 		default: false, | ||||
| 		type: 'boolean' | ||||
| 	}) | ||||
| 	.option('verbose', { | ||||
| 		alias: 'vb', | ||||
| 		describe: 'Log all output to console', | ||||
| 	.option('key-as-default-value', { | ||||
| 		alias: 'k', | ||||
| 		describe: 'Use key as default value for translations', | ||||
| 		default: false, | ||||
| 		type: 'boolean' | ||||
| 	}) | ||||
| 	.exitProcess(true) | ||||
| 	.parse(process.argv); | ||||
|  | ||||
| const extract = new ExtractTask(cli.input, cli.output, { | ||||
| const extractTask = new ExtractTask(cli.input, cli.output, { | ||||
| 	replace: cli.replace, | ||||
| 	sort: cli.sort, | ||||
| 	clean: cli.clean, | ||||
| 	patterns: cli.patterns, | ||||
| 	verbose: cli.verbose | ||||
| 	patterns: cli.patterns | ||||
| }); | ||||
|  | ||||
| const compiler: CompilerInterface = CompilerFactory.create(cli.format, { | ||||
| 	indentation: cli.formatIndentation | ||||
| }); | ||||
| extract.setCompiler(compiler); | ||||
|  | ||||
| // Parsers | ||||
| const parsers: ParserInterface[] = [ | ||||
| 	new PipeParser(), | ||||
| 	new DirectiveParser(), | ||||
| @@ -114,6 +111,25 @@ if (cli.marker) { | ||||
| 		identifier: cli.marker | ||||
| 	})); | ||||
| } | ||||
| extract.setParsers(parsers); | ||||
| extractTask.setParsers(parsers); | ||||
|  | ||||
| extract.execute(); | ||||
| // Processors | ||||
| const processors: PostProcessorInterface[] = []; | ||||
| if (cli.clean) { | ||||
| 	processors.push(new PurgeObsoleteKeysPostProcessor()); | ||||
| } | ||||
| if (cli.keyAsDefaultValue) { | ||||
| 	processors.push(new KeyAsDefaultValuePostProcessor()); | ||||
| } | ||||
| if (cli.sort) { | ||||
| 	processors.push(new SortByKeyPostProcessor()); | ||||
| } | ||||
| extractTask.setProcessors(processors); | ||||
|  | ||||
| // Compiler | ||||
| const compiler: CompilerInterface = CompilerFactory.create(cli.format, { | ||||
| 	indentation: cli.formatIndentation | ||||
| }); | ||||
| extractTask.setCompiler(compiler); | ||||
|  | ||||
| extractTask.execute(); | ||||
|   | ||||
| @@ -1,9 +1,10 @@ | ||||
| import { TranslationCollection } from '../../utils/translation.collection'; | ||||
| import { TaskInterface } from './task.interface'; | ||||
| import { ParserInterface } from '../../parsers/parser.interface'; | ||||
| import { PostProcessorInterface } from '../../post-processors/post-processor.interface'; | ||||
| import { CompilerInterface } from '../../compilers/compiler.interface'; | ||||
|  | ||||
| import { green, bold, gray, dim } from 'colorette'; | ||||
| import { green, bold, gray, dim, cyan } from 'colorette'; | ||||
| import * as glob from 'glob'; | ||||
| import * as fs from 'fs'; | ||||
| import * as path from 'path'; | ||||
| @@ -11,128 +12,133 @@ import * as mkdirp from 'mkdirp'; | ||||
|  | ||||
| export interface ExtractTaskOptionsInterface { | ||||
| 	replace?: boolean; | ||||
| 	sort?: boolean; | ||||
| 	clean?: boolean; | ||||
| 	patterns?: string[]; | ||||
| 	verbose?: boolean; | ||||
| } | ||||
|  | ||||
| export class ExtractTask implements TaskInterface { | ||||
|  | ||||
| 	protected _options: ExtractTaskOptionsInterface = { | ||||
| 	protected options: ExtractTaskOptionsInterface = { | ||||
| 		replace: false, | ||||
| 		sort: false, | ||||
| 		clean: false, | ||||
| 		patterns: [], | ||||
| 		verbose: false | ||||
| 		patterns: [] | ||||
| 	}; | ||||
|  | ||||
| 	protected _parsers: ParserInterface[] = []; | ||||
| 	protected _compiler: CompilerInterface; | ||||
| 	protected parsers: ParserInterface[] = []; | ||||
| 	protected processors: PostProcessorInterface[] = []; | ||||
| 	protected compiler: CompilerInterface; | ||||
|  | ||||
| 	public constructor(protected _input: string[], protected _output: string[], options?: ExtractTaskOptionsInterface) { | ||||
| 		this._options = { ...this._options, ...options }; | ||||
| 	public constructor(protected inputs: string[], protected outputs: string[], options?: ExtractTaskOptionsInterface) { | ||||
| 		this.inputs = inputs.map(input => path.resolve(input)); | ||||
| 		this.outputs = outputs.map(output => path.resolve(output)); | ||||
| 		this.options = { ...this.options, ...options }; | ||||
| 	} | ||||
|  | ||||
| 	public execute(): void { | ||||
| 		if (!this._parsers) { | ||||
| 		if (!this.parsers.length) { | ||||
| 			throw new Error('No parsers configured'); | ||||
| 		} | ||||
| 		if (!this._compiler) { | ||||
| 		if (!this.compiler) { | ||||
| 			throw new Error('No compiler configured'); | ||||
| 		} | ||||
|  | ||||
| 		const collection = this._extract(); | ||||
| 		this._out(green('Extracted %d strings\n'), collection.count()); | ||||
| 		this._save(collection); | ||||
| 		this.out(bold('Extracting:')); | ||||
| 		const extracted = this.extract(); | ||||
| 		this.out(green(`\nFound %d strings.\n`), extracted.count()); | ||||
|  | ||||
| 		if (this.processors.length) { | ||||
| 			this.out(cyan('Enabled post processors:')); | ||||
| 			this.out(cyan(dim(this.processors.map(processor => `- ${processor.name}`).join('\n')))); | ||||
| 			this.out(); | ||||
| 		} | ||||
|  | ||||
| 		this.outputs.forEach(output => { | ||||
| 			let dir: string = output; | ||||
| 			let filename: string = `strings.${this.compiler.extension}`; | ||||
| 			if (!fs.existsSync(output) || !fs.statSync(output).isDirectory()) { | ||||
| 				dir = path.dirname(output); | ||||
| 				filename = path.basename(output); | ||||
| 			} | ||||
|  | ||||
| 			const outputPath: string = path.join(dir, filename); | ||||
|  | ||||
| 			this.out(`${bold('Saving:')} ${dim(outputPath)}`); | ||||
|  | ||||
| 			let existing: TranslationCollection = new TranslationCollection(); | ||||
| 			if (!this.options.replace && fs.existsSync(outputPath)) { | ||||
| 				this.out(dim(`- destination exists, merging existing translations`)); | ||||
| 				existing = this.compiler.parse(fs.readFileSync(outputPath, 'utf-8')); | ||||
| 			} | ||||
|  | ||||
| 			const working = extracted.union(existing); | ||||
|  | ||||
| 			// Run collection through processors | ||||
| 			this.out(dim('- applying post processors')); | ||||
| 			const final = this.process(working, extracted, existing); | ||||
|  | ||||
| 			// Save to file | ||||
| 			this.save(outputPath, final); | ||||
| 			this.out(green('\nOK.\n')); | ||||
| 		}); | ||||
| 	} | ||||
|  | ||||
| 	public setParsers(parsers: ParserInterface[]): this { | ||||
| 		this._parsers = parsers; | ||||
| 		this.parsers = parsers; | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public setProcessors(processors: PostProcessorInterface[]): this { | ||||
| 		this.processors = processors; | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	public setCompiler(compiler: CompilerInterface): this { | ||||
| 		this._compiler = compiler; | ||||
| 		this.compiler = compiler; | ||||
| 		return this; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Extract strings from input dirs using configured parsers | ||||
| 	 * Extract strings from specified input dirs using configured parsers | ||||
| 	 */ | ||||
| 	protected _extract(): TranslationCollection { | ||||
| 		this._out(bold('Extracting strings...')); | ||||
|  | ||||
| 		let collection: TranslationCollection = new TranslationCollection(); | ||||
| 		this._input.forEach(dir => { | ||||
| 			this._readDir(dir, this._options.patterns).forEach(path => { | ||||
| 				this._options.verbose && this._out(gray('- %s'), path); | ||||
| 	protected extract(): TranslationCollection { | ||||
| 		let extracted: TranslationCollection = new TranslationCollection(); | ||||
| 		this.inputs.forEach(dir => { | ||||
| 			this.readDir(dir, this.options.patterns).forEach(path => { | ||||
| 				this.out(gray('- %s'), path); | ||||
| 				const contents: string = fs.readFileSync(path, 'utf-8'); | ||||
| 				this._parsers.forEach((parser: ParserInterface) => { | ||||
| 					collection = collection.union(parser.extract(contents, path)); | ||||
| 				this.parsers.forEach(parser => { | ||||
| 					extracted = extracted.union(parser.extract(contents, path)); | ||||
| 				}); | ||||
| 			}); | ||||
| 		}); | ||||
|  | ||||
| 		return collection; | ||||
| 		return extracted; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Process collection according to options (merge, clean, sort), compile and save | ||||
| 	 * Run strings through configured processors | ||||
| 	 */ | ||||
| 	protected process(working: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection { | ||||
| 		this.processors.forEach(processor => { | ||||
| 			working = processor.process(working, extracted, existing); | ||||
| 		}); | ||||
| 		return working; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Compile and save translations | ||||
| 	 * @param collection | ||||
| 	 */ | ||||
| 	protected _save(collection: TranslationCollection): void { | ||||
| 		this._output.forEach(output => { | ||||
| 			const normalizedOutput: string = path.resolve(output); | ||||
|  | ||||
| 			let dir: string = normalizedOutput; | ||||
| 			let filename: string = `strings.${this._compiler.extension}`; | ||||
| 			if (!fs.existsSync(normalizedOutput) || !fs.statSync(normalizedOutput).isDirectory()) { | ||||
| 				dir = path.dirname(normalizedOutput); | ||||
| 				filename = path.basename(normalizedOutput); | ||||
| 			} | ||||
|  | ||||
| 			const outputPath: string = path.join(dir, filename); | ||||
| 			let processedCollection: TranslationCollection = collection; | ||||
|  | ||||
| 			this._out(bold('\nSaving: %s'), outputPath); | ||||
|  | ||||
| 			if (fs.existsSync(outputPath) && !this._options.replace) { | ||||
| 				const existingCollection: TranslationCollection = this._compiler.parse(fs.readFileSync(outputPath, 'utf-8')); | ||||
| 				if (!existingCollection.isEmpty()) { | ||||
| 					processedCollection = processedCollection.union(existingCollection); | ||||
| 					this._out(dim('- merged with %d existing strings'), existingCollection.count()); | ||||
| 				} | ||||
|  | ||||
| 				if (this._options.clean) { | ||||
| 					const collectionCount = processedCollection.count(); | ||||
| 					processedCollection = processedCollection.intersect(collection); | ||||
| 					const removeCount = collectionCount - processedCollection.count(); | ||||
| 					if (removeCount > 0) { | ||||
| 						this._out(dim('- removed %d obsolete strings'), removeCount); | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
| 			if (this._options.sort) { | ||||
| 				processedCollection = processedCollection.sort(); | ||||
| 				this._out(dim('- sorted strings')); | ||||
| 			} | ||||
|  | ||||
| 	protected save(output: string, collection: TranslationCollection): void { | ||||
| 		const dir = path.dirname(output); | ||||
| 		if (!fs.existsSync(dir)) { | ||||
| 			mkdirp.sync(dir); | ||||
| 				this._out(dim('- created dir: %s'), dir); | ||||
| 			this.out(dim('- created dir: %s'), dir); | ||||
| 		} | ||||
| 			fs.writeFileSync(outputPath, this._compiler.compile(processedCollection)); | ||||
|  | ||||
| 			this._out(green('Done!')); | ||||
| 		}); | ||||
| 		fs.writeFileSync(output, this.compiler.compile(collection)); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Get all files in dir matching patterns | ||||
| 	 */ | ||||
| 	protected _readDir(dir: string, patterns: string[]): string[] { | ||||
| 	protected readDir(dir: string, patterns: string[]): string[] { | ||||
| 		return patterns.reduce((results, pattern) => { | ||||
| 			return glob.sync(dir + pattern) | ||||
| 				.filter(path => fs.statSync(path).isFile()) | ||||
| @@ -140,7 +146,7 @@ export class ExtractTask implements TaskInterface { | ||||
| 		}, []); | ||||
| 	} | ||||
|  | ||||
| 	protected _out(...args: any[]): void { | ||||
| 	protected out(...args: any[]): void { | ||||
| 		console.log.apply(this, arguments); | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -21,13 +21,13 @@ export class JsonCompiler implements CompilerInterface { | ||||
|  | ||||
| 	public parse(contents: string): TranslationCollection { | ||||
| 		let values: any = JSON.parse(contents); | ||||
| 		if (this._isNamespacedJsonFormat(values)) { | ||||
| 		if (this.isNamespacedJsonFormat(values)) { | ||||
| 			values = flat.flatten(values); | ||||
| 		} | ||||
| 		return new TranslationCollection(values); | ||||
| 	} | ||||
|  | ||||
| 	protected _isNamespacedJsonFormat(values: any): boolean { | ||||
| 	protected isNamespacedJsonFormat(values: any): boolean { | ||||
| 		return Object.keys(values).some(key => typeof values[key] === 'object'); | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -1,17 +1,18 @@ | ||||
| import * as ts from 'typescript'; | ||||
| import { yellow } from 'colorette'; | ||||
|  | ||||
| export abstract class AbstractAstParser { | ||||
|  | ||||
| 	protected _sourceFile: ts.SourceFile; | ||||
| 	protected sourceFile: ts.SourceFile; | ||||
|  | ||||
| 	protected _createSourceFile(path: string, contents: string): ts.SourceFile { | ||||
| 	protected createSourceFile(path: string, contents: string): ts.SourceFile { | ||||
| 		return ts.createSourceFile(path, contents, null, /*setParentNodes */ false); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Get strings from function call's first argument | ||||
| 	 */ | ||||
| 	protected _getCallArgStrings(callNode: ts.CallExpression): string[] { | ||||
| 	protected getCallArgStrings(callNode: ts.CallExpression): string[] { | ||||
| 		if (!callNode.arguments.length) { | ||||
| 			return; | ||||
| 		} | ||||
| @@ -25,41 +26,51 @@ export abstract class AbstractAstParser { | ||||
| 				return (firstArg as ts.ArrayLiteralExpression).elements | ||||
| 					.map((element: ts.StringLiteral) => element.text); | ||||
| 			case ts.SyntaxKind.Identifier: | ||||
| 				console.log('WARNING: We cannot extract variable values passed to TranslateService (yet)'); | ||||
| 				// TODO | ||||
| 				console.log(yellow('[Line: %d] We do not support values passed to TranslateService'), this.getLine(firstArg)); | ||||
| 				break; | ||||
| 			case ts.SyntaxKind.BinaryExpression: | ||||
| 				// TODO | ||||
| 				console.log(yellow('[Line: %d] We do not support binary expressions (yet)'), this.getLine(firstArg)); | ||||
| 				break; | ||||
| 			default: | ||||
| 				console.log(`SKIP: Unknown argument type: '${this._syntaxKindToName(firstArg.kind)}'`, firstArg); | ||||
| 				console.log(yellow(`[Line: %d] Unknown argument type: %s`), this.getLine(firstArg), this.syntaxKindToName(firstArg.kind), firstArg); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Find all child nodes of a kind | ||||
| 	 */ | ||||
| 	protected _findNodes(node: ts.Node, kind: ts.SyntaxKind): ts.Node[] { | ||||
| 		const childrenNodes: ts.Node[] = node.getChildren(this._sourceFile); | ||||
| 	protected findNodes(node: ts.Node, kind: ts.SyntaxKind): ts.Node[] { | ||||
| 		const childrenNodes: ts.Node[] = node.getChildren(this.sourceFile); | ||||
| 		const initialValue: ts.Node[] = node.kind === kind ? [node] : []; | ||||
|  | ||||
| 		return childrenNodes.reduce((result: ts.Node[], childNode: ts.Node) => { | ||||
| 			return result.concat(this._findNodes(childNode, kind)); | ||||
| 			return result.concat(this.findNodes(childNode, kind)); | ||||
| 		}, initialValue); | ||||
| 	} | ||||
|  | ||||
| 	protected _syntaxKindToName(kind: ts.SyntaxKind): string { | ||||
| 	protected getLine(node: ts.Node): number { | ||||
| 		const { line } = this.sourceFile.getLineAndCharacterOfPosition(node.pos); | ||||
| 		return line + 1; | ||||
| 	} | ||||
|  | ||||
| 	protected syntaxKindToName(kind: ts.SyntaxKind): string { | ||||
| 		return ts.SyntaxKind[kind]; | ||||
| 	} | ||||
|  | ||||
| 	protected _printAllChildren(sourceFile: ts.SourceFile, node: ts.Node, depth = 0): void { | ||||
| 	protected printAllChildren(sourceFile: ts.SourceFile, node: ts.Node, depth = 0): void { | ||||
| 		console.log( | ||||
| 			new Array(depth + 1).join('----'), | ||||
| 			`[${node.kind}]`, | ||||
| 			this._syntaxKindToName(node.kind), | ||||
| 			this.syntaxKindToName(node.kind), | ||||
| 			`[pos: ${node.pos}-${node.end}]`, | ||||
| 			':\t\t\t', | ||||
| 			node.getFullText(sourceFile).trim() | ||||
| 		); | ||||
|  | ||||
| 		depth++; | ||||
| 		node.getChildren(sourceFile).forEach(childNode => this._printAllChildren(sourceFile, childNode, depth)); | ||||
| 		node.getChildren(sourceFile).forEach(childNode => this.printAllChildren(sourceFile, childNode, depth)); | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -4,14 +4,14 @@ export abstract class AbstractTemplateParser { | ||||
| 	 * Checks if file is of type javascript or typescript and | ||||
| 	 * makes the assumption that it is an Angular Component | ||||
| 	 */ | ||||
| 	protected _isAngularComponent(path: string): boolean { | ||||
| 	protected isAngularComponent(path: string): boolean { | ||||
| 		return (/\.ts|js$/i).test(path); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Extracts inline template from components | ||||
| 	 */ | ||||
| 	protected _extractInlineTemplate(contents: string): string { | ||||
| 	protected extractInlineTemplate(contents: string): string { | ||||
| 		const regExp: RegExp = /template\s*:\s*(["'`])([^\1]*?)\1/; | ||||
| 		const match = regExp.exec(contents); | ||||
| 		if (match !== null) { | ||||
|   | ||||
| @@ -9,17 +9,17 @@ const $ = cheerio.load('', {xmlMode: true}); | ||||
| export class DirectiveParser extends AbstractTemplateParser implements ParserInterface { | ||||
|  | ||||
| 	public extract(contents: string, path?: string): TranslationCollection { | ||||
| 		if (path && this._isAngularComponent(path)) { | ||||
| 			contents = this._extractInlineTemplate(contents); | ||||
| 		if (path && this.isAngularComponent(path)) { | ||||
| 			contents = this.extractInlineTemplate(contents); | ||||
| 		} | ||||
|  | ||||
| 		return this._parseTemplate(contents); | ||||
| 		return this.parseTemplate(contents); | ||||
| 	} | ||||
|  | ||||
| 	protected _parseTemplate(template: string): TranslationCollection { | ||||
| 	protected parseTemplate(template: string): TranslationCollection { | ||||
| 		let collection: TranslationCollection = new TranslationCollection(); | ||||
|  | ||||
| 		template = this._normalizeTemplateAttributes(template); | ||||
| 		template = this.normalizeTemplateAttributes(template); | ||||
|  | ||||
| 		const selector = '[translate], [ng2-translate]'; | ||||
| 		$(template) | ||||
| @@ -50,7 +50,7 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt | ||||
| 	 * so it can't be parsed by standard HTML parsers. | ||||
| 	 * This method replaces `[attr]="'val'""` with `attr="val"` | ||||
| 	 */ | ||||
| 	protected _normalizeTemplateAttributes(template: string): string { | ||||
| 	protected normalizeTemplateAttributes(template: string): string { | ||||
| 		return template.replace(/\[([^\]]+)\]="'([^']*)'"/g, '$1="$2"'); | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -6,23 +6,23 @@ import * as ts from 'typescript'; | ||||
|  | ||||
| export class FunctionParser extends AbstractAstParser implements ParserInterface { | ||||
|  | ||||
| 	protected _functionIdentifier: string = '_'; | ||||
| 	protected functionIdentifier: string = '_'; | ||||
|  | ||||
| 	public constructor(options?: any) { | ||||
| 		super(); | ||||
| 		if (options && typeof options.identifier !== 'undefined') { | ||||
| 			this._functionIdentifier = options.identifier; | ||||
| 			this.functionIdentifier = options.identifier; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public extract(contents: string, path?: string): TranslationCollection { | ||||
| 		let collection: TranslationCollection = new TranslationCollection(); | ||||
|  | ||||
| 		this._sourceFile = this._createSourceFile(path, contents); | ||||
| 		this.sourceFile = this.createSourceFile(path, contents); | ||||
|  | ||||
| 		const callNodes = this._findCallNodes(); | ||||
| 		const callNodes = this.findCallNodes(); | ||||
| 		callNodes.forEach(callNode => { | ||||
| 			const keys: string[] = this._getCallArgStrings(callNode); | ||||
| 			const keys: string[] = this.getCallArgStrings(callNode); | ||||
| 			if (keys && keys.length) { | ||||
| 				collection = collection.addKeys(keys); | ||||
| 			} | ||||
| @@ -34,12 +34,12 @@ export class FunctionParser extends AbstractAstParser implements ParserInterface | ||||
| 	/** | ||||
| 	 * Find all calls to marker function | ||||
| 	 */ | ||||
| 	protected _findCallNodes(node?: ts.Node): ts.CallExpression[] { | ||||
| 	protected findCallNodes(node?: ts.Node): ts.CallExpression[] { | ||||
| 		if (!node) { | ||||
| 			node = this._sourceFile; | ||||
| 			node = this.sourceFile; | ||||
| 		} | ||||
|  | ||||
| 		let callNodes = this._findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[]; | ||||
| 		let callNodes = this.findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[]; | ||||
| 		callNodes = callNodes | ||||
| 			.filter(callNode => { | ||||
| 				// Only call expressions with arguments | ||||
| @@ -48,7 +48,7 @@ export class FunctionParser extends AbstractAstParser implements ParserInterface | ||||
| 				} | ||||
|  | ||||
| 				const identifier = (callNode.getChildAt(0) as ts.Identifier).text; | ||||
| 				if (identifier !== this._functionIdentifier) { | ||||
| 				if (identifier !== this.functionIdentifier) { | ||||
| 					return false; | ||||
| 				} | ||||
|  | ||||
|   | ||||
| @@ -5,14 +5,14 @@ import { TranslationCollection } from '../utils/translation.collection'; | ||||
| export class PipeParser extends AbstractTemplateParser implements ParserInterface { | ||||
|  | ||||
| 	public extract(contents: string, path?: string): TranslationCollection { | ||||
| 		if (path && this._isAngularComponent(path)) { | ||||
| 			contents = this._extractInlineTemplate(contents); | ||||
| 		if (path && this.isAngularComponent(path)) { | ||||
| 			contents = this.extractInlineTemplate(contents); | ||||
| 		} | ||||
|  | ||||
| 		return this._parseTemplate(contents); | ||||
| 		return this.parseTemplate(contents); | ||||
| 	} | ||||
|  | ||||
| 	protected _parseTemplate(template: string): TranslationCollection { | ||||
| 	protected parseTemplate(template: string): TranslationCollection { | ||||
| 		let collection: TranslationCollection = new TranslationCollection(); | ||||
|  | ||||
| 		const regExp: RegExp = /(['"`])((?:(?!\1).|\\\1)+)\1\s*\|\s*translate/g; | ||||
|   | ||||
| @@ -6,27 +6,27 @@ import * as ts from 'typescript'; | ||||
|  | ||||
| export class ServiceParser extends AbstractAstParser implements ParserInterface { | ||||
|  | ||||
| 	protected _sourceFile: ts.SourceFile; | ||||
| 	protected sourceFile: ts.SourceFile; | ||||
|  | ||||
| 	public extract(contents: string, path?: string): TranslationCollection { | ||||
| 		let collection: TranslationCollection = new TranslationCollection(); | ||||
|  | ||||
| 		this._sourceFile = this._createSourceFile(path, contents); | ||||
| 		const classNodes = this._findClassNodes(this._sourceFile); | ||||
| 		this.sourceFile = this.createSourceFile(path, contents); | ||||
| 		const classNodes = this.findClassNodes(this.sourceFile); | ||||
| 		classNodes.forEach(classNode => { | ||||
| 			const constructorNode = this._findConstructorNode(classNode); | ||||
| 			const constructorNode = this.findConstructorNode(classNode); | ||||
| 			if (!constructorNode) { | ||||
| 				return; | ||||
| 			} | ||||
|  | ||||
| 			const propertyName: string = this._findTranslateServicePropertyName(constructorNode); | ||||
| 			const propertyName: string = this.findTranslateServicePropertyName(constructorNode); | ||||
| 			if (!propertyName) { | ||||
| 				return; | ||||
| 			} | ||||
|  | ||||
| 			const callNodes = this._findCallNodes(classNode, propertyName); | ||||
| 			const callNodes = this.findCallNodes(classNode, propertyName); | ||||
| 			callNodes.forEach(callNode => { | ||||
| 				const keys: string[] = this._getCallArgStrings(callNode); | ||||
| 				const keys: string[] = this.getCallArgStrings(callNode); | ||||
| 				if (keys && keys.length) { | ||||
| 					collection = collection.addKeys(keys); | ||||
| 				} | ||||
| @@ -40,7 +40,7 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface | ||||
| 	 * Detect what the TranslateService instance property | ||||
| 	 * is called by inspecting constructor arguments | ||||
| 	 */ | ||||
| 	protected _findTranslateServicePropertyName(constructorNode: ts.ConstructorDeclaration): string { | ||||
| 	protected findTranslateServicePropertyName(constructorNode: ts.ConstructorDeclaration): string { | ||||
| 		if (!constructorNode) { | ||||
| 			return null; | ||||
| 		} | ||||
| @@ -77,15 +77,15 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface | ||||
| 	/** | ||||
| 	 * Find class nodes | ||||
| 	 */ | ||||
| 	protected _findClassNodes(node: ts.Node): ts.ClassDeclaration[] { | ||||
| 		return this._findNodes(node, ts.SyntaxKind.ClassDeclaration) as ts.ClassDeclaration[]; | ||||
| 	protected findClassNodes(node: ts.Node): ts.ClassDeclaration[] { | ||||
| 		return this.findNodes(node, ts.SyntaxKind.ClassDeclaration) as ts.ClassDeclaration[]; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Find constructor | ||||
| 	 */ | ||||
| 	protected _findConstructorNode(node: ts.ClassDeclaration): ts.ConstructorDeclaration { | ||||
| 		const constructorNodes = this._findNodes(node, ts.SyntaxKind.Constructor) as ts.ConstructorDeclaration[]; | ||||
| 	protected findConstructorNode(node: ts.ClassDeclaration): ts.ConstructorDeclaration { | ||||
| 		const constructorNodes = this.findNodes(node, ts.SyntaxKind.Constructor) as ts.ConstructorDeclaration[]; | ||||
| 		if (constructorNodes) { | ||||
| 			return constructorNodes[0]; | ||||
| 		} | ||||
| @@ -94,8 +94,8 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface | ||||
| 	/** | ||||
| 	 * Find all calls to TranslateService methods | ||||
| 	 */ | ||||
| 	protected _findCallNodes(node: ts.Node, propertyIdentifier: string): ts.CallExpression[] { | ||||
| 		let callNodes = this._findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[]; | ||||
| 	protected findCallNodes(node: ts.Node, propertyIdentifier: string): ts.CallExpression[] { | ||||
| 		let callNodes = this.findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[]; | ||||
| 		callNodes = callNodes | ||||
| 			.filter(callNode => { | ||||
| 				// Only call expressions with arguments | ||||
|   | ||||
							
								
								
									
										12
									
								
								src/post-processors/key-as-default-value.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/post-processors/key-as-default-value.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| import { TranslationCollection } from '../utils/translation.collection'; | ||||
| import { PostProcessorInterface } from './post-processor.interface'; | ||||
|  | ||||
| export class KeyAsDefaultValuePostProcessor implements PostProcessorInterface { | ||||
|  | ||||
| 	public name: string = 'KeyAsDefaultValue'; | ||||
|  | ||||
| 	public process(working: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection { | ||||
| 		return working.map((key, val) => val === '' ? key : val); | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										9
									
								
								src/post-processors/post-processor.interface.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/post-processors/post-processor.interface.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| import { TranslationCollection } from '../utils/translation.collection'; | ||||
|  | ||||
| export interface PostProcessorInterface { | ||||
|  | ||||
| 	name: string; | ||||
|  | ||||
| 	process(working: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection; | ||||
|  | ||||
| } | ||||
							
								
								
									
										12
									
								
								src/post-processors/purge-obsolete-keys.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/post-processors/purge-obsolete-keys.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| import { TranslationCollection } from '../utils/translation.collection'; | ||||
| import { PostProcessorInterface } from './post-processor.interface'; | ||||
|  | ||||
| export class PurgeObsoleteKeysPostProcessor implements PostProcessorInterface { | ||||
|  | ||||
| 	public name: string = 'PurgeObsoleteKeys'; | ||||
|  | ||||
| 	public process(working: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection { | ||||
| 		return working.intersect(extracted); | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										12
									
								
								src/post-processors/sort-by-key.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/post-processors/sort-by-key.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| import { TranslationCollection } from '../utils/translation.collection'; | ||||
| import { PostProcessorInterface } from './post-processor.interface'; | ||||
|  | ||||
| export class SortByKeyPostProcessor implements PostProcessorInterface { | ||||
|  | ||||
| 	public name: string = 'SortByKey'; | ||||
|  | ||||
| 	public process(working: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection { | ||||
| 		return working.sort(); | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @@ -41,6 +41,14 @@ export class TranslationCollection { | ||||
| 		return new TranslationCollection(values); | ||||
| 	} | ||||
|  | ||||
| 	public map(callback: (key?: string, val?: string) => string): TranslationCollection { | ||||
| 		let values: TranslationType = {}; | ||||
| 		this.forEach((key: string, val: string) => { | ||||
| 			values[key] = callback.call(this, key, val); | ||||
| 		}); | ||||
| 		return new TranslationCollection(values); | ||||
| 	} | ||||
|  | ||||
| 	public union(collection: TranslationCollection): TranslationCollection { | ||||
| 		return new TranslationCollection({ ...this.values, ...collection.values }); | ||||
| 	} | ||||
|   | ||||
| @@ -5,11 +5,11 @@ import { AbstractTemplateParser } from '../../src/parsers/abstract-template.pars | ||||
| class TestTemplateParser extends AbstractTemplateParser { | ||||
|  | ||||
| 	public isAngularComponent(filePath: string): boolean { | ||||
| 		return this._isAngularComponent(filePath); | ||||
| 		return super.isAngularComponent(filePath); | ||||
| 	} | ||||
|  | ||||
| 	public extractInlineTemplate(contents: string): string { | ||||
| 		return this._extractInlineTemplate(contents); | ||||
| 		return super.extractInlineTemplate(contents); | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -5,7 +5,7 @@ import { DirectiveParser } from '../../src/parsers/directive.parser'; | ||||
| class TestDirectiveParser extends DirectiveParser { | ||||
|  | ||||
| 	public normalizeTemplateAttributes(template: string): string { | ||||
| 		return this._normalizeTemplateAttributes(template); | ||||
| 		return super.normalizeTemplateAttributes(template); | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -4,10 +4,6 @@ import { ServiceParser } from '../../src/parsers/service.parser'; | ||||
|  | ||||
| class TestServiceParser extends ServiceParser { | ||||
|  | ||||
| 	/*public getInstancePropertyName(): string { | ||||
| 		return this._getInstancePropertyName(); | ||||
| 	}*/ | ||||
|  | ||||
| } | ||||
|  | ||||
| describe('ServiceParser', () => { | ||||
| @@ -20,20 +16,6 @@ describe('ServiceParser', () => { | ||||
| 		parser = new TestServiceParser(); | ||||
| 	}); | ||||
|  | ||||
| 	/*it('should extract variable used for TranslateService', () => { | ||||
| 		const contents = ` | ||||
| 			@Component({ }) | ||||
| 			export class AppComponent { | ||||
| 				public constructor( | ||||
| 					_serviceA: ServiceA, | ||||
| 					public _serviceB: ServiceB, | ||||
| 					protected _translateService: TranslateService | ||||
| 			) { } | ||||
| 		`; | ||||
| 		const name = parser.getInstancePropertyName(); | ||||
| 		expect(name).to.equal('_translateService'); | ||||
| 	});*/ | ||||
|  | ||||
| 	it('should extract strings in TranslateService\'s get() method', () => { | ||||
| 		const contents = ` | ||||
| 			@Component({ }) | ||||
|   | ||||
| @@ -0,0 +1,31 @@ | ||||
| import { expect } from 'chai'; | ||||
|  | ||||
| import { PostProcessorInterface } from '../../src/post-processors/post-processor.interface'; | ||||
| import { KeyAsDefaultValuePostProcessor } from '../../src/post-processors/key-as-default-value.post-processor'; | ||||
| import { TranslationCollection } from '../../src/utils/translation.collection'; | ||||
|  | ||||
| describe('KeyAsDefaultValuePostProcessor', () => { | ||||
|  | ||||
| 	let processor: PostProcessorInterface; | ||||
|  | ||||
| 	beforeEach(() => { | ||||
| 		processor = new KeyAsDefaultValuePostProcessor(); | ||||
| 	}); | ||||
|  | ||||
| 	it('should use key as default value', () => { | ||||
| 		const collection = new TranslationCollection({ | ||||
| 			'I have no value': '', | ||||
| 			'I am already translated': 'Jeg er allerede oversat', | ||||
| 			'Use this key as value as well': '' | ||||
| 		}); | ||||
| 		const extracted = new TranslationCollection(); | ||||
| 		const existing = new TranslationCollection(); | ||||
|  | ||||
| 		expect(processor.process(collection, extracted, existing).values).to.deep.equal({ | ||||
| 			'I have no value': 'I have no value', | ||||
| 			'I am already translated': 'Jeg er allerede oversat', | ||||
| 			'Use this key as value as well': 'Use this key as value as well' | ||||
| 		}); | ||||
| 	}); | ||||
|  | ||||
| }); | ||||
| @@ -0,0 +1,36 @@ | ||||
| import { expect } from 'chai'; | ||||
|  | ||||
| import { PostProcessorInterface } from '../../src/post-processors/post-processor.interface'; | ||||
| import { PurgeObsoleteKeysPostProcessor } from '../../src/post-processors/purge-obsolete-keys.post-processor'; | ||||
| import { TranslationCollection } from '../../src/utils/translation.collection'; | ||||
|  | ||||
| describe('KeyAsDefaultValuePostProcessor', () => { | ||||
|  | ||||
| 	let processor: PostProcessorInterface; | ||||
|  | ||||
| 	beforeEach(() => { | ||||
| 		processor = new PurgeObsoleteKeysPostProcessor(); | ||||
| 	}); | ||||
|  | ||||
| 	it('should purge obsolete keys', () => { | ||||
| 		const collection = new TranslationCollection({ | ||||
| 			'I am completely new': '', | ||||
| 			'I already exist': '', | ||||
| 			'I already exist but was not present in extract': '' | ||||
| 		}); | ||||
| 		const extracted = new TranslationCollection({ | ||||
| 			'I am completely new': '', | ||||
| 			'I already exist': '' | ||||
| 		}); | ||||
| 		const existing = new TranslationCollection({ | ||||
| 			'I already exist': '', | ||||
| 			'I already exist but was not present in extract': '' | ||||
| 		}); | ||||
|  | ||||
| 		expect(processor.process(collection, extracted, existing).values).to.deep.equal({ | ||||
| 			'I am completely new': '', | ||||
| 			'I already exist': '' | ||||
| 		}); | ||||
| 	}); | ||||
|  | ||||
| }); | ||||
							
								
								
									
										33
									
								
								tests/post-processors/sort-by-key.post-processor.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								tests/post-processors/sort-by-key.post-processor.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,33 @@ | ||||
| import { expect } from 'chai'; | ||||
|  | ||||
| import { PostProcessorInterface } from '../../src/post-processors/post-processor.interface'; | ||||
| import { SortByKeyPostProcessor } from '../../src/post-processors/sort-by-key.post-processor'; | ||||
| import { TranslationCollection } from '../../src/utils/translation.collection'; | ||||
|  | ||||
| describe('SortByKeyPostProcessor', () => { | ||||
|  | ||||
| 	let processor: PostProcessorInterface; | ||||
|  | ||||
| 	beforeEach(() => { | ||||
| 		processor = new SortByKeyPostProcessor(); | ||||
| 	}); | ||||
|  | ||||
| 	it('should sort keys alphanumerically', () => { | ||||
| 		const collection = new TranslationCollection({ | ||||
| 			'z': 'last value', | ||||
| 			'a': 'a value', | ||||
| 			'9': 'a numeric key', | ||||
| 			'b': 'another value' | ||||
| 		}); | ||||
| 		const extracted = new TranslationCollection(); | ||||
| 		const existing = new TranslationCollection(); | ||||
|  | ||||
| 		expect(processor.process(collection, extracted, existing).values).to.deep.equal({ | ||||
| 			'9': 'a numeric key', | ||||
| 			'a': 'a value', | ||||
| 			'b': 'another value', | ||||
| 			'z': 'last value' | ||||
| 		}); | ||||
| 	}); | ||||
|  | ||||
| }); | ||||
| @@ -69,7 +69,7 @@ describe('StringCollection', () => { | ||||
|  | ||||
| 	it('should intersect with passed collection', () => { | ||||
| 		collection = collection.addKeys(['red', 'green', 'blue']); | ||||
| 		const newCollection = new TranslationCollection( { red: '', blue: '' }); | ||||
| 		const newCollection = new TranslationCollection( { red: '', blue: '' }); | ||||
| 		expect(collection.intersect(newCollection).values).to.deep.equal({ red: '', blue: '' }); | ||||
| 	}); | ||||
|  | ||||
| @@ -79,10 +79,16 @@ describe('StringCollection', () => { | ||||
| 		expect(collection.intersect(newCollection).values).to.deep.equal({ red: 'rød', blue: 'blå' }); | ||||
| 	}); | ||||
|  | ||||
| 	it('should sort translations in alphabetical order', () => { | ||||
| 	it('should sort keys alphabetically', () => { | ||||
| 		collection = new TranslationCollection({ red: 'rød', green: 'grøn', blue: 'blå' }); | ||||
| 		collection = collection.sort(); | ||||
| 		expect(collection.keys()).deep.equal(['blue', 'green', 'red']); | ||||
| 	}); | ||||
|  | ||||
| 	it('should map values', () => { | ||||
| 		collection = new TranslationCollection({ red: 'rød', green: 'grøn', blue: 'blå' }); | ||||
| 		collection = collection.map((key, val) => 'mapped value'); | ||||
| 		expect(collection.values).to.deep.equal({ red: 'mapped value', green: 'mapped value', blue: 'mapped value' }); | ||||
| 	}); | ||||
|  | ||||
| }); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user