Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some incorrect typeof parsing in flow #10657

Merged
merged 1 commit into from Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 40 additions & 16 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -22,12 +22,15 @@ import {
SCOPE_OTHER,
} from "../util/scopeflags";

const reservedTypes = [
const reservedTypes = new Set([
"_",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish there was something like https://eslint.org/docs/rules/sort-keys for arrays

"any",
"bool",
"boolean",
"empty",
"extends",
"false",
"interface",
"mixed",
"null",
"number",
Expand All @@ -36,10 +39,7 @@ const reservedTypes = [
"true",
"typeof",
"void",
"interface",
"extends",
"_",
];
]);

function isEsModuleType(bodyElement: N.Node): boolean {
return (
Expand Down Expand Up @@ -483,7 +483,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node: N.FlowDeclare,
isClass?: boolean = false,
): void {
node.id = this.flowParseRestrictedIdentifier(/*liberal*/ !isClass);
node.id = this.flowParseRestrictedIdentifier(
/* liberal */ !isClass,
/* declaration */ true,
);

this.scope.declareName(
node.id.name,
Expand Down Expand Up @@ -557,21 +560,32 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}

checkReservedType(word: string, startLoc: number) {
if (reservedTypes.indexOf(word) > -1) {
checkReservedType(word: string, startLoc: number, declaration?: boolean) {
if (!reservedTypes.has(word)) return;

if (declaration) {
this.raise(startLoc, `Cannot overwrite reserved type ${word}`);
return;
}

this.raise(startLoc, `Unexpected reserved type ${word}`);
}

flowParseRestrictedIdentifier(liberal?: boolean): N.Identifier {
this.checkReservedType(this.state.value, this.state.start);
flowParseRestrictedIdentifier(
liberal?: boolean,
declaration?: boolean,
): N.Identifier {
this.checkReservedType(this.state.value, this.state.start, declaration);
return this.parseIdentifier(liberal);
}

// Type aliases

flowParseTypeAlias(node: N.FlowTypeAlias): N.FlowTypeAlias {
node.id = this.flowParseRestrictedIdentifier();
node.id = this.flowParseRestrictedIdentifier(
/* liberal */ false,
/* declaration */ true,
);
this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);

if (this.isRelational("<")) {
Expand All @@ -591,7 +605,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
declare: boolean,
): N.FlowOpaqueType {
this.expectContextual("type");
node.id = this.flowParseRestrictedIdentifier(/*liberal*/ true);
node.id = this.flowParseRestrictedIdentifier(
/* liberal */ true,
/* declaration */ true,
);
this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);

if (this.isRelational("<")) {
Expand Down Expand Up @@ -1134,12 +1151,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
): N.FlowQualifiedTypeIdentifier {
startPos = startPos || this.state.start;
startLoc = startLoc || this.state.startLoc;
let node = id || this.parseIdentifier();
let node = id || this.flowParseRestrictedIdentifier(true);

while (this.eat(tt.dot)) {
const node2 = this.startNodeAt(startPos, startLoc);
node2.qualification = node;
node2.id = this.parseIdentifier();
node2.id = this.flowParseRestrictedIdentifier(true);
node = this.finishNode(node2, "QualifiedTypeIdentifier");
}

Expand Down Expand Up @@ -2353,7 +2370,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
contextDescription: string,
): void {
specifier.local = hasTypeImportKind(node)
? this.flowParseRestrictedIdentifier(true)
? this.flowParseRestrictedIdentifier(
/* liberal */ true,
/* declaration */ true,
)
: this.parseIdentifier();

this.checkLVal(
Expand Down Expand Up @@ -2459,7 +2479,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

if (nodeIsTypeImport || specifierIsTypeImport) {
this.checkReservedType(specifier.local.name, specifier.local.start);
this.checkReservedType(
specifier.local.name,
specifier.local.start,
/* declaration */ true,
);
}

if (isBinding && !nodeIsTypeImport && !specifierIsTypeImport) {
Expand Down
@@ -0,0 +1,2 @@
// @flow
interface I extends X, bool {}
@@ -0,0 +1,197 @@
{
"type": "File",
"start": 0,
"end": 39,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 30
}
},
"errors": [
"SyntaxError: Unexpected reserved type bool (2:23)"
],
"program": {
"type": "Program",
"start": 0,
"end": 39,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 30
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "InterfaceDeclaration",
"start": 9,
"end": 39,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 30
}
},
"id": {
"type": "Identifier",
"start": 19,
"end": 20,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 11
},
"identifierName": "I"
},
"name": "I"
},
"typeParameters": null,
"extends": [
{
"type": "InterfaceExtends",
"start": 29,
"end": 30,
"loc": {
"start": {
"line": 2,
"column": 20
},
"end": {
"line": 2,
"column": 21
}
},
"id": {
"type": "Identifier",
"start": 29,
"end": 30,
"loc": {
"start": {
"line": 2,
"column": 20
},
"end": {
"line": 2,
"column": 21
},
"identifierName": "X"
},
"name": "X"
},
"typeParameters": null
},
{
"type": "InterfaceExtends",
"start": 32,
"end": 36,
"loc": {
"start": {
"line": 2,
"column": 23
},
"end": {
"line": 2,
"column": 27
}
},
"id": {
"type": "Identifier",
"start": 32,
"end": 36,
"loc": {
"start": {
"line": 2,
"column": 23
},
"end": {
"line": 2,
"column": 27
},
"identifierName": "bool"
},
"name": "bool"
},
"typeParameters": null
}
],
"implements": [],
"mixins": [],
"body": {
"type": "ObjectTypeAnnotation",
"start": 37,
"end": 39,
"loc": {
"start": {
"line": 2,
"column": 28
},
"end": {
"line": 2,
"column": 30
}
},
"callProperties": [],
"properties": [],
"indexers": [],
"internalSlots": [],
"exact": false
},
"leadingComments": [
{
"type": "CommentLine",
"value": " @flow",
"start": 0,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
}
}
]
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": " @flow",
"start": 0,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
}
}
]
}
@@ -0,0 +1,2 @@
// @flow
interface I extends X, bool.m {}