Skip to content

Commit f7aa937

Browse files
committed
fix(forms): Make some minor fixups for forward-compatibility with typed forms. (#44540)
Make the following fixes: * When submitting the entire migration in a disabled state, I commented out more code than strictly required * Responding to some final review comments caused two conditions to become flipped * Always use explicit checks instead of boolean corecion * Fix one missed any cast in a test case PR Close #44540
1 parent 7cb2999 commit f7aa937

File tree

6 files changed

+29
-33
lines changed

6 files changed

+29
-33
lines changed

packages/core/schematics/migrations.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
"version": "14.0.0-beta",
2020
"description": "As of Angular version 13, `entryComponents` are no longer necessary.",
2121
"factory": "./migrations/entry-components/index"
22+
},
23+
"migration-v14-typed-forms": {
24+
"version": "9999.0.0",
25+
"description": "Experimental migration that adds <any>s for Typed Forms.",
26+
"factory": "./migrations/typed-forms/index"
2227
}
2328
}
2429
}

packages/core/schematics/migrations/google3/typedFormsRule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class Rule extends Rules.TypedRule {
2222
const failures: RuleFailure[] = [];
2323

2424
// If no relevant classes are imported, we can exit early.
25-
if (controlClassImports.length === 0 && formBuilderImport !== null) return failures;
25+
if (controlClassImports.length === 0 && formBuilderImport === null) return failures;
2626

2727
// For each control class, migrate all of its uses.
2828
for (const importSpecifier of controlClassImports) {
@@ -39,9 +39,9 @@ export class Rule extends Rules.TypedRule {
3939
}
4040

4141
// Add the any symbol used by the migrated calls.
42-
if (getAnyImport(sourceFile) !== null) {
42+
if (getAnyImport(sourceFile) === null) {
4343
const firstValidFormsImport =
44-
[...controlClassImports, formBuilderImport].sort().filter(i => i)[0]!;
44+
[...controlClassImports, formBuilderImport].sort().filter(i => i !== null)[0]!;
4545
failures.push(this.getImportFailure(firstValidFormsImport, sourceFile));
4646
}
4747

packages/core/schematics/migrations/typed-forms/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function runTypedFormsMigration(tree: Tree, tsconfigPath: string, basePath: stri
4343
const formBuilderImport = getFormBuilderImport(sourceFile);
4444

4545
// If no relevant classes are imported, we can exit early.
46-
if (controlClassImports.length === 0 && formBuilderImport !== null) return;
46+
if (controlClassImports.length === 0 && formBuilderImport === null) return;
4747

4848
const update = tree.beginUpdate(relative(basePath, sourceFile.fileName));
4949

@@ -62,7 +62,7 @@ function runTypedFormsMigration(tree: Tree, tsconfigPath: string, basePath: stri
6262
}
6363

6464
// Add the any symbol used by the migrated calls.
65-
if (getAnyImport(sourceFile) !== null) {
65+
if (getAnyImport(sourceFile) === null) {
6666
const firstValidFormsImport =
6767
[...controlClassImports, formBuilderImport].sort().filter(i => i !== null)[0]!;
6868
insertAnyImport(update, firstValidFormsImport);

packages/core/schematics/test/google3/typed_forms_spec.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import {Configuration, Linter} from 'tslint';
1313

1414
const anySymbolName = 'AnyForUntypedForms';
1515

16-
// Tests disabled, as the migration is currently disabled in package.json.
17-
/*
18-
1916
describe('Google3 typedForms TSLint rule', () => {
2017
const rulesDirectory = dirname(require.resolve('../../migrations/google3/typedFormsRule'));
2118

@@ -91,8 +88,7 @@ describe('Google3 typedForms TSLint rule', () => {
9188
it('should migrate a complete example', () => {
9289
writeFile('/index.ts', `
9390
import { Component } from '@angular/core';
94-
import { AbstractControl, FormArray, FormBuilder, FormControl as FC, FormGroup } from
95-
'@angular/forms';
91+
import { AbstractControl, FormArray, FormBuilder, FormControl as FC, FormGroup } from '@angular/forms';
9692
9793
@Component({template: ''})
9894
export class MyComponent {
@@ -114,13 +110,13 @@ describe('Google3 typedForms TSLint rule', () => {
114110
const linter = runTSLint(true);
115111

116112
[`import { ${
117-
anySymbolName}, AbstractControl, FormArray, FormBuilder, FormControl as FC, FormGroup }
118-
from '@angular/forms';`, `private _control = new FC<${anySymbolName}>(42)`, `private _group = new
119-
FormGroup<${anySymbolName}>({})`, `private _array = new FormArray<${anySymbolName}[]>([])`, `const
120-
fc2 = new FC<${anySymbolName}>(0)`, `const c = this.fb.control<${anySymbolName}>(42)`, `const g =
121-
this.fb.group<${anySymbolName}>({one: this.fb.control<${anySymbolName}>('')})`, `const a =
122-
this.fb.array<${anySymbolName}[]>([42])`] .forEach(t => expect(getFile(`/index.ts`)).toContain(t));
113+
anySymbolName}, AbstractControl, FormArray, FormBuilder, FormControl as FC, FormGroup } from '@angular/forms';`,
114+
`private _control = new FC<${anySymbolName}>(42)`,
115+
`private _group = new FormGroup<${anySymbolName}>({})`,
116+
`private _array = new FormArray<${anySymbolName}[]>([])`,
117+
`const fc2 = new FC<${anySymbolName}>(0)`, `const c = this.fb.control<${anySymbolName}>(42)`,
118+
`const g = this.fb.group<${anySymbolName}>({one: this.fb.control<${anySymbolName}>('')})`,
119+
`const a = this.fb.array<${anySymbolName}[]>([42])`]
120+
.forEach(t => expect(getFile(`/index.ts`)).toContain(t));
123121
});
124122
});
125-
126-
*/

packages/core/schematics/test/typed_forms_spec.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ import * as shx from 'shelljs';
1414

1515
const anySymbolName = 'AnyForUntypedForms';
1616

17-
// Tests disabled, as the migration is currently disabled in package.json.
18-
/*
19-
2017
describe('Typed Forms migration', () => {
2118
let runner: SchematicTestRunner;
2219
let host: TempScopedNodeJsSyncHost;
@@ -255,8 +252,7 @@ describe('Typed Forms migration', () => {
255252
it('an integrated example', async () => {
256253
writeFile('/index.ts', `
257254
import { Component } from '@angular/core';
258-
import { AbstractControl, FormArray, FormBuilder, FormControl as FC, FormGroup } from
259-
'@angular/forms';
255+
import { AbstractControl, FormArray, FormBuilder, FormControl as FC, FormGroup } from '@angular/forms';
260256
261257
@Component({template: ''})
262258
export class MyComponent {
@@ -276,13 +272,14 @@ describe('Typed Forms migration', () => {
276272
`);
277273
await runMigration();
278274
[`import { ${
279-
anySymbolName}, AbstractControl, FormArray, FormBuilder, FormControl as FC, FormGroup }
280-
from '@angular/forms';`, `private _control = new FC<${anySymbolName}>(42)`, `private _group = new
281-
FormGroup<${anySymbolName}>({})`, `private _array = new FormArray<${anySymbolName}[]>([])`, `const
282-
fc2 = new FC<${anySymbolName}>(0)`, `const c = this.fb.control<${anySymbolName}>(42)`, `const g =
283-
this.fb.group<${anySymbolName}>({one: this.fb.control<${anySymbolName}>('')})`, `const a =
284-
this.fb.array<${anySymbolName}[]>([42])`] .forEach(t =>
285-
expect(tree.readContent('/index.ts')).toContain(t));
275+
anySymbolName}, AbstractControl, FormArray, FormBuilder, FormControl as FC, FormGroup } from '@angular/forms';`,
276+
`private _control = new FC<${anySymbolName}>(42)`,
277+
`private _group = new FormGroup<${anySymbolName}>({})`,
278+
`private _array = new FormArray<${anySymbolName}[]>([])`,
279+
`const fc2 = new FC<${anySymbolName}>(0)`, `const c = this.fb.control<${anySymbolName}>(42)`,
280+
`const g = this.fb.group<${anySymbolName}>({one: this.fb.control<${anySymbolName}>('')})`,
281+
`const a = this.fb.array<${anySymbolName}[]>([42])`]
282+
.forEach(t => expect(tree.readContent('/index.ts')).toContain(t));
286283
});
287284
});
288285

@@ -294,5 +291,3 @@ expect(tree.readContent('/index.ts')).toContain(t));
294291
return runner.runSchematicAsync('migration-v14-typed-forms', {}, tree).toPromise();
295292
}
296293
});
297-
298-
*/

packages/forms/test/form_array_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ describe('FormArray', () => {
180180
it('should work with nested form groups/arrays', () => {
181181
a = new FormArray([
182182
new FormGroup(
183-
{'c2': new FormControl('v2') as AbstractControl, 'c3': new FormControl('v3')}),
183+
{'c2': new FormControl('v2') as AbstractControl, 'c3': new FormControl('v3') as any}),
184184
new FormArray([new FormControl('v4'), new FormControl('v5')])
185185
]);
186186
a.at(0).get('c3')!.disable();

0 commit comments

Comments
 (0)