(refactor) rename variables
This commit is contained in:
parent
8fd157802b
commit
a72dbf0494
@ -6,14 +6,14 @@ import { parseTemplate, TmplAstNode, TmplAstElement, TmplAstTextAttribute } from
|
|||||||
|
|
||||||
export class DirectiveParser implements ParserInterface {
|
export class DirectiveParser implements ParserInterface {
|
||||||
|
|
||||||
public extract(template: string, path: string): TranslationCollection {
|
public extract(source: string, filePath: string): TranslationCollection | null {
|
||||||
if (path && isPathAngularComponent(path)) {
|
if (filePath && isPathAngularComponent(filePath)) {
|
||||||
template = extractComponentInlineTemplate(template);
|
source = extractComponentInlineTemplate(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
let collection: TranslationCollection = new TranslationCollection();
|
let collection: TranslationCollection = new TranslationCollection();
|
||||||
|
|
||||||
const nodes: TmplAstNode[] = this.parseTemplate(template, path);
|
const nodes: TmplAstNode[] = this.parseTemplate(source, filePath);
|
||||||
this.getTranslatableElements(nodes).forEach(element => {
|
this.getTranslatableElements(nodes).forEach(element => {
|
||||||
const key = this.getElementTranslateAttrValue(element) || this.getElementContents(element);
|
const key = this.getElementTranslateAttrValue(element) || this.getElementContents(element);
|
||||||
collection = collection.add(key);
|
collection = collection.add(key);
|
||||||
|
@ -12,20 +12,20 @@ export class MarkerParser implements ParserInterface {
|
|||||||
public extract(contents: string, filePath: string): TranslationCollection | null {
|
public extract(contents: string, filePath: string): TranslationCollection | null {
|
||||||
const sourceFile = tsquery.ast(contents, filePath);
|
const sourceFile = tsquery.ast(contents, filePath);
|
||||||
|
|
||||||
const markerFnName = getNamedImportAlias(sourceFile, MARKER_PACKAGE_MODULE_NAME, MARKER_PACKAGE_IMPORT_NAME);
|
const markerImportName = getNamedImportAlias(sourceFile, MARKER_PACKAGE_MODULE_NAME, MARKER_PACKAGE_IMPORT_NAME);
|
||||||
if (!markerFnName) {
|
if (!markerImportName) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let collection: TranslationCollection = new TranslationCollection();
|
let collection: TranslationCollection = new TranslationCollection();
|
||||||
|
|
||||||
const callNodes = findFunctionCallExpressions(sourceFile, markerFnName);
|
const callExpressions = findFunctionCallExpressions(sourceFile, markerImportName);
|
||||||
callNodes.forEach(callNode => {
|
callExpressions.forEach(callExpression => {
|
||||||
const [firstArgNode] = callNode.arguments;
|
const [firstArg] = callExpression.arguments;
|
||||||
if (!firstArgNode) {
|
if (!firstArg) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const strings = getStringsFromExpression(firstArgNode);
|
const strings = getStringsFromExpression(firstArg);
|
||||||
collection = collection.addKeys(strings);
|
collection = collection.addKeys(strings);
|
||||||
});
|
});
|
||||||
return collection;
|
return collection;
|
||||||
|
@ -4,12 +4,12 @@ import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils
|
|||||||
|
|
||||||
export class PipeParser implements ParserInterface {
|
export class PipeParser implements ParserInterface {
|
||||||
|
|
||||||
public extract(template: string, path: string): TranslationCollection {
|
public extract(source: string, filePath: string): TranslationCollection | null {
|
||||||
if (path && isPathAngularComponent(path)) {
|
if (filePath && isPathAngularComponent(filePath)) {
|
||||||
template = extractComponentInlineTemplate(template);
|
source = extractComponentInlineTemplate(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.parseTemplate(template);
|
return this.parseTemplate(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected parseTemplate(template: string): TranslationCollection {
|
protected parseTemplate(template: string): TranslationCollection {
|
||||||
|
@ -2,7 +2,7 @@ import { tsquery } from '@phenomnomnominal/tsquery';
|
|||||||
|
|
||||||
import { ParserInterface } from './parser.interface';
|
import { ParserInterface } from './parser.interface';
|
||||||
import { TranslationCollection } from '../utils/translation.collection';
|
import { TranslationCollection } from '../utils/translation.collection';
|
||||||
import { findClasses, findClassPropertyByType, findMethodCallExpressions, getStringsFromExpression } from '../utils/ast-helpers';
|
import { findClassDeclarations, findClassPropertyByType, findMethodCallExpressions, getStringsFromExpression } from '../utils/ast-helpers';
|
||||||
|
|
||||||
const TRANSLATE_SERVICE_TYPE_REFERENCE = 'TranslateService';
|
const TRANSLATE_SERVICE_TYPE_REFERENCE = 'TranslateService';
|
||||||
const TRANSLATE_SERVICE_METHOD_NAMES = ['get', 'instant', 'stream'];
|
const TRANSLATE_SERVICE_METHOD_NAMES = ['get', 'instant', 'stream'];
|
||||||
@ -12,26 +12,26 @@ export class ServiceParser implements ParserInterface {
|
|||||||
public extract(source: string, filePath: string): TranslationCollection | null {
|
public extract(source: string, filePath: string): TranslationCollection | null {
|
||||||
const sourceFile = tsquery.ast(source, filePath);
|
const sourceFile = tsquery.ast(source, filePath);
|
||||||
|
|
||||||
const classNodes = findClasses(sourceFile);
|
const classDeclarations = findClassDeclarations(sourceFile);
|
||||||
if (!classNodes) {
|
if (!classDeclarations) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let collection: TranslationCollection = new TranslationCollection();
|
let collection: TranslationCollection = new TranslationCollection();
|
||||||
|
|
||||||
classNodes.forEach(classNode => {
|
classDeclarations.forEach(classDeclaration => {
|
||||||
const propName: string = findClassPropertyByType(classNode, TRANSLATE_SERVICE_TYPE_REFERENCE);
|
const propName: string = findClassPropertyByType(classDeclaration, TRANSLATE_SERVICE_TYPE_REFERENCE);
|
||||||
if (!propName) {
|
if (!propName) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const callNodes = findMethodCallExpressions(classNode, propName, TRANSLATE_SERVICE_METHOD_NAMES);
|
const callExpressions = findMethodCallExpressions(classDeclaration, propName, TRANSLATE_SERVICE_METHOD_NAMES);
|
||||||
callNodes.forEach(callNode => {
|
callExpressions.forEach(callExpression => {
|
||||||
const [firstArgNode] = callNode.arguments;
|
const [firstArg] = callExpression.arguments;
|
||||||
if (!firstArgNode) {
|
if (!firstArg) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const strings = getStringsFromExpression(firstArgNode);
|
const strings = getStringsFromExpression(firstArg);
|
||||||
collection = collection.addKeys(strings);
|
collection = collection.addKeys(strings);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -36,7 +36,7 @@ export function getNamedImportAlias(node: Node, moduleName: string, importName:
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findClasses(node: Node): ClassDeclaration[] {
|
export function findClassDeclarations(node: Node): ClassDeclaration[] {
|
||||||
const query = 'ClassDeclaration';
|
const query = 'ClassDeclaration';
|
||||||
return tsquery<ClassDeclaration>(node, query);
|
return tsquery<ClassDeclaration>(node, query);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user