X Tutup
Skip to content

Commit 14f0e9a

Browse files
committed
chore: fix DDC errors / warnings
Closes angular#7195
1 parent ef9e40e commit 14f0e9a

File tree

88 files changed

+526
-467
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+526
-467
lines changed

modules/angular2/src/common/forms/directives/ng_control.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {ControlValueAccessor} from './control_value_accessor';
22
import {AbstractControlDirective} from './abstract_control_directive';
33
import {unimplemented} from 'angular2/src/facade/exceptions';
4+
import {AsyncValidatorFn, ValidatorFn} from './validators';
45

56
/**
67
* A base class that all control directive extend.
@@ -12,8 +13,8 @@ export abstract class NgControl extends AbstractControlDirective {
1213
name: string = null;
1314
valueAccessor: ControlValueAccessor = null;
1415

15-
get validator(): Function { return unimplemented(); }
16-
get asyncValidator(): Function { return unimplemented(); }
16+
get validator(): ValidatorFn { return <ValidatorFn>unimplemented(); }
17+
get asyncValidator(): AsyncValidatorFn { return <AsyncValidatorFn>unimplemented(); }
1718

1819
abstract viewToModelUpdate(newValue: any): void;
1920
}

modules/angular2/src/common/forms/directives/ng_control_group.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {ControlContainer} from './control_container';
1616
import {controlPath, composeValidators, composeAsyncValidators} from './shared';
1717
import {ControlGroup} from '../model';
1818
import {Form} from './form_interface';
19-
import {Validators, NG_VALIDATORS, NG_ASYNC_VALIDATORS} from '../validators';
19+
import {NG_VALIDATORS, NG_ASYNC_VALIDATORS} from '../validators';
20+
import {AsyncValidatorFn, ValidatorFn} from './validators';
2021

2122
const controlGroupProvider =
2223
CONST_EXPR(new Provider(ControlContainer, {useExisting: forwardRef(() => NgControlGroup)}));
@@ -106,7 +107,7 @@ export class NgControlGroup extends ControlContainer implements OnInit,
106107
*/
107108
get formDirective(): Form { return this._parent.formDirective; }
108109

109-
get validator(): Function { return composeValidators(this._validators); }
110+
get validator(): ValidatorFn { return composeValidators(this._validators); }
110111

111-
get asyncValidator(): Function { return composeAsyncValidators(this._asyncValidators); }
112+
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
112113
}

modules/angular2/src/common/forms/directives/ng_control_name.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import {
2727
selectValueAccessor
2828
} from './shared';
2929
import {Control} from '../model';
30-
import {Validators, NG_VALIDATORS, NG_ASYNC_VALIDATORS} from '../validators';
30+
import {NG_VALIDATORS, NG_ASYNC_VALIDATORS} from '../validators';
31+
import {ValidatorFn, AsyncValidatorFn} from './validators';
3132

3233

3334
const controlNameBinding =
@@ -136,9 +137,9 @@ export class NgControlName extends NgControl implements OnChanges,
136137

137138
get formDirective(): any { return this._parent.formDirective; }
138139

139-
get validator(): Function { return composeValidators(this._validators); }
140+
get validator(): ValidatorFn { return composeValidators(this._validators); }
140141

141-
get asyncValidator(): Function { return composeAsyncValidators(this._asyncValidators); }
142+
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
142143

143144
get control(): Control { return this.formDirective.getControl(this); }
144145
}

modules/angular2/src/common/forms/directives/ng_form_control.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
isPropertyUpdated,
2424
selectValueAccessor
2525
} from './shared';
26+
import {ValidatorFn, AsyncValidatorFn} from './validators';
2627

