diff --git a/packages/babylon/README.md b/packages/babylon/README.md index 78dd00be33..110ab291ba 100644 --- a/packages/babylon/README.md +++ b/packages/babylon/README.md @@ -121,6 +121,7 @@ require("babylon").parse("code", { | `estree` ([repo](https://github.com/estree/estree)) | n/a | | `jsx` ([repo](https://facebook.github.io/jsx/)) | `{s}` | | `flow` ([repo](https://github.com/facebook/flow)) | `var a: string = "";` | +| `flowComments` ([docs](https://flow.org/en/docs/types/comments/)) | `/*:: type Foo = {...}; */` | | `typescript` ([repo](https://github.com/Microsoft/TypeScript)) | `var a: string = "";` | | `doExpressions` | `var a = do { if (true) { 'hi'; } };` | | `objectRestSpread` ([proposal](https://github.com/tc39/proposal-object-rest-spread)) | `var a = { b, ...c };` | diff --git a/packages/babylon/src/plugins/flow.js b/packages/babylon/src/plugins/flow.js index 6cdee82a8c..7981df789a 100644 --- a/packages/babylon/src/plugins/flow.js +++ b/packages/babylon/src/plugins/flow.js @@ -7,6 +7,7 @@ import { types as tt, type TokenType } from "../tokenizer/types"; import * as N from "../types"; import type { Pos, Position } from "../util/location"; import type State from "../tokenizer/state"; +import * as charCodes from "charcodes"; const primitiveTypes = [ "any", @@ -2278,4 +2279,66 @@ export default (superClass: Class): Class => /* isAsync */ true, ); } + + readToken_mult_modulo(code: number): void { + const next = this.input.charCodeAt(this.state.pos + 1); + if ( + code === charCodes.asterisk && + next === charCodes.slash && + this.state.hasFlowComment + ) { + this.state.hasFlowComment = false; + this.state.pos += 2; + this.nextToken(); + return; + } + + super.readToken_mult_modulo(code); + } + + skipBlockComment(): void { + if ( + this.hasPlugin("flow") && + this.hasPlugin("flowComments") && + this.skipFlowComment() + ) { + this.hasFlowCommentCompletion(); + this.state.pos += this.skipFlowComment(); + this.state.hasFlowComment = true; + return; + } + + let end; + if (this.hasPlugin("flow") && this.state.hasFlowComment) { + end = this.input.indexOf("*-/", (this.state.pos += 2)); + if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment"); + this.state.pos = end + 3; + return; + } + + super.skipBlockComment(); + } + + skipFlowComment(): number | boolean { + const ch2 = this.input.charCodeAt(this.state.pos + 2); + const ch3 = this.input.charCodeAt(this.state.pos + 3); + + if (ch2 === charCodes.colon && ch3 === charCodes.colon) { + return 4; // check for /*:: + } + if (this.input.slice(this.state.pos + 2, 14) === "flow-include") { + return 14; // check for /*flow-include + } + if (ch2 === charCodes.colon && ch3 !== charCodes.colon && 2) { + return 2; // check for /*:, advance only 2 steps + } + return false; + } + + hasFlowCommentCompletion(): void { + const end = this.input.indexOf("*/", this.state.pos); + if (end === -1) { + this.raise(this.state.pos, "Unterminated comment"); + } + } }; diff --git a/packages/babylon/test/fixtures/flow/comment-disabled/01-type-include/actual.js b/packages/babylon/test/fixtures/flow/comment-disabled/01-type-include/actual.js new file mode 100644 index 0000000000..b42988128e --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment-disabled/01-type-include/actual.js @@ -0,0 +1,3 @@ +class MyClass { + /*:: prop: string; */ +} diff --git a/packages/babylon/test/fixtures/flow/comment-disabled/01-type-include/expected.json b/packages/babylon/test/fixtures/flow/comment-disabled/01-type-include/expected.json new file mode 100644 index 0000000000..0946d0fca4 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment-disabled/01-type-include/expected.json @@ -0,0 +1,120 @@ +{ + "type": "File", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "MyClass" + }, + "name": "MyClass" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 14, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [], + "leadingComments": null, + "innerComments": [ + { + "type": "CommentBlock", + "value": ":: prop: string; ", + "start": 18, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 23 + } + } + } + ] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": ":: prop: string; ", + "start": 18, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 23 + } + } + } + ] +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/comment-disabled/02-type-include/actual.js b/packages/babylon/test/fixtures/flow/comment-disabled/02-type-include/actual.js new file mode 100644 index 0000000000..1a098629f4 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment-disabled/02-type-include/actual.js @@ -0,0 +1,7 @@ +/*:: +type Foo = { + foo: number, + bar: boolean, + baz: string +}; +*/ diff --git a/packages/babylon/test/fixtures/flow/comment-disabled/02-type-include/expected.json b/packages/babylon/test/fixtures/flow/comment-disabled/02-type-include/expected.json new file mode 100644 index 0000000000..c54b11f4f6 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment-disabled/02-type-include/expected.json @@ -0,0 +1,70 @@ +{ + "type": "File", + "start": 0, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + }, + "sourceType": "module", + "body": [], + "directives": [], + "leadingComments": null, + "innerComments": [ + { + "type": "CommentBlock", + "value": "::\ntype Foo = {\n foo: number,\n bar: boolean,\n baz: string\n};\n", + "start": 0, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + } + } + ] + }, + "comments": [ + { + "type": "CommentBlock", + "value": "::\ntype Foo = {\n foo: number,\n bar: boolean,\n baz: string\n};\n", + "start": 0, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + } + } + ] +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/comment-disabled/03-type-flow-include/actual.js b/packages/babylon/test/fixtures/flow/comment-disabled/03-type-flow-include/actual.js new file mode 100644 index 0000000000..d1644a305b --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment-disabled/03-type-flow-include/actual.js @@ -0,0 +1,7 @@ +/*flow-include +type Foo = { + foo: number, + bar: boolean, + baz: string +}; +*/ diff --git a/packages/babylon/test/fixtures/flow/comment-disabled/03-type-flow-include/expected.json b/packages/babylon/test/fixtures/flow/comment-disabled/03-type-flow-include/expected.json new file mode 100644 index 0000000000..e20f18a473 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment-disabled/03-type-flow-include/expected.json @@ -0,0 +1,70 @@ +{ + "type": "File", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + }, + "sourceType": "module", + "body": [], + "directives": [], + "leadingComments": null, + "innerComments": [ + { + "type": "CommentBlock", + "value": "flow-include\ntype Foo = {\n foo: number,\n bar: boolean,\n baz: string\n};\n", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + } + } + ] + }, + "comments": [ + { + "type": "CommentBlock", + "value": "flow-include\ntype Foo = {\n foo: number,\n bar: boolean,\n baz: string\n};\n", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + } + } + ] +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/comment-disabled/04-type-flow-include/actual.js b/packages/babylon/test/fixtures/flow/comment-disabled/04-type-flow-include/actual.js new file mode 100644 index 0000000000..6dc6ca809b --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment-disabled/04-type-flow-include/actual.js @@ -0,0 +1,3 @@ +class MyClass { + /*flow-include prop: string; */ +} diff --git a/packages/babylon/test/fixtures/flow/comment-disabled/04-type-flow-include/expected.json b/packages/babylon/test/fixtures/flow/comment-disabled/04-type-flow-include/expected.json new file mode 100644 index 0000000000..813c160d1a --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment-disabled/04-type-flow-include/expected.json @@ -0,0 +1,120 @@ +{ + "type": "File", + "start": 0, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "MyClass" + }, + "name": "MyClass" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 14, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [], + "leadingComments": null, + "innerComments": [ + { + "type": "CommentBlock", + "value": "flow-include prop: string; ", + "start": 18, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 33 + } + } + } + ] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": "flow-include prop: string; ", + "start": 18, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 33 + } + } + } + ] +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/comment-disabled/05-type-annotation/actual.js b/packages/babylon/test/fixtures/flow/comment-disabled/05-type-annotation/actual.js new file mode 100644 index 0000000000..c7727eac1a --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment-disabled/05-type-annotation/actual.js @@ -0,0 +1,2 @@ +function method(param /*: string */) /*: number */ { +} diff --git a/packages/babylon/test/fixtures/flow/comment-disabled/05-type-annotation/expected.json b/packages/babylon/test/fixtures/flow/comment-disabled/05-type-annotation/expected.json new file mode 100644 index 0000000000..263df904eb --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment-disabled/05-type-annotation/expected.json @@ -0,0 +1,191 @@ +{ + "type": "File", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "method" + }, + "name": "method" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 16, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "param" + }, + "name": "param", + "leadingComments": null, + "trailingComments": [ + { + "type": "CommentBlock", + "value": ": string ", + "start": 22, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 35 + } + } + } + ] + } + ], + "body": { + "type": "BlockStatement", + "start": 51, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 51 + }, + "end": { + "line": 2, + "column": 1 + } + }, + "body": [], + "directives": [], + "leadingComments": [ + { + "type": "CommentBlock", + "value": ": string ", + "start": 22, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 35 + } + } + }, + { + "type": "CommentBlock", + "value": ": number ", + "start": 37, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 50 + } + } + } + ] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": ": string ", + "start": 22, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 35 + } + } + }, + { + "type": "CommentBlock", + "value": ": number ", + "start": 37, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 50 + } + } + } + ] +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/comment-disabled/options.json b/packages/babylon/test/fixtures/flow/comment-disabled/options.json new file mode 100644 index 0000000000..d822c40fbb --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment-disabled/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "plugins": ["jsx", "flow"] +} diff --git a/packages/babylon/test/fixtures/flow/comment/01-type-include/actual.js b/packages/babylon/test/fixtures/flow/comment/01-type-include/actual.js new file mode 100644 index 0000000000..b42988128e --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/01-type-include/actual.js @@ -0,0 +1,3 @@ +class MyClass { + /*:: prop: string; */ +} diff --git a/packages/babylon/test/fixtures/flow/comment/01-type-include/expected.json b/packages/babylon/test/fixtures/flow/comment/01-type-include/expected.json new file mode 100644 index 0000000000..889d665fbd --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/01-type-include/expected.json @@ -0,0 +1,150 @@ +{ + "type": "File", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "MyClass" + }, + "name": "MyClass" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 14, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassProperty", + "start": 23, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 23, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "prop" + }, + "name": "prop" + }, + "computed": false, + "variance": null, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 27, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start": 29, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 19 + } + } + } + }, + "value": null + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/comment/02-type-include/actual.js b/packages/babylon/test/fixtures/flow/comment/02-type-include/actual.js new file mode 100644 index 0000000000..1a098629f4 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/02-type-include/actual.js @@ -0,0 +1,7 @@ +/*:: +type Foo = { + foo: number, + bar: boolean, + baz: string +}; +*/ diff --git a/packages/babylon/test/fixtures/flow/comment/02-type-include/expected.json b/packages/babylon/test/fixtures/flow/comment/02-type-include/expected.json new file mode 100644 index 0000000000..bd476ff5d7 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/02-type-include/expected.json @@ -0,0 +1,243 @@ +{ + "type": "File", + "start": 0, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TypeAlias", + "start": 5, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 6, + "column": 2 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 13, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "Foo" + }, + "name": "Foo" + }, + "typeParameters": null, + "right": { + "type": "ObjectTypeAnnotation", + "start": 16, + "end": 64, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start": 20, + "end": 31, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "key": { + "type": "Identifier", + "start": 20, + "end": 23, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "static": false, + "kind": "init", + "method": false, + "value": { + "type": "NumberTypeAnnotation", + "start": 25, + "end": 31, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + "variance": null, + "optional": false + }, + { + "type": "ObjectTypeProperty", + "start": 35, + "end": 47, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 14 + } + }, + "key": { + "type": "Identifier", + "start": 35, + "end": 38, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "bar" + }, + "name": "bar" + }, + "static": false, + "kind": "init", + "method": false, + "value": { + "type": "BooleanTypeAnnotation", + "start": 40, + "end": 47, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 14 + } + } + }, + "variance": null, + "optional": false + }, + { + "type": "ObjectTypeProperty", + "start": 51, + "end": 62, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 13 + } + }, + "key": { + "type": "Identifier", + "start": 51, + "end": 54, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 5 + }, + "identifierName": "baz" + }, + "name": "baz" + }, + "static": false, + "kind": "init", + "method": false, + "value": { + "type": "StringTypeAnnotation", + "start": 56, + "end": 62, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 13 + } + } + }, + "variance": null, + "optional": false + } + ], + "indexers": [], + "exact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/comment/03-type-flow-include/actual.js b/packages/babylon/test/fixtures/flow/comment/03-type-flow-include/actual.js new file mode 100644 index 0000000000..d1644a305b --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/03-type-flow-include/actual.js @@ -0,0 +1,7 @@ +/*flow-include +type Foo = { + foo: number, + bar: boolean, + baz: string +}; +*/ diff --git a/packages/babylon/test/fixtures/flow/comment/03-type-flow-include/expected.json b/packages/babylon/test/fixtures/flow/comment/03-type-flow-include/expected.json new file mode 100644 index 0000000000..05a7bcd068 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/03-type-flow-include/expected.json @@ -0,0 +1,243 @@ +{ + "type": "File", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 2 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TypeAlias", + "start": 15, + "end": 75, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 6, + "column": 2 + } + }, + "id": { + "type": "Identifier", + "start": 20, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "Foo" + }, + "name": "Foo" + }, + "typeParameters": null, + "right": { + "type": "ObjectTypeAnnotation", + "start": 26, + "end": 74, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start": 30, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "key": { + "type": "Identifier", + "start": 30, + "end": 33, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "static": false, + "kind": "init", + "method": false, + "value": { + "type": "NumberTypeAnnotation", + "start": 35, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + "variance": null, + "optional": false + }, + { + "type": "ObjectTypeProperty", + "start": 45, + "end": 57, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 14 + } + }, + "key": { + "type": "Identifier", + "start": 45, + "end": 48, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "bar" + }, + "name": "bar" + }, + "static": false, + "kind": "init", + "method": false, + "value": { + "type": "BooleanTypeAnnotation", + "start": 50, + "end": 57, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 14 + } + } + }, + "variance": null, + "optional": false + }, + { + "type": "ObjectTypeProperty", + "start": 61, + "end": 72, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 13 + } + }, + "key": { + "type": "Identifier", + "start": 61, + "end": 64, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 5 + }, + "identifierName": "baz" + }, + "name": "baz" + }, + "static": false, + "kind": "init", + "method": false, + "value": { + "type": "StringTypeAnnotation", + "start": 66, + "end": 72, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 13 + } + } + }, + "variance": null, + "optional": false + } + ], + "indexers": [], + "exact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/comment/04-type-flow-include/actual.js b/packages/babylon/test/fixtures/flow/comment/04-type-flow-include/actual.js new file mode 100644 index 0000000000..6dc6ca809b --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/04-type-flow-include/actual.js @@ -0,0 +1,3 @@ +class MyClass { + /*flow-include prop: string; */ +} diff --git a/packages/babylon/test/fixtures/flow/comment/04-type-flow-include/expected.json b/packages/babylon/test/fixtures/flow/comment/04-type-flow-include/expected.json new file mode 100644 index 0000000000..813c160d1a --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/04-type-flow-include/expected.json @@ -0,0 +1,120 @@ +{ + "type": "File", + "start": 0, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "MyClass" + }, + "name": "MyClass" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 14, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [], + "leadingComments": null, + "innerComments": [ + { + "type": "CommentBlock", + "value": "flow-include prop: string; ", + "start": 18, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 33 + } + } + } + ] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": "flow-include prop: string; ", + "start": 18, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 33 + } + } + } + ] +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/comment/05-type-annotation/actual.js b/packages/babylon/test/fixtures/flow/comment/05-type-annotation/actual.js new file mode 100644 index 0000000000..c7727eac1a --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/05-type-annotation/actual.js @@ -0,0 +1,2 @@ +function method(param /*: string */) /*: number */ { +} diff --git a/packages/babylon/test/fixtures/flow/comment/05-type-annotation/expected.json b/packages/babylon/test/fixtures/flow/comment/05-type-annotation/expected.json new file mode 100644 index 0000000000..bda3a62937 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/05-type-annotation/expected.json @@ -0,0 +1,165 @@ +{ + "type": "File", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "method" + }, + "name": "method" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 16, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 32 + }, + "identifierName": "param" + }, + "name": "param", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 24, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 32 + } + }, + "typeAnnotation": { + "type": "StringTypeAnnotation", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 32 + } + } + } + } + } + ], + "predicate": null, + "returnType": { + "type": "TypeAnnotation", + "start": 39, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 39 + }, + "end": { + "line": 1, + "column": 47 + } + }, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start": 41, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 41 + }, + "end": { + "line": 1, + "column": 47 + } + } + } + }, + "body": { + "type": "BlockStatement", + "start": 51, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 51 + }, + "end": { + "line": 2, + "column": 1 + } + }, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/comment/06-type-include-error/actual.js b/packages/babylon/test/fixtures/flow/comment/06-type-include-error/actual.js new file mode 100644 index 0000000000..24b92dba6e --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/06-type-include-error/actual.js @@ -0,0 +1,3 @@ +class MyClass { + /*:: prop: string; +} diff --git a/packages/babylon/test/fixtures/flow/comment/06-type-include-error/options.json b/packages/babylon/test/fixtures/flow/comment/06-type-include-error/options.json new file mode 100644 index 0000000000..88f910bceb --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/06-type-include-error/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unterminated comment (2:2)" +} diff --git a/packages/babylon/test/fixtures/flow/comment/07-type-include-error/actual.js b/packages/babylon/test/fixtures/flow/comment/07-type-include-error/actual.js new file mode 100644 index 0000000000..75bf8fbb4f --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/07-type-include-error/actual.js @@ -0,0 +1,6 @@ +/*:: +type Foo = { + foo: number, + bar: boolean, + baz: string +}; diff --git a/packages/babylon/test/fixtures/flow/comment/07-type-include-error/options.json b/packages/babylon/test/fixtures/flow/comment/07-type-include-error/options.json new file mode 100644 index 0000000000..eb40f4f052 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/07-type-include-error/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unterminated comment (1:0)" +} diff --git a/packages/babylon/test/fixtures/flow/comment/08-type-flow-include-error/actual.js b/packages/babylon/test/fixtures/flow/comment/08-type-flow-include-error/actual.js new file mode 100644 index 0000000000..495661d6a0 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/08-type-flow-include-error/actual.js @@ -0,0 +1,6 @@ +/*flow-include +type Foo = { + foo: number, + bar: boolean, + baz: string +}; diff --git a/packages/babylon/test/fixtures/flow/comment/08-type-flow-include-error/options.json b/packages/babylon/test/fixtures/flow/comment/08-type-flow-include-error/options.json new file mode 100644 index 0000000000..eb40f4f052 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/08-type-flow-include-error/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unterminated comment (1:0)" +} diff --git a/packages/babylon/test/fixtures/flow/comment/09-type-flow-include-error/actual.js b/packages/babylon/test/fixtures/flow/comment/09-type-flow-include-error/actual.js new file mode 100644 index 0000000000..7966a6c825 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/09-type-flow-include-error/actual.js @@ -0,0 +1,3 @@ +class MyClass { + /*flow-include prop: string; +} diff --git a/packages/babylon/test/fixtures/flow/comment/09-type-flow-include-error/options.json b/packages/babylon/test/fixtures/flow/comment/09-type-flow-include-error/options.json new file mode 100644 index 0000000000..88f910bceb --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/09-type-flow-include-error/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unterminated comment (2:2)" +} diff --git a/packages/babylon/test/fixtures/flow/comment/10-type-annotation-error/actual.js b/packages/babylon/test/fixtures/flow/comment/10-type-annotation-error/actual.js new file mode 100644 index 0000000000..5639eac908 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/10-type-annotation-error/actual.js @@ -0,0 +1,2 @@ +function method(param /*: string) { +} diff --git a/packages/babylon/test/fixtures/flow/comment/10-type-annotation-error/options.json b/packages/babylon/test/fixtures/flow/comment/10-type-annotation-error/options.json new file mode 100644 index 0000000000..4e97b8b844 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/10-type-annotation-error/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unterminated comment (1:22)" +} diff --git a/packages/babylon/test/fixtures/flow/comment/options.json b/packages/babylon/test/fixtures/flow/comment/options.json new file mode 100644 index 0000000000..602fa0531e --- /dev/null +++ b/packages/babylon/test/fixtures/flow/comment/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "plugins": ["jsx", "flow", "flowComments"] +} diff --git a/scripts/tests/flow/flow_tests_whitelist.txt b/scripts/tests/flow/flow_tests_whitelist.txt index 3f0abc41bf..3acff5ef56 100644 --- a/scripts/tests/flow/flow_tests_whitelist.txt +++ b/scripts/tests/flow/flow_tests_whitelist.txt @@ -36,11 +36,7 @@ invalid_syntax/migrated_0003.js invalid_syntax/migrated_0010.js private_class_properties/valid.js types/annotations/migrated_0001.js -types/annotations_in_comments_invalid/migrated_0000.js -types/annotations_in_comments_invalid/migrated_0001.js -types/annotations_in_comments_invalid/migrated_0002.js types/annotations_in_comments_invalid/migrated_0003.js -types/annotations_in_comments_invalid/migrated_0004.js types/annotations/static_is_reserved_param.js types/annotations/static_is_reserved_type.js types/annotations/void_is_reserved_param.js diff --git a/scripts/tests/flow/run_babylon_flow_tests.js b/scripts/tests/flow/run_babylon_flow_tests.js index 05dc65d57d..a17d629465 100644 --- a/scripts/tests/flow/run_babylon_flow_tests.js +++ b/scripts/tests/flow/run_babylon_flow_tests.js @@ -99,7 +99,13 @@ function update_whitelist(summary) { } const options = { - plugins: ["jsx", "flow", "asyncGenerators", "objectRestSpread"], + plugins: [ + "jsx", + "flow", + "flowComments", + "asyncGenerators", + "objectRestSpread", + ], sourceType: "module", ranges: true, }; @@ -109,6 +115,7 @@ const flowOptionsMapping = { esproposal_class_static_fields: "classProperties", esproposal_export_star_as: "exportNamespaceFrom", esproposal_decorators: "decorators", + types: "flowComments", }; const summary = { @@ -142,7 +149,15 @@ tests.forEach(section => { if (test.options) { Object.keys(test.options).forEach(option => { - if (!test.options[option]) return; + if (!test.options[option]) { + const idx = babylonOptions.plugins.indexOf( + flowOptionsMapping[option] + ); + if (idx) { + babylonOptions.plugins.splice(idx, 1); + } + return; + } if (!flowOptionsMapping[option]) { throw new Error("Parser options not mapped " + option); }