Skip to content

Commit 9cbebc6

Browse files
crisbetopkozlowski-opensource
authored andcommitted
fix(migrations): preserve type literals and tuples in inject migrations (#58959)
Updates the inject migration to preserve type literals and tuple types, based on some internal feedback. PR Close #58959
1 parent 7c5f990 commit 9cbebc6

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -553,14 +553,10 @@ function migrateInjectDecorator(
553553
}
554554
}
555555
} else if (
556-
// Pass the type for cases like `@Inject(FOO_TOKEN) foo: Foo`, because:
557-
// 1. It guarantees that the type stays the same as before.
558-
// 2. Avoids leaving unused imports behind.
559-
// We only do this for type references since the `@Inject` pattern above is fairly common and
560-
// apps don't necessarily type their injection tokens correctly, whereas doing it for literal
561-
// types will add a lot of noise to the generated code.
562556
type &&
563557
(ts.isTypeReferenceNode(type) ||
558+
ts.isTypeLiteralNode(type) ||
559+
ts.isTupleTypeNode(type) ||
564560
(ts.isUnionTypeNode(type) && type.types.some(ts.isTypeReferenceNode)))
565561
) {
566562
typeArguments = [type];

packages/core/schematics/test/inject_migration_spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,65 @@ describe('inject migration', () => {
12821282
]);
12831283
});
12841284

1285+
it('should preserve type literals in @Inject parameter', async () => {
1286+
writeFile(
1287+
'/dir.ts',
1288+
[
1289+
`import { Directive, Inject } from '@angular/core';`,
1290+
`import { FOO_TOKEN } from 'foo';`,
1291+
``,
1292+
`@Directive()`,
1293+
`class MyDir {`,
1294+
` constructor(@Inject(FOO_TOKEN) private foo: {id: number}) {}`,
1295+
`}`,
1296+
].join('\n'),
1297+
);
1298+
1299+
await runMigration();
1300+
1301+
expect(tree.readContent('/dir.ts').split('\n')).toEqual([
1302+
`import { Directive, inject } from '@angular/core';`,
1303+
`import { FOO_TOKEN } from 'foo';`,
1304+
``,
1305+
`@Directive()`,
1306+
`class MyDir {`,
1307+
` private foo = inject<{`,
1308+
` id: number;`,
1309+
`}>(FOO_TOKEN);`,
1310+
`}`,
1311+
]);
1312+
});
1313+
1314+
it('should preserve tuple types in @Inject parameter', async () => {
1315+
writeFile(
1316+
'/dir.ts',
1317+
[
1318+
`import { Directive, Inject } from '@angular/core';`,
1319+
`import { FOO_TOKEN } from 'foo';`,
1320+
``,
1321+
`@Directive()`,
1322+
`class MyDir {`,
1323+
` constructor(@Inject(FOO_TOKEN) private foo: [a: number, b: number]) {}`,
1324+
`}`,
1325+
].join('\n'),
1326+
);
1327+
1328+
await runMigration();
1329+
1330+
expect(tree.readContent('/dir.ts').split('\n')).toEqual([
1331+
`import { Directive, inject } from '@angular/core';`,
1332+
`import { FOO_TOKEN } from 'foo';`,
1333+
``,
1334+
`@Directive()`,
1335+
`class MyDir {`,
1336+
` private foo = inject<[`,
1337+
` a: number,`,
1338+
` b: number`,
1339+
`]>(FOO_TOKEN);`,
1340+
`}`,
1341+
]);
1342+
});
1343+
12851344
it('should unwrap forwardRef with an implicit return', async () => {
12861345
writeFile(
12871346
'/dir.ts',

0 commit comments

Comments
 (0)