Skip to content

Commit

Permalink
[parser] reserved types in import-typeof declarations should error
Browse files Browse the repository at this point in the history
Summary: `import typeof { string } from ...` would define a type named `string`, which is reserved. `import { typeof string } from ...` already errors.

Reviewed By: samwgoldman

Differential Revision: D6197490

fbshipit-source-id: 9ed1722df19a1324ff797326b4046fa5320ffe03
  • Loading branch information
mroch authored and facebook-github-bot committed Oct 31, 2017
1 parent 2a8bc50 commit a3e936a
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/parser/statement_parser.ml
Expand Up @@ -1325,10 +1325,11 @@ module Statement
| Some error_if_type, T_TYPE
| Some error_if_type, T_TYPEOF ->
error env error_if_type;
Eat.token env (* consume `type` or `typeof` *)
| _ -> ()
end;
identifier env, None
Eat.token env; (* consume `type` or `typeof` *)
Type.type_identifier env, None
| _ ->
identifier env, None
end

(*
ImportSpecifier[Type]:
Expand Down Expand Up @@ -1414,7 +1415,7 @@ module Statement
(* specifier in an `import typeof { ... }` *)
in let typeof_specifier env =
let remote, local = with_maybe_as env
~for_type:false
~for_type:true
~error_if_type:Error.ImportTypeShorthandOnlyInPureImport
in
{ remote; local; kind = None }
Expand Down
@@ -0,0 +1,2 @@
// 2 errors: typeof specifier in typeof declaration, and `string` is reserved
import typeof {typeof string} from "foo";
@@ -0,0 +1,62 @@
{
"errors":[
{
"loc":{"source":null,"start":{"line":2,"column":15},"end":{"line":2,"column":21}},
"message":"The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements"
},
{
"loc":{"source":null,"start":{"line":2,"column":22},"end":{"line":2,"column":28}},
"message":"Unexpected reserved type"
}
],
"type":"Program",
"loc":{"source":null,"start":{"line":2,"column":0},"end":{"line":2,"column":41}},
"range":[78,119],
"body":[
{
"type":"ImportDeclaration",
"loc":{"source":null,"start":{"line":2,"column":0},"end":{"line":2,"column":41}},
"range":[78,119],
"specifiers":[
{
"type":"ImportSpecifier",
"loc":{"source":null,"start":{"line":2,"column":22},"end":{"line":2,"column":28}},
"range":[100,106],
"imported":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":2,"column":22},"end":{"line":2,"column":28}},
"range":[100,106],
"name":"string",
"typeAnnotation":null,
"optional":false
},
"local":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":2,"column":22},"end":{"line":2,"column":28}},
"range":[100,106],
"name":"string",
"typeAnnotation":null,
"optional":false
},
"importKind":null
}
],
"source":{
"type":"Literal",
"loc":{"source":null,"start":{"line":2,"column":35},"end":{"line":2,"column":40}},
"range":[113,118],
"value":"foo",
"raw":"\"foo\""
},
"importKind":"typeof"
}
],
"comments":[
{
"type":"Line",
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":77}},
"range":[0,77],
"value":" 2 errors: typeof specifier in typeof declaration, and `string` is reserved"
}
]
}
@@ -0,0 +1,2 @@
// error: typeof specifier in typeof declaration
import typeof {typeof switch} from "foo";
@@ -0,0 +1,58 @@
{
"errors":[
{
"loc":{"source":null,"start":{"line":2,"column":15},"end":{"line":2,"column":21}},
"message":"The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements"
}
],
"type":"Program",
"loc":{"source":null,"start":{"line":2,"column":0},"end":{"line":2,"column":41}},
"range":[49,90],
"body":[
{
"type":"ImportDeclaration",
"loc":{"source":null,"start":{"line":2,"column":0},"end":{"line":2,"column":41}},
"range":[49,90],
"specifiers":[
{
"type":"ImportSpecifier",
"loc":{"source":null,"start":{"line":2,"column":22},"end":{"line":2,"column":28}},
"range":[71,77],
"imported":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":2,"column":22},"end":{"line":2,"column":28}},
"range":[71,77],
"name":"switch",
"typeAnnotation":null,
"optional":false
},
"local":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":2,"column":22},"end":{"line":2,"column":28}},
"range":[71,77],
"name":"switch",
"typeAnnotation":null,
"optional":false
},
"importKind":null
}
],
"source":{
"type":"Literal",
"loc":{"source":null,"start":{"line":2,"column":35},"end":{"line":2,"column":40}},
"range":[84,89],
"value":"foo",
"raw":"\"foo\""
},
"importKind":"typeof"
}
],
"comments":[
{
"type":"Line",
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":48}},
"range":[0,48],
"value":" error: typeof specifier in typeof declaration"
}
]
}
@@ -1,4 +1,10 @@
{
"errors":[
{
"loc":{"source":null,"start":{"line":2,"column":16},"end":{"line":2,"column":22}},
"message":"Unexpected reserved type"
}
],
"type":"Program",
"loc":{"source":null,"start":{"line":2,"column":0},"end":{"line":2,"column":36}},
"range":[43,79],
Expand Down
@@ -0,0 +1,2 @@
// ok, `string` is a reserved type but it's renamed
import typeof { string as StringT } from 'foo';
@@ -0,0 +1,52 @@
{
"type":"Program",
"loc":{"source":null,"start":{"line":2,"column":0},"end":{"line":2,"column":47}},
"range":[52,99],
"body":[
{
"type":"ImportDeclaration",
"loc":{"source":null,"start":{"line":2,"column":0},"end":{"line":2,"column":47}},
"range":[52,99],
"specifiers":[
{
"type":"ImportSpecifier",
"loc":{"source":null,"start":{"line":2,"column":16},"end":{"line":2,"column":33}},
"range":[68,85],
"imported":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":2,"column":16},"end":{"line":2,"column":22}},
"range":[68,74],
"name":"string",
"typeAnnotation":null,
"optional":false
},
"local":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":2,"column":26},"end":{"line":2,"column":33}},
"range":[78,85],
"name":"StringT",
"typeAnnotation":null,
"optional":false
},
"importKind":null
}
],
"source":{
"type":"Literal",
"loc":{"source":null,"start":{"line":2,"column":41},"end":{"line":2,"column":46}},
"range":[93,98],
"value":"foo",
"raw":"'foo'"
},
"importKind":"typeof"
}
],
"comments":[
{
"type":"Line",
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":51}},
"range":[0,51],
"value":" ok, `string` is a reserved type but it's renamed"
}
]
}

0 comments on commit a3e936a

Please sign in to comment.