Refactor code. Add DirectiveParser (Thanks ocombe\!)
This commit is contained in:
23
src/parsers/abstract-template.parser.ts
Normal file
23
src/parsers/abstract-template.parser.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
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(filePath: string): boolean {
|
||||
return new RegExp('\.(ts|js)$', 'i').test(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts inline template from components
|
||||
*/
|
||||
protected _extractInlineTemplate(contents: string): string {
|
||||
const match = new RegExp('template\\s?:\\s?(("|\'|`)(.|[\\r\\n])+?[^\\\\]\\2)').exec(contents);
|
||||
if (match !== null) {
|
||||
return match[1];
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,66 +1,45 @@
|
||||
import {ParserInterface} from './parser.interface';
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { AbstractTemplateParser } from './abstract-template.parser';
|
||||
import * as $ from 'cheerio';
|
||||
|
||||
export class DirectiveParser implements ParserInterface {
|
||||
export class DirectiveParser extends AbstractTemplateParser implements ParserInterface {
|
||||
|
||||
public patterns = {
|
||||
template: `template:\\s?(("|'|\`)(.|[\\r\\n])+?[^\\\\]\\2)`
|
||||
};
|
||||
public process(filePath: string, contents: string): string[] {
|
||||
if (this._isAngularComponent(filePath)) {
|
||||
contents = this._extractInlineTemplate(contents);
|
||||
}
|
||||
|
||||
protected _parseTemplate(content) {
|
||||
let results: string[] = [],
|
||||
template = content.trim()
|
||||
// hack for cheerio that doesn't support wrapped attributes
|
||||
.replace('[translate]=', '__translate__=');
|
||||
return this._parseTemplate(contents);
|
||||
}
|
||||
|
||||
$(template).find('[translate],[__translate__]').contents().filter(function() {
|
||||
return this.nodeType === 3; // node type 3 = text node
|
||||
}).each(function() {
|
||||
let key,
|
||||
$this = $(this),
|
||||
element = $(this).parent(),
|
||||
wrappedAttr = element.attr('__translate__'), // previously [translate]=
|
||||
attr = element.attr('translate'); // translate=
|
||||
protected _parseTemplate(template: string): string[] {
|
||||
let results: string[] = [];
|
||||
|
||||
// only support string values for now
|
||||
if(wrappedAttr && wrappedAttr.match(/^['"].*['"]$/)) {
|
||||
key = wrappedAttr.substr(1, wrappedAttr.length - 2);
|
||||
} else if(attr) {
|
||||
key = attr;
|
||||
}
|
||||
template = this._normalizeTemplateAttributes(template);
|
||||
|
||||
if(!key) {
|
||||
key = $this.text().replace(/\\n/gi, '').trim();
|
||||
}
|
||||
$(template).find('[translate]')
|
||||
.each((i: number, element: CheerioElement) => {
|
||||
const $element = $(element);
|
||||
const attr = $element.attr('translate');
|
||||
const text = $element.text();
|
||||
|
||||
if(key) {
|
||||
results.push(key);
|
||||
}
|
||||
});
|
||||
if (attr) {
|
||||
results.push(attr);
|
||||
} else if (text) {
|
||||
results.push(text);
|
||||
}
|
||||
});
|
||||
|
||||
return results;
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public process(contents: string): string[] {
|
||||
const regExp = new RegExp(this.patterns.template, 'gi');
|
||||
|
||||
let results: string[] = [],
|
||||
hasTemplate = false,
|
||||
matches;
|
||||
|
||||
while(matches = regExp.exec(contents)) {
|
||||
let content = matches[1]
|
||||
.substr(1, matches[1].length - 2);
|
||||
|
||||
hasTemplate = true;
|
||||
results = results.concat(this._parseTemplate(content));
|
||||
}
|
||||
|
||||
if(!hasTemplate) {
|
||||
this._parseTemplate(contents);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
/**
|
||||
* Angular's `[attr]="'val'"` syntax is not valid HTML,
|
||||
* so Cheerio is not able to parse it.
|
||||
* This method replaces `[attr]="'val'""` with `attr="val"`
|
||||
*/
|
||||
protected _normalizeTemplateAttributes(template: string): string {
|
||||
return template.replace(/\[([^\]]+)\]="'([^\"]*)'"/g, '$1="$2"');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export interface ParserInterface {
|
||||
|
||||
process(contents: string): string[];
|
||||
process(filePath: string, contents: string): string[];
|
||||
|
||||
}
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { AbstractTemplateParser } from './abstract-template.parser';
|
||||
|
||||
export class PipeParser implements ParserInterface {
|
||||
export class PipeParser extends AbstractTemplateParser implements ParserInterface {
|
||||
|
||||
public patterns = {
|
||||
pipe: `(['"\`])([^\\1\\r\\n]*)\\1\\s+\\|\\s*translate(:.*?)?`
|
||||
};
|
||||
public process(filePath: string, contents: string): string[] {
|
||||
if (this._isAngularComponent(filePath)) {
|
||||
contents = this._extractInlineTemplate(contents);
|
||||
}
|
||||
|
||||
public process(contents: string): string[] {
|
||||
return this._parseTemplate(contents);
|
||||
}
|
||||
|
||||
protected _parseTemplate(template: string): string[] {
|
||||
let results: string[] = [];
|
||||
|
||||
for (let patternName in this.patterns) {
|
||||
const regExp = new RegExp(this.patterns[patternName], 'g');
|
||||
const regExp = new RegExp('([\'"`])([^\\1\\r\\n]*)\\1\\s+\\|\\s*translate(:.*?)?', 'g');
|
||||
|
||||
let matches;
|
||||
while (matches = regExp.exec(contents)) {
|
||||
results.push(matches[2]);
|
||||
}
|
||||
let matches;
|
||||
while (matches = regExp.exec(template)) {
|
||||
results.push(matches[2]);
|
||||
}
|
||||
|
||||
return results;
|
||||
|
||||
@@ -2,48 +2,25 @@ import { ParserInterface } from './parser.interface';
|
||||
|
||||
export class ServiceParser implements ParserInterface {
|
||||
|
||||
public patterns = {
|
||||
translateServiceMethods: `{{TRANSLATE_SERVICE}}\.(?:get|instant)\\s*\\\(\\s*(['"\`])([^\\1\\r\\n]+)\\1`,
|
||||
};
|
||||
|
||||
public process(contents: string): string[] {
|
||||
public process(filePath: string, contents: string): string[] {
|
||||
let results: string[] = [];
|
||||
|
||||
const translateServiceVar = this._extractTranslateServiceVar(contents);
|
||||
if (!translateServiceVar) {
|
||||
return [];
|
||||
return results;
|
||||
}
|
||||
|
||||
for (let patternName in this.patterns) {
|
||||
const regExp = this._createRegExp(patternName, {
|
||||
'TRANSLATE_SERVICE': translateServiceVar
|
||||
});
|
||||
const methodPattern: string = '(?:get|instant)\\s*\\\(\\s*([\'"`])([^\\1\\r\\n]+)\\1';
|
||||
const regExp: RegExp = new RegExp(`${translateServiceVar}\.${methodPattern}`, 'g');
|
||||
|
||||
let matches;
|
||||
while (matches = regExp.exec(contents)) {
|
||||
results.push(matches[2]);
|
||||
}
|
||||
let matches;
|
||||
while (matches = regExp.exec(contents)) {
|
||||
results.push(matches[2]);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create regular expression, replacing placeholders with real values
|
||||
*/
|
||||
protected _createRegExp(patternName: string, replaceVars: {} = {}): RegExp {
|
||||
if (!this.patterns.hasOwnProperty(patternName)) {
|
||||
throw new Error('Invalid pattern name');
|
||||
}
|
||||
|
||||
let pattern = this.patterns[patternName];
|
||||
Object.keys(replaceVars).forEach(key => {
|
||||
pattern = pattern.replace('{{' + key + '}}', replaceVars[key]);
|
||||
});
|
||||
|
||||
return new RegExp(pattern, 'g');
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract name of TranslateService variable for use in patterns
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user