Skip to content

Commit

Permalink
Tilde calls now generate CallExpression nodes with tilde: true
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed Sep 24, 2017
1 parent 926a863 commit 33e044c
Show file tree
Hide file tree
Showing 19 changed files with 451 additions and 406 deletions.
2 changes: 1 addition & 1 deletion src/plugins/lightscript.js
Expand Up @@ -144,7 +144,7 @@ pp.parseObjectWhiteBlock = function(node, blockIndentLevel) {
obj.type !== "ObjectExpression" &&
obj.type !== "ObjectComprehension" &&
// A tilde call can begin with { if an ObjectExpression is the thisArg.
obj.type !== "TildeCallExpression"
(obj.type !== "CallExpression" || !obj.tilde)
) {
throw new Error("WRONG_SPECULATIVE_BRANCH");
}
Expand Down
21 changes: 14 additions & 7 deletions src/plugins/tildeCall.js
Expand Up @@ -8,13 +8,12 @@ export default function(parser) {

// Parse `a~b(...)` or `a~>b(...)` subscript. Returns truthy iff
// the call is further subscriptable.
pp.parseTildeCall = function(node, left) {
pp.parseTildeCall = function(node, firstArg) {
this.next();
node.left = left;

// allow `this`, Identifier or MemberExpression, but not calls
const right = this.match(tt._this) ? this.parseExprAtom() : this.parseIdentifierOrPlaceholder();
node.right = this.parseSubscripts(right, this.state.start, this.state.startLoc, true);
const callee = this.match(tt._this) ? this.parseExprAtom() : this.parseIdentifierOrPlaceholder();
node.callee = this.parseSubscripts(callee, this.state.start, this.state.startLoc, true);

// Allow safe tilde calls (a~b?(c))
if (this.hasPlugin("safeCallExpression") && this.eat(tt.question)) {
Expand All @@ -23,12 +22,20 @@ export default function(parser) {

// Allow bang tilde calls
if (this.hasPlugin("bangCall") && this.isAdjacentBang()) {
const next = this.parseBangCall(node, "TildeCallExpression");
if (next) return next; else return false;
const next = this.parseBangCall(node, "CallExpression");
if (next) {
next.arguments.unshift(firstArg);
next.tilde = true;
return next;
} else {
return false;
}
} else {
this.expect(tt.parenL);
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
return this.finishNode(node, "TildeCallExpression");
node.arguments.unshift(firstArg);
node.tilde = true;
return this.finishNode(node, "CallExpression");
}
};
}
Expand Up @@ -89,7 +89,7 @@
}
},
"expression": {
"type": "TildeCallExpression",
"type": "CallExpression",
"start": 6,
"end": 12,
"loc": {
Expand All @@ -102,23 +102,7 @@
"column": 12
}
},
"left": {
"type": "ObjectExpression",
"start": 6,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 8
}
},
"properties": []
},
"right": {
"callee": {
"type": "Identifier",
"start": 9,
"end": 10,
Expand All @@ -135,7 +119,25 @@
},
"name": "y"
},
"arguments": []
"arguments": [
{
"type": "ObjectExpression",
"start": 6,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 8
}
},
"properties": []
}
],
"tilde": true
}
}
],
Expand Down
41 changes: 21 additions & 20 deletions test/fixtures/lightscript/tilde-calls/args/expected.json
Expand Up @@ -43,7 +43,7 @@
}
},
"expression": {
"type": "TildeCallExpression",
"type": "CallExpression",
"start": 0,
"end": 6,
"loc": {
Expand All @@ -56,24 +56,7 @@
"column": 6
}
},
"left": {
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "a"
},
"name": "a"
},
"right": {
"callee": {
"type": "Identifier",
"start": 2,
"end": 3,
Expand All @@ -91,6 +74,23 @@
"name": "b"
},
"arguments": [
{
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "a"
},
"name": "a"
},
{
"type": "Identifier",
"start": 4,
Expand All @@ -108,7 +108,8 @@
},
"name": "c"
}
]
],
"tilde": true
}
}
],
Expand Down
42 changes: 22 additions & 20 deletions test/fixtures/lightscript/tilde-calls/basic/expected.json
Expand Up @@ -43,7 +43,7 @@
}
},
"expression": {
"type": "TildeCallExpression",
"type": "CallExpression",
"start": 0,
"end": 5,
"loc": {
Expand All @@ -56,24 +56,7 @@
"column": 5
}
},
"left": {
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "a"
},
"name": "a"
},
"right": {
"callee": {
"type": "Identifier",
"start": 2,
"end": 3,
Expand All @@ -90,7 +73,26 @@
},
"name": "b"
},
"arguments": []
"arguments": [
{
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "a"
},
"name": "a"
}
],
"tilde": true
}
}
],
Expand Down
42 changes: 22 additions & 20 deletions test/fixtures/lightscript/tilde-calls/member-called/expected.json
Expand Up @@ -71,7 +71,7 @@
}
},
"object": {
"type": "TildeCallExpression",
"type": "CallExpression",
"start": 0,
"end": 5,
"loc": {
Expand All @@ -84,24 +84,7 @@
"column": 5
}
},
"left": {
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "a"
},
"name": "a"
},
"right": {
"callee": {
"type": "Identifier",
"start": 2,
"end": 3,
Expand All @@ -118,7 +101,26 @@
},
"name": "b"
},
"arguments": []
"arguments": [
{
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "a"
},
"name": "a"
}
],
"tilde": true
},
"property": {
"type": "Identifier",
Expand Down
42 changes: 22 additions & 20 deletions test/fixtures/lightscript/tilde-calls/member/expected.json
Expand Up @@ -43,7 +43,7 @@
}
},
"expression": {
"type": "TildeCallExpression",
"type": "CallExpression",
"start": 0,
"end": 7,
"loc": {
Expand All @@ -56,24 +56,7 @@
"column": 7
}
},
"left": {
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "a"
},
"name": "a"
},
"right": {
"callee": {
"type": "MemberExpression",
"start": 3,
"end": 5,
Expand Down Expand Up @@ -123,7 +106,26 @@
},
"computed": false
},
"arguments": []
"arguments": [
{
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "a"
},
"name": "a"
}
],
"tilde": true
}
}
],
Expand Down

0 comments on commit 33e044c

Please sign in to comment.