From 74189a07c56eb9b1b8e74e727b523561e499c125 Mon Sep 17 00:00:00 2001 From: Marshall Roch Date: Tue, 31 Oct 2017 12:52:09 -0700 Subject: [PATCH] [parser] forbid reserved types in `opaque type` Summary: `opaque type string = number` would overwrite the `string` builtin. reserved types should be disallowed. Reviewed By: samwgoldman Differential Revision: D6199767 fbshipit-source-id: f0f5bdf44d8cff0d444381747f0ebb5a94df3ffa --- src/parser/statement_parser.ml | 2 +- .../opaque_aliases/invalid/reserved_type.js | 1 + .../invalid/reserved_type.tree.json | 34 +++++++++++++++++++ .../opaque_aliases/valid/reserved_value.js | 1 + .../valid/reserved_value.tree.json | 28 +++++++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/parser/test/flow/types/opaque_aliases/invalid/reserved_type.js create mode 100644 src/parser/test/flow/types/opaque_aliases/invalid/reserved_type.tree.json create mode 100644 src/parser/test/flow/types/opaque_aliases/valid/reserved_value.js create mode 100644 src/parser/test/flow/types/opaque_aliases/valid/reserved_value.tree.json diff --git a/src/parser/statement_parser.ml b/src/parser/statement_parser.ml index c61ae2994d0..2f913fbbf77 100644 --- a/src/parser/statement_parser.ml +++ b/src/parser/statement_parser.ml @@ -558,7 +558,7 @@ module Statement Expect.token env T_OPAQUE; Expect.token env T_TYPE; Eat.push_lex_mode env Lex_mode.TYPE; - let id = Parse.identifier env in + let id = Type.type_identifier env in let typeParameters = Type.type_parameter_declaration_with_defaults env in let supertype = match Peek.token env with | T_COLON -> diff --git a/src/parser/test/flow/types/opaque_aliases/invalid/reserved_type.js b/src/parser/test/flow/types/opaque_aliases/invalid/reserved_type.js new file mode 100644 index 00000000000..af9115c3219 --- /dev/null +++ b/src/parser/test/flow/types/opaque_aliases/invalid/reserved_type.js @@ -0,0 +1 @@ +opaque type string = number; diff --git a/src/parser/test/flow/types/opaque_aliases/invalid/reserved_type.tree.json b/src/parser/test/flow/types/opaque_aliases/invalid/reserved_type.tree.json new file mode 100644 index 00000000000..6d21a6fe256 --- /dev/null +++ b/src/parser/test/flow/types/opaque_aliases/invalid/reserved_type.tree.json @@ -0,0 +1,34 @@ +{ + "errors":[ + { + "loc":{"source":null,"start":{"line":1,"column":12},"end":{"line":1,"column":18}}, + "message":"Unexpected reserved type" + } + ], + "type":"Program", + "loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "range":[0,28], + "body":[ + { + "type":"OpaqueType", + "loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "range":[0,28], + "id":{ + "type":"Identifier", + "loc":{"source":null,"start":{"line":1,"column":12},"end":{"line":1,"column":18}}, + "range":[12,18], + "name":"string", + "typeAnnotation":null, + "optional":false + }, + "typeParameters":null, + "impltype":{ + "type":"NumberTypeAnnotation", + "loc":{"source":null,"start":{"line":1,"column":21},"end":{"line":1,"column":27}}, + "range":[21,27] + }, + "supertype":null + } + ], + "comments":[] +} diff --git a/src/parser/test/flow/types/opaque_aliases/valid/reserved_value.js b/src/parser/test/flow/types/opaque_aliases/valid/reserved_value.js new file mode 100644 index 00000000000..ee2234d431b --- /dev/null +++ b/src/parser/test/flow/types/opaque_aliases/valid/reserved_value.js @@ -0,0 +1 @@ +opaque type switch = number; diff --git a/src/parser/test/flow/types/opaque_aliases/valid/reserved_value.tree.json b/src/parser/test/flow/types/opaque_aliases/valid/reserved_value.tree.json new file mode 100644 index 00000000000..48a55cc1a83 --- /dev/null +++ b/src/parser/test/flow/types/opaque_aliases/valid/reserved_value.tree.json @@ -0,0 +1,28 @@ +{ + "type":"Program", + "loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "range":[0,28], + "body":[ + { + "type":"OpaqueType", + "loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "range":[0,28], + "id":{ + "type":"Identifier", + "loc":{"source":null,"start":{"line":1,"column":12},"end":{"line":1,"column":18}}, + "range":[12,18], + "name":"switch", + "typeAnnotation":null, + "optional":false + }, + "typeParameters":null, + "impltype":{ + "type":"NumberTypeAnnotation", + "loc":{"source":null,"start":{"line":1,"column":21},"end":{"line":1,"column":27}}, + "range":[21,27] + }, + "supertype":null + } + ], + "comments":[] +}