Skip to content

Commit

Permalink
Add parsing support for proto modifier in declare class
Browse files Browse the repository at this point in the history
Summary:
`declare class` creates a nominal instance type. The syntax mirrors the syntax
for ES2015 classes, which supports class properties.

Class properties are still in the standardization process, but it looks very
likely that they will continue to create "own" properties on instances.

Upcoming rowk forces us to be more precise about property layout, so I will be
making changes to improve our precision about own vs. proto.

Class properties are set on the constructed instance as an own property:

```
class A {
  x = () => "A";
}

class B extends A {
  x() { return "B" }
}

var b = new B;

console.log(b.x()); // "A" (own prop shadows proto)
console.log(b.hasOwnProperty("x")); // true
console.log(B.prototype.x()); // "B" (proto still exists)
```

Updating Flow to understand the property layout for class hierarchies like this
is necessary to be precise about object spread, Object.values, etc.

However, in interfaces and declared classes, we occasionally use the class
property syntax to declare what are actually prototype properties:

```
declare class Function {
  bind: Function$Prototype$Apply;
  // ...
}
```

I propose that we add a modifier to indicate that this should be interpreted as
a proto property with the given type instead of a class property.

```
declare class Function {
  proto bind: Function$Prototype$Apply;
  // ...
}
```

Reviewed By: avikchaudhuri

Differential Revision: D7824119

fbshipit-source-id: a553d3c891171a72ff0e1941cdeba8ae31d04f58
  • Loading branch information
samwgoldman authored and facebook-github-bot committed May 1, 2018
1 parent 649c126 commit eb815be
Show file tree
Hide file tree
Showing 99 changed files with 898 additions and 38 deletions.
4 changes: 4 additions & 0 deletions src/common/ty/ty_serializer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ and obj_named_prop x =
value = T.Object.Property.Init t;
optional = fld.fld_optional;
static = false;
proto = false;
_method = false;
variance = variance_ fld.fld_polarity;
}
Expand All @@ -161,6 +162,7 @@ and obj_named_prop x =
value = T.Object.Property.Init (Loc.none, T.Function fun_t);
optional = false;
static = false;
proto = false;
_method = true;
variance = None;
}
Expand All @@ -169,6 +171,7 @@ and obj_named_prop x =
value = T.Object.Property.Get (Loc.none, t);
optional = false;
static = false;
proto = false;
_method = false;
variance = None;
}
Expand All @@ -177,6 +180,7 @@ and obj_named_prop x =
value = T.Object.Property.Set (Loc.none, t);
optional = false;
static = false;
proto = false;
_method = false;
variance = None;
}
Expand Down
1 change: 1 addition & 0 deletions src/parser/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ and Type : sig
value: 'M value;
optional: bool;
static: bool;
proto: bool;
_method: bool;
variance: 'M Variance.t option;
}
Expand Down
3 changes: 2 additions & 1 deletion src/parser/estree_translator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ end with type t = Impl.t) = struct
)

and object_type_property (loc, { Type.Object.Property.
key; value; optional; static; variance = variance_; _method;
key; value; optional; static; proto; variance = variance_; _method;
}) =
let key = match key with
| Expression.Object.Property.Literal lit -> literal lit
Expand All @@ -1134,6 +1134,7 @@ end with type t = Impl.t) = struct
"method", bool _method;
"optional", bool optional;
"static", bool static;
"proto", bool proto;
"variance", option variance variance_;
"kind", string kind;
]
Expand Down
2 changes: 2 additions & 0 deletions src/parser/parse_error.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type t =
| UnexpectedEOS
| UnexpectedVariance
| UnexpectedStatic
| UnexpectedProto
| UnexpectedTypeAlias
| UnexpectedOpaqueTypeAlias
| UnexpectedTypeAnnotation
Expand Down Expand Up @@ -137,6 +138,7 @@ module PP =
| UnexpectedEOS -> "Unexpected end of input"
| UnexpectedVariance -> "Unexpected variance sigil"
| UnexpectedStatic -> "Unexpected static modifier"
| UnexpectedProto -> "Unexpected proto modifier"
| UnexpectedTypeAlias -> "Type aliases are not allowed in untyped mode"
| UnexpectedOpaqueTypeAlias -> "Opaque type aliases are not allowed in untyped mode"
| UnexpectedTypeAnnotation -> "Type annotations are not allowed in untyped mode"
Expand Down
4 changes: 2 additions & 2 deletions src/parser/statement_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ module Statement
Expect.token env T_EXTENDS;
supers env []
end else [] in
let body = Type._object ~allow_static:false env in
let body = Type._object ~allow_static:false ~allow_proto:false env in
Statement.Interface.({
id;
tparams;
Expand Down Expand Up @@ -661,7 +661,7 @@ module Statement
| T_IMPLEMENTS -> Eat.token env; Object.class_implements env []
| _ -> []
in
let body = Type._object ~allow_static:true env in
let body = Type._object ~allow_static:true ~allow_proto:true env in
Statement.DeclareClass.({
id;
tparams;
Expand Down
1 change: 1 addition & 0 deletions src/parser/test/flow/ES6/modules/migrated_0007.tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand All @@ -82,8 +83,8 @@
}
]
},
"superClass":null,
"typeParameters":null,
"superClass":null,
"superTypeParameters":null,
"implements":[],
"decorators":[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand All @@ -75,6 +76,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
},
Expand All @@ -70,6 +71,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
},
Expand All @@ -70,6 +71,7 @@
"method":false,
"optional":true,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"method":true,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"get"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"set"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"get"
},
Expand Down Expand Up @@ -106,6 +107,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"set"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"method":false,
"optional":true,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
},
Expand All @@ -70,6 +71,7 @@
"method":false,
"optional":true,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"get"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"set"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"get"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"set"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
"method":true,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
"method":true,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"method":true,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"method":true,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"method":true,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"method":true,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"method":false,
"optional":false,
"static":false,
"proto":false,
"variance":null,
"kind":"init"
}
Expand Down
Loading

0 comments on commit eb815be

Please sign in to comment.