Do not allow overwritting of primitive types (#314)

* Do not allow overwritting of primitive types

* Better name for method
This commit is contained in:
Daniel Tschinder 2017-01-20 22:22:25 +01:00 committed by GitHub
parent 461ed45942
commit 0a00aff2fe
9 changed files with 41 additions and 3 deletions

View File

@ -4,6 +4,18 @@ import { types as tt } from "../tokenizer/types";
import { types as ct } from "../tokenizer/context"; import { types as ct } from "../tokenizer/context";
import Parser from "../parser"; import Parser from "../parser";
const primitiveTypes = [
"any",
"mixed",
"empty",
"bool",
"boolean",
"number",
"string",
"void",
"null"
];
const pp = Parser.prototype; const pp = Parser.prototype;
pp.flowParseTypeInitialiser = function (tok) { pp.flowParseTypeInitialiser = function (tok) {
@ -188,10 +200,18 @@ pp.flowParseInterface = function (node) {
return this.finishNode(node, "InterfaceDeclaration"); return this.finishNode(node, "InterfaceDeclaration");
}; };
pp.flowParseRestrictedIdentifier = function(liberal) {
if (primitiveTypes.indexOf(this.state.value) > -1) {
this.raise(this.state.start, `Cannot overwrite primitive type ${this.state.value}`);
}
return this.parseIdentifier(liberal);
};
// Type aliases // Type aliases
pp.flowParseTypeAlias = function (node) { pp.flowParseTypeAlias = function (node) {
node.id = this.parseIdentifier(); node.id = this.flowParseRestrictedIdentifier();
if (this.isRelational("<")) { if (this.isRelational("<")) {
node.typeParameters = this.flowParseTypeParameterDeclaration(); node.typeParameters = this.flowParseTypeParameterDeclaration();
@ -770,7 +790,7 @@ pp.flowParseTypeAnnotation = function () {
}; };
pp.flowParseTypeAnnotatableIdentifier = function () { pp.flowParseTypeAnnotatableIdentifier = function () {
const ident = this.parseIdentifier(); const ident = this.flowParseRestrictedIdentifier();
if (this.match(tt.colon)) { if (this.match(tt.colon)) {
ident.typeAnnotation = this.flowParseTypeAnnotation(); ident.typeAnnotation = this.flowParseTypeAnnotation();
this.finishNode(ident, ident.type); this.finishNode(ident, ident.type);

View File

@ -0,0 +1 @@
type number = string;

View File

@ -0,0 +1,3 @@
{
"throws": "Cannot overwrite primitive type number (1:5)"
}

View File

@ -0,0 +1 @@
type foo<number> = string;

View File

@ -0,0 +1,3 @@
{
"throws": "Cannot overwrite primitive type number (1:9)"
}

View File

@ -0,0 +1,3 @@
function a<string>(x: string): string {
return x;
}

View File

@ -0,0 +1,3 @@
{
"throws": "Cannot overwrite primitive type string (1:11)"
}

View File

@ -0,0 +1 @@
declare type bool = any;

View File

@ -0,0 +1,3 @@
{
"throws": "Cannot overwrite primitive type bool (1:13)"
}