Skip to content

Commit

Permalink
Fix for bang call subscripting issue across block boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed Sep 25, 2017
1 parent bda54e5 commit 64f066f
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/parser/statement.js
Expand Up @@ -608,6 +608,8 @@ pp.parseBlockBody = function (node, allowDirectives, topLevel, end) {
let oldStrict;
let octalPosition;

this.state.nestedBlockLevel++;

let isEnd;
if (this.hasPlugin("lightscript") && typeof end === "number") {
isEnd = () => this.state.indentLevel <= end || this.match(tt.eof);
Expand Down Expand Up @@ -642,6 +644,8 @@ pp.parseBlockBody = function (node, allowDirectives, topLevel, end) {
node.body.push(stmt);
}

this.state.nestedBlockLevel--;

if (oldStrict === false) {
this.setStrict(false);
}
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/bangCall.js
Expand Up @@ -43,6 +43,8 @@ export default function(parser) {
// Read args
let first = true;
const oldBangUnwindLevel = this.state.bangUnwindLevel;
const oldBangBlockLevel = this.state.bangBlockLevel;
this.state.bangBlockLevel = this.state.nestedBlockLevel;
this.state.bangUnwindLevel = bangIndentLevel + 1;

while (true) {
Expand Down Expand Up @@ -81,6 +83,7 @@ export default function(parser) {
}

this.state.bangUnwindLevel = oldBangUnwindLevel;
this.state.bangBlockLevel = oldBangBlockLevel;

node = this.finishNode(node, nodeType);

Expand All @@ -93,6 +96,8 @@ export default function(parser) {

// Subscripts to a bang call must appear at the arg indent level
pp.shouldUnwindBangSubscript = function() {
return this.isLineBreak() && (this.state.indentLevel <= this.state.bangUnwindLevel);
return this.isLineBreak() &&
(this.state.bangBlockLevel == this.state.nestedBlockLevel) &&
(this.state.indentLevel <= this.state.bangUnwindLevel);
};
}
20 changes: 15 additions & 5 deletions src/plugins/lightscript.js
Expand Up @@ -187,7 +187,9 @@ pp.rethrowObjParseError = function(objParseResult, blockParseError) {
pp.parseInlineWhiteBlock = function(node) {
if (this.state.type.startsExpr) return this.parseMaybeAssign();
// oneline statement case
this.state.nestedBlockLevel++;
node.body = [this.parseStatement(true)];
this.state.nestedBlockLevel--;
node.directives = [];
this.addExtra(node, "curly", false);
return this.finishNode(node, "BlockStatement");
Expand All @@ -198,15 +200,20 @@ pp.parseMultilineWhiteBlock = function(node, indentLevel) {
if (this.match(tt.braceL) && this.hasPlugin("objectBlockAmbiguity_preferObject")) {
objParseResult = this.tryParseObjectWhiteBlock(node, indentLevel);
if (objParseResult[0]) return objParseResult[0];
}

try {
try {
this.parseBlockBody(node, false, false, indentLevel);
if (!node.body.length) {
this.unexpected(node.start, "Expected an Indent or Statement");
}
} catch (err) {
this.rethrowObjParseError(objParseResult, err);
}
} else {
this.parseBlockBody(node, false, false, indentLevel);
if (!node.body.length) {
this.unexpected(node.start, "Expected an Indent or Statement");
}
} catch (err) {
this.rethrowObjParseError(objParseResult, err);
}

this.addExtra(node, "curly", false);
Expand All @@ -230,7 +237,10 @@ pp.parseWhiteBlock = function (isExpression?) {
if (objParseResult[0]) return objParseResult[0];
}
try {
return this.parseStatement(false);
this.state.nestedBlockLevel++;
const stmt = this.parseStatement(false);
this.state.nestedBlockLevel--;
return stmt;
} catch (err) {
this.rethrowObjParseError(objParseResult, err);
}
Expand Down
1 change: 1 addition & 0 deletions src/tokenizer/state.js
Expand Up @@ -40,6 +40,7 @@ export default class State {

// for lightscript
this.indentLevel = 0;
this.nestedBlockLevel = 0;
this.inMatchCaseTest = false;

this.type = tt.eof;
Expand Down
@@ -0,0 +1,3 @@
a! ->
b
~c()
193 changes: 193 additions & 0 deletions test/fixtures/bang-call/subscripts/ambiguous-arrow-body/expected.json
@@ -0,0 +1,193 @@
{
"type": "File",
"start": 0,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 6
}
},
"program": {
"type": "Program",
"start": 0,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 6
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 6
}
},
"expression": {
"type": "CallExpression",
"start": 0,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 6
}
},
"callee": {
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "a"
},
"name": "a"
},
"arguments": [
{
"type": "ArrowFunctionExpression",
"start": 3,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 3
},
"end": {
"line": 3,
"column": 6
}
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"skinny": true,
"body": {
"type": "BlockStatement",
"start": 3,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 3
},
"end": {
"line": 3,
"column": 6
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 8,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 3,
"column": 6
}
},
"expression": {
"type": "TildeCallExpression",
"start": 8,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 3,
"column": 6
}
},
"left": {
"type": "Identifier",
"start": 8,
"end": 9,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 3
},
"identifierName": "b"
},
"name": "b"
},
"right": {
"type": "Identifier",
"start": 13,
"end": 14,
"loc": {
"start": {
"line": 3,
"column": 3
},
"end": {
"line": 3,
"column": 4
},
"identifierName": "c"
},
"name": "c"
},
"arguments": []
}
}
],
"directives": [],
"extra": {
"curly": false
}
}
}
],
"extra": {
"bang": true
}
}
}
],
"directives": []
}
}
@@ -0,0 +1,11 @@
{
"alternatives": {
"default": {
"throws": "Indentation required. (3:2)"
},
"noEnforcedSubscriptIndentation": {
"allPlugins": true,
"excludePlugins": ["enforceSubscriptIndentation"]
}
}
}
@@ -0,0 +1,2 @@
a
~b()

0 comments on commit 64f066f

Please sign in to comment.