From eaa42cd17f2f88cc50e4bc09b4b0c49414984fb9 Mon Sep 17 00:00:00 2001 From: "William C. Johnson" Date: Mon, 25 Sep 2017 14:38:44 -0400 Subject: [PATCH] Comprehension fixes --- src/parser/expression.js | 6 +- .../object-comprehension/actual.js | 3 + .../object-comprehension/expected.json | 199 ++++++++++++++++++ .../expected.objectBlockAmbiguity.json | 168 +++++++++++++++ 4 files changed, 373 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/lightscript/object-block-ambiguity/object-comprehension/actual.js create mode 100644 test/fixtures/lightscript/object-block-ambiguity/object-comprehension/expected.json create mode 100644 test/fixtures/lightscript/object-block-ambiguity/object-comprehension/expected.objectBlockAmbiguity.json diff --git a/src/parser/expression.js b/src/parser/expression.js index f55e40c689..d43373bfe7 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -1100,10 +1100,10 @@ pp.parseObj = function (isPattern, refShorthandDefaultPos) { this.hasPlugin("enhancedComprehension") && (this.match(tt._for) || this.match(tt._case)) ) { - if (isPattern) { - this.unexpected(null, "Comprehensions are illegal in patterns."); - } if (this.lookahead().type !== tt.colon) { + if (isPattern) { + this.unexpected(null, "Comprehensions are illegal in patterns."); + } node.properties.push(this.parseSomeComprehension()); hasComprehension = true; continue; diff --git a/test/fixtures/lightscript/object-block-ambiguity/object-comprehension/actual.js b/test/fixtures/lightscript/object-block-ambiguity/object-comprehension/actual.js new file mode 100644 index 0000000000..497e7c4086 --- /dev/null +++ b/test/fixtures/lightscript/object-block-ambiguity/object-comprehension/actual.js @@ -0,0 +1,3 @@ +if true: { + for elem e in arr: e +} diff --git a/test/fixtures/lightscript/object-block-ambiguity/object-comprehension/expected.json b/test/fixtures/lightscript/object-block-ambiguity/object-comprehension/expected.json new file mode 100644 index 0000000000..814c1eb48e --- /dev/null +++ b/test/fixtures/lightscript/object-block-ambiguity/object-comprehension/expected.json @@ -0,0 +1,199 @@ +{ + "type": "File", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "IfStatement", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "test": { + "type": "BooleanLiteral", + "start": 3, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "value": true + }, + "consequent": { + "type": "BlockStatement", + "start": 7, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 9, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "expression": { + "type": "ObjectComprehension", + "start": 9, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "properties": [], + "loop": { + "type": "ForInArrayStatement", + "start": 13, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "elem": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + }, + "identifierName": "e" + }, + "name": "e" + }, + "body": { + "type": "ExpressionStatement", + "start": 32, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "expression": { + "type": "Identifier", + "start": 32, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + }, + "identifierName": "e" + }, + "name": "e" + } + }, + "array": { + "type": "Identifier", + "start": 27, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 19 + }, + "identifierName": "arr" + }, + "name": "arr" + } + } + } + } + ], + "directives": [], + "extra": { + "curly": false + } + }, + "alternate": null + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/lightscript/object-block-ambiguity/object-comprehension/expected.objectBlockAmbiguity.json b/test/fixtures/lightscript/object-block-ambiguity/object-comprehension/expected.objectBlockAmbiguity.json new file mode 100644 index 0000000000..f2b3febedc --- /dev/null +++ b/test/fixtures/lightscript/object-block-ambiguity/object-comprehension/expected.objectBlockAmbiguity.json @@ -0,0 +1,168 @@ +{ + "type": "File", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "IfStatement", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "test": { + "type": "BooleanLiteral", + "start": 3, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "value": true + }, + "consequent": { + "type": "BlockStatement", + "start": 9, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ForInArrayStatement", + "start": 13, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "elem": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + }, + "identifierName": "e" + }, + "name": "e" + }, + "body": { + "type": "ExpressionStatement", + "start": 32, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "expression": { + "type": "Identifier", + "start": 32, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + }, + "identifierName": "e" + }, + "name": "e" + } + }, + "array": { + "type": "Identifier", + "start": 27, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 19 + }, + "identifierName": "arr" + }, + "name": "arr" + } + } + ], + "directives": [], + "extra": { + "curly": true + } + }, + "alternate": null + } + ], + "directives": [] + } +} \ No newline at end of file