diff --git a/Makefile b/Makefile index a23354d7b1..2ed85e6bbb 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ MAKEFLAGS = -j1 -FLOW_COMMIT = 395e045c18d537fcbbc552a96ef2cdcd70b4ab52 +FLOW_COMMIT = bea8b83f50f597454941d2a7ecef6e93a881e576 TEST262_COMMIT = f90a52b39609a620c0854e0bd0b3a906c930fd17 # Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967 diff --git a/packages/babel-generator/src/generators/classes.js b/packages/babel-generator/src/generators/classes.js index 4e27f2fe12..22780dc3f0 100644 --- a/packages/babel-generator/src/generators/classes.js +++ b/packages/babel-generator/src/generators/classes.js @@ -124,6 +124,7 @@ export function ClassPrivateProperty(node: Object) { this.space(); } this.print(node.key, node); + this.print(node.typeAnnotation, node); if (node.value) { this.space(); this.token("="); diff --git a/packages/babel-generator/test/fixtures/flow/type-annotations/input.js b/packages/babel-generator/test/fixtures/flow/type-annotations/input.js index 03e37dd3fb..d94dd1ce66 100644 --- a/packages/babel-generator/test/fixtures/flow/type-annotations/input.js +++ b/packages/babel-generator/test/fixtures/flow/type-annotations/input.js @@ -72,6 +72,10 @@ class Foo { static prop1: string; prop2: number; } +class Foo { + #prop1: string; + prop2: number; +} var x: number | string = 4; class Array { concat(items:number | string) {}; } var x: () => number | () => string = fn; diff --git a/packages/babel-generator/test/fixtures/flow/type-annotations/options.json b/packages/babel-generator/test/fixtures/flow/type-annotations/options.json new file mode 100644 index 0000000000..53abfdb616 --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/type-annotations/options.json @@ -0,0 +1 @@ +{ "plugins": ["classPrivateProperties", "flow"] } \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/flow/type-annotations/output.js b/packages/babel-generator/test/fixtures/flow/type-annotations/output.js index 84200366ba..4333545052 100644 --- a/packages/babel-generator/test/fixtures/flow/type-annotations/output.js +++ b/packages/babel-generator/test/fixtures/flow/type-annotations/output.js @@ -168,6 +168,11 @@ class Foo { prop2: number; } +class Foo { + #prop1: string; + prop2: number; +} + var x: number | string = 4; class Array { diff --git a/packages/babel-parser/src/plugins/flow.js b/packages/babel-parser/src/plugins/flow.js index 945b771f19..4f574776dc 100644 --- a/packages/babel-parser/src/plugins/flow.js +++ b/packages/babel-parser/src/plugins/flow.js @@ -1917,6 +1917,15 @@ export default (superClass: Class): Class => return super.parseClassProperty(node); } + parseClassPrivateProperty( + node: N.ClassPrivateProperty, + ): N.ClassPrivateProperty { + if (this.match(tt.colon)) { + node.typeAnnotation = this.flowParseTypeAnnotation(); + } + return super.parseClassPrivateProperty(node); + } + // determine whether or not we're currently in the position where a class method would appear isClassMethod(): boolean { return this.isRelational("<") || super.isClassMethod(); diff --git a/packages/babel-parser/src/types.js b/packages/babel-parser/src/types.js index 4369302cca..f895707cdd 100644 --- a/packages/babel-parser/src/types.js +++ b/packages/babel-parser/src/types.js @@ -717,6 +717,7 @@ export type ClassPrivateProperty = NodeBase & { value: ?Expression, // TODO: Not in spec that this is nullable. static: boolean, computed: false, + typeAnnotation?: ?TypeAnnotation, // TODO: Not in spec }; export type OptClassDeclaration = ClassBase & diff --git a/packages/babel-parser/test/fixtures/flow/class-private-property/1/input.js b/packages/babel-parser/test/fixtures/flow/class-private-property/1/input.js new file mode 100644 index 0000000000..0ad82dd437 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/class-private-property/1/input.js @@ -0,0 +1,4 @@ +class A { + #prop1: string; + #prop2: number = value; +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/class-private-property/1/output.json b/packages/babel-parser/test/fixtures/flow/class-private-property/1/output.json new file mode 100644 index 0000000000..a00beeae9d --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/class-private-property/1/output.json @@ -0,0 +1,261 @@ +{ + "type": "File", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "A" + }, + "name": "A" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ClassPrivateProperty", + "start": 12, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "static": false, + "key": { + "type": "PrivateName", + "start": 12, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "prop1" + }, + "name": "prop1" + } + }, + "variance": null, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 18, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start": 20, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 16 + } + } + } + }, + "value": null + }, + { + "type": "ClassPrivateProperty", + "start": 30, + "end": 53, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 25 + } + }, + "static": false, + "key": { + "type": "PrivateName", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 8 + } + }, + "id": { + "type": "Identifier", + "start": 31, + "end": 36, + "loc": { + "start": { + "line": 3, + "column": 3 + }, + "end": { + "line": 3, + "column": 8 + }, + "identifierName": "prop2" + }, + "name": "prop2" + } + }, + "variance": null, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 36, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 16 + } + }, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 16 + } + } + } + }, + "value": { + "type": "Identifier", + "start": 47, + "end": 52, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 24 + }, + "identifierName": "value" + }, + "name": "value" + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/class-private-property/2/input.js b/packages/babel-parser/test/fixtures/flow/class-private-property/2/input.js new file mode 100644 index 0000000000..1586a6cc47 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/class-private-property/2/input.js @@ -0,0 +1,4 @@ +class A { + #prop1: string; + #prop2: number; +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/class-private-property/2/output.json b/packages/babel-parser/test/fixtures/flow/class-private-property/2/output.json new file mode 100644 index 0000000000..ab488ef216 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/class-private-property/2/output.json @@ -0,0 +1,245 @@ +{ + "type": "File", + "start": 0, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "A" + }, + "name": "A" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ClassPrivateProperty", + "start": 12, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "static": false, + "key": { + "type": "PrivateName", + "start": 12, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "prop1" + }, + "name": "prop1" + } + }, + "variance": null, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 18, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start": 20, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 16 + } + } + } + }, + "value": null + }, + { + "type": "ClassPrivateProperty", + "start": 30, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 17 + } + }, + "static": false, + "key": { + "type": "PrivateName", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 8 + } + }, + "id": { + "type": "Identifier", + "start": 31, + "end": 36, + "loc": { + "start": { + "line": 3, + "column": 3 + }, + "end": { + "line": 3, + "column": 8 + }, + "identifierName": "prop2" + }, + "name": "prop2" + } + }, + "variance": null, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 36, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 16 + } + }, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 16 + } + } + } + }, + "value": null + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/class-private-property/options.json b/packages/babel-parser/test/fixtures/flow/class-private-property/options.json new file mode 100644 index 0000000000..af63e97c60 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/class-private-property/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "plugins": [ "flow", "classPrivateProperties" ] +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-flow-strip-types/src/index.js b/packages/babel-plugin-transform-flow-strip-types/src/index.js index 063319cf9d..294ff7be1a 100644 --- a/packages/babel-plugin-transform-flow-strip-types/src/index.js +++ b/packages/babel-plugin-transform-flow-strip-types/src/index.js @@ -76,6 +76,11 @@ export default declare(api => { if (!path.node.value) path.remove(); }, + ClassPrivateProperty(path) { + if (skipStrip) return; + path.node.typeAnnotation = null; + }, + Class(path) { if (skipStrip) return; path.node.implements = null; diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/input.mjs b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/input.mjs index a9aec24aa5..2d40466b5b 100644 --- a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/input.mjs +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/input.mjs @@ -69,6 +69,10 @@ class Foo10 { static prop1: string; prop2: number; } +class Foo11 { + #prop1: string; + #prop2: number; +} var x: number | string = 4; class Array { concat(items:number | string) {}; } var x: () => number | () => string = fn; diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/options.json b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/options.json new file mode 100644 index 0000000000..5abfa483e5 --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/options.json @@ -0,0 +1 @@ +{ "plugins": [ "transform-flow-strip-types", "syntax-class-properties" ] } \ No newline at end of file diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/output.mjs b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/output.mjs index 9f1fe551ff..74989f4e7e 100644 --- a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/output.mjs +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-type-annotations/output.mjs @@ -123,6 +123,11 @@ class Foo9 {} class Foo10 {} +class Foo11 { + #prop1; + #prop2; +} + var x = 4; class Array { diff --git a/scripts/tests/flow/flow_tests_whitelist.txt b/scripts/tests/flow/flow_tests_whitelist.txt index 89872e94e8..e9f6d0bc42 100644 --- a/scripts/tests/flow/flow_tests_whitelist.txt +++ b/scripts/tests/flow/flow_tests_whitelist.txt @@ -15,6 +15,7 @@ async_await/migrated_0020.js async_await/migrated_0024.js async_await/migrated_0027.js async_generators/migrated_0007.js +catch/optional_catch_binding.js class_properties/migrated_0000.js class_properties/migrated_0005.js class_properties/migrated_0011.js @@ -23,7 +24,8 @@ class_properties/migrated_0021.js class_properties/migrated_0026.js decorators/migrated_0003.js decorators/migrated_0007.js -private_class_properties/valid.js +private_class_properties/multiple.js +private_class_properties/super.js types/annotations/migrated_0001.js types/annotations_in_comments_invalid/migrated_0003.js types/annotations/void_is_reserved_param.js diff --git a/scripts/tests/flow/run_babel_parser_flow_tests.js b/scripts/tests/flow/run_babel_parser_flow_tests.js index ec9818721a..5595262e6a 100644 --- a/scripts/tests/flow/run_babel_parser_flow_tests.js +++ b/scripts/tests/flow/run_babel_parser_flow_tests.js @@ -115,6 +115,7 @@ const options = { "flowComments", "jsx", "objectRestSpread", + "classPrivateProperties", ], sourceType: "module", ranges: true,