Skip to content

Commit 598a75e

Browse files
committed
style(ChangeDetection): idiomatic TS
1 parent cdfb635 commit 598a75e

16 files changed

+133
-233
lines changed

modules/angular2/src/change_detection/abstract_change_detector.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,38 @@ import {ChangeDetector} from './interfaces';
55
import {CHECK_ALWAYS, CHECK_ONCE, CHECKED, DETACHED, ON_PUSH} from './constants';
66

77
export class AbstractChangeDetector extends ChangeDetector {
8-
lightDomChildren: List<any>;
9-
shadowDomChildren: List<any>;
8+
lightDomChildren: List<any> = [];
9+
shadowDomChildren: List<any> = [];
1010
parent: ChangeDetector;
11-
mode: string;
11+
mode: string = null;
1212
ref: ChangeDetectorRef;
1313

1414
constructor() {
1515
super();
16-
this.lightDomChildren = [];
17-
this.shadowDomChildren = [];
1816
this.ref = new ChangeDetectorRef(this);
19-
this.mode = null;
2017
}
2118

22-
addChild(cd: ChangeDetector) {
19+
addChild(cd: ChangeDetector): void {
2320
ListWrapper.push(this.lightDomChildren, cd);
2421
cd.parent = this;
2522
}
2623

27-
removeChild(cd: ChangeDetector) { ListWrapper.remove(this.lightDomChildren, cd); }
24+
removeChild(cd: ChangeDetector): void { ListWrapper.remove(this.lightDomChildren, cd); }
2825

29-
addShadowDomChild(cd: ChangeDetector) {
26+
addShadowDomChild(cd: ChangeDetector): void {
3027
ListWrapper.push(this.shadowDomChildren, cd);
3128
cd.parent = this;
3229
}
3330

34-
removeShadowDomChild(cd: ChangeDetector) { ListWrapper.remove(this.shadowDomChildren, cd); }
31+
removeShadowDomChild(cd: ChangeDetector): void { ListWrapper.remove(this.shadowDomChildren, cd); }
3532

36-
remove() { this.parent.removeChild(this); }
33+
remove(): void { this.parent.removeChild(this); }
3734

38-
detectChanges() { this._detectChanges(false); }
35+
detectChanges(): void { this._detectChanges(false); }
3936

40-
checkNoChanges() { this._detectChanges(true); }
37+
checkNoChanges(): void { this._detectChanges(true); }
4138

42-
_detectChanges(throwOnChange: boolean) {
39+
_detectChanges(throwOnChange: boolean): void {
4340
if (this.mode === DETACHED || this.mode === CHECKED) return;
4441

4542
this.detectChangesInRecords(throwOnChange);
@@ -53,26 +50,26 @@ export class AbstractChangeDetector extends ChangeDetector {
5350
if (this.mode === CHECK_ONCE) this.mode = CHECKED;
5451
}
5552

56-
detectChangesInRecords(throwOnChange: boolean) {}
57-
callOnAllChangesDone() {}
53+
detectChangesInRecords(throwOnChange: boolean): void {}
54+
callOnAllChangesDone(): void {}
5855

59-
_detectChangesInLightDomChildren(throwOnChange: boolean) {
56+
_detectChangesInLightDomChildren(throwOnChange: boolean): void {
6057
var c = this.lightDomChildren;
6158
for (var i = 0; i < c.length; ++i) {
6259
c[i]._detectChanges(throwOnChange);
6360
}
6461
}
6562

66-
_detectChangesInShadowDomChildren(throwOnChange: boolean) {
63+
_detectChangesInShadowDomChildren(throwOnChange: boolean): void {
6764
var c = this.shadowDomChildren;
6865
for (var i = 0; i < c.length; ++i) {
6966
c[i]._detectChanges(throwOnChange);
7067
}
7168
}
7269

73-
markAsCheckOnce() { this.mode = CHECK_ONCE; }
70+
markAsCheckOnce(): void { this.mode = CHECK_ONCE; }
7471

75-
markPathToRootAsCheckOnce() {
72+
markPathToRootAsCheckOnce(): void {
7673
var c: ChangeDetector = this;
7774
while (isPresent(c) && c.mode != DETACHED) {
7875
if (c.mode === CHECKED) c.mode = CHECK_ONCE;

modules/angular2/src/change_detection/binding_record.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,53 @@ export class BindingRecord {
1313
public elementIndex: number, public propertyName: string, public setter: SetterFn,
1414
public lifecycleEvent: string, public directiveRecord: DirectiveRecord) {}
1515

16-
callOnChange() { return isPresent(this.directiveRecord) && this.directiveRecord.callOnChange; }
16+
callOnChange(): boolean {
17+
return isPresent(this.directiveRecord) && this.directiveRecord.callOnChange;
18+
}
1719

18-
isOnPushChangeDetection() {
20+
isOnPushChangeDetection(): boolean {
1921
return isPresent(this.directiveRecord) && this.directiveRecord.isOnPushChangeDetection();
2022
}
2123

22-
isDirective() { return this.mode === DIRECTIVE; }
24+
isDirective(): boolean { return this.mode === DIRECTIVE; }
2325

24-
isDirectiveLifecycle() { return this.mode === DIRECTIVE_LIFECYCLE; }
26+
isDirectiveLifecycle(): boolean { return this.mode === DIRECTIVE_LIFECYCLE; }
2527

26-
isElement() { return this.mode === ELEMENT; }
28+
isElement(): boolean { return this.mode === ELEMENT; }
2729

28-
isTextNode() { return this.mode === TEXT_NODE; }
30+
isTextNode(): boolean { return this.mode === TEXT_NODE; }
2931

3032
static createForDirective(ast: AST, propertyName: string, setter: SetterFn,
31-
directiveRecord: DirectiveRecord) {
33+
directiveRecord: DirectiveRecord): BindingRecord {
3234
return new BindingRecord(DIRECTIVE, 0, ast, 0, propertyName, setter, null, directiveRecord);
3335
}
3436

35-
static createDirectiveOnCheck(directiveRecord: DirectiveRecord) {
37+
static createDirectiveOnCheck(directiveRecord: DirectiveRecord): BindingRecord {
3638
return new BindingRecord(DIRECTIVE_LIFECYCLE, 0, null, 0, null, null, "onCheck",
3739
directiveRecord);
3840
}
3941

40-
static createDirectiveOnInit(directiveRecord: DirectiveRecord) {
42+
static createDirectiveOnInit(directiveRecord: DirectiveRecord): BindingRecord {
4143
return new BindingRecord(DIRECTIVE_LIFECYCLE, 0, null, 0, null, null, "onInit",
4244
directiveRecord);
4345
}
4446

45-
static createDirectiveOnChange(directiveRecord: DirectiveRecord) {
47+
static createDirectiveOnChange(directiveRecord: DirectiveRecord): BindingRecord {
4648
return new BindingRecord(DIRECTIVE_LIFECYCLE, 0, null, 0, null, null, "onChange",
4749
directiveRecord);
4850
}
4951

50-
static createForElement(ast: AST, elementIndex: number, propertyName: string) {
52+
static createForElement(ast: AST, elementIndex: number, propertyName: string): BindingRecord {
5153
return new BindingRecord(ELEMENT, 0, ast, elementIndex, propertyName, null, null, null);
5254
}
5355

54-
static createForHostProperty(directiveIndex: DirectiveIndex, ast: AST, propertyName: string) {
56+
static createForHostProperty(directiveIndex: DirectiveIndex, ast: AST,
57+
propertyName: string): BindingRecord {
5558
return new BindingRecord(ELEMENT, directiveIndex, ast, directiveIndex.elementIndex,
5659
propertyName, null, null, null);
5760
}
5861

59-
static createForTextNode(ast: AST, elementIndex: number) {
62+
static createForTextNode(ast: AST, elementIndex: number): BindingRecord {
6063
return new BindingRecord(TEXT_NODE, 0, ast, elementIndex, null, null, null, null);
6164
}
62-
}
65+
}

modules/angular2/src/change_detection/change_detector_ref.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ export class ChangeDetectorRef {
1616
/**
1717
* Request to check all ON_PUSH ancestors.
1818
*/
19-
requestCheck() { this._cd.markPathToRootAsCheckOnce(); }
19+
requestCheck(): void { this._cd.markPathToRootAsCheckOnce(); }
2020

2121
/**
2222
* Detaches the change detector from the change detector tree.
2323
*
2424
* The detached change detector will not be checked until it is reattached.
2525
*/
26-
detach() { this._cd.mode = DETACHED; }
26+
detach(): void { this._cd.mode = DETACHED; }
2727

2828
/**
2929
* Reattach the change detector to the change detector tree.
@@ -32,7 +32,7 @@ export class ChangeDetectorRef {
3232
*checked during the
3333
* next change detection run.
3434
*/
35-
reattach() {
35+
reattach(): void {
3636
this._cd.mode = CHECK_ALWAYS;
3737
this.requestCheck();
3838
}

modules/angular2/src/change_detection/coalesce.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import {RecordType, ProtoRecord} from './proto_record';
1313
* replaced with very cheap SELF records.
1414
*/
1515
export function coalesce(records: List<ProtoRecord>): List<ProtoRecord> {
16-
var res = ListWrapper.create();
17-
var indexMap = MapWrapper.create();
16+
var res: List<ProtoRecord> = ListWrapper.create();
17+
var indexMap: Map<number, number> = MapWrapper.create();
1818

1919
for (var i = 0; i < records.length; ++i) {
2020
var r = records[i];

modules/angular2/src/change_detection/exceptions.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ export class ExpressionChangedAfterItHasBeenChecked extends BaseException {
1616

1717
export class ChangeDetectionError extends BaseException {
1818
message: string;
19-
originalException: any;
2019
location: string;
2120

22-
constructor(proto: ProtoRecord, originalException: any) {
21+
constructor(proto: ProtoRecord, public originalException: any) {
2322
super();
24-
this.originalException = originalException;
2523
this.location = proto.expressionAsString;
2624
this.message = `${this.originalException} in [${this.location}]`;
2725
}

modules/angular2/src/change_detection/interfaces.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ export class ChangeDetector {
4747
parent: ChangeDetector;
4848
mode: string;
4949

50-
addChild(cd: ChangeDetector) {}
51-
addShadowDomChild(cd: ChangeDetector) {}
52-
removeChild(cd: ChangeDetector) {}
53-
removeShadowDomChild(cd: ChangeDetector) {}
54-
remove() {}
55-
hydrate(context: any, locals: Locals, directives: any) {}
56-
dehydrate() {}
57-
markPathToRootAsCheckOnce() {}
50+
addChild(cd: ChangeDetector): void {}
51+
addShadowDomChild(cd: ChangeDetector): void {}
52+
removeChild(cd: ChangeDetector): void {}
53+
removeShadowDomChild(cd: ChangeDetector): void {}
54+
remove(): void {}
55+
hydrate(context: any, locals: Locals, directives: any): void {}
56+
dehydrate(): void {}
57+
markPathToRootAsCheckOnce(): void {}
5858

59-
detectChanges() {}
60-
checkNoChanges() {}
59+
detectChanges(): void {}
60+
checkNoChanges(): void {}
6161
}
6262

6363
export class ChangeDetectorDefinition {

modules/angular2/src/change_detection/parser/lexer.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import {
88
isPresent
99
} from "angular2/src/facade/lang";
1010

11-
export const TOKEN_TYPE_CHARACTER = 1;
12-
export const TOKEN_TYPE_IDENTIFIER = 2;
13-
export const TOKEN_TYPE_KEYWORD = 3;
14-
export const TOKEN_TYPE_STRING = 4;
15-
export const TOKEN_TYPE_OPERATOR = 5;
16-
export const TOKEN_TYPE_NUMBER = 6;
11+
const TOKEN_TYPE_CHARACTER = 1;
12+
const TOKEN_TYPE_IDENTIFIER = 2;
13+
const TOKEN_TYPE_KEYWORD = 3;
14+
const TOKEN_TYPE_STRING = 4;
15+
const TOKEN_TYPE_OPERATOR = 5;
16+
const TOKEN_TYPE_NUMBER = 6;
1717

1818
@Injectable()
1919
export class Lexer {
@@ -160,24 +160,18 @@ const $NBSP = 160;
160160

161161

162162
export class ScannerError extends BaseException {
163-
message: string;
164-
constructor(message) {
165-
super();
166-
this.message = message;
167-
}
163+
constructor(public message) { super(); }
168164

169-
toString() { return this.message; }
165+
toString(): string { return this.message; }
170166
}
171167

172168
class _Scanner {
173169
length: number;
174-
peek: number;
175-
index: number;
170+
peek: number = 0;
171+
index: number = -1;
176172

177173
constructor(public input: string) {
178174
this.length = input.length;
179-
this.peek = 0;
180-
this.index = -1;
181175
this.advance();
182176
}
183177

modules/angular2/src/change_detection/parser/parser.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ var INTERPOLATION_REGEXP = RegExpWrapper.create('\\{\\{(.*?)\\}\\}');
5555

5656
@Injectable()
5757
export class Parser {
58-
_lexer: Lexer;
5958
_reflector: Reflector;
60-
constructor(lexer: Lexer, providedReflector: Reflector = null) {
61-
this._lexer = lexer;
59+
60+
constructor(public _lexer: Lexer, providedReflector: Reflector = null) {
6261
this._reflector = isPresent(providedReflector) ? providedReflector : reflector;
6362
}
6463

@@ -116,11 +115,9 @@ export class Parser {
116115
}
117116

118117
class _ParseAST {
119-
index: int;
118+
index: int = 0;
120119
constructor(public input: string, public location: any, public tokens: List<any>,
121-
public reflector: Reflector, public parseAction: boolean) {
122-
this.index = 0;
123-
}
120+
public reflector: Reflector, public parseAction: boolean) {}
124121

125122
peek(offset: int): Token {
126123
var i = this.index + offset;

0 commit comments

Comments
 (0)