Skip to content

Commit ca89ef9

Browse files
cexbrayatdylhunn
authored andcommitted
fix(core): handle shorthand assignment in the inject migration (#57134)
Currently the migration updates: ```ts constructor(@Inject(LOCALE_ID) locale: string) { console.log({ locale }); } ``` to: ```ts constructor() { console.log({ locale }); } ``` This fixes the migration, and results in: ``` constructor() { const locale = inject(LOCALE_ID); console.log({ locale }); } ``` PR Close #57134
1 parent 6609a94 commit ca89ef9

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

packages/core/schematics/ng-generate/inject-migration/analysis.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ export function detectClassesUsingDI(sourceFile: ts.SourceFile, localTypeChecker
6464
export function getConstructorUnusedParameters(
6565
declaration: ts.ConstructorDeclaration,
6666
localTypeChecker: ts.TypeChecker,
67-
): Set<ts.ParameterDeclaration> {
68-
const accessedTopLevelParameters = new Set<ts.ParameterDeclaration>();
69-
const topLevelParameters = new Set<ts.ParameterDeclaration>();
67+
): Set<ts.Declaration> {
68+
const accessedTopLevelParameters = new Set<ts.Declaration>();
69+
const topLevelParameters = new Set<ts.Declaration>();
7070
const topLevelParameterNames = new Set<string>();
71-
const unusedParams = new Set<ts.ParameterDeclaration>();
71+
const unusedParams = new Set<ts.Declaration>();
7272

7373
// Prepare the parameters for quicker checks further down.
7474
for (const param of declaration.parameters) {
@@ -102,6 +102,12 @@ export function getConstructorUnusedParameters(
102102
if (ts.isParameter(decl) && topLevelParameters.has(decl)) {
103103
accessedTopLevelParameters.add(decl);
104104
}
105+
if (ts.isShorthandPropertyAssignment(decl)) {
106+
const symbol = localTypeChecker.getShorthandAssignmentValueSymbol(decl);
107+
if (symbol && symbol.valueDeclaration && ts.isParameter(symbol.valueDeclaration)) {
108+
accessedTopLevelParameters.add(symbol.valueDeclaration);
109+
}
110+
}
105111
});
106112
});
107113

@@ -110,7 +116,6 @@ export function getConstructorUnusedParameters(
110116
unusedParams.add(param);
111117
}
112118
}
113-
114119
return unusedParams;
115120
}
116121

packages/core/schematics/test/inject_migration_spec.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,37 @@ describe('inject migration', () => {
503503
]);
504504
});
505505

506+
it('should declare a variable if an injected parameter with modifiers is referenced in the constructor via shorthand assignment', async () => {
507+
writeFile(
508+
'/dir.ts',
509+
[
510+
`import { Directive, Inject, LOCALE_ID } from '@angular/core';`,
511+
``,
512+
`@Directive()`,
513+
`class MyDir {`,
514+
` constructor(@Inject(LOCALE_ID) locale: string) {`,
515+
` console.log({ locale });`,
516+
` }`,
517+
`}`,
518+
].join('\n'),
519+
);
520+
521+
await runMigration();
522+
523+
expect(tree.readContent('/dir.ts').split('\n')).toEqual([
524+
`import { Directive, Inject, LOCALE_ID, inject } from '@angular/core';`,
525+
``,
526+
`@Directive()`,
527+
`class MyDir {`,
528+
` constructor() {`,
529+
` const locale = inject(LOCALE_ID);`,
530+
``,
531+
` console.log({ locale });`,
532+
` }`,
533+
`}`,
534+
]);
535+
});
536+
506537
it('should not declare a variable in the constructor if the only references to the parameter are shadowed', async () => {
507538
writeFile(
508539
'/dir.ts',
@@ -824,7 +855,7 @@ describe('inject migration', () => {
824855
]);
825856
});
826857

827-
it('should be able to opt into generating backwads-compatible constructors for a class with existing members', async () => {
858+
it('should be able to opt into generating backwards-compatible constructors for a class with existing members', async () => {
828859
writeFile(
829860
'/dir.ts',
830861
[
@@ -870,7 +901,7 @@ describe('inject migration', () => {
870901
]);
871902
});
872903

873-
it('should be able to opt into generating backwads-compatible constructors for a class that only has a constructor', async () => {
904+
it('should be able to opt into generating backwards-compatible constructors for a class that only has a constructor', async () => {
874905
writeFile(
875906
'/dir.ts',
876907
[

0 commit comments

Comments
 (0)