Add support for flow's SymbolTypeAnnotation (#11077)
This commit is contained in:
parent
2bce1e5e20
commit
4f394e30d2
@ -596,6 +596,10 @@ export function QualifiedTypeIdentifier(node: Object) {
|
|||||||
this.print(node.id, node);
|
this.print(node.id, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function SymbolTypeAnnotation() {
|
||||||
|
this.word("symbol");
|
||||||
|
}
|
||||||
|
|
||||||
function orSeparator() {
|
function orSeparator() {
|
||||||
this.space();
|
this.space();
|
||||||
this.token("|");
|
this.token("|");
|
||||||
|
|||||||
@ -23,3 +23,4 @@ declare opaque type Foo<T>: Bar<T>;
|
|||||||
declare opaque type ID;
|
declare opaque type ID;
|
||||||
declare opaque type num: number;
|
declare opaque type num: number;
|
||||||
declare opaque type NumArray;
|
declare opaque type NumArray;
|
||||||
|
declare var sym: symbol;
|
||||||
|
|||||||
@ -41,4 +41,5 @@ declare module.exports: {
|
|||||||
declare opaque type Foo<T>: Bar<T>;
|
declare opaque type Foo<T>: Bar<T>;
|
||||||
declare opaque type ID;
|
declare opaque type ID;
|
||||||
declare opaque type num: number;
|
declare opaque type num: number;
|
||||||
declare opaque type NumArray;
|
declare opaque type NumArray;
|
||||||
|
declare var sym: symbol;
|
||||||
@ -128,3 +128,4 @@ var a34: {| id<T>(x: T): T; |};
|
|||||||
function foo27(numVal: number = 2) {}
|
function foo27(numVal: number = 2) {}
|
||||||
function foo28(numVal?: number = 2) {}
|
function foo28(numVal?: number = 2) {}
|
||||||
export type * from "foo";
|
export type * from "foo";
|
||||||
|
const foo29: symbol = Symbol();
|
||||||
|
|||||||
@ -293,4 +293,5 @@ function foo27(numVal: number = 2) {}
|
|||||||
|
|
||||||
function foo28(numVal?: number = 2) {}
|
function foo28(numVal?: number = 2) {}
|
||||||
|
|
||||||
export type * from "foo";
|
export type * from "foo";
|
||||||
|
const foo29: symbol = Symbol();
|
||||||
@ -1333,6 +1333,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
case "string":
|
case "string":
|
||||||
return this.finishNode(node, "StringTypeAnnotation");
|
return this.finishNode(node, "StringTypeAnnotation");
|
||||||
|
|
||||||
|
case "symbol":
|
||||||
|
return this.finishNode(node, "SymbolTypeAnnotation");
|
||||||
|
|
||||||
default:
|
default:
|
||||||
this.checkNotUnderscore(id.name);
|
this.checkNotUnderscore(id.name);
|
||||||
return this.flowParseGenericType(startPos, startLoc, id);
|
return this.flowParseGenericType(startPos, startLoc, id);
|
||||||
|
|||||||
1
packages/babel-parser/test/fixtures/flow/declare-statements/symbol/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/flow/declare-statements/symbol/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
declare var x: symbol;
|
||||||
97
packages/babel-parser/test/fixtures/flow/declare-statements/symbol/output.json
vendored
Normal file
97
packages/babel-parser/test/fixtures/flow/declare-statements/symbol/output.json
vendored
Normal file
@ -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": []
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/babel-parser/test/fixtures/flow/type-annotations/symbol/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/flow/type-annotations/symbol/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
const x: symbol = Symbol();
|
||||||
148
packages/babel-parser/test/fixtures/flow/type-annotations/symbol/output.json
vendored
Normal file
148
packages/babel-parser/test/fixtures/flow/type-annotations/symbol/output.json
vendored
Normal file
@ -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": []
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -524,6 +524,12 @@ export function assertStringTypeAnnotation(
|
|||||||
): void {
|
): void {
|
||||||
assert("StringTypeAnnotation", node, opts);
|
assert("StringTypeAnnotation", node, opts);
|
||||||
}
|
}
|
||||||
|
export function assertSymbolTypeAnnotation(
|
||||||
|
node: Object,
|
||||||
|
opts?: Object = {},
|
||||||
|
): void {
|
||||||
|
assert("SymbolTypeAnnotation", node, opts);
|
||||||
|
}
|
||||||
export function assertThisTypeAnnotation(
|
export function assertThisTypeAnnotation(
|
||||||
node: Object,
|
node: Object,
|
||||||
opts?: Object = {},
|
opts?: Object = {},
|
||||||
|
|||||||
@ -469,6 +469,10 @@ export function StringTypeAnnotation(...args: Array<any>): Object {
|
|||||||
return builder("StringTypeAnnotation", ...args);
|
return builder("StringTypeAnnotation", ...args);
|
||||||
}
|
}
|
||||||
export { StringTypeAnnotation as stringTypeAnnotation };
|
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 {
|
export function ThisTypeAnnotation(...args: Array<any>): Object {
|
||||||
return builder("ThisTypeAnnotation", ...args);
|
return builder("ThisTypeAnnotation", ...args);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -372,6 +372,10 @@ defineType("StringTypeAnnotation", {
|
|||||||
aliases: ["Flow", "FlowType", "FlowBaseAnnotation"],
|
aliases: ["Flow", "FlowType", "FlowBaseAnnotation"],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
defineType("SymbolTypeAnnotation", {
|
||||||
|
aliases: ["Flow", "FlowType", "FlowBaseAnnotation"],
|
||||||
|
});
|
||||||
|
|
||||||
defineType("ThisTypeAnnotation", {
|
defineType("ThisTypeAnnotation", {
|
||||||
aliases: ["Flow", "FlowType", "FlowBaseAnnotation"],
|
aliases: ["Flow", "FlowType", "FlowBaseAnnotation"],
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1692,6 +1692,20 @@ export function isStringTypeAnnotation(node: ?Object, opts?: Object): boolean {
|
|||||||
|
|
||||||
return false;
|
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 {
|
export function isThisTypeAnnotation(node: ?Object, opts?: Object): boolean {
|
||||||
if (!node) return false;
|
if (!node) return false;
|
||||||
|
|
||||||
@ -4270,6 +4284,7 @@ export function isFlow(node: ?Object, opts?: Object): boolean {
|
|||||||
"QualifiedTypeIdentifier" === nodeType ||
|
"QualifiedTypeIdentifier" === nodeType ||
|
||||||
"StringLiteralTypeAnnotation" === nodeType ||
|
"StringLiteralTypeAnnotation" === nodeType ||
|
||||||
"StringTypeAnnotation" === nodeType ||
|
"StringTypeAnnotation" === nodeType ||
|
||||||
|
"SymbolTypeAnnotation" === nodeType ||
|
||||||
"ThisTypeAnnotation" === nodeType ||
|
"ThisTypeAnnotation" === nodeType ||
|
||||||
"TupleTypeAnnotation" === nodeType ||
|
"TupleTypeAnnotation" === nodeType ||
|
||||||
"TypeofTypeAnnotation" === nodeType ||
|
"TypeofTypeAnnotation" === nodeType ||
|
||||||
@ -4316,6 +4331,7 @@ export function isFlowType(node: ?Object, opts?: Object): boolean {
|
|||||||
"ObjectTypeAnnotation" === nodeType ||
|
"ObjectTypeAnnotation" === nodeType ||
|
||||||
"StringLiteralTypeAnnotation" === nodeType ||
|
"StringLiteralTypeAnnotation" === nodeType ||
|
||||||
"StringTypeAnnotation" === nodeType ||
|
"StringTypeAnnotation" === nodeType ||
|
||||||
|
"SymbolTypeAnnotation" === nodeType ||
|
||||||
"ThisTypeAnnotation" === nodeType ||
|
"ThisTypeAnnotation" === nodeType ||
|
||||||
"TupleTypeAnnotation" === nodeType ||
|
"TupleTypeAnnotation" === nodeType ||
|
||||||
"TypeofTypeAnnotation" === nodeType ||
|
"TypeofTypeAnnotation" === nodeType ||
|
||||||
@ -4344,6 +4360,7 @@ export function isFlowBaseAnnotation(node: ?Object, opts?: Object): boolean {
|
|||||||
"EmptyTypeAnnotation" === nodeType ||
|
"EmptyTypeAnnotation" === nodeType ||
|
||||||
"NumberTypeAnnotation" === nodeType ||
|
"NumberTypeAnnotation" === nodeType ||
|
||||||
"StringTypeAnnotation" === nodeType ||
|
"StringTypeAnnotation" === nodeType ||
|
||||||
|
"SymbolTypeAnnotation" === nodeType ||
|
||||||
"ThisTypeAnnotation" === nodeType ||
|
"ThisTypeAnnotation" === nodeType ||
|
||||||
"VoidTypeAnnotation" === nodeType
|
"VoidTypeAnnotation" === nodeType
|
||||||
) {
|
) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user