Skip to content

Commit

Permalink
fix: πŸ› support inject assignment to a variable
Browse files Browse the repository at this point in the history
βœ… Closes: #141
  • Loading branch information
shaharkazaz committed Jul 18, 2024
1 parent 559de78 commit a326b78
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export function testServiceExtraction(fileFormat: Config['fileFormat']) {
...generateKeys({ start: 24, end: 33 }),
'inject.test': defaultValue,
'private-class-field.test': defaultValue,
'permission.snackbar.no-permission': defaultValue,
'permission.snackbar.close': defaultValue,
};

buildTranslationFiles(config);
Expand Down
31 changes: 31 additions & 0 deletions __tests__/buildTranslationFiles/ts-extraction/service/src/5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { TranslocoService } from '@jsverse/transloco';
import { inject } from '@angular/core';
import { Observable, zip, switchMap, tap, map, of } from 'rxjs';
import { PermissionService } from './permission.service';
import { MatSnackBar } from '@angular/material/snack-bar';

export function hasPermissionFactory(): Observable<string> {
const permission = inject(PermissionService);
const translate = inject(TranslocoService);
const snackBarManager = inject(MatSnackBar);

return permission.hasPermissions().pipe(
switchMap((hasPermission) => {
if (!hasPermission) {
return zip([
translate.selectTranslate('permission.snackbar.no-permission'),
translate.selectTranslate('permission.snackbar.close'),
]).pipe(
tap(([message, close]) => {
snackBarManager.open(message, close, {
duration: 3000,
horizontalPosition: 'right',
});
}),
map(() => false),
);
}
return of(true);
}),
);
}
3 changes: 3 additions & 0 deletions __tests__/resolveConfig/resolveConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ describe('resolveConfig', () => {
});

it('should should handle paths prefixed with the sourceRoot', () => {
const spy = spyOnConsole('warn');
const config = resolveConfig({
input: [`${sourceRoot}/folder`],
translationsPath: `${sourceRoot}/1`,
Expand All @@ -221,6 +222,8 @@ describe('resolveConfig', () => {
(['output', 'translationsPath'] as const).forEach((prop) =>
assertPath(config[prop]),
);
expect(spy).toHaveBeenCalledTimes(3);
spy.mockClear();
});
});
});
20 changes: 13 additions & 7 deletions src/keys-builder/typescript/service.extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@ import ts from 'typescript';
import { buildKeysFromASTNodes } from './build-keys-from-ast-nodes';
import { TSExtractorResult } from './types';

function buildInjectFunctionQuery(nodeType: string) {
return `${nodeType}:has(CallExpression:has(Identifier[name=inject]):has(Identifier[name=TranslocoService]))`;
}

export function serviceExtractor(ast: SourceFile): TSExtractorResult {
const constructorInjection =
'Constructor Parameter:has(TypeReference Identifier[name=TranslocoService])';
const injectFunction =
'PropertyDeclaration:has(CallExpression:has(Identifier[name=TranslocoService],Identifier[name=inject]))';

const serviceNameNodes = tsquery(
ast,
`${constructorInjection},${injectFunction}`,
const injectFunction = ['PropertyDeclaration', 'VariableDeclaration'].map(
buildInjectFunctionQuery,
);
const serviceNameQuery = [constructorInjection, injectFunction].join(',');
const serviceNameNodes = tsquery(ast, serviceNameQuery);

let result: TSExtractorResult = [];

for (const serviceName of serviceNameNodes) {
if (ts.isParameter(serviceName) || ts.isPropertyDeclaration(serviceName)) {
if (
ts.isParameter(serviceName) ||
ts.isPropertyDeclaration(serviceName) ||
ts.isVariableDeclaration(serviceName)
) {
const propName = serviceName.name.getText();
const methodNodes = tsquery(
ast,
Expand Down

0 comments on commit a326b78

Please sign in to comment.