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

Add support for flow's SymbolTypeAnnotation #11077

Merged
merged 1 commit into from Mar 16, 2020
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
4 changes: 4 additions & 0 deletions packages/babel-generator/src/generators/flow.js
Expand Up @@ -596,6 +596,10 @@ export function QualifiedTypeIdentifier(node: Object) {
this.print(node.id, node);
}

export function SymbolTypeAnnotation() {
this.word("symbol");
}

function orSeparator() {
this.space();
this.token("|");
Expand Down
Expand Up @@ -23,3 +23,4 @@ declare opaque type Foo<T>: Bar<T>;
declare opaque type ID;
declare opaque type num: number;
declare opaque type NumArray;
declare var sym: symbol;
Expand Up @@ -41,4 +41,5 @@ declare module.exports: {
declare opaque type Foo<T>: Bar<T>;
declare opaque type ID;
declare opaque type num: number;
declare opaque type NumArray;
declare opaque type NumArray;
declare var sym: symbol;
Expand Up @@ -128,3 +128,4 @@ var a34: {| id<T>(x: T): T; |};
function foo27(numVal: number = 2) {}
function foo28(numVal?: number = 2) {}
export type * from "foo";
const foo29: symbol = Symbol();
Expand Up @@ -293,4 +293,5 @@ function foo27(numVal: number = 2) {}

function foo28(numVal?: number = 2) {}

export type * from "foo";
export type * from "foo";
const foo29: symbol = Symbol();
3 changes: 3 additions & 0 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -1275,6 +1275,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
case "string":
return this.finishNode(node, "StringTypeAnnotation");

case "symbol":
return this.finishNode(node, "SymbolTypeAnnotation");

default:
this.checkNotUnderscore(id.name);
return this.flowParseGenericType(startPos, startLoc, id);
Expand Down
@@ -0,0 +1 @@
declare var x: symbol;
@@ -0,0 +1,97 @@
{
"type": "File",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 22
}
},
"program": {
"type": "Program",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 22
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "DeclareVariable",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 22
}
},
"id": {
"type": "Identifier",
"start": 12,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 21
},
"identifierName": "x"
},
"name": "x",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 13,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 21
}
},
"typeAnnotation": {
"type": "SymbolTypeAnnotation",
"start": 15,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 21
}
}
}
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
const x: symbol = Symbol();
@@ -0,0 +1,148 @@
{
"type": "File",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
},
"program": {
"type": "Program",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 6,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 26
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 15,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 15
},
"identifierName": "x"
},
"name": "x",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 7,
"end": 15,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 15
}
},
"typeAnnotation": {
"type": "SymbolTypeAnnotation",
"start": 9,
"end": 15,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 15
}
}
}
}
},
"init": {
"type": "CallExpression",
"start": 18,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 18
},
"end": {
"line": 1,
"column": 26
}
},
"callee": {
"type": "Identifier",
"start": 18,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 18
},
"end": {
"line": 1,
"column": 24
},
"identifierName": "Symbol"
},
"name": "Symbol"
},
"arguments": []
}
}
],
"kind": "const"
}
],
"directives": []
}
}
6 changes: 6 additions & 0 deletions packages/babel-types/src/asserts/generated/index.js
Expand Up @@ -524,6 +524,12 @@ export function assertStringTypeAnnotation(
): void {
assert("StringTypeAnnotation", node, opts);
}
export function assertSymbolTypeAnnotation(
node: Object,
opts?: Object = {},
): void {
assert("SymbolTypeAnnotation", node, opts);
}
export function assertThisTypeAnnotation(
node: Object,
opts?: Object = {},
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-types/src/builders/generated/index.js
Expand Up @@ -469,6 +469,10 @@ export function StringTypeAnnotation(...args: Array<any>): Object {
return builder("StringTypeAnnotation", ...args);
}
export { StringTypeAnnotation as stringTypeAnnotation };
export function SymbolTypeAnnotation(...args: Array<any>): Object {
return builder("SymbolTypeAnnotation", ...args);
}
export { SymbolTypeAnnotation as symbolTypeAnnotation };
export function ThisTypeAnnotation(...args: Array<any>): Object {
return builder("ThisTypeAnnotation", ...args);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-types/src/definitions/flow.js
Expand Up @@ -372,6 +372,10 @@ defineType("StringTypeAnnotation", {
aliases: ["Flow", "FlowType", "FlowBaseAnnotation"],
});

defineType("SymbolTypeAnnotation", {
aliases: ["Flow", "FlowType", "FlowBaseAnnotation"],
});

defineType("ThisTypeAnnotation", {
aliases: ["Flow", "FlowType", "FlowBaseAnnotation"],
});
Expand Down
17 changes: 17 additions & 0 deletions packages/babel-types/src/validators/generated/index.js
Expand Up @@ -1692,6 +1692,20 @@ export function isStringTypeAnnotation(node: ?Object, opts?: Object): boolean {

return false;
}
export function isSymbolTypeAnnotation(node: ?Object, opts?: Object): boolean {
if (!node) return false;

const nodeType = node.type;
if (nodeType === "SymbolTypeAnnotation") {
if (typeof opts === "undefined") {
return true;
} else {
return shallowEqual(node, opts);
}
}

return false;
}
export function isThisTypeAnnotation(node: ?Object, opts?: Object): boolean {
if (!node) return false;

Expand Down Expand Up @@ -4270,6 +4284,7 @@ export function isFlow(node: ?Object, opts?: Object): boolean {
"QualifiedTypeIdentifier" === nodeType ||
"StringLiteralTypeAnnotation" === nodeType ||
"StringTypeAnnotation" === nodeType ||
"SymbolTypeAnnotation" === nodeType ||
"ThisTypeAnnotation" === nodeType ||
"TupleTypeAnnotation" === nodeType ||
"TypeofTypeAnnotation" === nodeType ||
Expand Down Expand Up @@ -4316,6 +4331,7 @@ export function isFlowType(node: ?Object, opts?: Object): boolean {
"ObjectTypeAnnotation" === nodeType ||
"StringLiteralTypeAnnotation" === nodeType ||
"StringTypeAnnotation" === nodeType ||
"SymbolTypeAnnotation" === nodeType ||
"ThisTypeAnnotation" === nodeType ||
"TupleTypeAnnotation" === nodeType ||
"TypeofTypeAnnotation" === nodeType ||
Expand Down Expand Up @@ -4344,6 +4360,7 @@ export function isFlowBaseAnnotation(node: ?Object, opts?: Object): boolean {
"EmptyTypeAnnotation" === nodeType ||
"NumberTypeAnnotation" === nodeType ||
"StringTypeAnnotation" === nodeType ||
"SymbolTypeAnnotation" === nodeType ||
"ThisTypeAnnotation" === nodeType ||
"VoidTypeAnnotation" === nodeType
) {
Expand Down