From 4506590557b71c816d212f15392acfea9e37a934 Mon Sep 17 00:00:00 2001 From: Yuri Karadzhov Date: Thu, 25 Jul 2019 09:55:03 +0200 Subject: [PATCH] Add static parameter to class property builder (#10248) --- .../babel-types/src/definitions/es2015.js | 2 +- .../src/definitions/experimental.js | 9 ++++- .../__snapshots__/classProperty.js.snap | 37 +++++++++++++++++++ .../builders/experimental/classProperty.js | 24 ++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 packages/babel-types/test/builders/experimental/__snapshots__/classProperty.js.snap create mode 100644 packages/babel-types/test/builders/experimental/classProperty.js diff --git a/packages/babel-types/src/definitions/es2015.js b/packages/babel-types/src/definitions/es2015.js index 26445747cdd3..18649b1df19e 100644 --- a/packages/babel-types/src/definitions/es2015.js +++ b/packages/babel-types/src/definitions/es2015.js @@ -403,8 +403,8 @@ export const classMethodOrPropertyCommon = { optional: true, }, static: { + default: false, validate: assertValueType("boolean"), - optional: true, }, computed: { default: false, diff --git a/packages/babel-types/src/definitions/experimental.js b/packages/babel-types/src/definitions/experimental.js index ed088af74bd6..32481fffd31b 100644 --- a/packages/babel-types/src/definitions/experimental.js +++ b/packages/babel-types/src/definitions/experimental.js @@ -33,7 +33,14 @@ defineType("BindExpression", { defineType("ClassProperty", { visitor: ["key", "value", "typeAnnotation", "decorators"], - builder: ["key", "value", "typeAnnotation", "decorators", "computed"], + builder: [ + "key", + "value", + "typeAnnotation", + "decorators", + "computed", + "static", + ], aliases: ["Property"], fields: { ...classMethodOrPropertyCommon, diff --git a/packages/babel-types/test/builders/experimental/__snapshots__/classProperty.js.snap b/packages/babel-types/test/builders/experimental/__snapshots__/classProperty.js.snap new file mode 100644 index 000000000000..4d10897ac020 --- /dev/null +++ b/packages/babel-types/test/builders/experimental/__snapshots__/classProperty.js.snap @@ -0,0 +1,37 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`builders experimental classProperty should validate 1`] = ` +Object { + "computed": false, + "decorators": null, + "key": Object { + "type": "StringLiteral", + "value": "test", + }, + "static": false, + "type": "ClassProperty", + "typeAnnotation": null, + "value": Object { + "type": "NumericLiteral", + "value": 1, + }, +} +`; + +exports[`builders experimental classProperty should validate 2`] = ` +Object { + "computed": false, + "decorators": null, + "key": Object { + "type": "StringLiteral", + "value": "test", + }, + "static": true, + "type": "ClassProperty", + "typeAnnotation": null, + "value": Object { + "type": "NumericLiteral", + "value": 1, + }, +} +`; diff --git a/packages/babel-types/test/builders/experimental/classProperty.js b/packages/babel-types/test/builders/experimental/classProperty.js new file mode 100644 index 000000000000..3b35a1317c50 --- /dev/null +++ b/packages/babel-types/test/builders/experimental/classProperty.js @@ -0,0 +1,24 @@ +import * as t from "../../.."; + +describe("builders", function() { + describe("experimental", function() { + describe("classProperty", function() { + it("should validate", function() { + expect( + t.classProperty(t.stringLiteral("test"), t.numericLiteral(1)), + ).toMatchSnapshot(); + + expect( + t.classProperty( + t.stringLiteral("test"), + t.numericLiteral(1), + null, + null, + false, + true, + ), + ).toMatchSnapshot(); + }); + }); + }); +});