(refactor) get rid of AbstractTemplateParser

This commit is contained in:
Kim Biesbjerg
2019-06-12 11:50:23 +02:00
parent 102286a209
commit 53eb4d1202
7 changed files with 38 additions and 61 deletions

View File

@@ -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 '';
}
}

View File

@@ -1,16 +1,16 @@
import { ParserInterface } from './parser.interface';
import { AbstractTemplateParser } from './abstract-template.parser';
import { TranslationCollection } from '../utils/translation.collection';
import { isPathAngularComponent, extractComponentInlineTemplate } from '../../src/utils/utils';
import * as cheerio from 'cheerio';
const $ = cheerio.load('', {xmlMode: true});
export class DirectiveParser extends AbstractTemplateParser implements ParserInterface {
export class DirectiveParser implements ParserInterface {
public extract(contents: string, path?: string): TranslationCollection {
if (path && this.isAngularComponent(path)) {
contents = this.extractInlineTemplate(contents);
if (path && isPathAngularComponent(path)) {
contents = extractComponentInlineTemplate(contents);
}
return this.parseTemplate(contents);

View File

@@ -1,12 +1,12 @@
import { ParserInterface } from './parser.interface';
import { AbstractTemplateParser } from './abstract-template.parser';
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 {
if (path && this.isAngularComponent(path)) {
contents = this.extractInlineTemplate(contents);
if (path && isPathAngularComponent(path)) {
contents = extractComponentInlineTemplate(contents);
}
return this.parseTemplate(contents);

View File

@@ -1,3 +1,22 @@
export function _(key: string | string[]): string | string[] {
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 '';
}