Skip to content

Commit

Permalink
Throw on empty comprehensions
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed Sep 28, 2017
1 parent 8b37b95 commit d809a5e
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/comprehension.lsc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ isSimpleObject(objExpr) ->
retailObject(info, path, id, transformPathName, returnPathName) ->
transformPath = path.get(transformPathName)
validateComprehensionBody(transformPath)
let foundOne = false
transformTails(
transformPath
true
Expand All @@ -59,6 +60,7 @@ retailObject(info, path, id, transformPathName, returnPathName) ->
throw tailPath.buildCodeFrameError("Object comprehensions must end" +
" with a (key, value) pair.")

now foundOne = true
[ keyExpr, valExpr ] = expr.expressions

return t.assignmentExpression("=",
Expand All @@ -69,6 +71,8 @@ retailObject(info, path, id, transformPathName, returnPathName) ->
if not expr~isa("ObjectExpression"):
throw tailPath.buildCodeFrameError("Object comprehensions must end with an object expression.")

now foundOne = true

if expr~isSimpleObject():
// Simple object case: { [k]: v } --> obj[k] = v
{ properties: [prop] } = expr
Expand All @@ -87,16 +91,21 @@ retailObject(info, path, id, transformPathName, returnPathName) ->
)~atNode(expr)
)

if not foundOne:
throw path.buildCodeFrameError("Object comprehensions must end with an object expression.")

path.get(returnPathName).node

retailArray(info, path, id, transformPathName, returnPathName) ->
transformPath = path.get(transformPathName)
validateComprehensionBody(transformPath)
let foundOne = false
transformTails(
transformPath
true
false
(expr) ->
now foundOne = true
t.callExpression(
t.memberExpression(id, t.identifier("push")~atNode(expr))~atNode(expr)
[expr]
Expand All @@ -117,6 +126,9 @@ retailArray(info, path, id, transformPathName, returnPathName) ->
// )~atNode(expr)
)

if not foundOne:
throw path.buildCodeFrameError("Array comprehensions must end with an expression.")

path.get(returnPathName).node

transformLoop(info, path, ref, stmts) ->
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/enhanced-comprehensions/object-assign/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{...for elem e in arr:
{ [e]: e, [e+1]: e+1 }
}
5 changes: 5 additions & 0 deletions test/fixtures/enhanced-comprehensions/object-assign/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
obj = {...for let i=0; i<2; i++:
{ [`${i}.0`]: i, [`${i}.1`]: i+1 }
}

assert.deepEqual(obj, { "0.0": 0, "0.1": 1, "1.0": 1, "1.1": 2 })
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(() => {
const _obj = {};for (let _i = 0, _len = arr.length; _i < _len; _i++) {
const e = arr[_i];
Object.assign(_obj, { [e]: e, [e + 1]: e + 1 });
}return _obj;
})();
7 changes: 7 additions & 0 deletions test/fixtures/enhanced-comprehensions/object-basic/actual.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
{ ...for elem x in arr: ({a: x}) }

{ ...for elem x in arr: ({[x]: f(x)}) }

{
a: b
...if c: ({d: e})
}
15 changes: 14 additions & 1 deletion test/fixtures/enhanced-comprehensions/object-basic/expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
(() => {
const _obj = {};for (let _i = 0, _len = arr.length; _i < _len; _i++) {
const x = arr[_i];_obj[x] = f(x);
const x = arr[_i];_obj.a = x;
}return _obj;
})();

(() => {
const _obj2 = {};for (let _i2 = 0, _len2 = arr.length; _i2 < _len2; _i2++) {
const x = arr[_i2];_obj2[x] = f(x);
}return _obj2;
})();

(() => {
const _obj3 = {
a: b
};if (c) _obj3.d = e;
return _obj3;
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ ...if true: { k: v } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Object comprehensions must end with an object expression."
}

0 comments on commit d809a5e

Please sign in to comment.