Skip to content

Commit

Permalink
Allow subscripting of argless bangcalls
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed Oct 19, 2017
1 parent b3eeb55 commit 60e1682
Show file tree
Hide file tree
Showing 13 changed files with 665 additions and 7 deletions.
23 changes: 22 additions & 1 deletion src/plugins/bangCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ export default function(parser) {
);
};

pp.couldBeginSubscript = function() {
return (
this.match(tt.bracketL) ||
this.match(tt.parenL) ||
this.match(tt.dot) ||
this.match(tt.tilde) ||
this.match(tt.elvis) ||
this.isBang()
);
};

// c/p parseExprListItem
pp.parseBangArg = function () {
let elt;
Expand Down Expand Up @@ -47,8 +58,18 @@ export default function(parser) {
return this.finishNode(node, nodeType);
}

// Disambiguate no-whitespace-after-! situations.
if (this.state.lastTokEnd === this.state.start) {
this.unexpected(null, "Whitespace required between `!` and first argument.");
// If next token could initiate a subscript, treat as no-arg bang call with
// subscript.
if (this.couldBeginSubscript()) {
return this.finishNode(node, nodeType);
}

// Otherwise parse error.
if (this.state.type.startsExpr) {
this.unexpected(null, "Whitespace required between `!` and first argument.");
}
}

// Collect state
Expand Down
3 changes: 0 additions & 3 deletions test/fixtures/bang-call/illegal/double-illegal/options.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/bang-call/illegal/paren-illegal/options.json

This file was deleted.

104 changes: 104 additions & 0 deletions test/fixtures/bang-call/subscripts/double-bang/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"type": "File",
"start": 0,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
},
"program": {
"type": "Program",
"start": 0,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
},
"expression": {
"type": "CallExpression",
"start": 0,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
},
"callee": {
"type": "CallExpression",
"start": 0,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 5
}
},
"callee": {
"type": "Identifier",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
},
"identifierName": "bang"
},
"name": "bang"
},
"arguments": [],
"extra": {
"bang": true
}
},
"arguments": [],
"extra": {
"bang": true
}
}
}
],
"directives": []
}
}
162 changes: 162 additions & 0 deletions test/fixtures/bang-call/subscripts/no-space-call/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
{
"type": "File",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"program": {
"type": "Program",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"expression": {
"type": "CallExpression",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"callee": {
"type": "CallExpression",
"start": 0,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 5
}
},
"callee": {
"type": "Identifier",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
},
"identifierName": "bang"
},
"name": "bang"
},
"arguments": [],
"extra": {
"bang": true
}
},
"arguments": [
{
"type": "NumericLiteral",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
},
{
"type": "NumericLiteral",
"start": 9,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 10
}
},
"extra": {
"rawValue": 2,
"raw": "2"
},
"value": 2
},
{
"type": "NumericLiteral",
"start": 12,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 13
}
},
"extra": {
"rawValue": 3,
"raw": "3"
},
"value": 3
}
]
}
}
],
"directives": []
}
}
1 change: 1 addition & 0 deletions test/fixtures/bang-call/subscripts/no-space-dot/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bang!.x
Loading

0 comments on commit 60e1682

Please sign in to comment.