Skip to content

Commit d1cbdd6

Browse files
crisbetopkozlowski-opensource
authored andcommittedDec 2, 2024
fix(migrations): correctly strip away parameters surrounded by comments in inject migration (#58959)
Fixes that the inject migration was sometimes producing invalid code if there are comments around the parameters. PR Close #58959
1 parent 7b5bacc commit d1cbdd6

File tree

2 files changed

+54
-26
lines changed

2 files changed

+54
-26
lines changed
 

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

+14-26
Original file line numberDiff line numberDiff line change
@@ -586,37 +586,25 @@ function stripConstructorParameters(node: ts.ConstructorDeclaration, tracker: Ch
586586
const constructorText = node.getText();
587587
const lastParamText = node.parameters[node.parameters.length - 1].getText();
588588
const lastParamStart = constructorText.indexOf(lastParamText);
589-
const whitespacePattern = /\s/;
590-
let trailingCharacters = 0;
591589

592-
if (lastParamStart > -1) {
593-
let lastParamEnd = lastParamStart + lastParamText.length;
594-
let closeParenIndex = -1;
590+
// This shouldn't happen, but bail out just in case so we don't mangle the code.
591+
if (lastParamStart === -1) {
592+
return;
593+
}
595594

596-
for (let i = lastParamEnd; i < constructorText.length; i++) {
597-
const char = constructorText[i];
595+
for (let i = lastParamStart + lastParamText.length; i < constructorText.length; i++) {
596+
const char = constructorText[i];
598597

599-
if (char === ')') {
600-
closeParenIndex = i;
601-
break;
602-
} else if (!whitespacePattern.test(char)) {
603-
// The end of the last parameter won't include
604-
// any trailing commas which we need to account for.
605-
lastParamEnd = i + 1;
606-
}
607-
}
608-
609-
if (closeParenIndex > -1) {
610-
trailingCharacters = closeParenIndex - lastParamEnd;
598+
if (char === ')') {
599+
tracker.replaceText(
600+
node.getSourceFile(),
601+
node.parameters.pos,
602+
node.getStart() + i - node.parameters.pos,
603+
'',
604+
);
605+
break;
611606
}
612607
}
613-
614-
tracker.replaceText(
615-
node.getSourceFile(),
616-
node.parameters.pos,
617-
node.parameters.end - node.parameters.pos + trailingCharacters,
618-
'',
619-
);
620608
}
621609

622610
/**

‎packages/core/schematics/test/inject_migration_spec.ts

+40
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,46 @@ describe('inject migration', () => {
16441644
]);
16451645
});
16461646

1647+
it('should handle removing parameters surrounded by comments', async () => {
1648+
writeFile(
1649+
'/dir.ts',
1650+
[
1651+
`import { Directive } from '@angular/core';`,
1652+
`import { Foo } from 'foo';`,
1653+
`import { Bar } from 'bar';`,
1654+
``,
1655+
`@Directive()`,
1656+
`class MyClass {`,
1657+
` constructor(`,
1658+
` // start`,
1659+
` private foo: Foo,`,
1660+
` readonly bar: Bar, // end`,
1661+
` ) {`,
1662+
` console.log(this.bar);`,
1663+
` }`,
1664+
`}`,
1665+
].join('\n'),
1666+
);
1667+
1668+
await runMigration();
1669+
1670+
expect(tree.readContent('/dir.ts').split('\n')).toEqual([
1671+
`import { Directive, inject } from '@angular/core';`,
1672+
`import { Foo } from 'foo';`,
1673+
`import { Bar } from 'bar';`,
1674+
``,
1675+
`@Directive()`,
1676+
`class MyClass {`,
1677+
` private foo = inject(Foo);`,
1678+
` readonly bar = inject(Bar);`,
1679+
``,
1680+
` constructor() {`,
1681+
` console.log(this.bar);`,
1682+
` }`,
1683+
`}`,
1684+
]);
1685+
});
1686+
16471687
describe('internal-only behavior', () => {
16481688
function runInternalMigration() {
16491689
return runMigration({_internalCombineMemberInitializers: true});

0 commit comments

Comments
 (0)
Please sign in to comment.