Partial Application Syntax: Stage 1 (#9343)

* add partial application syntax and some tests

* remove unnecessary error message and hasPartial function from parseNewArguments

* add types for PartialExpression

* Update the tests

* rename PartialExpression to Partial

* move Partial from expressions to types and rename to ArgumentPlaceholder

* add tests for ArgumentPlaceholder in babel-generator

* rename Partial to ArgumentPlaceholder

* update the tests

* remove alias from the type and undo changes in generated folder

* adds a nice error message

* better definition for the type

* auto-generated files

* update the conditional for allowPlaceholder message and tests

* update CallExpression definition to accept ArgumentPlaceholder

* change description

* clean up

* indent ArgumentPlaceholder entry and revert unwanted changes
This commit is contained in:
Behrang Yarahmadi
2019-01-14 17:54:21 +01:00
committed by Nicolò Ribaudo
parent 37a427f692
commit c60c4dd375
39 changed files with 1323 additions and 2 deletions

View File

@@ -72,6 +72,7 @@ These are the core @babel/parser (babylon) AST node types.
- [LogicalExpression](#logicalexpression)
- [LogicalOperator](#logicaloperator)
- [SpreadElement](#spreadelement)
- [ArgumentPlaceholder](#argumentplaceholder)
- [MemberExpression](#memberexpression)
- [BindExpression](#bindexpression)
- [ConditionalExpression](#conditionalexpression)
@@ -862,6 +863,14 @@ interface SpreadElement <: Node {
}
```
### ArgumentPlaceholder
```js
interface ArgumentPlaceholder <: Node {
type: "ArgumentPlaceholder";
}
```
### MemberExpression
```js

View File

@@ -323,7 +323,6 @@ export default class ExpressionParser extends LValParser {
const operator = this.state.value;
node.left = left;
node.operator = operator;
if (
operator === "**" &&
left.type === "UnaryExpression" &&
@@ -634,6 +633,7 @@ export default class ExpressionParser extends LValParser {
tt.parenR,
possibleAsync,
base.type === "Import",
base.type !== "Super",
);
if (!state.optionalChainMember) {
this.finishCallExpression(node);
@@ -744,6 +744,7 @@ export default class ExpressionParser extends LValParser {
close: TokenType,
possibleAsyncArrow: boolean,
dynamicImport?: boolean,
allowPlaceholder?: boolean,
): $ReadOnlyArray<?N.Expression> {
const elts = [];
let innerParenStart;
@@ -776,6 +777,7 @@ export default class ExpressionParser extends LValParser {
false,
possibleAsyncArrow ? { start: 0 } : undefined,
possibleAsyncArrow ? { start: 0 } : undefined,
allowPlaceholder,
),
);
}
@@ -1945,6 +1947,7 @@ export default class ExpressionParser extends LValParser {
allowEmpty: ?boolean,
refShorthandDefaultPos: ?Pos,
refNeedsArrowPos: ?Pos,
allowPlaceholder: ?boolean,
): ?N.Expression {
let elt;
if (allowEmpty && this.match(tt.comma)) {
@@ -1957,6 +1960,14 @@ export default class ExpressionParser extends LValParser {
spreadNodeStartPos,
spreadNodeStartLoc,
);
} else if (this.match(tt.question)) {
this.expectPlugin("partialApplication");
if (!allowPlaceholder) {
this.raise(this.state.start, "Unexpected argument placeholder");
}
const node = this.startNode();
this.next();
elt = this.finishNode(node, "ArgumentPlaceholder");
} else {
elt = this.parseMaybeAssign(
false,

View File

@@ -340,6 +340,8 @@ export type VariableDeclarator = NodeBase & {
// Misc
export type ArgumentPlaceholder = NodeBase & { type: "ArgumentPlaceholder" };
export type Decorator = NodeBase & {
type: "Decorator",
expression: Expression,

View File

@@ -0,0 +1,3 @@
{
"plugins": ["partialApplication"]
}

View File

@@ -0,0 +1,99 @@
{
"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",
"interpreter": null,
"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": "Identifier",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
},
"identifierName": "foo"
},
"name": "foo"
},
"arguments": [
{
"type": "ArgumentPlaceholder",
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
}
}
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,5 @@
class foo {
constructor() {
baz(this, () => super.bar(?));
}
}

View File

@@ -0,0 +1,3 @@
{
"plugins": ["partialApplication"]
}

View File

@@ -0,0 +1,305 @@
{
"type": "File",
"start": 0,
"end": 78,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 78,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 78,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 9
},
"identifierName": "foo"
},
"name": "foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 10,
"end": 78,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 5,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 16,
"end": 76,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 4,
"column": 5
}
},
"static": false,
"key": {
"type": "Identifier",
"start": 16,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 15
},
"identifierName": "constructor"
},
"name": "constructor"
},
"computed": false,
"kind": "constructor",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 30,
"end": 76,
"loc": {
"start": {
"line": 2,
"column": 18
},
"end": {
"line": 4,
"column": 5
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 40,
"end": 70,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 38
}
},
"expression": {
"type": "CallExpression",
"start": 40,
"end": 69,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 37
}
},
"callee": {
"type": "Identifier",
"start": 40,
"end": 43,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 11
},
"identifierName": "baz"
},
"name": "baz"
},
"arguments": [
{
"type": "ThisExpression",
"start": 44,
"end": 48,
"loc": {
"start": {
"line": 3,
"column": 12
},
"end": {
"line": 3,
"column": 16
}
}
},
{
"type": "ArrowFunctionExpression",
"start": 50,
"end": 68,
"loc": {
"start": {
"line": 3,
"column": 18
},
"end": {
"line": 3,
"column": 36
}
},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "CallExpression",
"start": 56,
"end": 68,
"loc": {
"start": {
"line": 3,
"column": 24
},
"end": {
"line": 3,
"column": 36
}
},
"callee": {
"type": "MemberExpression",
"start": 56,
"end": 65,
"loc": {
"start": {
"line": 3,
"column": 24
},
"end": {
"line": 3,
"column": 33
}
},
"object": {
"type": "Super",
"start": 56,
"end": 61,
"loc": {
"start": {
"line": 3,
"column": 24
},
"end": {
"line": 3,
"column": 29
}
}
},
"property": {
"type": "Identifier",
"start": 62,
"end": 65,
"loc": {
"start": {
"line": 3,
"column": 30
},
"end": {
"line": 3,
"column": 33
},
"identifierName": "bar"
},
"name": "bar"
},
"computed": false
},
"arguments": [
{
"type": "ArgumentPlaceholder",
"start": 66,
"end": 67,
"loc": {
"start": {
"line": 3,
"column": 34
},
"end": {
"line": 3,
"column": 35
}
}
}
]
}
}
]
}
}
],
"directives": []
}
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,3 @@
foo(?,x,?)
bar.foo(?, x, ?)

