Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flow] Explicit inexact objects with ... #8884

Merged
merged 7 commits into from Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 72 additions & 16 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -463,7 +463,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} while (this.eat(tt.comma));
}

node.body = this.flowParseObjectType(isClass, false, false, isClass);
node.body = this.flowParseObjectType(
isClass,
false,
false,
isClass,
false,
);
}

flowParseInterfaceExtends(): N.FlowInterfaceExtends {
Expand Down Expand Up @@ -656,7 +662,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} while (this.eat(tt.comma));
}

node.body = this.flowParseObjectType(false, false, false, false);
node.body = this.flowParseObjectType(false, false, false, false, false);

return this.finishNode(node, "InterfaceTypeAnnotation");
}
Expand Down Expand Up @@ -759,6 +765,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
allowExact: boolean,
allowSpread: boolean,
allowProto: boolean,
allowInexact: boolean,
jbrown215 marked this conversation as resolved.
Show resolved Hide resolved
): N.FlowObjectTypeAnnotation {
const oldInType = this.state.inType;
this.state.inType = true;
Expand All @@ -772,6 +779,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

let endDelim;
let exact;
let inexact = false;
if (allowExact && this.match(tt.braceBarL)) {
this.expect(tt.braceBarL);
endDelim = tt.braceBarR;
Expand Down Expand Up @@ -852,23 +860,37 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}

nodeStart.properties.push(
this.flowParseObjectTypeProperty(
node,
isStatic,
protoStart,
variance,
kind,
allowSpread,
),
const propOrInexact = this.flowParseObjectTypeProperty(
node,
isStatic,
protoStart,
variance,
kind,
allowSpread,
allowInexact,
);

if (typeof propOrInexact === "boolean") {
inexact = inexact || propOrInexact;
} else {
nodeStart.properties.push(propOrInexact);
}
}

this.flowObjectTypeSemicolon();
}

this.expect(endDelim);

/* The inexact flag should only be added on ObjectTypeAnnotations that
* are not the body of an interface, declare interface, or declare class.
* Since spreads are only allowed in objec types, checking that is
* sufficient here.
*/
if (allowSpread) {
jbrown215 marked this conversation as resolved.
Show resolved Hide resolved
nodeStart.inexact = inexact;
}

const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");

