Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix parser not locating TranslateService in private fields using the # syntax #55

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions src/utils/ast-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import pkg, {
CallExpression,
Expression,
StringLiteral,
SourceFile
SourceFile,
PropertyDeclaration,
PropertyAccessExpression,
isPropertyAccessExpression,
isCallExpression
} from 'typescript';
const { SyntaxKind, isStringLiteralLike, isArrayLiteralExpression, isBinaryExpression, isConditionalExpression } = pkg;

Expand Down Expand Up @@ -142,15 +146,15 @@ export function findClassPropertiesConstructorParameterByType(node: ClassDeclara
}

export function findClassPropertiesDeclarationByType(node: ClassDeclaration, type: string): string[] {
const query = `PropertyDeclaration:has(TypeReference > Identifier[name="${type}"]) > Identifier`;
const result = tsquery<Identifier>(node, query);
return result.map((n) => n.text);
const query = `PropertyDeclaration:has(TypeReference > Identifier[name="${type}"])`;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it capture both Identifier and PrivateIdentifier I reduced the specificity of the query and then extract the identifier from the AST instead of getting it directly from the query.

const result = tsquery<PropertyDeclaration>(node, query);
return result.map((n) => n.name.getText());
}

export function findClassPropertiesDeclarationByInject(node: ClassDeclaration, type: string): string[] {
const query = `PropertyDeclaration:has(CallExpression > Identifier[name="inject"]):has(CallExpression > Identifier[name="${type}"]) > Identifier`;
const result = tsquery<Identifier>(node, query);
return result.map((n) => n.text);
const query = `PropertyDeclaration:has(CallExpression > Identifier[name="inject"]):has(CallExpression > Identifier[name="${type}"])`;
const result = tsquery<PropertyDeclaration>(node, query);
return result.map((n) => n.name.getText());
}

export function findClassPropertiesGetterByType(node: ClassDeclaration, type: string): string[] {
Expand Down Expand Up @@ -179,22 +183,23 @@ export function findPropertyCallExpressions(node: Node, prop: string, fnName: st
if (Array.isArray(fnName)) {
fnName = fnName.join('|');
}
const query = 'CallExpression > ' +
`PropertyAccessExpression:has(Identifier[name=/^(${fnName})$/]):has(PropertyAccessExpression:has(Identifier[name="${prop}"]):has(ThisKeyword)) > ` +
`PropertyAccessExpression:has(ThisKeyword) > Identifier[name="${prop}"]`;
const nodes = tsquery<Identifier>(node, query);
// Since the direct descendant operator (>) is not supported in :has statements, we need to
// check manually whether everything is correctly matched
const filtered = nodes.reduce<CallExpression[]>((result: CallExpression[], n: Node) => {
if (
tsquery(n.parent, 'PropertyAccessExpression > ThisKeyword').length > 0 &&
tsquery(n.parent.parent, `PropertyAccessExpression > Identifier[name=/^(${fnName})$/]`).length > 0
) {
result.push(n.parent.parent.parent as CallExpression);

const query = `CallExpression > PropertyAccessExpression:has(Identifier[name=/^(${fnName})$/]):has(PropertyAccessExpression:has(ThisKeyword))`;
const result = tsquery<PropertyAccessExpression>(node, query);

const nodes: CallExpression[] = [];
result.forEach((n) => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar here, I reduced the specificity of the query and then manually filter out the CallExpression nodes from the AST, so that both private and public properties are matched.

const identifier = isPropertyAccessExpression(n.expression) ? n.expression.name : null;
const property = identifier?.parent;
const method = property?.parent;
const callExpression = method?.parent;

if (identifier?.getText() === prop && isCallExpression(callExpression)) {
nodes.push(callExpression);
}
return result;
}, []);
return filtered;
});

return nodes;
}

export function getStringsFromExpression(expression: Expression): string[] {
Expand Down
33 changes: 33 additions & 0 deletions tests/parsers/service.parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,22 @@ describe('ServiceParser', () => {
expect(keys).to.deep.equal(['Hello World']);
});

it('should extract strings when TranslateService is declared as a private property', () => {
const contents = `
export class MyComponent {
readonly #translate: TranslateService;
public constructor(translateService: TranslateService) {
this.#translate = translateService;
}
public test() {
this.#translate.instant('Hello World');
}
}
`;
const keys = parser.extract(contents, componentFilename)?.keys();
expect(keys).to.deep.equal(['Hello World']);
});

it('should extract strings when TranslateService is injected using the inject function ', () => {
const contents = `
export class MyComponent {
Expand All @@ -328,6 +344,23 @@ describe('ServiceParser', () => {
expect(keys).to.deep.equal(['Hello World']);
});

it('should locate TranslateService when it is a JavaScript native private property', () => {
const contents = `
@Component({})
export class MyComponent {
readonly #translate = inject(TranslateService);

public test() {
this.#translate.get('get.works');
this.#translate.stream('stream.works');
this.#translate.instant('instant.works');
}
}
`;
const keys = parser.extract(contents, componentFilename)?.keys();
expect(keys).to.deep.equal(['get.works', 'stream.works', 'instant.works']);
});

it('should extract strings passed to TranslateServices methods only', () => {
const contents = `
export class AppComponent implements OnInit {
Expand Down
Loading