View File

@@ -0,0 +1,3 @@
{
"plugins": ["partialApplication"]
}

View File

@@ -0,0 +1,260 @@
{
"type": "File",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 16
}
},
"program": {
"type": "Program",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 16
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"expression": {
"type": "CallExpression",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"callee": {
"type": "Identifier",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
},
"identifierName": "foo"
},
"name": "foo"
},
"arguments": [
{
"type": "ArgumentPlaceholder",
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
}
}
},
{
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "x"
},
"name": "x"
},
{
"type": "ArgumentPlaceholder",
"start": 8,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 9
}
}
}
]
}
},
{
"type": "ExpressionStatement",
"start": 12,
"end": 28,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 16
}
},
"expression": {
"type": "CallExpression",
"start": 12,
"end": 28,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 16
}
},
"callee": {
"type": "MemberExpression",
"start": 12,
"end": 19,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 7
}
},
"object": {
"type": "Identifier",
"start": 12,
"end": 15,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 3
},
"identifierName": "bar"
},
"name": "bar"
},
"property": {
"type": "Identifier",
"start": 16,
"end": 19,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 7
},
"identifierName": "foo"
},
"name": "foo"
},
"computed": false
},
"arguments": [
{
"type": "ArgumentPlaceholder",
"start": 20,
"end": 21,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 9
}
}
},
{
"type": "Identifier",
"start": 23,
"end": 24,
"loc": {
"start": {
"line": 3,
"column": 11
},
"end": {
"line": 3,
"column": 12
},
"identifierName": "x"
},
"name": "x"
},
{
"type": "ArgumentPlaceholder",
"start": 26,
"end": 27,
"loc": {
"start": {
"line": 3,
"column": 14
},
"end": {
"line": 3,
"column": 15
}
}
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,3 @@
foo(x,?)
bar.foo(x,?)

View File

@@ -0,0 +1,3 @@
{
"plugins": ["partialApplication"]
}

View File

@@ -0,0 +1,230 @@
{
"type": "File",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 12
}
},
"program": {
"type": "Program",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 12
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
},
"expression": {
"type": "CallExpression",
"start": 0,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
},
"callee": {
"type": "Identifier",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
},
"identifierName": "foo"
},
"name": "foo"
},
"arguments": [
{
"type": "Identifier",
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
},
"identifierName": "x"
},
"name": "x"
},
{
"type": "ArgumentPlaceholder",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
}
}
}
]
}
},
{
"type": "ExpressionStatement",
"start": 10,
"end": 22,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 12
}
},
"expression": {
"type": "CallExpression",
"start": 10,
"end": 22,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 12
}
},
"callee": {
"type": "MemberExpression",
"start": 10,
"end": 17,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 7
}
},
"object": {
"type": "Identifier",
"start": 10,
"end": 13,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 3
},
"identifierName": "bar"
},
"name": "bar"
},
"property": {
"type": "Identifier",
"start": 14,
"end": 17,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 7
},
"identifierName": "foo"
},
"name": "foo"
},
"computed": false
},
"arguments": [
{
"type": "Identifier",
"start": 18,
"end": 19,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 9
},
"identifierName": "x"
},
"name": "x"
},
{
"type": "ArgumentPlaceholder",
"start": 20,
"end": 21,
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 3,
"column": 11
}
}
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,3 @@
foo(?,x)
bar.foo(?,x)

