Skip to content

Commit

Permalink
Fix t.arrowFunctionExpression and t.functionExpression builders by ad…
Browse files Browse the repository at this point in the history
…ding all fields
  • Loading branch information
Even Alander committed Nov 8, 2020
1 parent 3227914 commit 56aa9b9
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 2 deletions.
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();
});
});
});
});

0 comments on commit 56aa9b9

Please sign in to comment.