diff --git a/packages/babel-generator/test/fixtures/flow/iterator-inside-declare/input.js b/packages/babel-generator/test/fixtures/flow/iterator-inside-declare/input.js new file mode 100644 index 0000000000..5988be7988 --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/iterator-inside-declare/input.js @@ -0,0 +1,7 @@ +declare class A { + @@iterator(): Iterator; +} + +declare class A { + @@asyncIterator(): Iterator; +} diff --git a/packages/babel-generator/test/fixtures/flow/iterator-inside-declare/output.js b/packages/babel-generator/test/fixtures/flow/iterator-inside-declare/output.js new file mode 100644 index 0000000000..f140cac1a2 --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/iterator-inside-declare/output.js @@ -0,0 +1,6 @@ +declare class A { + @@iterator(): Iterator +} +declare class A { + @@asyncIterator(): Iterator +} \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/flow/iterator-inside-interface/input.js b/packages/babel-generator/test/fixtures/flow/iterator-inside-interface/input.js new file mode 100644 index 0000000000..dab9a962ac --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/iterator-inside-interface/input.js @@ -0,0 +1,7 @@ +interface A { + @@iterator(): Iterator; +} + +interface A { + @@asyncIterator(): Iterator; +} diff --git a/packages/babel-generator/test/fixtures/flow/iterator-inside-interface/output.js b/packages/babel-generator/test/fixtures/flow/iterator-inside-interface/output.js new file mode 100644 index 0000000000..ef726ab2fc --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/iterator-inside-interface/output.js @@ -0,0 +1,6 @@ +interface A { + @@iterator(): Iterator +} +interface A { + @@asyncIterator(): Iterator +} \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/flow/iterator-inside-types/input.js b/packages/babel-generator/test/fixtures/flow/iterator-inside-types/input.js new file mode 100644 index 0000000000..a97778edc8 --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/iterator-inside-types/input.js @@ -0,0 +1,3 @@ +function foo(): { @@iterator: () => string } { + return (0: any); +} diff --git a/packages/babel-generator/test/fixtures/flow/iterator-inside-types/output.js b/packages/babel-generator/test/fixtures/flow/iterator-inside-types/output.js new file mode 100644 index 0000000000..42fc4f9473 --- /dev/null +++ b/packages/babel-generator/test/fixtures/flow/iterator-inside-types/output.js @@ -0,0 +1,5 @@ +function foo(): { + @@iterator: () => string +} { + return (0: any); +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-iterator/input.js b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-iterator/input.js new file mode 100644 index 0000000000..741c23987d --- /dev/null +++ b/packages/babel-plugin-transform-flow-strip-types/test/fixtures/strip-types/strip-iterator/input.js @@ -0,0 +1,15 @@ +declare class A { + @@iterator(): Iterator; +} + +declare class A { + @@asyncIterator(): Iterator; +} + +interface A { + @@iterator(): Iterator; +} + +interface A { + @@asyncIterator(): Iterator; +} diff --git a/packages/babylon/src/plugins/flow.js b/packages/babylon/src/plugins/flow.js index 7981df789a..b5c904ef19 100644 --- a/packages/babylon/src/plugins/flow.js +++ b/packages/babylon/src/plugins/flow.js @@ -8,6 +8,7 @@ import * as N from "../types"; import type { Pos, Position } from "../util/location"; import type State from "../tokenizer/state"; import * as charCodes from "charcodes"; +import { isIteratorStart } from "../util/identifier"; const primitiveTypes = [ "any", @@ -1626,8 +1627,12 @@ export default (superClass: Class): Class => // ensure that inside flow types, we bypass the jsx parser plugin readToken(code: number): void { + const next = this.input.charCodeAt(this.state.pos + 1); if (this.state.inType && (code === 62 || code === 60)) { return this.finishOp(tt.relational, 1); + } else if (isIteratorStart(code, next)) { + this.state.isIterator = true; + return super.readWord(code); } else { return super.readToken(code); } diff --git a/packages/babylon/src/tokenizer/context.js b/packages/babylon/src/tokenizer/context.js index 49393869a0..11fe53d2d2 100644 --- a/packages/babylon/src/tokenizer/context.js +++ b/packages/babylon/src/tokenizer/context.js @@ -73,6 +73,9 @@ tt.name.updateContext = function(prevType) { this.state.exprAllowed = true; } } + if (this.state.isIterator) { + this.state.isIterator = false; + } }; tt.braceL.updateContext = function(prevType) { diff --git a/packages/babylon/src/tokenizer/index.js b/packages/babylon/src/tokenizer/index.js index b6b8fdc501..3fce132907 100644 --- a/packages/babylon/src/tokenizer/index.js +++ b/packages/babylon/src/tokenizer/index.js @@ -1240,6 +1240,8 @@ export default class Tokenizer extends LocationParser { const ch = this.fullCharCodeAtPos(); if (isIdentifierChar(ch)) { this.state.pos += ch <= 0xffff ? 1 : 2; + } else if (this.state.isIterator && ch === charCodes.atSign) { + this.state.pos += 1; } else if (ch === charCodes.backslash) { this.state.containsEsc = true; @@ -1271,6 +1273,10 @@ export default class Tokenizer extends LocationParser { return word + this.input.slice(chunkStart, this.state.pos); } + isIterator(word): boolean { + return word === "@@iterator" || word === "@@asyncIterator"; + } + // Read an identifier or keyword token. Will check for reserved // words when necessary. @@ -1286,6 +1292,14 @@ export default class Tokenizer extends LocationParser { type = keywordTypes[word]; } + // Allow @@iterator and @@asyncIterator as a identifier only inside type + if ( + this.state.isIterator && + (!this.isIterator(word) || !this.state.inType) + ) { + this.raise(this.state.pos, `Invalid identifier ${word}`); + } + this.finishToken(type, word); } diff --git a/packages/babylon/src/util/identifier.js b/packages/babylon/src/util/identifier.js index 15c2dd1971..de5ccb0bcb 100644 --- a/packages/babylon/src/util/identifier.js +++ b/packages/babylon/src/util/identifier.js @@ -87,6 +87,12 @@ export function isIdentifierStart(code: number): boolean { return isInAstralSet(code, astralIdentifierStartCodes); } +// Test whether a current state character code and next character code is @ + +export function isIteratorStart(current: number, next: number): boolean { + return current === 64 && next === 64; +} + // Test whether a given character is part of an identifier. export function isIdentifierChar(code: number): boolean { diff --git a/packages/babylon/test/fixtures/flow/iterator/01/input.js b/packages/babylon/test/fixtures/flow/iterator/01/input.js new file mode 100644 index 0000000000..cc20f143c5 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/01/input.js @@ -0,0 +1,3 @@ +declare class A { + @@iterator(): Iterator; +} diff --git a/packages/babylon/test/fixtures/flow/iterator/01/output.json b/packages/babylon/test/fixtures/flow/iterator/01/output.json new file mode 100644 index 0000000000..c8b579bc72 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/01/output.json @@ -0,0 +1,225 @@ +{ + "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": "DeclareClass", + "start": 0, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "A" + }, + "name": "A" + }, + "typeParameters": null, + "extends": [], + "mixins": [], + "body": { + "type": "ObjectTypeAnnotation", + "start": 16, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start": 20, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "key": { + "type": "Identifier", + "start": 20, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 12 + }, + "identifierName": "@@iterator" + }, + "name": "@@iterator" + }, + "static": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start": 20, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "params": [], + "rest": null, + "typeParameters": null, + "returnType": { + "type": "GenericTypeAnnotation", + "start": 34, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 42, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "params": [ + { + "type": "GenericTypeAnnotation", + "start": 43, + "end": 47, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 43, + "end": 47, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 29 + }, + "identifierName": "File" + }, + "name": "File" + } + } + ] + }, + "id": { + "type": "Identifier", + "start": 34, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 24 + }, + "identifierName": "Iterator" + }, + "name": "Iterator" + } + } + }, + "optional": false + } + ], + "indexers": [], + "exact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/iterator/02/input.js b/packages/babylon/test/fixtures/flow/iterator/02/input.js new file mode 100644 index 0000000000..48ecd5cb3d --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/02/input.js @@ -0,0 +1,3 @@ +declare class A { + @@asyncIterator(): Iterator; +} diff --git a/packages/babylon/test/fixtures/flow/iterator/02/output.json b/packages/babylon/test/fixtures/flow/iterator/02/output.json new file mode 100644 index 0000000000..0b5dd41ee4 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/02/output.json @@ -0,0 +1,225 @@ +{ + "type": "File", + "start": 0, + "end": 56, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 56, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "DeclareClass", + "start": 0, + "end": 56, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "A" + }, + "name": "A" + }, + "typeParameters": null, + "extends": [], + "mixins": [], + "body": { + "type": "ObjectTypeAnnotation", + "start": 16, + "end": 56, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start": 20, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "key": { + "type": "Identifier", + "start": 20, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "@@asyncIterator" + }, + "name": "@@asyncIterator" + }, + "static": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start": 20, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "params": [], + "rest": null, + "typeParameters": null, + "returnType": { + "type": "GenericTypeAnnotation", + "start": 39, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 47, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 29 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "params": [ + { + "type": "GenericTypeAnnotation", + "start": 48, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 48, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 34 + }, + "identifierName": "File" + }, + "name": "File" + } + } + ] + }, + "id": { + "type": "Identifier", + "start": 39, + "end": 47, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 29 + }, + "identifierName": "Iterator" + }, + "name": "Iterator" + } + } + }, + "optional": false + } + ], + "indexers": [], + "exact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/iterator/03/input.js b/packages/babylon/test/fixtures/flow/iterator/03/input.js new file mode 100644 index 0000000000..16c6bb95da --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/03/input.js @@ -0,0 +1 @@ +var @@asyncIterator; diff --git a/packages/babylon/test/fixtures/flow/iterator/03/options.json b/packages/babylon/test/fixtures/flow/iterator/03/options.json new file mode 100644 index 0000000000..7bad2fe143 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/03/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Invalid identifier @@asyncIterator (1:19)" +} diff --git a/packages/babylon/test/fixtures/flow/iterator/04/input.js b/packages/babylon/test/fixtures/flow/iterator/04/input.js new file mode 100644 index 0000000000..cc6faf0462 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/04/input.js @@ -0,0 +1 @@ +var @@iterator; diff --git a/packages/babylon/test/fixtures/flow/iterator/04/options.json b/packages/babylon/test/fixtures/flow/iterator/04/options.json new file mode 100644 index 0000000000..ae3871cf99 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/04/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Invalid identifier @@iterator (1:14)" +} diff --git a/packages/babylon/test/fixtures/flow/iterator/05/input.js b/packages/babylon/test/fixtures/flow/iterator/05/input.js new file mode 100644 index 0000000000..5271a1c31f --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/05/input.js @@ -0,0 +1,3 @@ +class T { + @@asyncIterator(){} +} diff --git a/packages/babylon/test/fixtures/flow/iterator/05/options.json b/packages/babylon/test/fixtures/flow/iterator/05/options.json new file mode 100644 index 0000000000..9d235ec74a --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/05/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Invalid identifier @@asyncIterator (2:17)" +} diff --git a/packages/babylon/test/fixtures/flow/iterator/06/input.js b/packages/babylon/test/fixtures/flow/iterator/06/input.js new file mode 100644 index 0000000000..5732a16269 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/06/input.js @@ -0,0 +1,3 @@ +class T { + @@iterator(){} +} diff --git a/packages/babylon/test/fixtures/flow/iterator/06/options.json b/packages/babylon/test/fixtures/flow/iterator/06/options.json new file mode 100644 index 0000000000..515a2cba19 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/06/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Invalid identifier @@iterator (2:12)" +} diff --git a/packages/babylon/test/fixtures/flow/iterator/07/input.js b/packages/babylon/test/fixtures/flow/iterator/07/input.js new file mode 100644 index 0000000000..5bb6e3cf73 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/07/input.js @@ -0,0 +1 @@ +type @@iterator = number diff --git a/packages/babylon/test/fixtures/flow/iterator/07/options.json b/packages/babylon/test/fixtures/flow/iterator/07/options.json new file mode 100644 index 0000000000..4c1f323d16 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/07/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Invalid identifier @@iterator (1:15)" +} diff --git a/packages/babylon/test/fixtures/flow/iterator/08/input.js b/packages/babylon/test/fixtures/flow/iterator/08/input.js new file mode 100644 index 0000000000..fcc77cf3f8 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/08/input.js @@ -0,0 +1 @@ +type @@asyncIterator = number diff --git a/packages/babylon/test/fixtures/flow/iterator/08/options.json b/packages/babylon/test/fixtures/flow/iterator/08/options.json new file mode 100644 index 0000000000..3efa2ffcd0 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/08/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Invalid identifier @@asyncIterator (1:20)" +} diff --git a/packages/babylon/test/fixtures/flow/iterator/09/input.js b/packages/babylon/test/fixtures/flow/iterator/09/input.js new file mode 100644 index 0000000000..2bec164865 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/09/input.js @@ -0,0 +1,3 @@ +let x = { + @@iterator() {} +} diff --git a/packages/babylon/test/fixtures/flow/iterator/09/options.json b/packages/babylon/test/fixtures/flow/iterator/09/options.json new file mode 100644 index 0000000000..515a2cba19 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/09/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Invalid identifier @@iterator (2:12)" +} diff --git a/packages/babylon/test/fixtures/flow/iterator/10/input.js b/packages/babylon/test/fixtures/flow/iterator/10/input.js new file mode 100644 index 0000000000..b98960ea1d --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/10/input.js @@ -0,0 +1,3 @@ +let x = { + @@asyncIterator() {} +} diff --git a/packages/babylon/test/fixtures/flow/iterator/10/options.json b/packages/babylon/test/fixtures/flow/iterator/10/options.json new file mode 100644 index 0000000000..9d235ec74a --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/10/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Invalid identifier @@asyncIterator (2:17)" +} diff --git a/packages/babylon/test/fixtures/flow/iterator/11/input.js b/packages/babylon/test/fixtures/flow/iterator/11/input.js new file mode 100644 index 0000000000..bb9066173d --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/11/input.js @@ -0,0 +1,3 @@ +let x = { + @@random() {} +} diff --git a/packages/babylon/test/fixtures/flow/iterator/11/options.json b/packages/babylon/test/fixtures/flow/iterator/11/options.json new file mode 100644 index 0000000000..248b8a4d9d --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/11/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Invalid identifier @@random (2:10)" +} diff --git a/packages/babylon/test/fixtures/flow/iterator/12/input.js b/packages/babylon/test/fixtures/flow/iterator/12/input.js new file mode 100644 index 0000000000..a97778edc8 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/12/input.js @@ -0,0 +1,3 @@ +function foo(): { @@iterator: () => string } { + return (0: any); +} diff --git a/packages/babylon/test/fixtures/flow/iterator/12/output.json b/packages/babylon/test/fixtures/flow/iterator/12/output.json new file mode 100644 index 0000000000..c599c26962 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/12/output.json @@ -0,0 +1,277 @@ +{ + "type": "File", + "start": 0, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [], + "predicate": null, + "returnType": { + "type": "TypeAnnotation", + "start": 14, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "typeAnnotation": { + "type": "ObjectTypeAnnotation", + "start": 16, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start": 18, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "key": { + "type": "Identifier", + "start": 18, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 28 + }, + "identifierName": "@@iterator" + }, + "name": "@@iterator" + }, + "static": false, + "kind": "init", + "method": false, + "value": { + "type": "FunctionTypeAnnotation", + "start": 30, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "params": [], + "rest": null, + "returnType": { + "type": "StringTypeAnnotation", + "start": 36, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 42 + } + } + }, + "typeParameters": null + }, + "variance": null, + "optional": false + } + ], + "indexers": [], + "exact": false + } + }, + "body": { + "type": "BlockStatement", + "start": 45, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 45 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 49, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "argument": { + "type": "TypeCastExpression", + "start": 57, + "end": 63, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "expression": { + "type": "NumericLiteral", + "start": 57, + "end": 58, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 58, + "end": 63, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "typeAnnotation": { + "type": "AnyTypeAnnotation", + "start": 60, + "end": 63, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 16 + } + } + } + }, + "extra": { + "parenthesized": true, + "parenStart": 56 + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/iterator/13/input.js b/packages/babylon/test/fixtures/flow/iterator/13/input.js new file mode 100644 index 0000000000..568645dca8 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/13/input.js @@ -0,0 +1,3 @@ +function foo(): { @@asyncIterator: () => string } { + return (0: any); +} diff --git a/packages/babylon/test/fixtures/flow/iterator/13/output.json b/packages/babylon/test/fixtures/flow/iterator/13/output.json new file mode 100644 index 0000000000..43ac1dab55 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/13/output.json @@ -0,0 +1,277 @@ +{ + "type": "File", + "start": 0, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [], + "predicate": null, + "returnType": { + "type": "TypeAnnotation", + "start": 14, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 49 + } + }, + "typeAnnotation": { + "type": "ObjectTypeAnnotation", + "start": 16, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 49 + } + }, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start": 18, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 47 + } + }, + "key": { + "type": "Identifier", + "start": 18, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 33 + }, + "identifierName": "@@asyncIterator" + }, + "name": "@@asyncIterator" + }, + "static": false, + "kind": "init", + "method": false, + "value": { + "type": "FunctionTypeAnnotation", + "start": 35, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 47 + } + }, + "params": [], + "rest": null, + "returnType": { + "type": "StringTypeAnnotation", + "start": 41, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 41 + }, + "end": { + "line": 1, + "column": 47 + } + } + }, + "typeParameters": null + }, + "variance": null, + "optional": false + } + ], + "indexers": [], + "exact": false + } + }, + "body": { + "type": "BlockStatement", + "start": 50, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 50 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 54, + "end": 70, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "argument": { + "type": "TypeCastExpression", + "start": 62, + "end": 68, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "expression": { + "type": "NumericLiteral", + "start": 62, + "end": 63, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 63, + "end": 68, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "typeAnnotation": { + "type": "AnyTypeAnnotation", + "start": 65, + "end": 68, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 16 + } + } + } + }, + "extra": { + "parenthesized": true, + "parenStart": 61 + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/iterator/14/input.js b/packages/babylon/test/fixtures/flow/iterator/14/input.js new file mode 100644 index 0000000000..ad7b034dc6 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/14/input.js @@ -0,0 +1,3 @@ +interface A { + @@iterator(): Iterator; +} diff --git a/packages/babylon/test/fixtures/flow/iterator/14/output.json b/packages/babylon/test/fixtures/flow/iterator/14/output.json new file mode 100644 index 0000000000..79013398e6 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/14/output.json @@ -0,0 +1,225 @@ +{ + "type": "File", + "start": 0, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "InterfaceDeclaration", + "start": 0, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "typeParameters": null, + "extends": [], + "mixins": [], + "body": { + "type": "ObjectTypeAnnotation", + "start": 12, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start": 16, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "key": { + "type": "Identifier", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 12 + }, + "identifierName": "@@iterator" + }, + "name": "@@iterator" + }, + "static": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start": 16, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "params": [], + "rest": null, + "typeParameters": null, + "returnType": { + "type": "GenericTypeAnnotation", + "start": 30, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "params": [ + { + "type": "GenericTypeAnnotation", + "start": 39, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 39, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 29 + }, + "identifierName": "File" + }, + "name": "File" + } + } + ] + }, + "id": { + "type": "Identifier", + "start": 30, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 24 + }, + "identifierName": "Iterator" + }, + "name": "Iterator" + } + } + }, + "optional": false + } + ], + "indexers": [], + "exact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/flow/iterator/15/input.js b/packages/babylon/test/fixtures/flow/iterator/15/input.js new file mode 100644 index 0000000000..f8c4616be8 --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/15/input.js @@ -0,0 +1,3 @@ +interface A { + @@asyncIterator(): Iterator; +} diff --git a/packages/babylon/test/fixtures/flow/iterator/15/output.json b/packages/babylon/test/fixtures/flow/iterator/15/output.json new file mode 100644 index 0000000000..c9a648a19c --- /dev/null +++ b/packages/babylon/test/fixtures/flow/iterator/15/output.json @@ -0,0 +1,225 @@ +{ + "type": "File", + "start": 0, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "InterfaceDeclaration", + "start": 0, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "typeParameters": null, + "extends": [], + "mixins": [], + "body": { + "type": "ObjectTypeAnnotation", + "start": 12, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start": 16, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "key": { + "type": "Identifier", + "start": 16, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "@@asyncIterator" + }, + "name": "@@asyncIterator" + }, + "static": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start": 16, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "params": [], + "rest": null, + "typeParameters": null, + "returnType": { + "type": "GenericTypeAnnotation", + "start": 35, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 43, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 29 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "params": [ + { + "type": "GenericTypeAnnotation", + "start": 44, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 44, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 34 + }, + "identifierName": "File" + }, + "name": "File" + } + } + ] + }, + "id": { + "type": "Identifier", + "start": 35, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 29 + }, + "identifierName": "Iterator" + }, + "name": "Iterator" + } + } + }, + "optional": false + } + ], + "indexers": [], + "exact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file