Skip to content

Commit

Permalink
whiteblockPreferred updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed Oct 1, 2017
1 parent 5e45d5e commit d4184a6
Show file tree
Hide file tree
Showing 37 changed files with 1,132 additions and 83 deletions.
40 changes: 36 additions & 4 deletions src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ pp.parseStatement = function (declaration, topLevel) {

const starttype = this.state.type;
const node = this.startNode();
let expr = null;
let objParseError = null;

const isBlock = this.state.nextBraceIsBlock;
if (this.state.nextBraceIsBlock !== undefined) delete this.state.nextBraceIsBlock;

// Most types of statements are recognized by the keyword they
// start with. Many are trivial to parse, some require a bit of
Expand Down Expand Up @@ -94,10 +99,32 @@ pp.parseStatement = function (declaration, topLevel) {
case tt._while: return this.parseWhileStatement(node);
case tt._with: return this.parseWithStatement(node);
case tt.braceL:
// in lightscript, allow line-starting `{` to be parsed as obj or obj pattern
if (this.hasPlugin("lightscript") && this.isLineBreak()) {
if (this.hasPlugin("whiteblockOnly")) {
// whiteblockOnly = what follows must be an ObjectExpression/Pattern
break;
} else if (this.hasPlugin("whiteblockOnly")) {
} else if (this.hasPlugin("whiteblockPreferred") && !isBlock) {
// whiteblockPreferred = try to parse as obj, otherwise rewind and parse as block
const state = this.state.clone();
try {
expr = this.parseExpression();
break;
} catch (err) {
this.state = state;
objParseError = err;
}

try {
return this.parseBlock();
} catch (err) {
if (objParseError && objParseError.pos >= err.pos) {
throw objParseError;
} else {
throw err;
}
}
} else if (this.hasPlugin("lightscript") && this.isLineBreak() && !isBlock) {
// legacy lsc behavior
// allow line-starting `{` to be parsed as obj or obj pattern
break;
} else {
return this.parseBlock();
Expand Down Expand Up @@ -141,7 +168,7 @@ pp.parseStatement = function (declaration, topLevel) {
// next token is a colon and the expression was a simple
// Identifier node, we switch to interpreting it as a label.
const maybeName = this.state.value;
const expr = this.parseExpression();
if (!expr) expr = this.parseExpression();

// rewrite `x = val` and `x: type = val`
if (this.hasPlugin("lightscript") && this.isColonConstAssign(expr)) {
Expand Down Expand Up @@ -238,6 +265,8 @@ pp.parseDoStatement = function (node) {
isWhiteBlock = true;
indentLevel = this.state.indentLevel;
}

this.state.nextBraceIsBlock = true;
node.body = this.parseStatement(false);
this.state.labels.pop();
if (this.hasPlugin("lightscript") && isWhiteBlock && this.state.indentLevel !== indentLevel) {
Expand Down Expand Up @@ -530,6 +559,7 @@ pp.parseWhileStatement = function (node) {
this.next();
node.test = this.parseParenExpression();
this.state.labels.push(loopLabel);
this.state.nextBraceIsBlock = true;
node.body = this.parseStatement(false);
this.state.labels.pop();
return this.finishNode(node, "WhileStatement");
Expand Down Expand Up @@ -673,6 +703,7 @@ pp.parseFor = function (node, init) {
this.expect(tt.parenR);
}

this.state.nextBraceIsBlock = true;
node.body = this.parseStatement(false);
this.state.labels.pop();
return this.finishNode(node, "ForStatement");
Expand All @@ -699,6 +730,7 @@ pp.parseForIn = function (node, init, forAwait) {
this.expect(tt.parenR);
}

this.state.nextBraceIsBlock = true;
node.body = this.parseStatement(false);
this.state.labels.pop();
return this.finishNode(node, type);
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/lightscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pp.parseEnhancedForIn = function (node) {
const iterable = this.parseMaybeAssign(true);

this.expectParenFreeBlockStart(node);
this.state.nextBraceIsBlock = true;
node.body = this.parseStatement(false);

if ((matchingIterationType === "idx") || (matchingIterationType === "elem")) {
Expand Down Expand Up @@ -460,6 +461,7 @@ pp.parseIf = function (node, isExpression, requireColon = null) {
this.state.inFunction = oldInFunction;
this.state.labels = oldLabels;
} else {
this.state.nextBraceIsBlock = true;
node.consequent = this.parseStatement(false);
}

Expand Down Expand Up @@ -510,6 +512,7 @@ pp.parseIfAlternate = function (node, isExpression, ifIsWhiteBlock, ifIndentLeve
}
}

this.state.nextBraceIsBlock = true;
return this.parseStatement(false);
}

Expand Down Expand Up @@ -689,6 +692,7 @@ export default function (instance) {
instance.extend("parseStatement", function (inner) {
return function () {
if (this.match(tt.colon)) {
delete this.state.nextBraceIsBlock;
return this.parseWhiteBlock();
}
return inner.apply(this, arguments);
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/core/uncategorised/214/options.lightscript.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/core/uncategorised/235/options.lightscript.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/core/uncategorised/300/options.lightscript.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/core/uncategorised/301/options.lightscript.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/core/uncategorised/303/options.lightscript.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/core/uncategorised/313/options.lightscript.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/core/uncategorised/314/options.lightscript.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/core/uncategorised/315/options.lightscript.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/core/uncategorised/332/options.lightscript.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/core/uncategorised/336/options.lightscript.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token, expected , (1:5)"
"throws": "Unexpected token, expected ; (1:23)"
}
Loading

0 comments on commit d4184a6

Please sign in to comment.