Do not allow overwritting of primitive types (#314)
* Do not allow overwritting of primitive types * Better name for method
This commit is contained in:
parent
461ed45942
commit
0a00aff2fe
@ -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();
|
||||||
@ -219,7 +239,7 @@ pp.flowParseTypeParameter = function () {
|
|||||||
|
|
||||||
if (this.match(tt.eq)) {
|
if (this.match(tt.eq)) {
|
||||||
this.eat(tt.eq);
|
this.eat(tt.eq);
|
||||||
node.default = this.flowParseType ();
|
node.default = this.flowParseType();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.finishNode(node, "TypeParameter");
|
return this.finishNode(node, "TypeParameter");
|
||||||
@ -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);
|
||||||
|
|||||||
1
test/fixtures/flow/type-annotations/131/actual.js
vendored
Normal file
1
test/fixtures/flow/type-annotations/131/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
type number = string;
|
||||||
3
test/fixtures/flow/type-annotations/131/options.json
vendored
Normal file
3
test/fixtures/flow/type-annotations/131/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"throws": "Cannot overwrite primitive type number (1:5)"
|
||||||
|
}
|
||||||
1
test/fixtures/flow/type-annotations/132/actual.js
vendored
Normal file
1
test/fixtures/flow/type-annotations/132/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
type foo<number> = string;
|
||||||
3
test/fixtures/flow/type-annotations/132/options.json
vendored
Normal file
3
test/fixtures/flow/type-annotations/132/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"throws": "Cannot overwrite primitive type number (1:9)"
|
||||||
|
}
|
||||||
3
test/fixtures/flow/type-annotations/133/actual.js
vendored
Normal file
3
test/fixtures/flow/type-annotations/133/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
function a<string>(x: string): string {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
3
test/fixtures/flow/type-annotations/133/options.json
vendored
Normal file
3
test/fixtures/flow/type-annotations/133/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"throws": "Cannot overwrite primitive type string (1:11)"
|
||||||
|
}
|
||||||
1
test/fixtures/flow/type-annotations/134/actual.js
vendored
Normal file
1
test/fixtures/flow/type-annotations/134/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
declare type bool = any;
|
||||||
3
test/fixtures/flow/type-annotations/134/options.json
vendored
Normal file
3
test/fixtures/flow/type-annotations/134/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"throws": "Cannot overwrite primitive type bool (1:13)"
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user