this.state.inType = oldInType;
Expand All @@ -883,7 +905,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
variance: ?N.FlowVariance,
kind: string,
allowSpread: boolean,
): N.FlowObjectTypeProperty | N.FlowObjectTypeSpreadProperty {
allowInexact: boolean,
): (N.FlowObjectTypeProperty | N.FlowObjectTypeSpreadProperty) | boolean {
if (this.match(tt.ellipsis)) {
if (!allowSpread) {
this.unexpected(
Expand All @@ -901,9 +924,42 @@ export default (superClass: Class<Parser>): Class<Parser> =>
);
}
this.expect(tt.ellipsis);
node.argument = this.flowParseType();
switch (this.state.type) {
case tt.comma:
case tt.semi:
case tt.braceR:
case tt.braceBarR:
this.flowObjectTypeSemicolon();
switch (this.state.type) {
case tt.braceR:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think that this code would be more readable without nested switch statemets:

let isInexactToken = this.eat(tt.comma) || this.eat(tt.semi);

if (this.match(tt.braceR)) {
  if (allowInexact) return true;
  this.unexpected(null, "Explicit inexact syntax is only allowed inside inexact objects");
}

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");

Do what you prefer though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this better, will follow up

if (allowInexact) {
return true;
} else {
this.unexpected(
null,
"Explicit inexact syntax is only allowed inside inexact objects",
);
}
return false;
case tt.braceBarR:
this.unexpected(
null,
"Explicit inexact syntax cannot appear inside an explicit exact object type",
);
return false;

default:
this.unexpected(
null,
"Explicit inexact syntax must appear at the end of an inexact object",
);
return false;
}

return this.finishNode(node, "ObjectTypeSpreadProperty");
default:
node.argument = this.flowParseType();
return this.finishNode(node, "ObjectTypeSpreadProperty");
}
} else {
node.key = this.flowParseObjectPropertyKey();
node.static = isStatic;
Expand Down Expand Up @@ -1146,10 +1202,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
);

case tt.braceL:
return this.flowParseObjectType(false, false, true, false);
return this.flowParseObjectType(false, false, true, false, true);

case tt.braceBarL:
return this.flowParseObjectType(false, true, true, false);
return this.flowParseObjectType(false, true, true, false, false);

case tt.bracketL:
return this.flowParseTupleType();
Expand Down
Expand Up @@ -156,7 +156,8 @@
"properties": [],
"indexers": [],
"internalSlots": [],
"exact": false
"exact": false,
"inexact": false
}
}
},
Expand Down
Expand Up @@ -156,7 +156,8 @@
"properties": [],
"indexers": [],
"internalSlots": [],
"exact": false
"exact": false,
"inexact": false
}
}
},
Expand Down
Expand Up @@ -308,7 +308,8 @@
],
"indexers": [],
"internalSlots": [],
"exact": false
"exact": false,
"inexact": false
}
}
},
Expand Down
Expand Up @@ -256,7 +256,8 @@
"properties": [],
"indexers": [],
"internalSlots": [],
"exact": false
"exact": false,
"inexact": false
}
}
},
Expand Down
Expand Up @@ -239,7 +239,8 @@
],
"indexers": [],
"internalSlots": [],
"exact": false
"exact": false,
"inexact": false
}
}
],
Expand Down
Expand Up @@ -239,7 +239,8 @@
],
"indexers": [],
"internalSlots": [],
"exact": false
"exact": false,
"inexact": false
}
}
],
Expand Down
Expand Up @@ -146,7 +146,8 @@
],
"indexers": [],
"internalSlots": [],
"exact": false
"exact": false,
"inexact": false
}
}
}
Expand Down
Expand Up @@ -192,7 +192,8 @@
],
"indexers": [],
"internalSlots": [],
"exact": false
"exact": false,
"inexact": false
}
}
}
Expand Down
Expand Up @@ -243,7 +243,8 @@
}
],
"internalSlots": [],
"exact": false
"exact": false,
"inexact": false
}
}
],
Expand Down
Expand Up @@ -422,7 +422,8 @@
"properties": [],
"indexers": [],
"internalSlots": [],
"exact": false
"exact": false,
"inexact": false
}
}
],
Expand Down
@@ -0,0 +1,4 @@
//@flow
declare class A {
...;
}
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["jsx", "flow"],
"throws": "Spread operator cannot appear in class or interface definitions (3:2)"
}
@@ -0,0 +1,5 @@
//@flow
declare class B {
foo: number;
...;
}
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["jsx", "flow"],
"throws": "Spread operator cannot appear in class or interface definitions (4:2)"
}
@@ -0,0 +1,5 @@
//@flow
declare class C {
...;
foo: number;
}
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["jsx", "flow"],
"throws": "Spread operator cannot appear in class or interface definitions (3:2)"
}
@@ -0,0 +1,6 @@
//@flow
declare class D {
foo: number;
...;
bar: number;
}
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["jsx", "flow"],
"throws": "Spread operator cannot appear in class or interface definitions (4:2)"
}
@@ -0,0 +1,5 @@
//@flow
interface F {
foo: number;
...;
}
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["jsx", "flow"],
"throws": "Spread operator cannot appear in class or interface definitions (4:2)"
}
@@ -0,0 +1,5 @@
//@flow
interface G {
...;
foo: number;
}
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["jsx", "flow"],
"throws": "Spread operator cannot appear in class or interface definitions (3:2)"
}
@@ -0,0 +1,6 @@
//@flow
interface H {
foo: number;
...;
bar: number;
}
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["jsx", "flow"],
"throws": "Spread operator cannot appear in class or interface definitions (4:2)"
}
@@ -0,0 +1,2 @@
//@flow
type T = {| foo: number, ... |}
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["jsx", "flow"],
"throws": "Explicit inexact syntax cannot appear inside an explicit exact object type (2:29)"
}
@@ -0,0 +1,2 @@
//@flow
type T = {..., foo: number};
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["jsx", "flow"],
"throws": "Explicit inexact syntax must appear at the end of an inexact object (2:15)"
}
@@ -0,0 +1,4 @@
//@flow
type T = {...};
type U = {x: number, ...};
type V = {x: number, ...V, ...U};