- (feat) add concept of post processors
- (feat) add 'key as default value' post processor (closes #109) - (chore) move clean functionality to a post processor - (chore) move sort functionality to a post processor - (refactor) get rid of leading underscore on protected properties/methods
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
import * as ts from 'typescript';
|
||||
import { yellow } from 'colorette';
|
||||
|
||||
export abstract class AbstractAstParser {
|
||||
|
||||
protected _sourceFile: ts.SourceFile;
|
||||
protected sourceFile: ts.SourceFile;
|
||||
|
||||
protected _createSourceFile(path: string, contents: string): ts.SourceFile {
|
||||
protected createSourceFile(path: string, contents: string): ts.SourceFile {
|
||||
return ts.createSourceFile(path, contents, null, /*setParentNodes */ false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get strings from function call's first argument
|
||||
*/
|
||||
protected _getCallArgStrings(callNode: ts.CallExpression): string[] {
|
||||
protected getCallArgStrings(callNode: ts.CallExpression): string[] {
|
||||
if (!callNode.arguments.length) {
|
||||
return;
|
||||
}
|
||||
@@ -25,41 +26,51 @@ export abstract class AbstractAstParser {
|
||||
return (firstArg as ts.ArrayLiteralExpression).elements
|
||||
.map((element: ts.StringLiteral) => element.text);
|
||||
case ts.SyntaxKind.Identifier:
|
||||
console.log('WARNING: We cannot extract variable values passed to TranslateService (yet)');
|
||||
// TODO
|
||||
console.log(yellow('[Line: %d] We do not support values passed to TranslateService'), this.getLine(firstArg));
|
||||
break;
|
||||
case ts.SyntaxKind.BinaryExpression:
|
||||
// TODO
|
||||
console.log(yellow('[Line: %d] We do not support binary expressions (yet)'), this.getLine(firstArg));
|
||||
break;
|
||||
default:
|
||||
console.log(`SKIP: Unknown argument type: '${this._syntaxKindToName(firstArg.kind)}'`, firstArg);
|
||||
console.log(yellow(`[Line: %d] Unknown argument type: %s`), this.getLine(firstArg), this.syntaxKindToName(firstArg.kind), firstArg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all child nodes of a kind
|
||||
*/
|
||||
protected _findNodes(node: ts.Node, kind: ts.SyntaxKind): ts.Node[] {
|
||||
const childrenNodes: ts.Node[] = node.getChildren(this._sourceFile);
|
||||
protected findNodes(node: ts.Node, kind: ts.SyntaxKind): ts.Node[] {
|
||||
const childrenNodes: ts.Node[] = node.getChildren(this.sourceFile);
|
||||
const initialValue: ts.Node[] = node.kind === kind ? [node] : [];
|
||||
|
||||
return childrenNodes.reduce((result: ts.Node[], childNode: ts.Node) => {
|
||||
return result.concat(this._findNodes(childNode, kind));
|
||||
return result.concat(this.findNodes(childNode, kind));
|
||||
}, initialValue);
|
||||
}
|
||||
|
||||
protected _syntaxKindToName(kind: ts.SyntaxKind): string {
|
||||
protected getLine(node: ts.Node): number {
|
||||
const { line } = this.sourceFile.getLineAndCharacterOfPosition(node.pos);
|
||||
return line + 1;
|
||||
}
|
||||
|
||||
protected syntaxKindToName(kind: ts.SyntaxKind): string {
|
||||
return ts.SyntaxKind[kind];
|
||||
}
|
||||
|
||||
protected _printAllChildren(sourceFile: ts.SourceFile, node: ts.Node, depth = 0): void {
|
||||
protected printAllChildren(sourceFile: ts.SourceFile, node: ts.Node, depth = 0): void {
|
||||
console.log(
|
||||
new Array(depth + 1).join('----'),
|
||||
`[${node.kind}]`,
|
||||
this._syntaxKindToName(node.kind),
|
||||
this.syntaxKindToName(node.kind),
|
||||
`[pos: ${node.pos}-${node.end}]`,
|
||||
':\t\t\t',
|
||||
node.getFullText(sourceFile).trim()
|
||||
);
|
||||
|
||||
depth++;
|
||||
node.getChildren(sourceFile).forEach(childNode => this._printAllChildren(sourceFile, childNode, depth));
|
||||
node.getChildren(sourceFile).forEach(childNode => this.printAllChildren(sourceFile, childNode, depth));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,14 +4,14 @@ 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 {
|
||||
protected isAngularComponent(path: string): boolean {
|
||||
return (/\.ts|js$/i).test(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts inline template from components
|
||||
*/
|
||||
protected _extractInlineTemplate(contents: string): string {
|
||||
protected extractInlineTemplate(contents: string): string {
|
||||
const regExp: RegExp = /template\s*:\s*(["'`])([^\1]*?)\1/;
|
||||
const match = regExp.exec(contents);
|
||||
if (match !== null) {
|
||||
|
||||
@@ -9,17 +9,17 @@ const $ = cheerio.load('', {xmlMode: true});
|
||||
export class DirectiveParser extends AbstractTemplateParser implements ParserInterface {
|
||||
|
||||
public extract(contents: string, path?: string): TranslationCollection {
|
||||
if (path && this._isAngularComponent(path)) {
|
||||
contents = this._extractInlineTemplate(contents);
|
||||
if (path && this.isAngularComponent(path)) {
|
||||
contents = this.extractInlineTemplate(contents);
|
||||
}
|
||||
|
||||
return this._parseTemplate(contents);
|
||||
return this.parseTemplate(contents);
|
||||
}
|
||||
|
||||
protected _parseTemplate(template: string): TranslationCollection {
|
||||
protected parseTemplate(template: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
template = this._normalizeTemplateAttributes(template);
|
||||
template = this.normalizeTemplateAttributes(template);
|
||||
|
||||
const selector = '[translate], [ng2-translate]';
|
||||
$(template)
|
||||
@@ -50,7 +50,7 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt
|
||||
* so it can't be parsed by standard HTML parsers.
|
||||
* This method replaces `[attr]="'val'""` with `attr="val"`
|
||||
*/
|
||||
protected _normalizeTemplateAttributes(template: string): string {
|
||||
protected normalizeTemplateAttributes(template: string): string {
|
||||
return template.replace(/\[([^\]]+)\]="'([^']*)'"/g, '$1="$2"');
|
||||
}
|
||||
|
||||
|
||||
@@ -6,23 +6,23 @@ import * as ts from 'typescript';
|
||||
|
||||
export class FunctionParser extends AbstractAstParser implements ParserInterface {
|
||||
|
||||
protected _functionIdentifier: string = '_';
|
||||
protected functionIdentifier: string = '_';
|
||||
|
||||
public constructor(options?: any) {
|
||||
super();
|
||||
if (options && typeof options.identifier !== 'undefined') {
|
||||
this._functionIdentifier = options.identifier;
|
||||
this.functionIdentifier = options.identifier;
|
||||
}
|
||||
}
|
||||
|
||||
public extract(contents: string, path?: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
this._sourceFile = this._createSourceFile(path, contents);
|
||||
this.sourceFile = this.createSourceFile(path, contents);
|
||||
|
||||
const callNodes = this._findCallNodes();
|
||||
const callNodes = this.findCallNodes();
|
||||
callNodes.forEach(callNode => {
|
||||
const keys: string[] = this._getCallArgStrings(callNode);
|
||||
const keys: string[] = this.getCallArgStrings(callNode);
|
||||
if (keys && keys.length) {
|
||||
collection = collection.addKeys(keys);
|
||||
}
|
||||
@@ -34,12 +34,12 @@ export class FunctionParser extends AbstractAstParser implements ParserInterface
|
||||
/**
|
||||
* Find all calls to marker function
|
||||
*/
|
||||
protected _findCallNodes(node?: ts.Node): ts.CallExpression[] {
|
||||
protected findCallNodes(node?: ts.Node): ts.CallExpression[] {
|
||||
if (!node) {
|
||||
node = this._sourceFile;
|
||||
node = this.sourceFile;
|
||||
}
|
||||
|
||||
let callNodes = this._findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[];
|
||||
let callNodes = this.findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[];
|
||||
callNodes = callNodes
|
||||
.filter(callNode => {
|
||||
// Only call expressions with arguments
|
||||
@@ -48,7 +48,7 @@ export class FunctionParser extends AbstractAstParser implements ParserInterface
|
||||
}
|
||||
|
||||
const identifier = (callNode.getChildAt(0) as ts.Identifier).text;
|
||||
if (identifier !== this._functionIdentifier) {
|
||||
if (identifier !== this.functionIdentifier) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,14 +5,14 @@ import { TranslationCollection } from '../utils/translation.collection';
|
||||
export class PipeParser extends AbstractTemplateParser implements ParserInterface {
|
||||
|
||||
public extract(contents: string, path?: string): TranslationCollection {
|
||||
if (path && this._isAngularComponent(path)) {
|
||||
contents = this._extractInlineTemplate(contents);
|
||||
if (path && this.isAngularComponent(path)) {
|
||||
contents = this.extractInlineTemplate(contents);
|
||||
}
|
||||
|
||||
return this._parseTemplate(contents);
|
||||
return this.parseTemplate(contents);
|
||||
}
|
||||
|
||||
protected _parseTemplate(template: string): TranslationCollection {
|
||||
protected parseTemplate(template: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
const regExp: RegExp = /(['"`])((?:(?!\1).|\\\1)+)\1\s*\|\s*translate/g;
|
||||
|
||||
@@ -6,27 +6,27 @@ import * as ts from 'typescript';
|
||||
|
||||
export class ServiceParser extends AbstractAstParser implements ParserInterface {
|
||||
|
||||
protected _sourceFile: ts.SourceFile;
|
||||
protected sourceFile: ts.SourceFile;
|
||||
|
||||
public extract(contents: string, path?: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
this._sourceFile = this._createSourceFile(path, contents);
|
||||
const classNodes = this._findClassNodes(this._sourceFile);
|
||||
this.sourceFile = this.createSourceFile(path, contents);
|
||||
const classNodes = this.findClassNodes(this.sourceFile);
|
||||
classNodes.forEach(classNode => {
|
||||
const constructorNode = this._findConstructorNode(classNode);
|
||||
const constructorNode = this.findConstructorNode(classNode);
|
||||
if (!constructorNode) {
|
||||
return;
|
||||
}
|
||||
|
||||
const propertyName: string = this._findTranslateServicePropertyName(constructorNode);
|
||||
const propertyName: string = this.findTranslateServicePropertyName(constructorNode);
|
||||
if (!propertyName) {
|
||||
return;
|
||||
}
|
||||
|
||||
const callNodes = this._findCallNodes(classNode, propertyName);
|
||||
const callNodes = this.findCallNodes(classNode, propertyName);
|
||||
callNodes.forEach(callNode => {
|
||||
const keys: string[] = this._getCallArgStrings(callNode);
|
||||
const keys: string[] = this.getCallArgStrings(callNode);
|
||||
if (keys && keys.length) {
|
||||
collection = collection.addKeys(keys);
|
||||
}
|
||||
@@ -40,7 +40,7 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
* Detect what the TranslateService instance property
|
||||
* is called by inspecting constructor arguments
|
||||
*/
|
||||
protected _findTranslateServicePropertyName(constructorNode: ts.ConstructorDeclaration): string {
|
||||
protected findTranslateServicePropertyName(constructorNode: ts.ConstructorDeclaration): string {
|
||||
if (!constructorNode) {
|
||||
return null;
|
||||
}
|
||||
@@ -77,15 +77,15 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
/**
|
||||
* Find class nodes
|
||||
*/
|
||||
protected _findClassNodes(node: ts.Node): ts.ClassDeclaration[] {
|
||||
return this._findNodes(node, ts.SyntaxKind.ClassDeclaration) as ts.ClassDeclaration[];
|
||||
protected findClassNodes(node: ts.Node): ts.ClassDeclaration[] {
|
||||
return this.findNodes(node, ts.SyntaxKind.ClassDeclaration) as ts.ClassDeclaration[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Find constructor
|
||||
*/
|
||||
protected _findConstructorNode(node: ts.ClassDeclaration): ts.ConstructorDeclaration {
|
||||
const constructorNodes = this._findNodes(node, ts.SyntaxKind.Constructor) as ts.ConstructorDeclaration[];
|
||||
protected findConstructorNode(node: ts.ClassDeclaration): ts.ConstructorDeclaration {
|
||||
const constructorNodes = this.findNodes(node, ts.SyntaxKind.Constructor) as ts.ConstructorDeclaration[];
|
||||
if (constructorNodes) {
|
||||
return constructorNodes[0];
|
||||
}
|
||||
@@ -94,8 +94,8 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
/**
|
||||
* Find all calls to TranslateService methods
|
||||
*/
|
||||
protected _findCallNodes(node: ts.Node, propertyIdentifier: string): ts.CallExpression[] {
|
||||
let callNodes = this._findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[];
|
||||
protected findCallNodes(node: ts.Node, propertyIdentifier: string): ts.CallExpression[] {
|
||||
let callNodes = this.findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[];
|
||||
callNodes = callNodes
|
||||
.filter(callNode => {
|
||||
// Only call expressions with arguments
|
||||
|
||||
Reference in New Issue
Block a user