Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
fde5245731 | ||
|
4ee7258a31 | ||
|
a6c7af0630 | ||
|
0949bf765b | ||
|
d416c6b9fd |
16
package.json
16
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@biesbjerg/ngx-translate-extract",
|
||||
"version": "2.2.5",
|
||||
"version": "2.3.1",
|
||||
"description": "Extract strings from projects using ngx-translate",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
@@ -47,24 +47,24 @@
|
||||
},
|
||||
"config": {},
|
||||
"devDependencies": {
|
||||
"@types/chai": "3.4.35",
|
||||
"@types/chai": "3.5.2",
|
||||
"@types/glob": "5.0.30",
|
||||
"@types/mocha": "2.2.40",
|
||||
"@types/mocha": "2.2.41",
|
||||
"@types/cheerio": "0.22.1",
|
||||
"@types/chalk": "0.4.31",
|
||||
"@types/flat": "0.0.28",
|
||||
"@types/yargs": "6.6.0",
|
||||
"@types/mkdirp": "0.3.29",
|
||||
"chai": "3.5.0",
|
||||
"mocha": "3.2.0",
|
||||
"ts-node": "3.0.2",
|
||||
"tslint": "5.0.0",
|
||||
"mocha": "3.3.0",
|
||||
"ts-node": "3.0.4",
|
||||
"tslint": "5.2.0",
|
||||
"tslint-eslint-rules": "4.0.0",
|
||||
"typescript": "2.2.2"
|
||||
"typescript": "2.3.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"chalk": "1.1.3",
|
||||
"yargs": "7.0.2",
|
||||
"yargs": "8.0.1",
|
||||
"cheerio": "0.22.0",
|
||||
"fs": "0.0.1-security",
|
||||
"gettext-parser": "1.2.2",
|
||||
|
@@ -35,11 +35,7 @@ export abstract class AbstractAstParser {
|
||||
/**
|
||||
* Find all child nodes of a kind
|
||||
*/
|
||||
protected _findNodes(node: ts.Node, kind: ts.SyntaxKind, onlyOne: boolean = false): ts.Node[] {
|
||||
if (node.kind === kind && onlyOne) {
|
||||
return [node];
|
||||
}
|
||||
|
||||
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] : [];
|
||||
|
||||
|
@@ -8,26 +8,29 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
|
||||
protected _sourceFile: ts.SourceFile;
|
||||
|
||||
protected _instancePropertyName: any;
|
||||
protected _serviceClassName: string = 'TranslateService';
|
||||
protected _serviceMethodNames: string[] = ['get', 'instant'];
|
||||
|
||||
public extract(contents: string, path?: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
this._sourceFile = this._createSourceFile(path, contents);
|
||||
|
||||
this._instancePropertyName = this._getInstancePropertyName();
|
||||
if (!this._instancePropertyName) {
|
||||
return collection;
|
||||
}
|
||||
|
||||
const callNodes = this._findCallNodes();
|
||||
callNodes.forEach(callNode => {
|
||||
const keys: string[] = this._getCallArgStrings(callNode);
|
||||
if (keys && keys.length) {
|
||||
collection = collection.addKeys(keys);
|
||||
const classNodes = this._findClassNodes(this._sourceFile);
|
||||
classNodes.forEach(classNode => {
|
||||
const constructorNode = this._findConstructorNode(classNode);
|
||||
if (!constructorNode) {
|
||||
return;
|
||||
}
|
||||
|
||||
const propertyName: string = this._findTranslateServicePropertyName(constructorNode);
|
||||
if (!propertyName) {
|
||||
return;
|
||||
}
|
||||
|
||||
const callNodes = this._findCallNodes(classNode, propertyName);
|
||||
callNodes.forEach(callNode => {
|
||||
const keys: string[] = this._getCallArgStrings(callNode);
|
||||
if (keys && keys.length) {
|
||||
collection = collection.addKeys(keys);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return collection;
|
||||
@@ -35,10 +38,9 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
|
||||
/**
|
||||
* Detect what the TranslateService instance property
|
||||
* is called by inspecting constructor params
|
||||
* is called by inspecting constructor arguments
|
||||
*/
|
||||
protected _getInstancePropertyName(): string {
|
||||
const constructorNode = this._findConstructorNode();
|
||||
protected _findTranslateServicePropertyName(constructorNode: ts.ConstructorDeclaration): string {
|
||||
if (!constructorNode) {
|
||||
return null;
|
||||
}
|
||||
@@ -60,7 +62,7 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
return false;
|
||||
}
|
||||
const className: string = parameterType.text;
|
||||
if (className !== this._serviceClassName) {
|
||||
if (className !== 'TranslateService') {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -73,23 +75,26 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Find first constructor
|
||||
* Find class nodes
|
||||
*/
|
||||
protected _findConstructorNode(): ts.ConstructorDeclaration {
|
||||
const constructors = this._findNodes(this._sourceFile, ts.SyntaxKind.Constructor, true) as ts.ConstructorDeclaration[];
|
||||
if (constructors.length) {
|
||||
return constructors[0];
|
||||
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[];
|
||||
if (constructorNodes) {
|
||||
return constructorNodes[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all calls to TranslateService methods
|
||||
*/
|
||||
protected _findCallNodes(node?: ts.Node): ts.CallExpression[] {
|
||||
if (!node) {
|
||||
node = this._sourceFile;
|
||||
}
|
||||
|
||||
protected _findCallNodes(node: ts.Node, propertyIdentifier: string): ts.CallExpression[] {
|
||||
let callNodes = this._findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[];
|
||||
callNodes = callNodes
|
||||
.filter(callNode => {
|
||||
@@ -105,7 +110,7 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
if (!propAccess.getFirstToken() || propAccess.getFirstToken().kind !== ts.SyntaxKind.ThisKeyword) {
|
||||
return false;
|
||||
}
|
||||
if (propAccess.name.text !== this._instancePropertyName) {
|
||||
if (propAccess.name.text !== propertyIdentifier) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -113,7 +118,7 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
|
||||
if (!methodAccess || methodAccess.kind !== ts.SyntaxKind.PropertyAccessExpression) {
|
||||
return false;
|
||||
}
|
||||
if (!methodAccess.name || this._serviceMethodNames.indexOf(methodAccess.name.text) === -1) {
|
||||
if (!methodAccess.name || (methodAccess.name.text !== 'get' && methodAccess.name.text !== 'instant')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@@ -164,4 +164,38 @@ describe('ServiceParser', () => {
|
||||
expect(keys).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('should extract strings from all classes in the file', () => {
|
||||
const contents = `
|
||||
import { Injectable } from '@angular/core';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
export class Stuff {
|
||||
thing: string;
|
||||
translate: any;
|
||||
constructor(thing: string) {
|
||||
this.translate.get('Not me');
|
||||
this.thing = thing;
|
||||
}
|
||||
}
|
||||
@Injectable()
|
||||
export class MyComponent {
|
||||
constructor(public translate: TranslateService) {
|
||||
this.translate.instant("Extract me!");
|
||||
}
|
||||
}
|
||||
export class OtherClass {
|
||||
constructor(thing: string, _translate: TranslateService) {
|
||||
this._translate.get("Do not extract me");
|
||||
}
|
||||
}
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(public translate: TranslateService) {
|
||||
this.translate.instant("Hello!");
|
||||
}
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['Extract me!', 'Hello!']);
|
||||
});
|
||||
|
||||
});
|
||||
|
Reference in New Issue
Block a user