Skip to content

Commit 0c53970

Browse files
crisbetothePunderWoman
authored andcommitted
fix(migrations): handle shorthand assignments in super call (angular#60602)
Fixes that the logic which checks whether a parameter is used inside a `super` call wasn't accounting for shorthand assignments. PR Close angular#60602
1 parent 3ff48b6 commit 0c53970

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,16 @@ export function getSuperParameters(
302302
localTypeChecker.getSymbolAtLocation(node)?.declarations?.forEach((decl) => {
303303
if (ts.isParameter(decl) && topLevelParameters.has(decl)) {
304304
usedParams.add(decl);
305+
} else if (
306+
ts.isShorthandPropertyAssignment(decl) &&
307+
topLevelParameterNames.has(decl.name.text)
308+
) {
309+
for (const param of topLevelParameters) {
310+
if (ts.isIdentifier(param.name) && decl.name.text === param.name.text) {
311+
usedParams.add(param);
312+
break;
313+
}
314+
}
305315
}
306316
});
307317
// Parameters referenced inside callbacks can be used directly

packages/core/schematics/test/inject_migration_spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,45 @@ describe('inject migration', () => {
18351835
]);
18361836
});
18371837

1838+
it('should handle super parameter used in shorthand assignment', async () => {
1839+
writeFile(
1840+
'/dir.ts',
1841+
[
1842+
`import { Directive } from '@angular/core';`,
1843+
`import { Service } from './service';`,
1844+
`import { Parent } from './parent';`,
1845+
``,
1846+
`@Directive()`,
1847+
`export class MyDir extends Parent {`,
1848+
` constructor(private service: Service) {`,
1849+
` super({service});`,
1850+
` }`,
1851+
`}`,
1852+
].join('\n'),
1853+
);
1854+
1855+
await runMigration();
1856+
1857+
expect(tree.readContent('/dir.ts').split('\n')).toEqual([
1858+
`import { Directive, inject } from '@angular/core';`,
1859+
`import { Service } from './service';`,
1860+
`import { Parent } from './parent';`,
1861+
``,
1862+
`@Directive()`,
1863+
`export class MyDir extends Parent {`,
1864+
` private service: Service;`,
1865+
``,
1866+
` constructor() {`,
1867+
` const service = inject(Service);`,
1868+
``,
1869+
` super({service});`,
1870+
` `,
1871+
` this.service = service;`,
1872+
` }`,
1873+
`}`,
1874+
]);
1875+
});
1876+
18381877
describe('internal-only behavior', () => {
18391878
function runInternalMigration() {
18401879
return runMigration({_internalCombineMemberInitializers: true});

0 commit comments

Comments
 (0)