(refactor) get rid of AbstractTemplateParser
This commit is contained in:
		| @@ -1,24 +0,0 @@ | |||||||
| 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 { |  | ||||||
| 		return (/\.ts|js$/i).test(path); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	/** |  | ||||||
| 	 * Extracts inline template from components |  | ||||||
| 	 */ |  | ||||||
| 	protected extractInlineTemplate(contents: string): string { |  | ||||||
| 		const regExp: RegExp = /template\s*:\s*(["'`])([^\1]*?)\1/; |  | ||||||
| 		const match = regExp.exec(contents); |  | ||||||
| 		if (match !== null) { |  | ||||||
| 			return match[2]; |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		return ''; |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| } |  | ||||||
| @@ -1,16 +1,16 @@ | |||||||
| import { ParserInterface } from './parser.interface'; | import { ParserInterface } from './parser.interface'; | ||||||
| import { AbstractTemplateParser } from './abstract-template.parser'; |  | ||||||
| import { TranslationCollection } from '../utils/translation.collection'; | import { TranslationCollection } from '../utils/translation.collection'; | ||||||
|  | import { isPathAngularComponent, extractComponentInlineTemplate } from '../../src/utils/utils'; | ||||||
|  |  | ||||||
| import * as cheerio from 'cheerio'; | import * as cheerio from 'cheerio'; | ||||||
|  |  | ||||||
| const $ = cheerio.load('', {xmlMode: true}); | const $ = cheerio.load('', {xmlMode: true}); | ||||||
|  |  | ||||||
| export class DirectiveParser extends AbstractTemplateParser implements ParserInterface { | export class DirectiveParser implements ParserInterface { | ||||||
|  |  | ||||||
| 	public extract(contents: string, path?: string): TranslationCollection { | 	public extract(contents: string, path?: string): TranslationCollection { | ||||||
| 		if (path && this.isAngularComponent(path)) { | 		if (path && isPathAngularComponent(path)) { | ||||||
| 			contents = this.extractInlineTemplate(contents); | 			contents = extractComponentInlineTemplate(contents); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		return this.parseTemplate(contents); | 		return this.parseTemplate(contents); | ||||||
|   | |||||||
| @@ -1,12 +1,12 @@ | |||||||
| import { ParserInterface } from './parser.interface'; | import { ParserInterface } from './parser.interface'; | ||||||
| import { AbstractTemplateParser } from './abstract-template.parser'; |  | ||||||
| import { TranslationCollection } from '../utils/translation.collection'; | import { TranslationCollection } from '../utils/translation.collection'; | ||||||
|  | import { isPathAngularComponent, extractComponentInlineTemplate } from '../../src/utils/utils'; | ||||||
|  |  | ||||||
| export class PipeParser extends AbstractTemplateParser implements ParserInterface { | export class PipeParser implements ParserInterface { | ||||||
|  |  | ||||||
| 	public extract(contents: string, path?: string): TranslationCollection { | 	public extract(contents: string, path?: string): TranslationCollection { | ||||||
| 		if (path && this.isAngularComponent(path)) { | 		if (path && isPathAngularComponent(path)) { | ||||||
| 			contents = this.extractInlineTemplate(contents); | 			contents = extractComponentInlineTemplate(contents); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		return this.parseTemplate(contents); | 		return this.parseTemplate(contents); | ||||||
|   | |||||||
| @@ -1,3 +1,22 @@ | |||||||
| export function _(key: string | string[]): string | string[] { | export function _(key: string | string[]): string | string[] { | ||||||
| 	return key; | 	return key; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Assumes file is an Angular component if type is javascript/typescript | ||||||
|  |  */ | ||||||
|  | export function isPathAngularComponent(path: string): boolean { | ||||||
|  | 	return (/\.ts|js$/i).test(path); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Extract inline template from a component | ||||||
|  |  */ | ||||||
|  | export function extractComponentInlineTemplate(contents: string): string { | ||||||
|  | 	const regExp: RegExp = /template\s*:\s*(["'`])([^\1]*?)\1/; | ||||||
|  | 	const match = regExp.exec(contents); | ||||||
|  | 	if (match !== null) { | ||||||
|  | 		return match[2]; | ||||||
|  | 	} | ||||||
|  | 	return ''; | ||||||
|  | } | ||||||
|   | |||||||
| @@ -37,9 +37,9 @@ describe('NamespacedJsonCompiler', () => { | |||||||
|  |  | ||||||
| 	it('should preserve numeric values on compile', () => { | 	it('should preserve numeric values on compile', () => { | ||||||
| 		const collection = new TranslationCollection({ | 		const collection = new TranslationCollection({ | ||||||
| 			"option.0": '', | 			'option.0': '', | ||||||
| 			"option.1": '', | 			'option.1': '', | ||||||
| 			"option.2": '' | 			'option.2': '' | ||||||
| 		}); | 		}); | ||||||
| 		const result: string = compiler.compile(collection); | 		const result: string = compiler.compile(collection); | ||||||
| 		expect(result).to.equal('{\n\t"option": {\n\t\t"0": "",\n\t\t"1": "",\n\t\t"2": ""\n\t}\n}'); | 		expect(result).to.equal('{\n\t"option": {\n\t\t"0": "",\n\t\t"1": "",\n\t\t"2": ""\n\t}\n}'); | ||||||
|   | |||||||
| @@ -1,39 +1,21 @@ | |||||||
| import { expect } from 'chai'; | import { expect } from 'chai'; | ||||||
| 
 | 
 | ||||||
| import { AbstractTemplateParser } from '../../src/parsers/abstract-template.parser'; | import { isPathAngularComponent, extractComponentInlineTemplate } from '../../src/utils/utils'; | ||||||
| 
 | 
 | ||||||
| class TestTemplateParser extends AbstractTemplateParser { | describe('Utils', () => { | ||||||
| 
 |  | ||||||
| 	public isAngularComponent(filePath: string): boolean { |  | ||||||
| 		return super.isAngularComponent(filePath); |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	public extractInlineTemplate(contents: string): string { |  | ||||||
| 		return super.extractInlineTemplate(contents); |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| describe('AbstractTemplateParser', () => { |  | ||||||
| 
 |  | ||||||
| 	let parser: TestTemplateParser; |  | ||||||
| 
 |  | ||||||
| 	beforeEach(() => { |  | ||||||
| 		parser = new TestTemplateParser(); |  | ||||||
| 	}); |  | ||||||
| 
 | 
 | ||||||
| 	it('should recognize js extension as angular component', () => { | 	it('should recognize js extension as angular component', () => { | ||||||
| 		const result = parser.isAngularComponent('test.js'); | 		const result = isPathAngularComponent('test.js'); | ||||||
| 		expect(result).to.equal(true); | 		expect(result).to.equal(true); | ||||||
| 	}); | 	}); | ||||||
| 
 | 
 | ||||||
| 	it('should recognize ts extension as angular component', () => { | 	it('should recognize ts extension as angular component', () => { | ||||||
| 		const result = parser.isAngularComponent('test.ts'); | 		const result = isPathAngularComponent('test.ts'); | ||||||
| 		expect(result).to.equal(true); | 		expect(result).to.equal(true); | ||||||
| 	}); | 	}); | ||||||
| 
 | 
 | ||||||
| 	it('should not recognize html extension as angular component', () => { | 	it('should not recognize html extension as angular component', () => { | ||||||
| 		const result = parser.isAngularComponent('test.html'); | 		const result = isPathAngularComponent('test.html'); | ||||||
| 		expect(result).to.equal(false); | 		expect(result).to.equal(false); | ||||||
| 	}); | 	}); | ||||||
| 
 | 
 | ||||||
| @@ -45,7 +27,7 @@ describe('AbstractTemplateParser', () => { | |||||||
| 			}) | 			}) | ||||||
| 			export class TestComponent { } | 			export class TestComponent { } | ||||||
| 		`;
 | 		`;
 | ||||||
| 		const template = parser.extractInlineTemplate(contents); | 		const template = extractComponentInlineTemplate(contents); | ||||||
| 		expect(template).to.equal('<p translate>Hello World</p>'); | 		expect(template).to.equal('<p translate>Hello World</p>'); | ||||||
| 	}); | 	}); | ||||||
| 
 | 
 | ||||||
| @@ -66,7 +48,7 @@ describe('AbstractTemplateParser', () => { | |||||||
| 			}) | 			}) | ||||||
| 			export class TestComponent { } | 			export class TestComponent { } | ||||||
| 		`;
 | 		`;
 | ||||||
| 		const template = parser.extractInlineTemplate(contents); | 		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'); | 		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'); | ||||||
| 	}); | 	}); | ||||||
| 
 | 
 | ||||||
| @@ -4,7 +4,7 @@ import { PostProcessorInterface } from '../../src/post-processors/post-processor | |||||||
| import { PurgeObsoleteKeysPostProcessor } from '../../src/post-processors/purge-obsolete-keys.post-processor'; | import { PurgeObsoleteKeysPostProcessor } from '../../src/post-processors/purge-obsolete-keys.post-processor'; | ||||||
| import { TranslationCollection } from '../../src/utils/translation.collection'; | import { TranslationCollection } from '../../src/utils/translation.collection'; | ||||||
|  |  | ||||||
| describe('KeyAsDefaultValuePostProcessor', () => { | describe('PurgeObsoleteKeysPostProcessor', () => { | ||||||
|  |  | ||||||
| 	let processor: PostProcessorInterface; | 	let processor: PostProcessorInterface; | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user