Skip to content

Commit

Permalink
Fix flow inexact type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 31, 2019
1 parent 9f985e0 commit 11f1fc9
Show file tree
Hide file tree
Showing 22 changed files with 1,914 additions and 49 deletions.
73 changes: 45 additions & 28 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -853,6 +853,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
while (!this.match(endDelim)) {
let isStatic = false;
let protoStart: ?number = null;
let inexactStart: ?number = null;
const node = this.startNode();

if (allowProto && this.isContextual("proto")) {
Expand Down Expand Up @@ -925,17 +926,29 @@ export default (superClass: Class<Parser>): Class<Parser> =>
variance,
kind,
allowSpread,
allowInexact,
allowInexact == null ? !exact : allowInexact,
);

if (propOrInexact === null) {
inexact = true;
inexactStart = this.state.lastTokStart;
} else {
nodeStart.properties.push(propOrInexact);
}
}

this.flowObjectTypeSemicolon();

if (
inexactStart &&
!this.match(tt.braceR) &&
!this.match(tt.braceBarR)
) {
this.raise(
inexactStart,
"Explicit inexact syntax must appear at the end of an inexact object",
);
}
}

this.expect(endDelim);
Expand Down Expand Up @@ -965,10 +978,39 @@ export default (superClass: Class<Parser>): Class<Parser> =>
allowSpread: boolean,
allowInexact: boolean,
): (N.FlowObjectTypeProperty | N.FlowObjectTypeSpreadProperty) | null {
if (this.match(tt.ellipsis)) {
if (this.eat(tt.ellipsis)) {
const isInexactToken =
this.match(tt.comma) ||
this.match(tt.semi) ||
this.match(tt.braceR) ||
this.match(tt.braceBarR);

if (isInexactToken) {
if (!allowSpread) {
this.raise(
this.state.lastTokStart,
"Explicit inexact syntax cannot appear in class or interface definitions",
);
} else if (!allowInexact) {
this.raise(
this.state.lastTokStart,
//"Explicit inexact syntax is only allowed inside inexact objects",
"Explicit inexact syntax cannot appear inside an explicit exact object type",
);
}
if (variance) {
this.raise(
variance.start,
"Explicit inexact syntax cannot have variance",
);
}

return null;
}

if (!allowSpread) {
this.raise(
this.state.start,
this.state.lastTokStart,
"Spread operator cannot appear in class or interface definitions",
);
}
Expand All @@ -978,32 +1020,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (variance) {
this.raise(variance.start, "Spread properties cannot have variance");
}
this.expect(tt.ellipsis);
const isInexactToken = this.eat(tt.comma) || this.eat(tt.semi);

if (this.match(tt.braceR)) {
if (allowSpread && !allowInexact) {
this.raise(
this.state.start,
"Explicit inexact syntax is only allowed inside inexact objects",
);
}
return null;
}

if (this.match(tt.braceBarR)) {
this.unexpected(
null,
"Explicit inexact syntax cannot appear inside an explicit exact object type",
);
}

if (isInexactToken) {
this.unexpected(
null,
"Explicit inexact syntax must appear at the end of an inexact object",
);
}
node.argument = this.flowParseType();
return this.finishNode(node, "ObjectTypeSpreadProperty");
} else {
Expand Down
Expand Up @@ -13,7 +13,7 @@
}
},
"errors": [
"SyntaxError: Spread operator cannot appear in class or interface definitions (3:2)"
"SyntaxError: Explicit inexact syntax cannot appear in class or interface definitions (3:2)"
],
"program": {
"type": "Program",
Expand Down
Expand Up @@ -13,7 +13,7 @@
}
},
"errors": [
"SyntaxError: Spread operator cannot appear in class or interface definitions (4:2)"
"SyntaxError: Explicit inexact syntax cannot appear in class or interface definitions (4:2)"
],
"program": {
"type": "Program",
Expand Down
@@ -1,5 +1,4 @@
{
"sourceType": "module",
"plugins": ["jsx", "flow"],
"throws": "Explicit inexact syntax must appear at the end of an inexact object (4:2)"
"plugins": ["jsx", "flow"]
}
@@ -0,0 +1,185 @@
{
"type": "File",
"start": 0,
"end": 49,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"errors": [
"SyntaxError: Explicit inexact syntax cannot appear in class or interface definitions (3:2)",
"SyntaxError: Explicit inexact syntax must appear at the end of an inexact object (3:2)"
],
"program": {
"type": "Program",
"start": 0,
"end": 49,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "DeclareClass",
"start": 8,
"end": 49,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 22,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 15
},
"identifierName": "C"
},
"name": "C"
},
"typeParameters": null,
"extends": [],
"implements": [],
"mixins": [],
"body": {
"type": "ObjectTypeAnnotation",
"start": 24,
"end": 49,
"loc": {
"start": {
"line": 2,
"column": 16
},
"end": {
"line": 5,
"column": 1
}
},
"callProperties": [],
"properties": [
{
"type": "ObjectTypeProperty",
"start": 35,
"end": 46,
"loc": {
"start": {
"line": 4,
"column": 2
},
"end": {
"line": 4,
"column": 13
}
},
"key": {
"type": "Identifier",
"start": 35,
"end": 38,
"loc": {
"start": {
"line": 4,
"column": 2
},
"end": {
"line": 4,
"column": 5
},
"identifierName": "foo"
},
"name": "foo"
},
"static": false,
"proto": false,
"kind": "init",
"method": false,
"value": {
"type": "NumberTypeAnnotation",
"start": 40,
"end": 46,
"loc": {
"start": {
"line": 4,
"column": 7
},
"end": {
"line": 4,
"column": 13
}
}
},
"variance": null,
"optional": false
}
],
"indexers": [],
"internalSlots": [],
"exact": false
},
"leadingComments": [
{
"type": "CommentLine",
"value": "@flow",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
}
}
}
]
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": "@flow",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
}
}
}
]
}
@@ -1,5 +1,4 @@
{
"sourceType": "module",
"plugins": ["jsx", "flow"],
"throws": "Explicit inexact syntax must appear at the end of an inexact object (5:2)"
"plugins": ["jsx", "flow"]
}

0 comments on commit 11f1fc9

Please sign in to comment.