extract strings when TranslateService is accessed directly via constructor parameter. Closes #50 and #106

This commit is contained in:
Kim Biesbjerg
2020-03-22 13:33:40 +01:00
parent b07d929484
commit 33d8c26a28
3 changed files with 63 additions and 9 deletions

View File

@@ -1,8 +1,9 @@
import { ClassDeclaration, CallExpression } from 'typescript';
import { tsquery } from '@phenomnomnominal/tsquery';
import { ParserInterface } from './parser.interface';
import { TranslationCollection } from '../utils/translation.collection';
import { findClassDeclarations, findClassPropertyByType, findMethodCallExpressions, getStringsFromExpression } from '../utils/ast-helpers';
import { findClassDeclarations, findClassPropertyByType, findPropertyCallExpressions, findMethodCallExpressions, getStringsFromExpression, findMethodParameterByType, findConstructorDeclaration } from '../utils/ast-helpers';
const TRANSLATE_SERVICE_TYPE_REFERENCE = 'TranslateService';
const TRANSLATE_SERVICE_METHOD_NAMES = ['get', 'instant', 'stream'];
@@ -19,12 +20,11 @@ export class ServiceParser implements ParserInterface {
let collection: TranslationCollection = new TranslationCollection();
classDeclarations.forEach(classDeclaration => {
const propName: string = findClassPropertyByType(classDeclaration, TRANSLATE_SERVICE_TYPE_REFERENCE);
if (!propName) {
return;
}
const callExpressions = [
...this.findConstructorParamCallExpressions(classDeclaration),
...this.findPropertyCallExpressions(classDeclaration)
];
const callExpressions = findMethodCallExpressions(classDeclaration, propName, TRANSLATE_SERVICE_METHOD_NAMES);
callExpressions.forEach(callExpression => {
const [firstArg] = callExpression.arguments;
if (!firstArg) {
@@ -36,4 +36,21 @@ export class ServiceParser implements ParserInterface {
});
return collection;
}
protected findConstructorParamCallExpressions(classDeclaration: ClassDeclaration): CallExpression[] {
const constructorDeclaration = findConstructorDeclaration(classDeclaration);
if (!constructorDeclaration) {
return [];
}
const paramName = findMethodParameterByType(constructorDeclaration, TRANSLATE_SERVICE_TYPE_REFERENCE);
return findMethodCallExpressions(constructorDeclaration, paramName, TRANSLATE_SERVICE_METHOD_NAMES);
}
protected findPropertyCallExpressions(classDeclaration: ClassDeclaration): CallExpression[] {
const propName: string = findClassPropertyByType(classDeclaration, TRANSLATE_SERVICE_TYPE_REFERENCE);
if (!propName) {
return [];
}
return findPropertyCallExpressions(classDeclaration, propName, TRANSLATE_SERVICE_METHOD_NAMES);
}
}