Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial application plugin #9474

Merged
merged 16 commits into from Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/babel-generator/src/generators/types.js
Expand Up @@ -7,6 +7,10 @@ export function Identifier(node: Object) {
});
}

export function ArgumentPlaceholder() {
this.token("?");
}

export function RestElement(node: Object) {
this.token("...");
this.print(node.argument, node);
Expand Down
@@ -0,0 +1,12 @@
foo(?);
foo(?, x);
foo(x, ?);
foo(?, x, ?);
obj.foo(x, ?);
obj.foo(?, x);
obj.foo(?, x, ?);
class foo {
constructor() {
baz(this, () => super.bar(?));
}
}
@@ -0,0 +1 @@
{ "plugins": ["partialApplication"] }
@@ -0,0 +1,14 @@
foo(?);
foo(?, x);
foo(x, ?);
foo(?, x, ?);
obj.foo(x, ?);
obj.foo(?, x);
obj.foo(?, x, ?);

class foo {
constructor() {
baz(this, () => super.bar(?));
}

}
9 changes: 9 additions & 0 deletions packages/babel-parser/ast/spec.md
Expand Up @@ -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)
Expand Down Expand Up @@ -862,6 +863,14 @@ interface SpreadElement <: Node {
}
```

### ArgumentPlaceholder

```js
interface ArgumentPlaceholder <: Node {
type: "ArgumentPlaceholder";
}
```

### MemberExpression

```js
Expand Down
13 changes: 12 additions & 1 deletion packages/babel-parser/src/parser/expression.js
Expand Up @@ -305,7 +305,6 @@ export default class ExpressionParser extends LValParser {
const operator = this.state.value;
node.left = left;
node.operator = operator;

if (
operator === "**" &&
left.type === "UnaryExpression" &&
Expand Down Expand Up @@ -616,6 +615,7 @@ export default class ExpressionParser extends LValParser {
tt.parenR,
possibleAsync,
base.type === "Import",
base.type !== "Super",
);
if (!state.optionalChainMember) {
this.finishCallExpression(node);
Expand Down Expand Up @@ -726,6 +726,7 @@ export default class ExpressionParser extends LValParser {
close: TokenType,
possibleAsyncArrow: boolean,
dynamicImport?: boolean,
allowPlaceholder?: boolean,
): $ReadOnlyArray<?N.Expression> {
const elts = [];
let innerParenStart;
Expand Down Expand Up @@ -758,6 +759,7 @@ export default class ExpressionParser extends LValParser {
false,
possibleAsyncArrow ? { start: 0 } : undefined,
possibleAsyncArrow ? { start: 0 } : undefined,
allowPlaceholder,
),
);
}
Expand Down Expand Up @@ -1901,6 +1903,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)) {
Expand All @@ -1913,6 +1916,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,
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-parser/src/types.js
Expand Up @@ -340,6 +340,8 @@ export type VariableDeclarator = NodeBase & {

// Misc

export type ArgumentPlaceholder = NodeBase & { type: "ArgumentPlaceholder" };

export type Decorator = NodeBase & {
type: "Decorator",
expression: Expression,
Expand Down
@@ -0,0 +1 @@
foo(?)
@@ -0,0 +1,3 @@
{
"plugins": ["partialApplication"]
}
@@ -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": []
}
}
@@ -0,0 +1,5 @@
class foo {
constructor() {
baz(this, () => super.bar(?));
}
}
@@ -0,0 +1,3 @@
{
"plugins": ["partialApplication"]
}