(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);
}
/**

View File

@ -4,7 +4,6 @@ import { TranslationCollection } from '../../src/utils/translation.collection';
import { NamespacedJsonCompiler } from '../../src/compilers/namespaced-json.compiler';
describe('NamespacedJsonCompiler', () => {
let compiler: NamespacedJsonCompiler;
beforeEach(() => {
@ -23,7 +22,10 @@ describe('NamespacedJsonCompiler', () => {
}
`;
const collection: TranslationCollection = compiler.parse(contents);
expect(collection.values).to.deep.equal({'NAMESPACE.KEY.FIRST_KEY': '', 'NAMESPACE.KEY.SECOND_KEY': 'VALUE' });
expect(collection.values).to.deep.equal({
'NAMESPACE.KEY.FIRST_KEY': '',
'NAMESPACE.KEY.SECOND_KEY': 'VALUE'
});
});
it('should unflatten keys on compile', () => {
@ -59,11 +61,10 @@ describe('NamespacedJsonCompiler', () => {
it('should not reorder keys when compiled', () => {
const collection = new TranslationCollection({
'BROWSE': '',
'LOGIN': ''
BROWSE: '',
LOGIN: ''
});
const result: string = compiler.compile(collection);
expect(result).to.equal('{\n\t"BROWSE": "",\n\t"LOGIN": ""\n}');
});
});

View File

@ -3,7 +3,6 @@ import { expect } from 'chai';
import { DirectiveParser } from '../../src/parsers/directive.parser';
describe('DirectiveParser', () => {
const templateFilename: string = 'test.template.html';
const componentFilename: string = 'test.component.ts';
@ -86,5 +85,4 @@ describe('DirectiveParser', () => {
const keys = parser.extract(contents, componentFilename).keys();
expect(keys).to.deep.equal([]);
});
});

View File

@ -3,7 +3,6 @@ import { expect } from 'chai';
import { MarkerParser } from '../../src/parsers/marker.parser';
describe('MarkerParser', () => {
const componentFilename: string = 'test.component.ts';
let parser: MarkerParser;
@ -12,7 +11,6 @@ describe('MarkerParser', () => {
parser = new MarkerParser();
});
it('should extract strings using marker function', () => {
const contents = `
import { marker } from '@biesbjerg/ngx-translate-extract-marker';
@ -35,11 +33,6 @@ describe('MarkerParser', () => {
_('Mix ' + \`of \` + 'different ' + \`types\`);
`;
const keys = parser.extract(contents, componentFilename).keys();
expect(keys).to.deep.equal([
'Hello world',
'This is a very very very very long line.',
'Mix of different types'
]);
expect(keys).to.deep.equal(['Hello world', 'This is a very very very very long line.', 'Mix of different types']);
});
});

View File

@ -3,7 +3,6 @@ import { expect } from 'chai';
import { PipeParser } from '../../src/parsers/pipe.parser';
describe('PipeParser', () => {
const templateFilename: string = 'test.template.html';
let parser: PipeParser;
@ -118,5 +117,4 @@ describe('PipeParser', () => {
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['message']);
});
});

View File

@ -2,12 +2,9 @@ import { expect } from 'chai';
import { ServiceParser } from '../../src/parsers/service.parser';
class TestServiceParser extends ServiceParser {
}
class TestServiceParser extends ServiceParser {}
describe('ServiceParser', () => {
const componentFilename: string = 'test.component.ts';
let parser: TestServiceParser;
@ -44,7 +41,7 @@ describe('ServiceParser', () => {
expect(keys).to.deep.equal(['Fallback message']);
});
it('should extract strings in TranslateService\'s get() method', () => {
it("should extract strings in TranslateService's get() method", () => {
const contents = `
@Component({ })
export class AppComponent {
@ -57,7 +54,7 @@ describe('ServiceParser', () => {
expect(keys).to.deep.equal(['Hello World']);
});
it('should extract strings in TranslateService\'s instant() method', () => {
it("should extract strings in TranslateService's instant() method", () => {
const contents = `
@Component({ })
export class AppComponent {
@ -70,7 +67,7 @@ describe('ServiceParser', () => {
expect(keys).to.deep.equal(['Hello World']);
});
it('should extract strings in TranslateService\'s stream() method', () => {
it("should extract strings in TranslateService's stream() method", () => {
const contents = `
@Component({ })
export class AppComponent {
@ -83,7 +80,7 @@ describe('ServiceParser', () => {
expect(keys).to.deep.equal(['Hello World']);
});
it('should extract array of strings in TranslateService\'s get() method', () => {
it("should extract array of strings in TranslateService's get() method", () => {
const contents = `
@Component({ })
export class AppComponent {
@ -96,7 +93,7 @@ describe('ServiceParser', () => {
expect(keys).to.deep.equal(['Hello', 'World']);
});
it('should extract array of strings in TranslateService\'s instant() method', () => {
it("should extract array of strings in TranslateService's instant() method", () => {
const contents = `
@Component({ })
export class AppComponent {
@ -109,7 +106,7 @@ describe('ServiceParser', () => {
expect(key).to.deep.equal(['Hello', 'World']);
});
it('should extract array of strings in TranslateService\'s stream() method', () => {
it("should extract array of strings in TranslateService's stream() method", () => {
const contents = `
@Component({ })
export class AppComponent {
@ -298,5 +295,4 @@ describe('ServiceParser', () => {
const keys = parser.extract(contents, componentFilename).keys();
expect(keys).to.deep.equal(['Back']);
});
});

View File

@ -3,7 +3,6 @@ import { expect } from 'chai';
import { isPathAngularComponent, extractComponentInlineTemplate } from '../../src/utils/utils';
describe('Utils', () => {
it('should recognize js extension as angular component', () => {
const result = isPathAngularComponent('test.js');
expect(result).to.equal(true);
@ -63,5 +62,4 @@ describe('Utils', () => {
const template = extractComponentInlineTemplate(contents);
expect(template).to.equal('\n\t\t\t\t\t<p>\n\t\t\t\t\t\tHello World\n\t\t\t\t\t</p>\n\t\t\t\t');
});
});

View File

@ -5,7 +5,6 @@ import { KeyAsDefaultValuePostProcessor } from '../../src/post-processors/key-as
import { TranslationCollection } from '../../src/utils/translation.collection';
describe('KeyAsDefaultValuePostProcessor', () => {
let processor: PostProcessorInterface;
beforeEach(() => {
@ -27,5 +26,4 @@ describe('KeyAsDefaultValuePostProcessor', () => {
'Use this key as value as well': 'Use this key as value as well'
});
});
});

View File

@ -5,7 +5,6 @@ import { NullAsDefaultValuePostProcessor } from '../../src/post-processors/null-
import { TranslationCollection } from '../../src/utils/translation.collection';
describe('NullAsDefaultValuePostProcessor', () => {
let processor: PostProcessorInterface;
beforeEach(() => {
@ -38,5 +37,4 @@ describe('NullAsDefaultValuePostProcessor', () => {
'String A': 'Streng A'
});
});
});

View File

@ -5,15 +5,14 @@ import { PurgeObsoleteKeysPostProcessor } from '../../src/post-processors/purge-
import { TranslationCollection } from '../../src/utils/translation.collection';
describe('PurgeObsoleteKeysPostProcessor', () => {
let processor: PostProcessorInterface;
let postProcessor: PostProcessorInterface;
beforeEach(() => {
processor = new PurgeObsoleteKeysPostProcessor();
postProcessor = new PurgeObsoleteKeysPostProcessor();
});
it('should purge obsolete keys', () => {
const collection = new TranslationCollection({
const draft = new TranslationCollection({
'I am completely new': '',
'I already exist': '',
'I already exist but was not present in extract': ''
@ -27,10 +26,9 @@ describe('PurgeObsoleteKeysPostProcessor', () => {
'I already exist but was not present in extract': ''
});
expect(processor.process(collection, extracted, existing).values).to.deep.equal({
expect(postProcessor.process(draft, extracted, existing).values).to.deep.equal({
'I am completely new': '',
'I already exist': ''
});
});
});

View File

@ -5,7 +5,6 @@ import { SortByKeyPostProcessor } from '../../src/post-processors/sort-by-key.po
import { TranslationCollection } from '../../src/utils/translation.collection';
describe('SortByKeyPostProcessor', () => {
let processor: PostProcessorInterface;
beforeEach(() => {
@ -14,20 +13,19 @@ describe('SortByKeyPostProcessor', () => {
it('should sort keys alphanumerically', () => {
const collection = new TranslationCollection({
'z': 'last value',
'a': 'a value',
z: 'last value',
a: 'a value',
'9': 'a numeric key',
'b': 'another value'
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'
a: 'a value',
b: 'another value',
z: 'last value'
});
});
});

View File

@ -3,7 +3,6 @@ import { expect } from 'chai';
import { TranslationCollection } from '../../src/utils/translation.collection';
describe('StringCollection', () => {
let collection: TranslationCollection;
beforeEach(() => {
@ -64,12 +63,15 @@ describe('StringCollection', () => {
it('should merge with other collection', () => {
collection = collection.add('oldKey', 'oldVal');
const newCollection = new TranslationCollection({ newKey: 'newVal' });
expect(collection.union(newCollection).values).to.deep.equal({ oldKey: 'oldVal', newKey: 'newVal' });
expect(collection.union(newCollection).values).to.deep.equal({
oldKey: 'oldVal',
newKey: 'newVal'
});
});
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: '' });
});
@ -88,7 +90,10 @@ describe('StringCollection', () => {
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' });
expect(collection.values).to.deep.equal({
red: 'mapped value',
green: 'mapped value',
blue: 'mapped value'
});
});
});