Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 347e832

Browse files
vicbmhevery
authored andcommitted
style: code cleanup
1 parent 84781ef commit 347e832

File tree

4 files changed

+71
-65
lines changed

4 files changed

+71
-65
lines changed

lib/core/parser/syntax.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ abstract class Visitor {
2828
visitPrefix(Prefix expression) => visitExpression(expression);
2929

3030
visitLiteral(Literal expression) => visitExpression(expression);
31-
visitLiteralPrimitive(LiteralPrimitive expression)
32-
=> visitLiteral(expression);
31+
visitLiteralPrimitive(LiteralPrimitive expression) =>
32+
visitLiteral(expression);
3333
visitLiteralString(LiteralString expression) => visitLiteral(expression);
3434
visitLiteralArray(LiteralArray expression) => visitLiteral(expression);
3535
visitLiteralObject(LiteralObject expression) => visitLiteral(expression);
@@ -42,9 +42,9 @@ abstract class Expression {
4242
eval(scope, [FilterMap filters = defaultFilterMap]) =>
4343
throw new EvalError("Cannot evaluate $this");
4444
assign(scope, value) =>
45-
throw new EvalError("Cannot assign to $this");
45+
throw new EvalError("Cannot assign to $this");
4646
bind(context, [LocalsWrapper wrapper]) =>
47-
new BoundExpression(this, context, wrapper);
47+
new BoundExpression(this, context, wrapper);
4848

4949
accept(Visitor visitor);
5050
String toString() => Unparser.unparse(this);

lib/core_dom/view.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class View {
2929
class ViewPort {
3030
final dom.Node placeholder;
3131
final NgAnimate _animate;
32-
final List<View> _views = <View>[];
32+
final _views = <View>[];
3333

3434
ViewPort(this.placeholder, this._animate);
3535

@@ -56,12 +56,12 @@ class ViewPort {
5656
}
5757

5858
void _viewsInsertAfter(View view, View insertAfter) {
59-
int index = (insertAfter != null) ? _views.indexOf(insertAfter) : -1;
60-
_views.insert(index + 1, view);
59+
int index = insertAfter == null ? 0 : _views.indexOf(insertAfter) + 1;
60+
_views.insert(index, view);
6161
}
6262

6363
dom.Node _lastNode(View insertAfter) =>
6464
insertAfter == null
6565
? placeholder
66-
: insertAfter.nodes[insertAfter.nodes.length - 1];
66+
: insertAfter.nodes.last;
6767
}

lib/directive/ng_repeat.dart

+60-53
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
part of angular.directive;
22

3-
class _Row {
4-
final id;
5-
Scope scope;
6-
View view;
7-
dom.Element startNode;
8-
dom.Element endNode;
9-
List<dom.Element> nodes;
10-
11-
_Row(this.id);
12-
}
13-
143
/**
154
* The `ngRepeat` directive instantiates a template once per item from a
165
* collection. Each template instance gets its own scope, where the given loop
@@ -20,15 +9,15 @@ class _Row {
209
* Special properties are exposed on the local scope of each template instance,
2110
* including:
2211
*
23-
* <table>
24-
* <tr><th> Variable </th><th> Type </th><th> Details <th></tr>
25-
* <tr><td> `$index` </td><td>[num] </td><td> iterator offset of the repeated element (0..length-1) <td></tr>
26-
* <tr><td> `$first` </td><td>[bool]</td><td> true if the repeated element is first in the iterator. <td></tr>
27-
* <tr><td> `$middle` </td><td>[bool]</td><td> true if the repeated element is between the first and last in the iterator. <td></tr>
28-
* <tr><td> `$last` </td><td>[bool]</td><td> true if the repeated element is last in the iterator. <td></tr>
29-
* <tr><td> `$even` </td><td>[bool]</td><td> true if the iterator position `$index` is even (otherwise false). <td></tr>
30-
* <tr><td> `$odd` </td><td>[bool]</td><td> true if the iterator position `$index` is odd (otherwise false). <td></tr>
31-
* </table>
12+
* * `$index` ([:num:]) the iterator offset of the repeated element
13+
* (0..length-1)
14+
* * `$first` ([:bool:]) whether the repeated element is first in the
15+
* iterator.
16+
* * `$middle` ([:bool:]) whether the repeated element is between the first
17+
* and last in the iterator.
18+
* * `$last` ([:bool:]) whether the repeated element is last in the iterator.
19+
* * `$even` ([:bool:]) whether the iterator position `$index` is even.
20+
* * `$odd` ([:bool:]) whether the iterator position `$index` is odd.
3221
*
3322
*
3423
* [repeat_expression] ngRepeat The expression indicating how to enumerate a
@@ -57,7 +46,7 @@ class _Row {
5746
* function can be used to assign a unique `$$hashKey` property to each item
5847
* in the array. This property is then used as a key to associated DOM
5948
* elements with the corresponding item in the array by identity. Moving the
60-
* same object in array would move the DOM element in the same way ian the
49+
* same object in array would move the DOM element in the same way in the
6150
* DOM.
6251
*
6352
* For example: `item in items track by item.id` is a typical pattern when
@@ -81,8 +70,8 @@ class _Row {
8170
selector: '[ng-repeat]',
8271
map: const {'.': '@expression'})
8372
class NgRepeatDirective {
84-
static RegExp _SYNTAX = new RegExp(r'^\s*(.+)\s+in\s+(.*?)\s*(\s+track\s+by\s+(.+)\s*)?(\s+lazily\s*)?$');
85-
static RegExp _LHS_SYNTAX = new RegExp(r'^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$');
73+
static RegExp _SYNTAX = new RegExp(r'^\s*(.+)\s+in\s+(.*?)\s*(?:track\s+by\s+(.+)\s*)?(\s+lazily\s*)?$');
74+
static RegExp _LHS_SYNTAX = new RegExp(r'^(?:([$\w]+)|\(([$\w]+)\s*,\s*([$\w]+)\))$');
8675

8776
final ViewPort _viewPort;
8877
final BoundViewFactory _boundViewFactory;
@@ -96,55 +85,63 @@ class NgRepeatDirective {
9685
String _listExpr;
9786
Map<dynamic, _Row> _rows = {};
9887
Function _trackByIdFn = (key, value, index) => value;
99-
Watch _watch = null;
100-
Iterable _lastCollection;
88+
Watch _watch;
10189

10290
NgRepeatDirective(this._viewPort, this._boundViewFactory, this._scope,
103-
this._parser, this.filters);
91+
this._parser, this._astParser, this.filters);
10492

10593
set expression(value) {
10694
assert(value != null);
10795
_expression = value;
10896
if (_watch != null) _watch.remove();
97+
10998
Match match = _SYNTAX.firstMatch(_expression);
11099
if (match == null) {
111100
throw "[NgErr7] ngRepeat error! Expected expression in form of '_item_ "
112101
"in _collection_[ track by _id_]' but got '$_expression'.";
113102
}
103+
114104
_listExpr = match.group(2);
115-
var trackByExpr = match.group(4);
105+
106+
var trackByExpr = match.group(3);
116107
if (trackByExpr != null) {
117108
Expression trackBy = _parser(trackByExpr);
118109
_trackByIdFn = ((key, value, index) {
119110
final trackByLocals = <String, Object>{};
120111
if (_keyIdentifier != null) trackByLocals[_keyIdentifier] = key;
121-
trackByLocals
122-
..[_valueIdentifier] = value
123-
..[r'$index'] = index
124-
..[r'$id'] = (obj) => obj;
125-
return relaxFnArgs(trackBy.eval)(new ScopeLocals(_scope.context, trackByLocals));
112+
trackByLocals..[_valueIdentifier] = value
113+
..[r'$index'] = index
114+
..[r'$id'] = (obj) => obj;
115+
return relaxFnArgs(trackBy.eval)(new ScopeLocals(_scope.context,
116+
trackByLocals));
126117
});
127118
}
119+
128120
var assignExpr = match.group(1);
129121
match = _LHS_SYNTAX.firstMatch(assignExpr);
130122
if (match == null) {
131123
throw "[NgErr8] ngRepeat error! '_item_' in '_item_ in _collection_' "
132124
"should be an identifier or '(_key_, _value_)' expression, but got "
133125
"'$assignExpr'.";
134126
}
127+
135128
_valueIdentifier = match.group(3);
136129
if (_valueIdentifier == null) _valueIdentifier = match.group(1);
137130
_keyIdentifier = match.group(2);
138131

132+
_watch = _scope.watch(
133+
_astParser(_listExpr, collection: true, filters: filters),
134+
(CollectionChangeRecord changeRecord, _) {
135+
//TODO(misko): we should take advantage of the CollectionChangeRecord!
136+
if (changeRecord == null) return;
137+
_onCollectionChange(changeRecord.iterable);
139138

140-
_watch = _scope.watch(_listExpr, (CollectionChangeRecord collection, _) {
141-
//TODO(misko): we should take advantage of the CollectionChangeRecord!
142-
_onCollectionChange(collection == null ? [] : collection.iterable);
143-
},
144-
collection: true,
145-
filters: filters);
139+
}
140+
);
146141
}
147142

143+
144+
// todo -> collection
148145
List<_Row> _computeNewRows(Iterable collection, trackById) {
149146
final newRowOrder = new List<_Row>(collection.length);
150147
// Same as lastViewMap but it has the current state. It will become the
@@ -155,8 +152,7 @@ class NgRepeatDirective {
155152
var value = collection.elementAt(index);
156153
trackById = _trackByIdFn(index, value, index);
157154
if (_rows.containsKey(trackById)) {
158-
var row = _rows[trackById];
159-
_rows.remove(trackById);
155+
var row = _rows.remove(trackById);
160156
newRows[trackById] = row;
161157
newRowOrder[index] = row;
162158
} else if (newRows.containsKey(trackById)) {
@@ -183,9 +179,9 @@ class NgRepeatDirective {
183179
return newRowOrder;
184180
}
185181

186-
_onCollectionChange(Iterable collection) {
187-
dom.Node previousNode = _viewPort.placeholder; // current position of the
188-
// node
182+
void _onCollectionChange(Iterable collection) {
183+
// current position of the node
184+
dom.Node previousNode = _viewPort.placeholder;
189185
dom.Node nextNode;
190186
Scope childScope;
191187
Map childContext;
@@ -216,33 +212,44 @@ class NgRepeatDirective {
216212
previousNode = row.endNode;
217213
} else {
218214
// new item which we don't know about
219-
childScope = _scope.createChild(childContext = new PrototypeMap(_scope.context));
215+
childScope = _scope.createChild(childContext =
216+
new PrototypeMap(_scope.context));
220217
}
221218

222219
if (!identical(childScope.context[_valueIdentifier], value)) {
223220
childContext[_valueIdentifier] = value;
224221
}
225222
var first = (index == 0);
226223
var last = (index == collection.length - 1);
227-
childContext
228-
..[r'$index'] = index
229-
..[r'$first'] = first
230-
..[r'$last'] = last
231-
..[r'$middle'] = !first && !last
232-
..[r'$odd'] = index & 1 == 1
233-
..[r'$even'] = index & 1 == 0;
224+
childContext..[r'$index'] = index
225+
..[r'$first'] = first
226+
..[r'$last'] = last
227+
..[r'$middle'] = !first && !last
228+
..[r'$odd'] = index & 1 == 1
229+
..[r'$even'] = index & 1 == 0;
234230

235231
if (row.startNode == null) {
236232
var view = _boundViewFactory(childScope);
237233
_rows[row.id] = row
238234
..view = view
239235
..scope = childScope
240236
..nodes = view.nodes
241-
..startNode = row.nodes[0]
242-
..endNode = row.nodes[row.nodes.length - 1];
237+
..startNode = row.nodes.first
238+
..endNode = row.nodes.last;
243239
_viewPort.insert(view, insertAfter: cursor);
244240
}
245241
cursor = row.view;
246242
}
247243
}
248244
}
245+
246+
class _Row {
247+
final id;
248+
Scope scope;
249+
View view;
250+
dom.Element startNode;
251+
dom.Element endNode;
252+
List<dom.Element> nodes;
253+
254+
_Row(this.id);
255+
}

test/change_detection/dirty_checking_change_detector_spec.dart

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ void main() {
2929
var user = new _User('', '');
3030
Iterator changeIterator;
3131

32-
detector
33-
..watch(user, 'first', null)
34-
..watch(user, 'last', null)
35-
..collectChanges(); // throw away first set
32+
detector..watch(user, 'first', null)
33+
..watch(user, 'last', null)
34+
..collectChanges(); // throw away first set
3635

3736
changeIterator = detector.collectChanges();
3837
expect(changeIterator.moveNext()).toEqual(false);

0 commit comments

Comments
 (0)