TypeScript: reserve unknown as TSUnknownKeyword (#8755)

This commit is contained in:
Pig Fang 2018-09-26 01:19:32 +08:00 committed by Brian Ng
parent 889cdb8da5
commit f38be13113
11 changed files with 485 additions and 371 deletions

View File

@ -127,6 +127,9 @@ export function TSIndexSignature(node) {
export function TSAnyKeyword() {
this.word("any");
}
export function TSUnknownKeyword() {
this.word("unknown");
}
export function TSNumberKeyword() {
this.word("number");
}

View File

@ -1,4 +1,5 @@
let a: any;
let un: unknown;
let b: boolean;
let ne: never;
let nul: null;

View File

@ -1,4 +1,5 @@
let a: any;
let un: unknown;
let b: boolean;
let ne: never;
let nul: null;

View File

@ -57,6 +57,8 @@ function keywordTypeFromName(
return "TSSymbolKeyword";
case "undefined":
return "TSUndefinedKeyword";
case "unknown":
return "TSUnknownKeyword";
default:
return undefined;
}

View File

@ -1119,6 +1119,7 @@ export type TsTypeBase = NodeBase;
export type TsKeywordTypeType =
| "TSAnyKeyword"
| "TSUnknownKeyword"
| "TSNumberKeyword"
| "TSObjectKeyword"
| "TSBooleanKeyword"

View File

@ -1,4 +1,5 @@
let a: any;
let un: unknown;
let b: boolean;
let ne: never;
let nul: null;

View File

@ -1,28 +1,28 @@
{
"type": "File",
"start": 0,
"end": 152,
"end": 169,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 10,
"line": 11,
"column": 12
}
},
"program": {
"type": "Program",
"start": 0,
"end": 152,
"end": 169,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 10,
"line": 11,
"column": 12
}
},
@ -113,7 +113,7 @@
{
"type": "VariableDeclaration",
"start": 12,
"end": 27,
"end": 28,
"loc": {
"start": {
"line": 2,
@ -121,14 +121,14 @@
},
"end": {
"line": 2,
"column": 15
"column": 16
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 16,
"end": 26,
"end": 27,
"loc": {
"start": {
"line": 2,
@ -136,13 +136,13 @@
},
"end": {
"line": 2,
"column": 14
"column": 15
}
},
"id": {
"type": "Identifier",
"start": 16,
"end": 26,
"end": 27,
"loc": {
"start": {
"line": 2,
@ -150,6 +150,87 @@
},
"end": {
"line": 2,
"column": 15
},
"identifierName": "un"
},
"name": "un",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 18,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 15
}
},
"typeAnnotation": {
"type": "TSUnknownKeyword",
"start": 20,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 15
}
}
}
}
},
"init": null
}
],
"kind": "let"
},
{
"type": "VariableDeclaration",
"start": 29,
"end": 44,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 15
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 33,
"end": 43,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 14
}
},
"id": {
"type": "Identifier",
"start": 33,
"end": 43,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 14
},
"identifierName": "b"
@ -157,29 +238,29 @@
"name": "b",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 17,
"end": 26,
"start": 34,
"end": 43,
"loc": {
"start": {
"line": 2,
"line": 3,
"column": 5
},
"end": {
"line": 2,
"line": 3,
"column": 14
}
},
"typeAnnotation": {
"type": "TSBooleanKeyword",
"start": 19,
"end": 26,
"start": 36,
"end": 43,
"loc": {
"start": {
"line": 2,
"line": 3,
"column": 7
},
"end": {
"line": 2,
"line": 3,
"column": 14
}
}
@ -193,44 +274,44 @@
},
{
"type": "VariableDeclaration",
"start": 28,
"end": 42,
"start": 45,
"end": 59,
"loc": {
"start": {
"line": 3,
"line": 4,
"column": 0
},
"end": {
"line": 3,
"line": 4,
"column": 14
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 32,
"end": 41,
"start": 49,
"end": 58,
"loc": {
"start": {
"line": 3,
"line": 4,
"column": 4
},
"end": {
"line": 3,
"line": 4,
"column": 13
}
},
"id": {
"type": "Identifier",
"start": 32,
"end": 41,
"start": 49,
"end": 58,
"loc": {
"start": {
"line": 3,
"line": 4,
"column": 4
},
"end": {
"line": 3,
"line": 4,
"column": 13
},
"identifierName": "ne"
@ -238,108 +319,27 @@
"name": "ne",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 34,
"end": 41,
"start": 51,
"end": 58,
"loc": {
"start": {
"line": 3,
"line": 4,
"column": 6
},
"end": {
"line": 3,
"line": 4,
"column": 13
}
},
"typeAnnotation": {
"type": "TSNeverKeyword",
"start": 36,
"end": 41,
"start": 53,
"end": 58,
"loc": {
"start": {
"line": 3,
"line": 4,
"column": 8
},
"end": {
"line": 3,
"column": 13
}
}
}
}
},
"init": null
}
],
"kind": "let"
},
{
"type": "VariableDeclaration",
"start": 43,
"end": 57,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 14
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 47,
"end": 56,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 13
}
},
"id": {
"type": "Identifier",
"start": 47,
"end": 56,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 13
},
"identifierName": "nul"
},
"name": "nul",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 50,
"end": 56,
"loc": {
"start": {
"line": 4,
"column": 7
},
"end": {
"line": 4,
"column": 13
}
},
"typeAnnotation": {
"type": "TSNullKeyword",
"start": 52,
"end": 56,
"loc": {
"start": {
"line": 4,
"column": 9
},
"end": {
"line": 4,
"column": 13
@ -355,7 +355,7 @@
},
{
"type": "VariableDeclaration",
"start": 58,
"start": 60,
"end": 74,
"loc": {
"start": {
@ -364,13 +364,13 @@
},
"end": {
"line": 5,
"column": 16
"column": 14
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 62,
"start": 64,
"end": 73,
"loc": {
"start": {
@ -379,12 +379,12 @@
},
"end": {
"line": 5,
"column": 15
"column": 13
}
},
"id": {
"type": "Identifier",
"start": 62,
"start": 64,
"end": 73,
"loc": {
"start": {
@ -393,14 +393,14 @@
},
"end": {
"line": 5,
"column": 15
"column": 13
},
"identifierName": "num"
"identifierName": "nul"
},
"name": "num",
"name": "nul",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 65,
"start": 67,
"end": 73,
"loc": {
"start": {
@ -409,12 +409,12 @@
},
"end": {
"line": 5,
"column": 15
"column": 13
}
},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start": 67,
"type": "TSNullKeyword",
"start": 69,
"end": 73,
"loc": {
"start": {
@ -423,7 +423,7 @@
},
"end": {
"line": 5,
"column": 15
"column": 13
}
}
}
@ -437,7 +437,7 @@
{
"type": "VariableDeclaration",
"start": 75,
"end": 89,
"end": 91,
"loc": {
"start": {
"line": 6,
@ -445,14 +445,14 @@
},
"end": {
"line": 6,
"column": 14
"column": 16
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 79,
"end": 88,
"end": 90,
"loc": {
"start": {
"line": 6,
@ -460,13 +460,13 @@
},
"end": {
"line": 6,
"column": 13
"column": 15
}
},
"id": {
"type": "Identifier",
"start": 79,
"end": 88,
"end": 90,
"loc": {
"start": {
"line": 6,
@ -474,6 +474,87 @@
},
"end": {
"line": 6,
"column": 15
},
"identifierName": "num"
},
"name": "num",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 82,
"end": 90,
"loc": {
"start": {
"line": 6,
"column": 7
},
"end": {
"line": 6,
"column": 15
}
},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start": 84,
"end": 90,
"loc": {
"start": {
"line": 6,
"column": 9
},
"end": {
"line": 6,
"column": 15
}
}
}
}
},
"init": null
}
],
"kind": "let"
},
{
"type": "VariableDeclaration",
"start": 92,
"end": 106,
"loc": {
"start": {
"line": 7,
"column": 0
},
"end": {
"line": 7,
"column": 14
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 96,
"end": 105,
"loc": {
"start": {
"line": 7,
"column": 4
},
"end": {
"line": 7,
"column": 13
}
},
"id": {
"type": "Identifier",
"start": 96,
"end": 105,
"loc": {
"start": {
"line": 7,
"column": 4
},
"end": {
"line": 7,
"column": 13
},
"identifierName": "o"
@ -481,29 +562,29 @@
"name": "o",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 80,
"end": 88,
"start": 97,
"end": 105,
"loc": {
"start": {
"line": 6,
"line": 7,
"column": 5
},
"end": {
"line": 6,
"line": 7,
"column": 13
}
},
"typeAnnotation": {
"type": "TSObjectKeyword",
"start": 82,
"end": 88,
"start": 99,
"end": 105,
"loc": {
"start": {
"line": 6,
"line": 7,
"column": 7
},
"end": {
"line": 6,
"line": 7,
"column": 13
}
}
@ -517,44 +598,44 @@
},
{
"type": "VariableDeclaration",
"start": 90,
"end": 105,
"start": 107,
"end": 122,
"loc": {
"start": {
"line": 7,
"line": 8,
"column": 0
},
"end": {
"line": 7,
"line": 8,
"column": 15
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 94,
"end": 104,
"start": 111,
"end": 121,
"loc": {
"start": {
"line": 7,
"line": 8,
"column": 4
},
"end": {
"line": 7,
"line": 8,
"column": 14
}
},
"id": {
"type": "Identifier",
"start": 94,
"end": 104,
"start": 111,
"end": 121,
"loc": {
"start": {
"line": 7,
"line": 8,
"column": 4
},
"end": {
"line": 7,
"line": 8,
"column": 14
},
"identifierName": "st"
@ -562,29 +643,29 @@
"name": "st",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 96,
"end": 104,
"start": 113,
"end": 121,
"loc": {
"start": {
"line": 7,
"line": 8,
"column": 6
},
"end": {
"line": 7,
"line": 8,
"column": 14
}
},
"typeAnnotation": {
"type": "TSStringKeyword",
"start": 98,
"end": 104,
"start": 115,
"end": 121,
"loc": {
"start": {
"line": 7,
"line": 8,
"column": 8
},
"end": {
"line": 7,
"line": 8,
"column": 14
}
}
@ -598,44 +679,44 @@
},
{
"type": "VariableDeclaration",
"start": 106,
"end": 121,
"start": 123,
"end": 138,
"loc": {
"start": {
"line": 8,
"line": 9,
"column": 0
},
"end": {
"line": 8,
"line": 9,
"column": 15
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 110,
"end": 120,
"start": 127,
"end": 137,
"loc": {
"start": {
"line": 8,
"line": 9,
"column": 4
},
"end": {
"line": 8,
"line": 9,
"column": 14
}
},
"id": {
"type": "Identifier",
"start": 110,
"end": 120,
"start": 127,
"end": 137,
"loc": {
"start": {
"line": 8,
"line": 9,
"column": 4
},
"end": {
"line": 8,
"line": 9,
"column": 14
},
"identifierName": "sy"
@ -643,29 +724,29 @@
"name": "sy",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 112,
"end": 120,
"start": 129,
"end": 137,
"loc": {
"start": {
"line": 8,
"line": 9,
"column": 6
},
"end": {
"line": 8,
"line": 9,
"column": 14
}
},
"typeAnnotation": {
"type": "TSSymbolKeyword",
"start": 114,
"end": 120,
"start": 131,
"end": 137,
"loc": {
"start": {
"line": 8,
"line": 9,
"column": 8
},
"end": {
"line": 8,
"line": 9,
"column": 14
}
}
@ -679,44 +760,44 @@
},
{
"type": "VariableDeclaration",
"start": 122,
"end": 139,
"start": 139,
"end": 156,
"loc": {
"start": {
"line": 9,
"line": 10,
"column": 0
},
"end": {
"line": 9,
"line": 10,
"column": 17
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 126,
"end": 138,
"start": 143,
"end": 155,
"loc": {
"start": {
"line": 9,
"line": 10,
"column": 4
},
"end": {
"line": 9,
"line": 10,
"column": 16
}
},
"id": {
"type": "Identifier",
"start": 126,
"end": 138,
"start": 143,
"end": 155,
"loc": {
"start": {
"line": 9,
"line": 10,
"column": 4
},
"end": {
"line": 9,
"line": 10,
"column": 16
},
"identifierName": "u"
@ -724,29 +805,29 @@
"name": "u",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 127,
"end": 138,
"start": 144,
"end": 155,
"loc": {
"start": {
"line": 9,
"line": 10,
"column": 5
},
"end": {
"line": 9,
"line": 10,
"column": 16
}
},
"typeAnnotation": {
"type": "TSUndefinedKeyword",
"start": 129,
"end": 138,
"start": 146,
"end": 155,
"loc": {
"start": {
"line": 9,
"line": 10,
"column": 7
},
"end": {
"line": 9,
"line": 10,
"column": 16
}
}
@ -760,44 +841,44 @@
},
{
"type": "VariableDeclaration",
"start": 140,
"end": 152,
"start": 157,
"end": 169,
"loc": {
"start": {
"line": 10,
"line": 11,
"column": 0
},
"end": {
"line": 10,
"line": 11,
"column": 12
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 144,
"end": 151,
"start": 161,
"end": 168,
"loc": {
"start": {
"line": 10,
"line": 11,
"column": 4
},
"end": {
"line": 10,
"line": 11,
"column": 11
}
},
"id": {
"type": "Identifier",
"start": 144,
"end": 151,
"start": 161,
"end": 168,
"loc": {
"start": {
"line": 10,
"line": 11,
"column": 4
},
"end": {
"line": 10,
"line": 11,
"column": 11
},
"identifierName": "v"
@ -805,29 +886,29 @@
"name": "v",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 145,
"end": 151,
"start": 162,
"end": 168,
"loc": {
"start": {
"line": 10,
"line": 11,
"column": 5
},
"end": {
"line": 10,
"line": 11,
"column": 11
}
},
"typeAnnotation": {
"type": "TSVoidKeyword",
"start": 147,
"end": 151,
"start": 164,
"end": 168,
"loc": {
"start": {
"line": 10,
"line": 11,
"column": 7
},
"end": {
"line": 10,
"line": 11,
"column": 11
}
}

View File

@ -762,6 +762,9 @@ export function assertTSIndexSignature(node: Object, opts?: Object = {}): void {
export function assertTSAnyKeyword(node: Object, opts?: Object = {}): void {
assert("TSAnyKeyword", node, opts);
}
export function assertTSUnknownKeyword(node: Object, opts?: Object = {}): void {
assert("TSUnknownKeyword", node, opts);
}
export function assertTSNumberKeyword(node: Object, opts?: Object = {}): void {
assert("TSNumberKeyword", node, opts);
}

View File

@ -698,6 +698,11 @@ export function TSAnyKeyword(...args: Array<any>): Object {
}
export { TSAnyKeyword as tsAnyKeyword };
export { TSAnyKeyword as tSAnyKeyword };
export function TSUnknownKeyword(...args: Array<any>): Object {
return builder("TSUnknownKeyword", ...args);
}
export { TSUnknownKeyword as tsUnknownKeyword };
export { TSUnknownKeyword as tSUnknownKeyword };
export function TSNumberKeyword(...args: Array<any>): Object {
return builder("TSNumberKeyword", ...args);
}

View File

@ -128,6 +128,7 @@ defineType("TSIndexSignature", {
const tsKeywordTypes = [
"TSAnyKeyword",
"TSUnknownKeyword",
"TSNumberKeyword",
"TSObjectKeyword",
"TSBooleanKeyword",

View File

@ -2406,6 +2406,20 @@ export function isTSAnyKeyword(node: Object, opts?: Object): boolean {
return false;
}
export function isTSUnknownKeyword(node: Object, opts?: Object): boolean {
if (!node) return false;
const nodeType = node.type;
if (nodeType === "TSUnknownKeyword") {
if (typeof opts === "undefined") {
return true;
} else {
return shallowEqual(node, opts);
}
}
return false;
}
export function isTSNumberKeyword(node: Object, opts?: Object): boolean {
if (!node) return false;
@ -4116,6 +4130,7 @@ export function isTSType(node: Object, opts?: Object): boolean {
if (
nodeType === "TSType" ||
"TSAnyKeyword" === nodeType ||
"TSUnknownKeyword" === nodeType ||
"TSNumberKeyword" === nodeType ||
"TSObjectKeyword" === nodeType ||
"TSBooleanKeyword" === nodeType ||