Skip to content

Commit

Permalink
fix(migrations): correctly strip away parameters surrounded by commen…
Browse files Browse the repository at this point in the history
…ts in inject migration (#58959)

Fixes that the inject migration was sometimes producing invalid code if there are comments around the parameters.

PR Close #58959
  • Loading branch information
crisbeto authored and pkozlowski-opensource committed Dec 2, 2024
1 parent 7b5bacc commit d1cbdd6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
40 changes: 14 additions & 26 deletions packages/core/schematics/ng-generate/inject-migration/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,37 +586,25 @@ function stripConstructorParameters(node: ts.ConstructorDeclaration, tracker: Ch
const constructorText = node.getText();
const lastParamText = node.parameters[node.parameters.length - 1].getText();
const lastParamStart = constructorText.indexOf(lastParamText);
const whitespacePattern = /\s/;
let trailingCharacters = 0;

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

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

if (char === ')') {
closeParenIndex = i;
break;
} else if (!whitespacePattern.test(char)) {
// The end of the last parameter won't include
// any trailing commas which we need to account for.
lastParamEnd = i + 1;
}
}

if (closeParenIndex > -1) {
trailingCharacters = closeParenIndex - lastParamEnd;
if (char === ')') {
tracker.replaceText(
node.getSourceFile(),
node.parameters.pos,
node.getStart() + i - node.parameters.pos,
'',
);
break;
}
}

tracker.replaceText(
node.getSourceFile(),
node.parameters.pos,
node.parameters.end - node.parameters.pos + trailingCharacters,
'',
);
}

/**
Expand Down
40 changes: 40 additions & 0 deletions packages/core/schematics/test/inject_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,46 @@ describe('inject migration', () => {
]);
});

it('should handle removing parameters surrounded by comments', async () => {
writeFile(
'/dir.ts',
[
`import { Directive } from '@angular/core';`,
`import { Foo } from 'foo';`,
`import { Bar } from 'bar';`,
``,
`@Directive()`,
`class MyClass {`,
` constructor(`,
` // start`,
` private foo: Foo,`,
` readonly bar: Bar, // end`,
` ) {`,
` console.log(this.bar);`,
` }`,
`}`,
].join('\n'),
);

await runMigration();

expect(tree.readContent('/dir.ts').split('\n')).toEqual([
`import { Directive, inject } from '@angular/core';`,
`import { Foo } from 'foo';`,
`import { Bar } from 'bar';`,
``,
`@Directive()`,
`class MyClass {`,
` private foo = inject(Foo);`,
` readonly bar = inject(Bar);`,
``,
` constructor() {`,
` console.log(this.bar);`,
` }`,
`}`,
]);
});

describe('internal-only behavior', () => {
function runInternalMigration() {
return runMigration({_internalCombineMemberInitializers: true});
Expand Down

0 comments on commit d1cbdd6

Please sign in to comment.