From 0a00aff2fe313f32b6019478527ec061b064d57f Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Fri, 20 Jan 2017 22:22:25 +0100 Subject: [PATCH] Do not allow overwritting of primitive types (#314) * Do not allow overwritting of primitive types * Better name for method --- src/plugins/flow.js | 26 ++++++++++++++++--- .../flow/type-annotations/131/actual.js | 1 + .../flow/type-annotations/131/options.json | 3 +++ .../flow/type-annotations/132/actual.js | 1 + .../flow/type-annotations/132/options.json | 3 +++ .../flow/type-annotations/133/actual.js | 3 +++ .../flow/type-annotations/133/options.json | 3 +++ .../flow/type-annotations/134/actual.js | 1 + .../flow/type-annotations/134/options.json | 3 +++ 9 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/flow/type-annotations/131/actual.js create mode 100644 test/fixtures/flow/type-annotations/131/options.json create mode 100644 test/fixtures/flow/type-annotations/132/actual.js create mode 100644 test/fixtures/flow/type-annotations/132/options.json create mode 100644 test/fixtures/flow/type-annotations/133/actual.js create mode 100644 test/fixtures/flow/type-annotations/133/options.json create mode 100644 test/fixtures/flow/type-annotations/134/actual.js create mode 100644 test/fixtures/flow/type-annotations/134/options.json diff --git a/src/plugins/flow.js b/src/plugins/flow.js index bd44f00f74..8927bed198 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -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) { @@ -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(); @@ -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"); @@ -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); diff --git a/test/fixtures/flow/type-annotations/131/actual.js b/test/fixtures/flow/type-annotations/131/actual.js new file mode 100644 index 0000000000..4823272478 --- /dev/null +++ b/test/fixtures/flow/type-annotations/131/actual.js @@ -0,0 +1 @@ +type number = string; diff --git a/test/fixtures/flow/type-annotations/131/options.json b/test/fixtures/flow/type-annotations/131/options.json new file mode 100644 index 0000000000..bb5c8425ac --- /dev/null +++ b/test/fixtures/flow/type-annotations/131/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Cannot overwrite primitive type number (1:5)" +} diff --git a/test/fixtures/flow/type-annotations/132/actual.js b/test/fixtures/flow/type-annotations/132/actual.js new file mode 100644 index 0000000000..3935d92d3c --- /dev/null +++ b/test/fixtures/flow/type-annotations/132/actual.js @@ -0,0 +1 @@ +type foo = string; diff --git a/test/fixtures/flow/type-annotations/132/options.json b/test/fixtures/flow/type-annotations/132/options.json new file mode 100644 index 0000000000..0148048697 --- /dev/null +++ b/test/fixtures/flow/type-annotations/132/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Cannot overwrite primitive type number (1:9)" +} diff --git a/test/fixtures/flow/type-annotations/133/actual.js b/test/fixtures/flow/type-annotations/133/actual.js new file mode 100644 index 0000000000..b8fa44f01c --- /dev/null +++ b/test/fixtures/flow/type-annotations/133/actual.js @@ -0,0 +1,3 @@ +function a(x: string): string { + return x; +} diff --git a/test/fixtures/flow/type-annotations/133/options.json b/test/fixtures/flow/type-annotations/133/options.json new file mode 100644 index 0000000000..fb0222c596 --- /dev/null +++ b/test/fixtures/flow/type-annotations/133/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Cannot overwrite primitive type string (1:11)" +} diff --git a/test/fixtures/flow/type-annotations/134/actual.js b/test/fixtures/flow/type-annotations/134/actual.js new file mode 100644 index 0000000000..e8b851175e --- /dev/null +++ b/test/fixtures/flow/type-annotations/134/actual.js @@ -0,0 +1 @@ +declare type bool = any; diff --git a/test/fixtures/flow/type-annotations/134/options.json b/test/fixtures/flow/type-annotations/134/options.json new file mode 100644 index 0000000000..839b0e04ef --- /dev/null +++ b/test/fixtures/flow/type-annotations/134/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Cannot overwrite primitive type bool (1:13)" +}