From 3827789346833735f1187a2b60b47af689fded7e Mon Sep 17 00:00:00 2001 From: Kim Biesbjerg Date: Wed, 18 Sep 2019 13:47:57 +0200 Subject: [PATCH] cleanup --- src/cli/tasks/extract.task.ts | 21 +++++++++-------- src/compilers/po.compiler.ts | 30 +++++++++++++----------- src/utils/ast-helpers.ts | 7 ++---- src/utils/translation.collection.ts | 36 ++++++++++++++++------------- 4 files changed, 51 insertions(+), 43 deletions(-) diff --git a/src/cli/tasks/extract.task.ts b/src/cli/tasks/extract.task.ts index 1e73c11..9783c52 100644 --- a/src/cli/tasks/extract.task.ts +++ b/src/cli/tasks/extract.task.ts @@ -16,7 +16,6 @@ export interface ExtractTaskOptionsInterface { } export class ExtractTask implements TaskInterface { - protected options: ExtractTaskOptionsInterface = { replace: false, patterns: [] @@ -102,11 +101,11 @@ export class ExtractTask implements TaskInterface { protected extract(): TranslationCollection { let collection: TranslationCollection = new TranslationCollection(); this.inputs.forEach(dir => { - this.readDir(dir, this.options.patterns).forEach(path => { - this.out(dim('- %s'), path); - const contents: string = fs.readFileSync(path, 'utf-8'); + this.readDir(dir, this.options.patterns).forEach(filePath => { + this.out(dim('- %s'), filePath); + const contents: string = fs.readFileSync(filePath, 'utf-8'); this.parsers.forEach(parser => { - const extracted = parser.extract(contents, path); + const extracted = parser.extract(contents, filePath); if (extracted instanceof TranslationCollection) { collection = collection.union(extracted); } @@ -119,7 +118,11 @@ export class ExtractTask implements TaskInterface { /** * Run strings through configured post processors */ - protected process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection { + protected process( + draft: TranslationCollection, + extracted: TranslationCollection, + existing: TranslationCollection + ): TranslationCollection { this.postProcessors.forEach(postProcessor => { draft = postProcessor.process(draft, extracted, existing); }); @@ -143,8 +146,9 @@ export class ExtractTask implements TaskInterface { */ protected readDir(dir: string, patterns: string[]): string[] { return patterns.reduce((results, pattern) => { - return glob.sync(dir + pattern) - .filter(path => fs.statSync(path).isFile()) + return glob + .sync(dir + pattern) + .filter(filePath => fs.statSync(filePath).isFile()) .concat(results); }, []); } @@ -178,5 +182,4 @@ export class ExtractTask implements TaskInterface { this.out(cyan(dim(`- ${this.compiler.constructor.name}`))); this.out(); } - } diff --git a/src/compilers/po.compiler.ts b/src/compilers/po.compiler.ts index a6ffa31..3cb5358 100644 --- a/src/compilers/po.compiler.ts +++ b/src/compilers/po.compiler.ts @@ -4,7 +4,6 @@ import { TranslationCollection, TranslationType } from '../utils/translation.col import * as gettext from 'gettext-parser'; export class PoCompiler implements CompilerInterface { - public extension: string = 'po'; /** @@ -23,13 +22,16 @@ export class PoCompiler implements CompilerInterface { 'content-transfer-encoding': '8bit' }, translations: { - [this.domain]: Object.keys(collection.values).reduce((translations, key) => { - translations[key] = { - msgid: key, - msgstr: collection.get(key) - }; - return translations; - }, {} as any) + [this.domain]: Object.keys(collection.values).reduce( + (translations, key) => { + translations[key] = { + msgid: key, + msgstr: collection.get(key) + }; + return translations; + }, + {} as any + ) } }; @@ -46,12 +48,14 @@ export class PoCompiler implements CompilerInterface { const values = Object.keys(po.translations[this.domain]) .filter(key => key.length > 0) - .reduce((values, key) => { - values[key] = po.translations[this.domain][key].msgstr.pop(); - return values; - }, {} as TranslationType); + .reduce( + (result, key) => { + result[key] = po.translations[this.domain][key].msgstr.pop(); + return result; + }, + {} as TranslationType + ); return new TranslationCollection(values); } - } diff --git a/src/utils/ast-helpers.ts b/src/utils/ast-helpers.ts index d71cd39..c58e1da 100644 --- a/src/utils/ast-helpers.ts +++ b/src/utils/ast-helpers.ts @@ -77,7 +77,7 @@ export function findMethodCallExpressions(node: Node, prop: string, fnName: stri fnName = fnName.join('|'); } const query = `CallExpression > PropertyAccessExpression:has(Identifier[name=/^(${fnName})$/]):has(PropertyAccessExpression:has(Identifier[name="${prop}"]):has(ThisKeyword))`; - let nodes = tsquery(node, query).map(node => node.parent as CallExpression); + const nodes = tsquery(node, query).map(n => n.parent as CallExpression); return nodes; } @@ -89,10 +89,7 @@ export function getStringsFromExpression(expression: Expression): string[] { if (isArrayLiteralExpression(expression)) { return expression.elements.reduce((result: string[], element: Expression) => { const strings = getStringsFromExpression(element); - return [ - ...result, - ...strings - ]; + return [...result, ...strings]; }, []); } diff --git a/src/utils/translation.collection.ts b/src/utils/translation.collection.ts index 380fc7e..8bfc71a 100644 --- a/src/utils/translation.collection.ts +++ b/src/utils/translation.collection.ts @@ -3,7 +3,6 @@ export interface TranslationType { } export class TranslationCollection { - public values: TranslationType = {}; public constructor(values: TranslationType = {}) { @@ -15,10 +14,13 @@ export class TranslationCollection { } public addKeys(keys: string[]): TranslationCollection { - const values = keys.reduce((results, key) => { - results[key] = ''; - return results; - }, {} as TranslationType); + const values = keys.reduce( + (results, key) => { + results[key] = ''; + return results; + }, + {} as TranslationType + ); return new TranslationCollection({ ...this.values, ...values }); } @@ -32,7 +34,7 @@ export class TranslationCollection { } public filter(callback: (key?: string, val?: string) => boolean): TranslationCollection { - let values: TranslationType = {}; + const values: TranslationType = {}; this.forEach((key, val) => { if (callback.call(this, key, val)) { values[key] = val; @@ -42,7 +44,7 @@ export class TranslationCollection { } public map(callback: (key?: string, val?: string) => string): TranslationCollection { - let values: TranslationType = {}; + const values: TranslationType = {}; this.forEach((key, val) => { values[key] = callback.call(this, key, val); }); @@ -54,11 +56,11 @@ export class TranslationCollection { } public intersect(collection: TranslationCollection): TranslationCollection { - let values: TranslationType = {}; - this.filter(key => collection.has(key)) - this.forEach((key, val) => { - values[key] = val; - }); + const values: TranslationType = {}; + this.filter(key => collection.has(key)); + this.forEach((key, val) => { + values[key] = val; + }); return new TranslationCollection(values); } @@ -84,10 +86,12 @@ export class TranslationCollection { } public sort(compareFn?: (a: string, b: string) => number): TranslationCollection { - let values: TranslationType = {}; - this.keys().sort(compareFn).forEach((key) => { - values[key] = this.get(key); - }); + const values: TranslationType = {}; + this.keys() + .sort(compareFn) + .forEach(key => { + values[key] = this.get(key); + }); return new TranslationCollection(values); }