Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
return isSafe ? this.parseSafeAwait(node) : this.parseAwait(node);
}

case tt._else:
case tt._else: case tt._elif:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if (this.hasPlugin("lightscript")) {
this.unexpected(null, "Unmatched `else` (must match indentation of the line with `if`).");
}
Expand Down
37 changes: 30 additions & 7 deletions src/plugins/lightscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,17 +386,32 @@ pp.parseNamedArrowFromCallExpression = function (node, call) {
return this.finishNode(node, isMember ? "NamedArrowMemberExpression" : "NamedArrowExpression");
};

pp.pushBlockState = function (blockType, indentLevel) {
this.state.blockStack.push({ blockType, indentLevel });
};

pp.matchBlockState = function(blockType, indentLevel) {
return this.state.blockStack.some( (x) => x.blockType === blockType && x.indentLevel === indentLevel );
};

pp.popBlockState = function() {
this.state.blockStack.pop();
};

// c/p parseIfStatement

pp.parseIf = function (node, isExpression) {
const indentLevel = this.state.indentLevel;
this.next();
node.test = this.parseParenExpression();

const isColon = this.match(tt.colon);
if (isColon) this.pushBlockState("if", indentLevel);

if (isExpression) {
if (this.match(tt.braceL)) {
node.consequent = this.parseBlock(false);
} else if (!this.match(tt.colon)) {
} else if (!isColon) {
node.consequent = this.parseMaybeAssign();
} else {
node.consequent = this.parseWhiteBlock(false, true);
Expand All @@ -405,16 +420,24 @@ pp.parseIf = function (node, isExpression) {
node.consequent = this.parseStatement(false);
}

node.alternate = this.parseIfAlternate(node, isExpression, indentLevel);
node.alternate = this.parseIfAlternate(node, isExpression, isColon, indentLevel);

if (isColon) this.popBlockState();

return this.finishNode(node, isExpression ? "IfExpression" : "IfStatement");
};

pp.parseIfAlternate = function (node, isExpression, indentLevel) {
const isColon = node.consequent.extra && node.consequent.extra.curly === false;

// if whitespace-block, must match indent level
if (isColon && this.state.indentLevel !== indentLevel) {
pp.parseIfAlternate = function (node, isExpression, ifIsWhiteBlock, ifIndentLevel) {
if (!this.match(tt._elif) && !this.match(tt._else)) return null;

// If the indent level here doesn't match with the current whiteblock `if`, or
// it matches with a whiteblock `if` higher on the stack, then this alternate
// clause does not match the current `if` -- so unwind the recursive descent.
const alternateIndentLevel = this.state.indentLevel;
if (
(alternateIndentLevel !== ifIndentLevel) &&
(ifIsWhiteBlock || this.matchBlockState("if", alternateIndentLevel))
) {
return null;
}

Expand Down
2 changes: 2 additions & 0 deletions src/tokenizer/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export default class State {
this.leadingComments = [];
this.commentStack = [];

this.blockStack = [];

this.pos = this.lineStart = 0;
this.curLine = options.startLine;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = if false: 1
elif true:
2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Unmatched `else` (must match indentation of the line with `if`). (2:2)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
x = if false: 1
x =
if false: 1
elif true:
2
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"type": "File",
"start": 0,
"end": 34,
"end": 36,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"line": 4,
"column": 5
}
},
"program": {
"type": "Program",
"start": 0,
"end": 34,
"end": 36,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"line": 4,
"column": 5
}
},
Expand All @@ -31,30 +31,33 @@
{
"type": "VariableDeclaration",
"start": 0,
"end": 34,
"end": 36,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"line": 4,
"column": 5
}
},
"kind": "const",
"extra": {
"implicit": true
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 0,
"end": 34,
"end": 36,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"line": 4,
"column": 5
}
},
Expand All @@ -77,46 +80,46 @@
},
"init": {
"type": "IfExpression",
"start": 4,
"end": 34,
"start": 6,
"end": 36,
"loc": {
"start": {
"line": 1,
"column": 4
"line": 2,
"column": 2
},
"end": {
"line": 3,
"line": 4,
"column": 5
}
},
"test": {
"type": "BooleanLiteral",
"start": 7,
"end": 12,
"start": 9,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 7
"line": 2,
"column": 5
},
"end": {
"line": 1,
"column": 12
"line": 2,
"column": 10
}
},
"value": false
},
"consequent": {
"type": "NumericLiteral",
"start": 14,
"end": 15,
"start": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 14
"line": 2,
"column": 12
},
"end": {
"line": 1,
"column": 15
"line": 2,
"column": 13
}
},
"extra": {
Expand All @@ -127,74 +130,74 @@
},
"alternate": {
"type": "IfExpression",
"start": 18,
"end": 34,
"start": 20,
"end": 36,
"loc": {
"start": {
"line": 2,
"line": 3,
"column": 2
},
"end": {
"line": 3,
"line": 4,
"column": 5
}
},
"test": {
"type": "BooleanLiteral",
"start": 23,
"end": 27,
"start": 25,
"end": 29,
"loc": {
"start": {
"line": 2,
"line": 3,
"column": 7
},
"end": {
"line": 2,
"line": 3,
"column": 11
}
},
"value": true
},
"consequent": {
"type": "BlockStatement",
"start": 27,
"end": 34,
"start": 29,
"end": 36,
"loc": {
"start": {
"line": 2,
"line": 3,
"column": 11
},
"end": {
"line": 3,
"line": 4,
"column": 5
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 33,
"end": 34,
"start": 35,
"end": 36,
"loc": {
"start": {
"line": 3,
"line": 4,
"column": 4
},
"end": {
"line": 3,
"line": 4,
"column": 5
}
},
"expression": {
"type": "NumericLiteral",
"start": 33,
"end": 34,
"start": 35,
"end": 36,
"loc": {
"start": {
"line": 3,
"line": 4,
"column": 4
},
"end": {
"line": 3,
"line": 4,
"column": 5
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if (a) {
b
if c:
d
elif e:
f
} else {
g
}
Loading