Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Do not allow overwritting of primitive types (#314)
Browse files Browse the repository at this point in the history
* Do not allow overwritting of primitive types

* Better name for method
  • Loading branch information
danez committed Jan 20, 2017
1 parent 461ed45 commit 0a00aff
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/plugins/flow.js
Expand Up @@ -4,6 +4,18 @@ import { types as tt } from "../tokenizer/types";
import { types as ct } from "../tokenizer/context";
import Parser from "../parser";

const primitiveTypes = [
"any",
"mixed",
"empty",
"bool",
"boolean",
"number",
"string",
"void",
"null"
];

const pp = Parser.prototype;

pp.flowParseTypeInitialiser = function (tok) {
Expand Down Expand Up @@ -188,10 +200,18 @@ pp.flowParseInterface = function (node) {
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

pp.flowParseTypeAlias = function (node) {
node.id = this.parseIdentifier();
node.id = this.flowParseRestrictedIdentifier();

if (this.isRelational("<")) {
node.typeParameters = this.flowParseTypeParameterDeclaration();
Expand Down Expand Up @@ -219,7 +239,7 @@ pp.flowParseTypeParameter = function () {

if (this.match(tt.eq)) {
this.eat(tt.eq);
node.default = this.flowParseType ();
node.default = this.flowParseType();
}

return this.finishNode(node, "TypeParameter");
Expand Down Expand Up @@ -770,7 +790,7 @@ pp.flowParseTypeAnnotation = function () {
};

pp.flowParseTypeAnnotatableIdentifier = function () {
const ident = this.parseIdentifier();
const ident = this.flowParseRestrictedIdentifier();
if (this.match(tt.colon)) {
ident.typeAnnotation = this.flowParseTypeAnnotation();
this.finishNode(ident, ident.type);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/flow/type-annotations/131/actual.js
@@ -0,0 +1 @@
type number = string;
3 changes: 3 additions & 0 deletions test/fixtures/flow/type-annotations/131/options.json
@@ -0,0 +1,3 @@
{
"throws": "Cannot overwrite primitive type number (1:5)"
}
1 change: 1 addition & 0 deletions test/fixtures/flow/type-annotations/132/actual.js
@@ -0,0 +1 @@
type foo<number> = string;
3 changes: 3 additions & 0 deletions test/fixtures/flow/type-annotations/132/options.json
@@ -0,0 +1,3 @@
{
"throws": "Cannot overwrite primitive type number (1:9)"
}
3 changes: 3 additions & 0 deletions test/fixtures/flow/type-annotations/133/actual.js
@@ -0,0 +1,3 @@
function a<string>(x: string): string {
return x;
}
3 changes: 3 additions & 0 deletions test/fixtures/flow/type-annotations/133/options.json
@@ -0,0 +1,3 @@
{
"throws": "Cannot overwrite primitive type string (1:11)"
}
1 change: 1 addition & 0 deletions test/fixtures/flow/type-annotations/134/actual.js
@@ -0,0 +1 @@
declare type bool = any;
3 changes: 3 additions & 0 deletions test/fixtures/flow/type-annotations/134/options.json
@@ -0,0 +1,3 @@
{
"throws": "Cannot overwrite primitive type bool (1:13)"
}

0 comments on commit 0a00aff

Please sign in to comment.