mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(migrations): handle shorthand assignments in super call (#60602)
Fixes that the logic which checks whether a parameter is used inside a `super` call wasn't accounting for shorthand assignments. PR Close #60602
This commit is contained in:
committed by
Jessica Janiuk
parent
3ff48b6467
commit
0c53970aeb
@@ -302,6 +302,16 @@ export function getSuperParameters(
|
||||
localTypeChecker.getSymbolAtLocation(node)?.declarations?.forEach((decl) => {
|
||||
if (ts.isParameter(decl) && topLevelParameters.has(decl)) {
|
||||
usedParams.add(decl);
|
||||
} else if (
|
||||
ts.isShorthandPropertyAssignment(decl) &&
|
||||
topLevelParameterNames.has(decl.name.text)
|
||||
) {
|
||||
for (const param of topLevelParameters) {
|
||||
if (ts.isIdentifier(param.name) && decl.name.text === param.name.text) {
|
||||
usedParams.add(param);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
// Parameters referenced inside callbacks can be used directly
|
||||
|
||||
@@ -1835,6 +1835,45 @@ describe('inject migration', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it('should handle super parameter used in shorthand assignment', async () => {
|
||||
writeFile(
|
||||
'/dir.ts',
|
||||
[
|
||||
`import { Directive } from '@angular/core';`,
|
||||
`import { Service } from './service';`,
|
||||
`import { Parent } from './parent';`,
|
||||
``,
|
||||
`@Directive()`,
|
||||
`export class MyDir extends Parent {`,
|
||||
` constructor(private service: Service) {`,
|
||||
` super({service});`,
|
||||
` }`,
|
||||
`}`,
|
||||
].join('\n'),
|
||||
);
|
||||
|
||||
await runMigration();
|
||||
|
||||
expect(tree.readContent('/dir.ts').split('\n')).toEqual([
|
||||
`import { Directive, inject } from '@angular/core';`,
|
||||
`import { Service } from './service';`,
|
||||
`import { Parent } from './parent';`,
|
||||
``,
|
||||
`@Directive()`,
|
||||
`export class MyDir extends Parent {`,
|
||||
` private service: Service;`,
|
||||
``,
|
||||
` constructor() {`,
|
||||
` const service = inject(Service);`,
|
||||
``,
|
||||
` super({service});`,
|
||||
` `,
|
||||
` this.service = service;`,
|
||||
` }`,
|
||||
`}`,
|
||||
]);
|
||||
});
|
||||
|
||||
describe('internal-only behavior', () => {
|
||||
function runInternalMigration() {
|
||||
return runMigration({_internalCombineMemberInitializers: true});
|
||||
|
||||
Reference in New Issue
Block a user