Parse flow nested array type annotations like number[][] (#219)

* Parse flow nested array type annotations like number[][]

Fixes #217.

* Do not parse a newline separated array statement into a flow type annotation

* Get rid of lineBreak dependency in flow.js in favor of higher level method
This commit is contained in:
Bernhard Häussner 2016-11-13 10:22:16 +01:00 committed by Daniel Tschinder
parent 6cb023590a
commit 01ed943deb
7 changed files with 457 additions and 6 deletions

View File

@ -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 () {

View File

@ -0,0 +1 @@
var a: number[][]

View File

@ -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": []
}

View File

@ -0,0 +1 @@
var a: number[][][]

View File

@ -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": []
}

View File

@ -0,0 +1,2 @@
var a: number
[]

View File

@ -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": []
}