diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 6c22a44ae3..79044f46bf 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -690,15 +690,16 @@ pp.flowParsePrimaryType = function () { }; pp.flowParsePostfixType = function () { - let node = this.startNode(); - let type = node.elementType = this.flowParsePrimaryType(); - if (this.match(tt.bracketL)) { + const startPos = this.state.start, startLoc = this.state.startLoc; + let type = this.flowParsePrimaryType(); + while (!this.canInsertSemicolon() && this.match(tt.bracketL)) { + const node = this.startNodeAt(startPos, startLoc); + node.elementType = type; this.expect(tt.bracketL); this.expect(tt.bracketR); - return this.finishNode(node, "ArrayTypeAnnotation"); - } else { - return type; + type = this.finishNode(node, "ArrayTypeAnnotation"); } + return type; }; pp.flowParsePrefixType = function () { diff --git a/test/fixtures/flow/array-types/7/actual.js b/test/fixtures/flow/array-types/7/actual.js new file mode 100644 index 0000000000..ff9f99dde5 --- /dev/null +++ b/test/fixtures/flow/array-types/7/actual.js @@ -0,0 +1 @@ +var a: number[][] diff --git a/test/fixtures/flow/array-types/7/expected.json b/test/fixtures/flow/array-types/7/expected.json new file mode 100644 index 0000000000..dc3060e046 --- /dev/null +++ b/test/fixtures/flow/array-types/7/expected.json @@ -0,0 +1,144 @@ +{ + "type": "File", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "typeAnnotation": { + "type": "ArrayTypeAnnotation", + "start": 7, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "elementType": { + "type": "ArrayTypeAnnotation", + "start": 7, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "elementType": { + "type": "NumberTypeAnnotation", + "start": 7, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 13 + } + } + } + } + } + } + }, + "init": null + } + ], + "kind": "var" + } + ] + }, + "comments": [] +} diff --git a/test/fixtures/flow/array-types/8/actual.js b/test/fixtures/flow/array-types/8/actual.js new file mode 100644 index 0000000000..3b6d7d2c73 --- /dev/null +++ b/test/fixtures/flow/array-types/8/actual.js @@ -0,0 +1 @@ +var a: number[][][] diff --git a/test/fixtures/flow/array-types/8/expected.json b/test/fixtures/flow/array-types/8/expected.json new file mode 100644 index 0000000000..624bd8732a --- /dev/null +++ b/test/fixtures/flow/array-types/8/expected.json @@ -0,0 +1,158 @@ +{ + "type": "File", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "typeAnnotation": { + "type": "ArrayTypeAnnotation", + "start": 7, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "elementType": { + "type": "ArrayTypeAnnotation", + "start": 7, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "elementType": { + "type": "ArrayTypeAnnotation", + "start": 7, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "elementType": { + "type": "NumberTypeAnnotation", + "start": 7, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 13 + } + } + } + } + } + } + } + }, + "init": null + } + ], + "kind": "var" + } + ] + }, + "comments": [] +} diff --git a/test/fixtures/flow/array-types/9/actual.js b/test/fixtures/flow/array-types/9/actual.js new file mode 100644 index 0000000000..6c5978ee47 --- /dev/null +++ b/test/fixtures/flow/array-types/9/actual.js @@ -0,0 +1,2 @@ +var a: number +[] diff --git a/test/fixtures/flow/array-types/9/expected.json b/test/fixtures/flow/array-types/9/expected.json new file mode 100644 index 0000000000..9ac33d433a --- /dev/null +++ b/test/fixtures/flow/array-types/9/expected.json @@ -0,0 +1,144 @@ +{ + "type": "File", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 2 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 2 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "typeAnnotation": { + "type": "NumberTypeAnnotation", + "start": 7, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 13 + } + } + } + } + }, + "init": null + } + ], + "kind": "var" + }, + { + "type": "ExpressionStatement", + "start": 14, + "end": 16, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 2 + } + }, + "expression": { + "type": "ArrayExpression", + "start": 14, + "end": 16, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 2 + } + }, + "elements": [] + } + } + ] + }, + "comments": [] +}