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 6 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
115 changes: 97 additions & 18 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({
allowStatic: isClass,
allowExact: false,
allowSpread: false,
allowProto: isClass,
allowInexact: false,
});
}

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

node.body = this.flowParseObjectType(false, false, false, false);
node.body = this.flowParseObjectType({
allowStatic: false,
allowExact: false,
allowSpread: false,
allowProto: false,
allowInexact: false,
});

return this.finishNode(node, "InterfaceTypeAnnotation");
}
Expand Down Expand Up @@ -754,12 +766,19 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, "ObjectTypeCallProperty");
}

flowParseObjectType(
flowParseObjectType({
allowStatic,
allowExact,
allowSpread,
allowProto,
allowInexact,
}: {
allowStatic: boolean,
allowExact: boolean,
allowSpread: boolean,
allowProto: boolean,
): N.FlowObjectTypeAnnotation {
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 +791,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 +872,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 +917,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 +936,41 @@ 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: {
Copy link
Member

Choose a reason for hiding this comment

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

I think that you can also remove this outer switch, since the ifs already handle only these four cases.

const 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",
);
return false;
Copy link
Member

Choose a reason for hiding this comment

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

Those retuens aren't needed, since this.unexpected already throws. If you want to be explicit, I think that it would be more correct to use throw this.unexpected.

}

return this.finishNode(node, "ObjectTypeSpreadProperty");
if (isInexactToken) {
this.unexpected(
null,
"Explicit inexact syntax must appear at the end of an inexact object",
);
return false;
}
}
default:
node.argument = this.flowParseType();
return this.finishNode(node, "ObjectTypeSpreadProperty");
}
} else {
node.key = this.flowParseObjectPropertyKey();
node.static = isStatic;
Expand Down Expand Up @@ -1146,10 +1213,22 @@ export default (superClass: Class<Parser>): Class<Parser> =>
);

case tt.braceL:
return this.flowParseObjectType(false, false, true, false);
return this.flowParseObjectType({
allowStatic: false,
allowExact: false,
allowSpread: true,
allowProto: false,
allowInexact: true,
});

case tt.braceBarL:
return this.flowParseObjectType(false, true, true, false);
return this.flowParseObjectType({
allowStatic: false,
allowExact: true,
allowSpread: true,
allowProto: false,
allowInexact: 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};