Skip to content

Commit

Permalink
(bugfix) extract strings encapsulated with backticks. Closes #139
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim Biesbjerg committed Aug 26, 2019
1 parent e1bb5bf commit 24ebd8f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@biesbjerg/ngx-translate-extract",
"version": "3.0.3",
"version": "3.0.4",
"description": "Extract strings from projects using ngx-translate",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
17 changes: 11 additions & 6 deletions src/parsers/abstract-ast.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
CallExpression,
Node,
SyntaxKind,
StringLiteral
StringLiteral,
NoSubstitutionTemplateLiteral
} from 'typescript';

export abstract class AbstractAstParser {
Expand All @@ -24,19 +25,23 @@ export abstract class AbstractAstParser {
}

const firstArg = callNode.arguments[0];
return this.findNodes(firstArg, SyntaxKind.StringLiteral)
.map((node: StringLiteral) => node.text);

return this.findNodes(firstArg, [
SyntaxKind.StringLiteral,
SyntaxKind.NoSubstitutionTemplateLiteral
])
.map((node: StringLiteral | NoSubstitutionTemplateLiteral) => node.text);
}

/**
* Find all child nodes of a kind
*/
protected findNodes(node: Node, kind: SyntaxKind): Node[] {
protected findNodes(node: Node, kinds: SyntaxKind[]): Node[] {
const childrenNodes: Node[] = node.getChildren(this.sourceFile);
const initialValue: Node[] = node.kind === kind ? [node] : [];
const initialValue: Node[] = kinds.includes(node.kind) ? [node] : [];

return childrenNodes.reduce((result: Node[], childNode: Node) => {
return result.concat(this.findNodes(childNode, kind));
return result.concat(this.findNodes(childNode, kinds));
}, initialValue);
}

Expand Down
2 changes: 1 addition & 1 deletion src/parsers/function.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class FunctionParser extends AbstractAstParser implements ParserInterface
node = this.sourceFile;
}

let callNodes = this.findNodes(node, SyntaxKind.CallExpression) as CallExpression[];
let callNodes = this.findNodes(node, [SyntaxKind.CallExpression]) as CallExpression[];
callNodes = callNodes
.filter(callNode => {
// Only call expressions with arguments
Expand Down
6 changes: 3 additions & 3 deletions src/parsers/service.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
* Find class nodes
*/
protected findClassNodes(node: Node): ClassDeclaration[] {
return this.findNodes(node, SyntaxKind.ClassDeclaration) as ClassDeclaration[];
return this.findNodes(node, [SyntaxKind.ClassDeclaration]) as ClassDeclaration[];
}

/**
* Find constructor
*/
protected findConstructorNode(node: ClassDeclaration): ConstructorDeclaration {
const constructorNodes = this.findNodes(node, SyntaxKind.Constructor) as ConstructorDeclaration[];
const constructorNodes = this.findNodes(node, [SyntaxKind.Constructor]) as ConstructorDeclaration[];
if (constructorNodes) {
return constructorNodes[0];
}
Expand All @@ -106,7 +106,7 @@ export class ServiceParser extends AbstractAstParser implements ParserInterface
* Find all calls to TranslateService methods
*/
protected findCallNodes(node: Node, propertyIdentifier: string): CallExpression[] {
let callNodes = this.findNodes(node, SyntaxKind.CallExpression) as CallExpression[];
let callNodes = this.findNodes(node, [SyntaxKind.CallExpression]) as CallExpression[];
callNodes = callNodes
.filter(callNode => {
// Only call expressions with arguments
Expand Down
13 changes: 13 additions & 0 deletions tests/parsers/service.parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ describe('ServiceParser', () => {
expect(key).to.deep.equal(['Hello', 'World']);
});

it('should extract string arrays encapsulated in backticks', () => {
const contents = `
@Component({ })
export class AppComponent {
public constructor(protected _translateService: TranslateService) { }
public test() {
this._translateService.get([\`Hello\`, \`World\`]);
}
`;
const keys = parser.extract(contents, componentFilename).keys();
expect(keys).to.deep.equal(['Hello', 'World']);
});

it('should not extract strings in get()/instant()/stream() methods of other services', () => {
const contents = `
@Component({ })
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"noImplicitAny": true,
"removeComments": true,
"declaration": true,
"target": "es6",
"target": "es2015",
"lib": [
"dom",
"es2015"
"es2018"
],
"module": "commonjs",
"outDir": "./dist/",
Expand Down

0 comments on commit 24ebd8f

Please sign in to comment.