Skip to content

Commit

Permalink
Add syntactic space for block params
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgoto committed Jun 11, 2021
1 parent 69f423b commit a781b8b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/babel-parser/src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export default class ExpressionParser extends LValParser {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
const expr = this.parseMaybeAssign(refExpressionErrors);

if (this.match(tt.comma)) {
const node = this.startNodeAt(startPos, startLoc);
node.expressions = [expr];
Expand Down Expand Up @@ -884,6 +885,12 @@ export default class ExpressionParser extends LValParser {
}
}
}
// throw new Error(node);
if (this.match(tt.braceL)) {
const param = this.parseBlockParam();
node.arguments.push(param);
// console.log(node);
}
return this.finishNode(
node,
optional ? "OptionalCallExpression" : "CallExpression",
Expand Down Expand Up @@ -2081,6 +2088,23 @@ export default class ExpressionParser extends LValParser {
);
}

// Parse block params as arrow function expressions.
parseBlockParam(): N.ArrowFunctionExpression {
const node = this.startNode();
const isAsync = false;
this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
const flags = functionFlags(isAsync, false);
this.prodParam.enter(flags);
this.initFunction(node, isAsync);
this.setArrowFunctionParameters(node, []);
this.state.maybeInArrowParameters = false;
this.parseFunctionBody(node, false, false);
this.prodParam.exit();
this.scope.exit();

return this.finishNode(node, "ArrowFunctionExpression");
}

// Parse arrow function expression.
// If the parameters are provided, they will be converted to an
// assignable list.
Expand Down
11 changes: 11 additions & 0 deletions packages/babel-parser/test/block-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { parse } from "../lib";

function getParser(code) {
return () => parse(code, { sourceType: "module" });
}

describe("block params syntax", () => {
it("should parse", () => {
expect(getParser(`foo() {}`)()).toMatchSnapshot();
});
});

0 comments on commit a781b8b

Please sign in to comment.