Skip to content

Commit 006ac7f

Browse files
aparzithePunderWoman
authored andcommitted
fix(core): fixes #592882 ng generate @angular/core:signal-queries-migration (angular#60688)
fixes #592882 - retain accessibility modifier if it's already present for signal migrations PR Close angular#60688
1 parent 635fb00 commit 006ac7f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

packages/core/schematics/migrations/signal-queries-migration/convert_query_property.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,17 @@ export function computeReplacementsToMigrateQuery(
150150
resolvedReadType === null && type !== undefined ? [type] : undefined,
151151
args,
152152
);
153+
154+
const accessibilityModifier = getAccessibilityModifier(node);
155+
let modifiers: (ts.ModifierLike | ts.ModifierToken<ts.SyntaxKind.ReadonlyKeyword>)[] = [
156+
ts.factory.createModifier(ts.SyntaxKind.ReadonlyKeyword),
157+
];
158+
if (accessibilityModifier) {
159+
modifiers = [accessibilityModifier, ...modifiers];
160+
}
161+
153162
const updated = ts.factory.createPropertyDeclaration(
154-
[ts.factory.createModifier(ts.SyntaxKind.ReadonlyKeyword)],
163+
modifiers,
155164
node.name,
156165
undefined,
157166
undefined,
@@ -169,3 +178,12 @@ export function computeReplacementsToMigrateQuery(
169178
),
170179
];
171180
}
181+
182+
function getAccessibilityModifier(node: ts.PropertyDeclaration): ts.ModifierLike | undefined {
183+
return node.modifiers?.find(
184+
(mod) =>
185+
mod.kind === ts.SyntaxKind.PublicKeyword ||
186+
mod.kind === ts.SyntaxKind.PrivateKeyword ||
187+
mod.kind === ts.SyntaxKind.ProtectedKeyword,
188+
);
189+
}

packages/core/schematics/migrations/signal-queries-migration/migration.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ const declarationTestCases: TestCase[] = [
5555
before: `@ViewChild('myBtn', {read: ElementRef}) buttonEl!: ElementRef;`,
5656
after: `readonly buttonEl = viewChild.required('myBtn', { read: ElementRef });`,
5757
},
58+
{
59+
id: 'viewChild retain accessibility modifier',
60+
before: `@ViewChild('sidenav') public sidenav: HTMLElement;`,
61+
after: `public readonly sidenav = viewChild<HTMLElement>('sidenav');`,
62+
},
5863
// Content Child
5964
{
6065
id: 'contentChild with string locator and nullable',
@@ -137,6 +142,11 @@ const declarationTestCases: TestCase[] = [
137142
before: `@ViewChildren('myBtn', {descendants: true}) buttonEl = new QueryList<ElementRef>()`,
138143
after: `readonly buttonEl = viewChildren<ElementRef>('myBtn');`,
139144
},
145+
{
146+
id: 'viewChildren retain accessibility modifier',
147+
before: `@ViewChildren('sidenav') public sidenav: HTMLElement;`,
148+
after: `public readonly sidenav = viewChildren('sidenav');`,
149+
},
140150
// ContentChildren
141151
{
142152
id: 'contentChildren with string locator and nullable',

0 commit comments

Comments
 (0)