2728
const formControlBinding =
2829
CONST_EXPR(new Provider(NgControl, {useExisting: forwardRef(() => NgFormControl)}));
@@ -110,9 +111,9 @@ export class NgFormControl extends NgControl implements OnChanges {
110111

111112
get path(): string[] { return []; }
112113

113-
get validator(): Function { return composeValidators(this._validators); }
114+
get validator(): ValidatorFn { return composeValidators(this._validators); }
114115

115-
get asyncValidator(): Function { return composeAsyncValidators(this._asyncValidators); }
116+
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
116117

117118
get control(): Control { return this.form; }
118119

modules/angular2/src/common/forms/directives/ng_model.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
33
import {
44
OnChanges,
55
SimpleChange,
6-
Query,
76
Directive,
87
forwardRef,
98
Provider,
@@ -14,14 +13,15 @@ import {
1413
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
1514
import {NgControl} from './ng_control';
1615
import {Control} from '../model';
17-
import {Validators, NG_VALIDATORS, NG_ASYNC_VALIDATORS} from '../validators';
16+
import {NG_VALIDATORS, NG_ASYNC_VALIDATORS} from '../validators';
1817
import {
1918
setUpControl,
2019
isPropertyUpdated,
2120
selectValueAccessor,
2221
composeValidators,
2322
composeAsyncValidators
2423
} from './shared';
24+
import {ValidatorFn, AsyncValidatorFn} from './validators';
2525

2626
const formControlBinding =
2727
CONST_EXPR(new Provider(NgControl, {useExisting: forwardRef(() => NgModel)}));
@@ -88,9 +88,9 @@ export class NgModel extends NgControl implements OnChanges {
8888

8989
get path(): string[] { return []; }
9090

91-
get validator(): Function { return composeValidators(this._validators); }
91+
get validator(): ValidatorFn { return composeValidators(this._validators); }
9292

93-
get asyncValidator(): Function { return composeAsyncValidators(this._asyncValidators); }
93+
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
9494

9595
viewToModelUpdate(newValue: any): void {
9696
this.viewModel = newValue;

modules/angular2/src/common/forms/directives/normalize_validator.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@ Function normalizeValidator(dynamic validator){
1010
}
1111
}
1212

13+
14+
Function normalizeAsyncValidator(dynamic validator){
15+
if (validator is Validator) {
16+
return (c) => validator.validate(c);
17+
} else {
18+
return validator;
19+
}
20+
}
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
import {Validator} from './validators';
2-
import {Control} from "../model";
1+
import {AbstractControl} from "../model";
2+
import {Validator, ValidatorFn, AsyncValidatorFn} from './validators';
33

4-
export type ctrlFunc = ((c: Control) => {
5-
[key: string]: any
6-
});
4+
export function normalizeValidator(validator: ValidatorFn | Validator): ValidatorFn {
5+
if ((<Validator>validator).validate !== undefined) {
6+
return (c: AbstractControl) => (<Validator>validator).validate(c);
7+
} else {
8+
return <ValidatorFn>validator;
9+
}
10+
}
711

8-
export function normalizeValidator(validator: (ctrlFunc | Validator)): ctrlFunc {
12+
export function normalizeAsyncValidator(validator: AsyncValidatorFn | Validator): AsyncValidatorFn {
913
if ((<Validator>validator).validate !== undefined) {
10-
return (c: Control) => (<Validator>validator).validate(c);
14+
return (c: AbstractControl) => Promise.resolve((<Validator>validator).validate(c));
1115
} else {
12-
return <ctrlFunc>validator;
16+
return <AsyncValidatorFn>validator;
1317
}
1418
}

modules/angular2/src/common/forms/directives/shared.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {NumberValueAccessor} from './number_value_accessor';
1414
import {CheckboxControlValueAccessor} from './checkbox_value_accessor';
1515
import {SelectControlValueAccessor} from './select_control_value_accessor';
1616
import {RadioControlValueAccessor} from './radio_control_value_accessor';
17-
import {normalizeValidator} from './normalize_validator';
17+
import {normalizeValidator, normalizeAsyncValidator} from './normalize_validator';
18+
import {ValidatorFn, AsyncValidatorFn} from './validators';
1819

1920

2021
export function controlPath(name: string, parent: ControlContainer): string[] {
@@ -56,13 +57,14 @@ function _throwError(dir: AbstractControlDirective, message: string): void {
5657
throw new BaseException(`${message} '${path}'`);
5758
}
5859

59-
export function composeValidators(validators: /* Array<Validator|Function> */ any[]): Function {
60+
export function composeValidators(validators: /* Array<Validator|Function> */ any[]): ValidatorFn {
6061
return isPresent(validators) ? Validators.compose(validators.map(normalizeValidator)) : null;
6162
}
6263

6364
export function composeAsyncValidators(
64-
validators: /* Array<Validator|Function> */ any[]): Function {
65-
return isPresent(validators) ? Validators.composeAsync(validators.map(normalizeValidator)) : null;
65+
validators: /* Array<Validator|Function> */ any[]): AsyncValidatorFn {
66+
return isPresent(validators) ? Validators.composeAsync(validators.map(normalizeAsyncValidator)) :
67+
null;
6668
}
6769

6870
export function isPropertyUpdated(changes: {[key: string]: any}, viewModel: any): boolean {

modules/angular2/src/common/forms/directives/validators.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import {forwardRef, Provider, OpaqueToken, Attribute, Directive} from 'angular2/core';
1+
import {forwardRef, Provider, Attribute, Directive} from 'angular2/core';
22
import {CONST_EXPR} from 'angular2/src/facade/lang';
33
import {Validators, NG_VALIDATORS} from '../validators';
4-
import {Control} from '../model';
4+
import {AbstractControl} from '../model';
55
import * as modelModule from '../model';
66
import {NumberWrapper} from "angular2/src/facade/lang";
77

88

9+
910
/**
1011
* An interface that can be implemented by classes that can act as validators.
1112
*
@@ -23,7 +24,7 @@ import {NumberWrapper} from "angular2/src/facade/lang";
2324
* }
2425
* ```
2526
*/
26-
export interface Validator { validate(c: modelModule.Control): {[key: string]: any}; }
27+
export interface Validator { validate(c: modelModule.AbstractControl): {[key: string]: any}; }
2728

2829
const REQUIRED_VALIDATOR =
2930
CONST_EXPR(new Provider(NG_VALIDATORS, {useValue: Validators.required, multi: true}));
@@ -45,6 +46,11 @@ const REQUIRED_VALIDATOR =
4546
export class RequiredValidator {
4647
}
4748

49+
export interface ValidatorFn { (c: AbstractControl): {[key: string]: any}; }
50+
export interface AsyncValidatorFn {
51+
(c: AbstractControl): any /*Promise<{[key: string]: any}>|Observable<{[key: string]: any}>*/;
52+
}
53+
4854
/**
4955
* Provivder which adds {@link MinLengthValidator} to {@link NG_VALIDATORS}.
5056
*
@@ -64,13 +70,13 @@ const MIN_LENGTH_VALIDATOR = CONST_EXPR(
6470
providers: [MIN_LENGTH_VALIDATOR]
6571
})
6672
export class MinLengthValidator implements Validator {
67-
private _validator: Function;
73+
private _validator: ValidatorFn;
6874

6975
constructor(@Attribute("minlength") minLength: string) {
7076
this._validator = Validators.minLength(NumberWrapper.parseInt(minLength, 10));
7177
}
7278

73-
validate(c: Control): {[key: string]: any} { return this._validator(c); }
79+
validate(c: AbstractControl): {[key: string]: any} { return this._validator(c); }
7480
}
7581

7682
/**
@@ -92,13 +98,13 @@ const MAX_LENGTH_VALIDATOR = CONST_EXPR(
9298
providers: [MAX_LENGTH_VALIDATOR]
9399
})
94100
export class MaxLengthValidator implements Validator {
95-
private _validator: Function;
101+
private _validator: ValidatorFn;
96102

97103
constructor(@Attribute("maxlength") maxLength: string) {
98104
this._validator = Validators.maxLength(NumberWrapper.parseInt(maxLength, 10));
99105
}
100106

101-
validate(c: Control): {[key: string]: any} { return this._validator(c); }
107+
validate(c: AbstractControl): {[key: string]: any} { return this._validator(c); }
102108
}
103109

104110

@@ -121,11 +127,11 @@ const PATTERN_VALIDATOR = CONST_EXPR(
121127
providers: [PATTERN_VALIDATOR]
122128
})
123129
export class PatternValidator implements Validator {
124-
private _validator: Function;
130+
private _validator: ValidatorFn;
125131

126132
constructor(@Attribute("pattern") pattern: string) {
127133
this._validator = Validators.pattern(pattern);
128134
}
129135

130-
validate(c: Control): {[key: string]: any} { return this._validator(c); }
136+
validate(c: AbstractControl): {[key: string]: any} { return this._validator(c); }
131137
}

modules/angular2/src/common/forms/form_builder.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Injectable} from 'angular2/core';
22
import {StringMapWrapper} from 'angular2/src/facade/collection';
33
import {isPresent, isArray, CONST_EXPR, Type} from 'angular2/src/facade/lang';
44
import * as modelModule from './model';
5+
import {ValidatorFn, AsyncValidatorFn} from './directives/validators';
56

67

78
/**
@@ -56,25 +57,27 @@ export class FormBuilder {
5657
group(controlsConfig: {[key: string]: any},
5758
extra: {[key: string]: any} = null): modelModule.ControlGroup {
5859
var controls = this._reduceControls(controlsConfig);
59-
var optionals = isPresent(extra) ? StringMapWrapper.get(extra, "optionals") : null;
60-
var validator = isPresent(extra) ? StringMapWrapper.get(extra, "validator") : null;
61-
var asyncValidator = isPresent(extra) ? StringMapWrapper.get(extra, "asyncValidator") : null;
60+
var optionals = <{[key: string]: boolean}>(
61+
isPresent(extra) ? StringMapWrapper.get(extra, "optionals") : null);
62+
var validator: ValidatorFn = isPresent(extra) ? StringMapWrapper.get(extra, "validator") : null;
63+
var asyncValidator: AsyncValidatorFn =
64+
isPresent(extra) ? StringMapWrapper.get(extra, "asyncValidator") : null;
6265
return new modelModule.ControlGroup(controls, optionals, validator, asyncValidator);
6366
}
6467
/**
6568
* Construct a new {@link Control} with the given `value`,`validator`, and `asyncValidator`.
6669
*/
67-
control(value: Object, validator: Function = null,
68-
asyncValidator: Function = null): modelModule.Control {
70+
control(value: Object, validator: ValidatorFn = null,
71+
asyncValidator: AsyncValidatorFn = null): modelModule.Control {
6972
return new modelModule.Control(value, validator, asyncValidator);
7073
}
7174

7275
/**
7376
* Construct an array of {@link Control}s from the given `controlsConfig` array of
7477
* configuration, with the given optional `validator` and `asyncValidator`.
7578
*/
76-
array(controlsConfig: any[], validator: Function = null,
77-
asyncValidator: Function = null): modelModule.ControlArray {
79+
array(controlsConfig: any[], validator: ValidatorFn = null,
80+
asyncValidator: AsyncValidatorFn = null): modelModule.ControlArray {
7881
var controls = controlsConfig.map(c => this._createControl(c));
7982
return new modelModule.ControlArray(controls, validator, asyncValidator);
8083
}
@@ -98,12 +101,12 @@ export class FormBuilder {
98101

99102
} else if (isArray(controlConfig)) {
100103
var value = controlConfig[0];
101-
var validator = controlConfig.length > 1 ? controlConfig[1] : null;
102-
var asyncValidator = controlConfig.length > 2 ? controlConfig[2] : null;
104+
var validator: ValidatorFn = controlConfig.length > 1 ? controlConfig[1] : null;
105+
var asyncValidator: AsyncValidatorFn = controlConfig.length > 2 ? controlConfig[2] : null;
103106
return this.control(value, validator, asyncValidator);
104107

105108
} else {
106109
return this.control(controlConfig);
107110
}
108111
}
109-
}
112+
}

0 commit comments

Comments
 (0)
X Tutup