View File

@@ -0,0 +1,3 @@
{
"plugins": ["partialApplication"]
}

View File

@@ -0,0 +1,230 @@
{
"type": "File",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 12
}
},
"program": {
"type": "Program",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 12
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
},
"expression": {
"type": "CallExpression",
"start": 0,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
},
"callee": {
"type": "Identifier",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
},
"identifierName": "foo"
},
"name": "foo"
},
"arguments": [
{
"type": "ArgumentPlaceholder",
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
}
}
},
{
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "x"
},
"name": "x"
}
]
}
},
{
"type": "ExpressionStatement",
"start": 10,
"end": 22,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 12
}
},
"expression": {
"type": "CallExpression",
"start": 10,
"end": 22,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 12
}
},
"callee": {
"type": "MemberExpression",
"start": 10,
"end": 17,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 7
}
},
"object": {
"type": "Identifier",
"start": 10,
"end": 13,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 3
},
"identifierName": "bar"
},
"name": "bar"
},
"property": {
"type": "Identifier",
"start": 14,
"end": 17,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 7
},
"identifierName": "foo"
},
"name": "foo"
},
"computed": false
},
"arguments": [
{
"type": "ArgumentPlaceholder",
"start": 18,
"end": 19,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 9
}
}
},
{
"type": "Identifier",
"start": 20,
"end": 21,
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 3,
"column": 11
},
"identifierName": "x"
},
"name": "x"
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,6 @@
class Foo extends Bar {
constructor(x){
super(x, 1, ?, ?);
this.x = x;
}
}

View File

@@ -0,0 +1,4 @@
{
"plugins": ["partialApplication"],
"throws": "Unexpected argument placeholder (3:16)"
}

View File

@@ -0,0 +1 @@
new bar(x, ?, 2, ?)

View File

@@ -0,0 +1,4 @@
{
"plugins": ["partialApplication"],
"throws": "Unexpected argument placeholder (1:11)"
}

View File

@@ -0,0 +1,4 @@
{
"plugins": ["partialApplication"],
"throws": "Unexpected token (1:0)"
}

View File

@@ -0,0 +1,4 @@
{
"plugins": ["partialApplication"],
"throws": "Unexpected token (1:0)"
}