Skip to content

Commit 7908533

Browse files
committed
refactor(Parser): cleanup
1 parent 693489c commit 7908533

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

modules/change_detection/src/parser/ast.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {FIELD, toBool, autoConvertAdd, isBlank, FunctionWrapper, BaseException} from "facade/lang";
1+
import {FIELD, autoConvertAdd, isBlank, isPresent, FunctionWrapper, BaseException} from "facade/lang";
22
import {List, Map, ListWrapper, MapWrapper} from "facade/collection";
33
import {ClosureMap} from "./closure_map";
44

@@ -29,6 +29,9 @@ export class ImplicitReceiver extends AST {
2929
}
3030
}
3131

32+
/**
33+
* Multiple expressions separated by a semicolon.
34+
*/
3235
export class Chain extends AST {
3336
@FIELD('final expressions:List')
3437
constructor(expressions:List) {
@@ -39,7 +42,7 @@ export class Chain extends AST {
3942
var result;
4043
for (var i = 0; i < this.expressions.length; i++) {
4144
var last = this.expressions[i].eval(context);
42-
if (last != null) result = last;
45+
if (isPresent(last)) result = last;
4346
}
4447
return result;
4548
}
@@ -118,7 +121,7 @@ export class KeyedAccess extends AST {
118121
} else if (obj instanceof List) {
119122
return ListWrapper.get(obj, key);
120123
} else {
121-
throw new BaseException(`Cannot access ${key} on ${obj}`);
124+
return obj[key];
122125
}
123126
}
124127

@@ -135,7 +138,7 @@ export class KeyedAccess extends AST {
135138
} else if (obj instanceof List) {
136139
ListWrapper.set(obj, key, value);
137140
} else {
138-
throw new BaseException(`Cannot access ${key} on ${obj}`);
141+
obj[key] = value;
139142
}
140143
return value;
141144
}
@@ -225,14 +228,14 @@ export class Binary extends AST {
225228
eval(context) {
226229
var left = this.left.eval(context);
227230
switch (this.operation) {
228-
case '&&': return toBool(left) && toBool(this.right.eval(context));
229-
case '||': return toBool(left) || toBool(this.right.eval(context));
231+
case '&&': return left && this.right.eval(context);
232+
case '||': return left || this.right.eval(context);
230233
}
231234
var right = this.right.eval(context);
232235

233236
// Null check for the operations.
234-
if (left == null || right == null) {
235-
throw new BaseException("One of the operands is null");
237+
if (isBlank(left)|| isBlank(right)) {
238+
throw new BaseException("One of the operands is not defined");
236239
}
237240

238241
switch (this.operation) {
@@ -266,7 +269,7 @@ export class PrefixNot extends AST {
266269
}
267270

268271
eval(context) {
269-
return !toBool(this.expression.eval(context));
272+
return !this.expression.eval(context);
270273
}
271274

272275
visit(visitor) {

modules/change_detection/src/parser/closure_map.es6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export class ClosureMap {
1010
}
1111

1212
fn(name:string) {
13-
return new Function('o', 'pos', 'return o.' + name + '.apply(o, pos);');
13+
return new Function('o', 'args', 'return o.' + name + '.apply(o, args);');
1414
}
1515
}

modules/change_detection/test/parser/parser_spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,11 @@ export function main() {
209209

210210
it('should evaluate map', () => {
211211
expectEval("{}").toEqual(MapWrapper.create());
212-
expectEval("{a:'b'}").toEqual(MapWrapper.createFromPairs([["a", "b"]]));
213-
expectEval("{'a':'b'}").toEqual(MapWrapper.createFromPairs([["a", "b"]]));
214-
expectEval("{\"a\":'b'}").toEqual(MapWrapper.createFromPairs([["a", "b"]]));
212+
expectEval("{a:'b'}['a']").toEqual('b');
213+
expectEval("{'a':'b'}['a']").toEqual('b');
214+
expectEval("{\"a\":'b'}['a']").toEqual('b');
215215
expectEval("{\"a\":'b'}['a']").toEqual("b");
216+
expectEval("{}['a']").not.toBeDefined();
216217
expectEval("{\"a\":'b'}['invalid']").not.toBeDefined();
217218
});
218219

modules/facade/src/collection.es6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export var Set = window.Set;
66

77
export class MapWrapper {
88
static create():Map { return new Map(); }
9-
static createFromPairs(pairs:List):Map { return new Map(pairs); }
9+
static createFromPairs(pairs:List):Map {return new Map(pairs);}
1010
static get(m, k) { return m.get(k); }
1111
static set(m, k, v) { m.set(k,v); }
1212
static contains(m, k) { return m.has(k); }

0 commit comments

Comments
 (0)