Skip to content

Commit bfa7faa

Browse files
committed
style: code cleanup
1 parent 613030a commit bfa7faa

File tree

4 files changed

+72
-67
lines changed

4 files changed

+72
-67
lines changed

lib/core/parser/syntax.dart

Lines changed: 8 additions & 8 deletions
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);
@@ -39,12 +39,12 @@ abstract class Expression {
3939
bool get isAssignable => false;
4040
bool get isChain => false;
4141

42-
eval(scope, [FilterMap filters = defaultFilterMap])
43-
=> throw new EvalError("Cannot evaluate $this");
44-
assign(scope, value)
45-
=> throw new EvalError("Cannot assign to $this");
46-
bind(context, [LocalsWrapper wrapper])
47-
=> new BoundExpression(this, context, wrapper);
42+
eval(scope, [FilterMap filters = defaultFilterMap]) =>
43+
throw new EvalError("Cannot evaluate $this");
44+
assign(scope, value) =>
45+
throw new EvalError("Cannot assign to $this");
46+
bind(context, [LocalsWrapper wrapper]) =>
47+
new BoundExpression(this, context, wrapper);
4848

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

lib/core_dom/view.dart

Lines changed: 4 additions & 4 deletions
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

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
part of angular.directive;
22

3-
class _Row {
4-
var 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;
@@ -97,56 +86,63 @@ class NgRepeatDirective {
9786
String _listExpr;
9887
Map<dynamic, _Row> _rows = {};
9988
Function _trackByIdFn = (key, value, index) => value;
100-
Watch _watch = null;
101-
Iterable _lastCollection;
89+
Watch _watch;
10290

103-
NgRepeatDirective(this._viewPort, this._boundViewFactory,
104-
this._scope, this._parser, this._astParser,
105-
this.filters);
91+
NgRepeatDirective(this._viewPort, this._boundViewFactory, this._scope,
92+
this._parser, this._astParser, this.filters);
10693

10794
set expression(value) {
10895
assert(value != null);
10996
_expression = value;
11097
if (_watch != null) _watch.remove();
98+
11199
Match match = _SYNTAX.firstMatch(_expression);
112100
if (match == null) {
113101
throw "[NgErr7] ngRepeat error! Expected expression in form of '_item_ "
114102
"in _collection_[ track by _id_]' but got '$_expression'.";
115103
}
104+
116105
_listExpr = match.group(2);
117-
var trackByExpr = match.group(4);
106+
107+
var trackByExpr = match.group(3);
118108
if (trackByExpr != null) {
119109
Expression trackBy = _parser(trackByExpr);
120110
_trackByIdFn = ((key, value, index) {
121111
final trackByLocals = <String, Object>{};
122112
if (_keyIdentifier != null) trackByLocals[_keyIdentifier] = key;
123-
trackByLocals
124-
..[_valueIdentifier] = value
125-
..[r'$index'] = index
126-
..[r'$id'] = (obj) => obj;
127-
return relaxFnArgs(trackBy.eval)(new ScopeLocals(_scope.context, trackByLocals));
113+
trackByLocals..[_valueIdentifier] = value
114+
..[r'$index'] = index
115+
..[r'$id'] = (obj) => obj;
116+
return relaxFnArgs(trackBy.eval)(new ScopeLocals(_scope.context,
117+
trackByLocals));
128118
});
129119
}
120+
130121
var assignExpr = match.group(1);
131122
match = _LHS_SYNTAX.firstMatch(assignExpr);
132123
if (match == null) {
133124
throw "[NgErr8] ngRepeat error! '_item_' in '_item_ in _collection_' "
134125
"should be an identifier or '(_key_, _value_)' expression, but got "
135126
"'$assignExpr'.";
136127
}
128+
137129
_valueIdentifier = match.group(3);
138130
if (_valueIdentifier == null) _valueIdentifier = match.group(1);
139131
_keyIdentifier = match.group(2);
140132

141133
_watch = _scope.watch(
142134
_astParser(_listExpr, collection: true, filters: filters),
143-
(CollectionChangeRecord collection, _) {
135+
(CollectionChangeRecord changeRecord, _) {
144136
//TODO(misko): we should take advantage of the CollectionChangeRecord!
145-
_onCollectionChange(collection == null ? [] : collection.iterable);
137+
if (changeRecord == null) return;
138+
_onCollectionChange(changeRecord.iterable);
139+
146140
}
147141
);
148142
}
149143

144+
145+
// todo -> collection
150146
List<_Row> _computeNewRows(Iterable collection, trackById) {
151147
final newRowOrder = new List<_Row>(collection.length);
152148
// Same as lastViewMap but it has the current state. It will become the
@@ -157,8 +153,7 @@ class NgRepeatDirective {
157153
var value = collection.elementAt(index);
158154
trackById = _trackByIdFn(index, value, index);
159155
if (_rows.containsKey(trackById)) {
160-
var row = _rows[trackById];
161-
_rows.remove(trackById);
156+
var row = _rows.remove(trackById);
162157
newRows[trackById] = row;
163158
newRowOrder[index] = row;
164159
} else if (newRows.containsKey(trackById)) {
@@ -185,9 +180,9 @@ class NgRepeatDirective {
185180
return newRowOrder;
186181
}
187182

188-
_onCollectionChange(Iterable collection) {
189-
dom.Node previousNode = _viewPort.placeholder; // current position of the
190-
// node
183+
void _onCollectionChange(Iterable collection) {
184+
// current position of the node
185+
dom.Node previousNode = _viewPort.placeholder;
191186
dom.Node nextNode;
192187
Scope childScope;
193188
Map childContext;
@@ -218,33 +213,44 @@ class NgRepeatDirective {
218213
previousNode = row.endNode;
219214
} else {
220215
// new item which we don't know about
221-
childScope = _scope.createChild(childContext = new PrototypeMap(_scope.context));
216+
childScope = _scope.createChild(childContext =
217+
new PrototypeMap(_scope.context));
222218
}
223219

224220
if (!identical(childScope.context[_valueIdentifier], value)) {
225221
childContext[_valueIdentifier] = value;
226222
}
227223
var first = (index == 0);
228224
var last = (index == collection.length - 1);
229-
childContext
230-
..[r'$index'] = index
231-
..[r'$first'] = first
232-
..[r'$last'] = last
233-
..[r'$middle'] = !first && !last
234-
..[r'$odd'] = index & 1 == 1
235-
..[r'$even'] = index & 1 == 0;
225+
childContext..[r'$index'] = index
226+
..[r'$first'] = first
227+
..[r'$last'] = last
228+
..[r'$middle'] = !first && !last
229+
..[r'$odd'] = index & 1 == 1
230+
..[r'$even'] = index & 1 == 0;
236231

237232
if (row.startNode == null) {
238233
var view = _boundViewFactory(childScope);
239234
_rows[row.id] = row
240235
..view = view
241236
..scope = childScope
242237
..nodes = view.nodes
243-
..startNode = row.nodes[0]
244-
..endNode = row.nodes[row.nodes.length - 1];
238+
..startNode = row.nodes.first
239+
..endNode = row.nodes.last;
245240
_viewPort.insert(view, insertAfter: cursor);
246241
}
247242
cursor = row.view;
248243
}
249244
}
250245
}
246+
247+
class _Row {
248+
final id;
249+
Scope scope;
250+
View view;
251+
dom.Element startNode;
252+
dom.Element endNode;
253+
List<dom.Element> nodes;
254+
255+
_Row(this.id);
256+
}

test/change_detection/dirty_checking_change_detector_spec.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ void main() {
2727
var user = new _User('', '');
2828
Iterator changeIterator;
2929

30-
detector
31-
..watch(user, 'first', null)
32-
..watch(user, 'last', null)
33-
..collectChanges(); // throw away first set
30+
detector..watch(user, 'first', null)
31+
..watch(user, 'last', null)
32+
..collectChanges(); // throw away first set
3433

3534
changeIterator = detector.collectChanges();
3635
expect(changeIterator.moveNext()).toEqual(false);

0 commit comments

Comments
 (0)