(chore) run prettier

This commit is contained in:
Kim Biesbjerg
2019-09-18 14:16:47 +02:00
parent 7d0d52429f
commit 306622b9a0
28 changed files with 61 additions and 113 deletions

View File

@@ -35,7 +35,6 @@ export const cli = yargs
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
throw new Error(`The path you supplied was not found: '${dir}'`);
}
});
return true;
})
@@ -100,12 +99,7 @@ const extractTask = new ExtractTask(cli.input, cli.output, {
});
// Parsers
const parsers: ParserInterface[] = [
new PipeParser(),
new DirectiveParser(),
new ServiceParser(),
new MarkerParser()
];
const parsers: ParserInterface[] = [new PipeParser(), new DirectiveParser(), new ServiceParser(), new MarkerParser()];
extractTask.setParsers(parsers);
// Post processors

View File

@@ -118,11 +118,7 @@ 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);
});

View File

@@ -4,14 +4,16 @@ import { NamespacedJsonCompiler } from '../compilers/namespaced-json.compiler';
import { PoCompiler } from '../compilers/po.compiler';
export class CompilerFactory {
public static create(format: string, options?: {}): CompilerInterface {
switch (format) {
case 'pot': return new PoCompiler(options);
case 'json': return new JsonCompiler(options);
case 'namespaced-json': return new NamespacedJsonCompiler(options);
default: throw new Error(`Unknown format: ${format}`);
case 'pot':
return new PoCompiler(options);
case 'json':
return new JsonCompiler(options);
case 'namespaced-json':
return new NamespacedJsonCompiler(options);
default:
throw new Error(`Unknown format: ${format}`);
}
}
}

View File

@@ -1,11 +1,9 @@
import { TranslationCollection } from '../utils/translation.collection';
export interface CompilerInterface {
extension: string;
compile(collection: TranslationCollection): string;
parse(contents: string): TranslationCollection;
}

View File

