forked from bartholomej/ngx-translate-extract
-
Notifications
You must be signed in to change notification settings - Fork 19
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
michaelbromley
merged 1 commit into
vendure-ecommerce:master
from
pmpak:fix/service-parser-not-locating-translate-service-in-private-fields
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,11 @@ import pkg, { | |
CallExpression, | ||
Expression, | ||
StringLiteral, | ||
SourceFile | ||
SourceFile, | ||
PropertyDeclaration, | ||
PropertyAccessExpression, | ||
isPropertyAccessExpression, | ||
isCallExpression | ||
} from 'typescript'; | ||
const { SyntaxKind, isStringLiteralLike, isArrayLiteralExpression, isBinaryExpression, isConditionalExpression } = pkg; | ||
|
||
|
@@ -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}"])`; | ||
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[] { | ||
|
@@ -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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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[] { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
andPrivateIdentifier
I reduced the specificity of the query and then extract the identifier from the AST instead of getting it directly from the query.