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

Fix t.arrowFunctionExpression and t.functionExpression builders by adding all fields #12334

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
20 changes: 18 additions & 2 deletions packages/babel-types/src/definitions/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,15 @@ export const functionDeclarationCommon = {
};

defineType("FunctionDeclaration", {
builder: ["id", "params", "body", "generator", "async"],
builder: [
"id",
"params",
"body",
"generator",
"async",
"returnType",
"typeParameters",
],
visitor: ["id", "params", "body", "returnType", "typeParameters"],
fields: {
...functionDeclarationCommon,
Expand Down Expand Up @@ -1209,7 +1217,15 @@ defineType("ArrayPattern", {
});

defineType("ArrowFunctionExpression", {
builder: ["params", "body", "async"],
builder: [
"params",
"body",
"async",
"expression",
"generator",
"returnType",
"typeParameters",
],
visitor: ["params", "body", "returnType", "typeParameters"],
aliases: [
"Scopable",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`builders es2015 arrowFunctionExpression accept all parameters 1`] = `
Object {
"async": true,
"body": Object {
"body": Array [
Object {
"argument": Object {
"type": "ThisExpression",
},
"type": "ReturnStatement",
},
],
"directives": Array [],
"type": "BlockStatement",
},
"expression": true,
"generator": true,
"params": Array [
Object {
"name": "foo",
"type": "Identifier",
},
],
"returnType": Object {
"type": "TSTypeAnnotation",
"typeAnnotation": Object {
"type": "TSStringKeyword",
},
},
"type": "ArrowFunctionExpression",
"typeParameters": Object {
"params": Array [
Object {
"constraint": Object {
"type": "TSObjectKeyword",
},
"default": null,
"name": "foo",
"type": "TSTypeParameter",
},
],
"type": "TSTypeParameterDeclaration",
},
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`builders core functionExpression accept all parameters 1`] = `
Object {
"async": true,
"body": Object {
"body": Array [
Object {
"argument": Object {
"type": "ThisExpression",
},
"type": "ReturnStatement",
},
],
"directives": Array [],
"type": "BlockStatement",
},
"generator": true,
"id": Object {
"name": "foo",
"type": "Identifier",
},
"params": Array [
Object {
"name": "bar",
"type": "Identifier",
},
],
"returnType": Object {
"type": "TSTypeAnnotation",
"typeAnnotation": Object {
"type": "TSStringKeyword",
},
},
"type": "FunctionExpression",
"typeParameters": Object {
"params": Array [
Object {
"constraint": Object {
"type": "TSObjectKeyword",
},
"default": null,
"name": "bar",
"type": "TSTypeParameter",
},
],
"type": "TSTypeParameterDeclaration",
},
}
`;
22 changes: 22 additions & 0 deletions packages/babel-types/test/builders/core/arrowFunctionExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as t from "@babel/types";

describe("builders", function () {
describe("es2015", function () {
describe("arrowFunctionExpression", function () {
it("accept all parameters", function () {
const arrowFunctionExpression = t.arrowFunctionExpression(
[t.identifier("foo")],
t.blockStatement([t.returnStatement(t.thisExpression())]),
true,
true,
true,
t.tsTypeAnnotation(t.tsStringKeyword()),
t.tsTypeParameterDeclaration([
t.tsTypeParameter(t.tsObjectKeyword(), null, "foo"),
]),
);
expect(arrowFunctionExpression).toMatchSnapshot();
});
});
});
});
22 changes: 22 additions & 0 deletions packages/babel-types/test/builders/core/functionExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as t from "../../..";

describe("builders", function () {
describe("core", function () {
describe("functionExpression", function () {
it("accept all parameters", function () {
const functionExpression = t.functionExpression(
t.identifier("foo"),
[t.identifier("bar")],
t.blockStatement([t.returnStatement(t.thisExpression())]),
true,
true,
t.tsTypeAnnotation(t.tsStringKeyword()),
t.tsTypeParameterDeclaration([
t.tsTypeParameter(t.tsObjectKeyword(), null, "bar"),
]),
);
expect(functionExpression).toMatchSnapshot();
});
});
});
});