@@ -5,7 +5,6 @@ import { stripBOM } from '../utils/utils';
import { flatten } from 'flat';
export class JsonCompiler implements CompilerInterface {
public indentation: string = '\t';
public extension: string = 'json';
@@ -31,5 +30,4 @@ export class JsonCompiler implements CompilerInterface {
protected isNamespacedJsonFormat(values: any): boolean {
return Object.keys(values).some(key => typeof values[key] === 'object');
}
}

View File

@@ -5,7 +5,6 @@ import { stripBOM } from '../utils/utils';
import { flatten, unflatten } from 'flat';
export class NamespacedJsonCompiler implements CompilerInterface {
public indentation: string = '\t';
public extension = 'json';
@@ -27,5 +26,4 @@ export class NamespacedJsonCompiler implements CompilerInterface {
const values: {} = flatten(JSON.parse(stripBOM(contents)));
return new TranslationCollection(values);
}
}

View File

@@ -5,7 +5,6 @@ import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils
import { parseTemplate, TmplAstNode, TmplAstElement, TmplAstTextAttribute } from '@angular/compiler';
export class DirectiveParser implements ParserInterface {
public extract(source: string, filePath: string): TranslationCollection | null {
if (filePath && isPathAngularComponent(filePath)) {
source = extractComponentInlineTemplate(source);
@@ -42,13 +41,16 @@ export class DirectiveParser implements ParserInterface {
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]);
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[] {
@@ -56,9 +58,7 @@ export class DirectiveParser implements ParserInterface {
}
protected isElement(node: any): node is TmplAstElement {
return node
&& node.attributes !== undefined
&& node.children !== undefined;
return node && node.attributes !== undefined && node.children !== undefined;
}
protected isTranslatable(node: TmplAstNode): boolean {
@@ -70,7 +70,7 @@ export class DirectiveParser implements ParserInterface {
protected getElementTranslateAttrValue(element: TmplAstElement): string {
const attr: TmplAstTextAttribute = element.attributes.find(attribute => attribute.name === 'translate');
return attr && attr.value || '';
return (attr && attr.value) || '';
}
protected getElementContents(element: TmplAstElement): string {
@@ -79,5 +79,4 @@ export class DirectiveParser implements ParserInterface {
const end = element.endSourceSpan.start.offset;
return contents.substring(start, end).trim();
}
}

View File

@@ -8,7 +8,6 @@ const MARKER_MODULE_NAME = '@biesbjerg/ngx-translate-extract-marker';
const MARKER_IMPORT_NAME = 'marker';
export class MarkerParser implements ParserInterface {
public extract(source: string, filePath: string): TranslationCollection | null {
const sourceFile = tsquery.ast(source, filePath);
@@ -30,5 +29,4 @@ export class MarkerParser implements ParserInterface {
});
return collection;
}
}

View File

@@ -1,7 +1,5 @@
import { TranslationCollection } from '../utils/translation.collection';
export interface ParserInterface {
extract(source: string, filePath: string): TranslationCollection | null;
}

View File

@@ -3,7 +3,6 @@ import { TranslationCollection } from '../utils/translation.collection';
import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils';
export class PipeParser implements ParserInterface {
public extract(source: string, filePath: string): TranslationCollection | null {
if (filePath && isPathAngularComponent(filePath)) {
source = extractComponentInlineTemplate(source);
@@ -17,11 +16,10 @@ export class PipeParser implements ParserInterface {
const regExp: RegExp = /(['"`])((?:(?!\1).|\\\1)+)\1\s*\|\s*translate/g;
let matches: RegExpExecArray;
while (matches = regExp.exec(template)) {
collection = collection.add(matches[2].split('\\\'').join('\''));
while ((matches = regExp.exec(template))) {
collection = collection.add(matches[2].split("\\'").join("'"));
}
return collection;
}
}

View File

@@ -8,7 +8,6 @@ const TRANSLATE_SERVICE_TYPE_REFERENCE = 'TranslateService';
const TRANSLATE_SERVICE_METHOD_NAMES = ['get', 'instant', 'stream'];
export class ServiceParser implements ParserInterface {
public extract(source: string, filePath: string): TranslationCollection | null {
const sourceFile = tsquery.ast(source, filePath);
@@ -37,5 +36,4 @@ export class ServiceParser implements ParserInterface {
});
return collection;
}
}

View File

@@ -2,11 +2,9 @@ import { TranslationCollection } from '../utils/translation.collection';
import { PostProcessorInterface } from './post-processor.interface';
export class KeyAsDefaultValuePostProcessor implements PostProcessorInterface {
public name: string = 'KeyAsDefaultValue';
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
return draft.map((key, val) => val === '' ? key : val);
return draft.map((key, val) => (val === '' ? key : val));
}
}

View File

@@ -2,11 +2,9 @@ import { TranslationCollection } from '../utils/translation.collection';
import { PostProcessorInterface } from './post-processor.interface';
export class NullAsDefaultValuePostProcessor implements PostProcessorInterface {
public name: string = 'NullAsDefaultValue';
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
return draft.map((key, val) => existing.get(key) === undefined ? null : val);
return draft.map((key, val) => (existing.get(key) === undefined ? null : val));
}
}

View File

@@ -1,9 +1,7 @@
import { TranslationCollection } from '../utils/translation.collection';
export interface PostProcessorInterface {
name: string;
process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection;
}

View File

@@ -2,11 +2,9 @@ import { TranslationCollection } from '../utils/translation.collection';
import { PostProcessorInterface } from './post-processor.interface';
export class PurgeObsoleteKeysPostProcessor implements PostProcessorInterface {
public name: string = 'PurgeObsoleteKeys';
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
return draft.intersect(extracted);
}
}

View File

@@ -2,11 +2,9 @@ import { TranslationCollection } from '../utils/translation.collection';
import { PostProcessorInterface } from './post-processor.interface';
export class SortByKeyPostProcessor implements PostProcessorInterface {
public name: string = 'SortByKey';
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
return draft.sort();
}
}

View File

@@ -2,7 +2,7 @@
* Assumes file is an Angular component if type is javascript/typescript
*/
export function isPathAngularComponent(path: string): boolean {
return (/\.ts|js$/i).test(path);
return /\.ts|js$/i.test(path);
}
/**