Skip to content

Commit 9366d6d

Browse files
Jackson KearlIvanGoncharov
Jackson Kearl
authored andcommitted
Sync language TS definitions with Flow (#2107)
* Sync language TS definitions with Flow * [] => never[], as empty tuple is invalid in TS@2
1 parent 49f86be commit 9366d6d

File tree

8 files changed

+181
-87
lines changed

8 files changed

+181
-87
lines changed

tstypes/language/ast.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Source } from './source';
2-
import { TokenKindEnum } from './lexer';
2+
import { TokenKindEnum } from './tokenKind';
33

44
/**
55
* Contains a range of UTF-8 character offsets and token references that

tstypes/language/blockString.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,17 @@
55
* This implements the GraphQL spec's BlockStringValue() static algorithm.
66
*/
77
export function dedentBlockStringValue(rawString: string): string;
8+
9+
// @internal
10+
export function getBlockStringIndentation(lines: ReadonlyArray<string>): number;
11+
12+
/**
13+
* Print a block string in the indented block form by adding a leading and
14+
* trailing blank line. However, if a block string starts with whitespace and is
15+
* a single-line, adding a leading blank line would strip that whitespace.
16+
*/
17+
export function printBlockString(
18+
value: string,
19+
indentation?: string,
20+
preferMultipleLines?: boolean,
21+
): string;

tstypes/language/index.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
export { Source } from './source';
12
export { getLocation, SourceLocation } from './location';
23
export { Kind, KindEnum } from './kinds';
3-
export { createLexer, TokenKind, Lexer, TokenKindEnum } from './lexer';
4+
export { TokenKind, TokenKindEnum } from './tokenKind';
5+
export { createLexer, Lexer } from './lexer';
46
export { parse, parseValue, parseType, ParseOptions } from './parser';
57
export { print } from './printer';
6-
export { Source } from './source';
78
export {
89
visit,
910
visitInParallel,
1011
visitWithTypeInfo,
1112
getVisitFn,
1213
BREAK,
13-
// type
1414
ASTVisitor,
1515
Visitor,
1616
VisitFn,

tstypes/language/lexer.d.ts

+3-40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { syntaxError } from '../error';
12
import { Token } from './ast';
23
import { Source } from './source';
3-
import { syntaxError } from '../error';
44

55
/**
66
* Given a Source object, this returns a Lexer for that source.
@@ -55,43 +55,6 @@ export interface Lexer<TOptions> {
5555
}
5656

5757
/**
58-
* An exported enum describing the different kinds of tokens that the
59-
* lexer emits.
60-
*/
61-
export const TokenKind: _TokenKind;
62-
63-
// @internal
64-
type _TokenKind = {
65-
SOF: '<SOF>';
66-
EOF: '<EOF>';
67-
BANG: '!';
68-
DOLLAR: '$';
69-
AMP: '&';
70-
PAREN_L: '(';
71-
PAREN_R: ')';
72-
SPREAD: '...';
73-
COLON: ':';
74-
EQUALS: '=';
75-
AT: '@';
76-
BRACKET_L: '[';
77-
BRACKET_R: ']';
78-
BRACE_L: '{';
79-
PIPE: '|';
80-
BRACE_R: '}';
81-
NAME: 'Name';
82-
INT: 'Int';
83-
FLOAT: 'Float';
84-
STRING: 'String';
85-
BLOCK_STRING: 'BlockString';
86-
COMMENT: 'Comment';
87-
};
88-
89-
/**
90-
* The enum type representing the token kinds values.
91-
*/
92-
export type TokenKindEnum = _TokenKind[keyof _TokenKind];
93-
94-
/**
95-
* A helper function to describe a token as a string for debugging
58+
* @internal
9659
*/
97-
export function getTokenDesc(token: Token): string;
60+
export function isPunctuatorToken(token: Token): boolean;

tstypes/language/parser.d.ts

+1-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { NamedTypeNode, TypeNode, ValueNode, DocumentNode } from './ast';
21
import { Source } from './source';
32
import { Lexer } from './lexer';
3+
import { NamedTypeNode, TypeNode, ValueNode, DocumentNode } from './ast';
44

55
/**
66
* Configuration options to control parser behavior
@@ -50,17 +50,6 @@ export interface ParseOptions {
5050
* future.
5151
*/
5252
experimentalFragmentVariables?: boolean;
53-
54-
/**
55-
* EXPERIMENTAL:
56-
*
57-
* If enabled, the parser understands directives on variable definitions:
58-
*
59-
* query Foo($var: String = "abc" @variable_definition_directive) {
60-
* ...
61-
* }
62-
*/
63-
experimentalVariableDefinitionDirectives?: boolean;
6453
}
6554

6655
/**
@@ -98,18 +87,3 @@ export function parseType(
9887
source: string | Source,
9988
options?: ParseOptions,
10089
): TypeNode;
101-
102-
export function parseConstValue<TOptions>(lexer: Lexer<TOptions>): ValueNode;
103-
104-
/**
105-
* Type :
106-
* - NamedType
107-
* - ListType
108-
* - NonNullType
109-
*/
110-
export function parseTypeReference<TOptions>(lexer: Lexer<TOptions>): TypeNode;
111-
112-
/**
113-
* NamedType : Name
114-
*/
115-
export function parseNamedType<TOptions>(lexer: Lexer<TOptions>): NamedTypeNode;

tstypes/language/printLocation.d.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Location } from '../language/ast';
2+
import { Source } from '../language/source';
3+
import { SourceLocation } from '../language/location';
4+
5+
/**
6+
* Render a helpful description of the location in the GraphQL Source document.
7+
*/
8+
export function printLocation(location: Location): string;
9+
10+
/**
11+
* Render a helpful description of the location in the GraphQL Source document.
12+
*/
13+
export function printSourceLocation(
14+
source: Source,
15+
sourceLocation: SourceLocation,
16+
): string;

tstypes/language/tokenKind.d.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* An exported enum describing the different kinds of tokens that the
3+
* lexer emits.
4+
*/
5+
export const TokenKind: _TokenKind;
6+
7+
type _TokenKind = {
8+
SOF: '<SOF>';
9+
EOF: '<EOF>';
10+
BANG: '!';
11+
DOLLAR: '$';
12+
AMP: '&';
13+
PAREN_L: '(';
14+
PAREN_R: ')';
15+
SPREAD: '...';
16+
COLON: ':';
17+
EQUALS: '=';
18+
AT: '@';
19+
BRACKET_L: '[';
20+
BRACKET_R: ']';
21+
BRACE_L: '{';
22+
PIPE: '|';
23+
BRACE_R: '}';
24+
NAME: 'Name';
25+
INT: 'Int';
26+
FLOAT: 'Float';
27+
STRING: 'String';
28+
BLOCK_STRING: 'BlockString';
29+
COMMENT: 'Comment';
30+
};
31+
32+
/**
33+
* The enum type representing the token kinds values.
34+
*/
35+
export type TokenKindEnum = _TokenKind[keyof _TokenKind];

tstypes/language/visitor.d.ts

+108-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import Maybe from '../tsutils/Maybe';
2-
import { ASTNode, ASTKindToNode } from './ast';
32
import { TypeInfo } from '../utilities/TypeInfo';
3+
import { ASTNode, ASTKindToNode } from './ast';
4+
5+
/**
6+
* A visitor is provided to visit, it contains the collection of
7+
* relevant functions to be called during the visitor's traversal.
8+
*/
9+
export type ASTVisitor = Visitor<ASTKindToNode>;
10+
export type Visitor<KindToNode, Nodes = KindToNode[keyof KindToNode]> =
11+
| EnterLeaveVisitor<KindToNode, Nodes>
12+
| ShapeMapVisitor<KindToNode, Nodes>;
413

514
interface EnterLeave<T> {
615
readonly enter?: T;
@@ -17,27 +26,23 @@ type ShapeMapVisitor<KindToNode, Nodes> = {
1726
| EnterLeave<VisitFn<Nodes, KindToNode[K]>>;
1827
};
1928

20-
export type ASTVisitor = Visitor<ASTKindToNode>;
21-
export type Visitor<KindToNode, Nodes = KindToNode[keyof KindToNode]> =
22-
| EnterLeaveVisitor<KindToNode, Nodes>
23-
| ShapeMapVisitor<KindToNode, Nodes>;
24-
2529
/**
2630
* A visitor is comprised of visit functions, which are called on each node
2731
* during the visitor's traversal.
2832
*/
2933
export type VisitFn<TAnyNode, TVisitedNode = TAnyNode> = (
30-
// The current node being visiting.
34+
/** The current node being visiting.*/
3135
node: TVisitedNode,
32-
// The index or key to this node from the parent node or Array.
36+
/** The index or key to this node from the parent node or Array. */
3337
key: string | number | undefined,
34-
// The parent immediately above this node, which may be an Array.
38+
/** The parent immediately above this node, which may be an Array. */
3539
parent: TAnyNode | ReadonlyArray<TAnyNode> | undefined,
36-
// The key path to get to this node from the root node.
40+
/** The key path to get to this node from the root node. */
3741
path: ReadonlyArray<string | number>,
38-
// All nodes and Arrays visited before reaching parent of this node.
39-
// These correspond to array indices in `path`.
40-
// Note: ancestors includes arrays which contain the parent of visited node.
42+
/** All nodes and Arrays visited before reaching parent of this node.
43+
* These correspond to array indices in `path`.
44+
* Note: ancestors includes arrays which contain the parent of visited node.
45+
*/
4146
ancestors: ReadonlyArray<TAnyNode | ReadonlyArray<TAnyNode>>,
4247
) => any;
4348

@@ -46,9 +51,96 @@ export type VisitFn<TAnyNode, TVisitedNode = TAnyNode> = (
4651
*/
4752
export type VisitorKeyMap<T> = { [P in keyof T]: ReadonlyArray<keyof T[P]> };
4853

49-
export const QueryDocumentKeys: { [key: string]: string[] };
54+
// TODO: Should be `[]`, but that requires TypeScript@3
55+
type EmptyTuple = never[];
56+
57+
export const QueryDocumentKeys: {
58+
Name: EmptyTuple;
59+
60+
Document: ['definitions'];
61+
// Prettier forces trailing commas, but TS pre 3.2 doesn't allow them.
62+
// prettier-ignore
63+
OperationDefinition: [
64+
'name',
65+
'variableDefinitions',
66+
'directives',
67+
'selectionSet'
68+
];
69+
VariableDefinition: ['variable', 'type', 'defaultValue', 'directives'];
70+
Variable: ['name'];
71+
SelectionSet: ['selections'];
72+
Field: ['alias', 'name', 'arguments', 'directives', 'selectionSet'];
73+
Argument: ['name', 'value'];
74+
75+
FragmentSpread: ['name', 'directives'];
76+
InlineFragment: ['typeCondition', 'directives', 'selectionSet'];
77+
// prettier-ignore
78+
FragmentDefinition: [
79+
'name',
80+
// Note: fragment variable definitions are experimental and may be changed
81+
// or removed in the future.
82+
'variableDefinitions',
83+
'typeCondition',
84+
'directives',
85+
'selectionSet'
86+
];
87+
88+
IntValue: EmptyTuple;
89+
FloatValue: EmptyTuple;
90+
StringValue: EmptyTuple;
91+
BooleanValue: EmptyTuple;
92+
NullValue: EmptyTuple;
93+
EnumValue: EmptyTuple;
94+
ListValue: ['values'];
95+
ObjectValue: ['fields'];
96+
ObjectField: ['name', 'value'];
97+
98+
Directive: ['name', 'arguments'];
99+
100+
NamedType: ['name'];
101+
ListType: ['type'];
102+
NonNullType: ['type'];
103+
104+
SchemaDefinition: ['directives', 'operationTypes'];
105+
OperationTypeDefinition: ['type'];
106+
107+
ScalarTypeDefinition: ['description', 'name', 'directives'];
108+
// prettier-ignore
109+
ObjectTypeDefinition: [
110+
'description',
111+
'name',
112+
'interfaces',
113+
'directives',
114+
'fields'
115+
];
116+
FieldDefinition: ['description', 'name', 'arguments', 'type', 'directives'];
117+
// prettier-ignore
118+
InputValueDefinition: [
119+
'description',
120+
'name',
121+
'type',
122+
'defaultValue',
123+
'directives'
124+
];
125+
InterfaceTypeDefinition: ['description', 'name', 'directives', 'fields'];
126+
UnionTypeDefinition: ['description', 'name', 'directives', 'types'];
127+
EnumTypeDefinition: ['description', 'name', 'directives', 'values'];
128+
EnumValueDefinition: ['description', 'name', 'directives'];
129+
InputObjectTypeDefinition: ['description', 'name', 'directives', 'fields'];
130+
131+
DirectiveDefinition: ['description', 'name', 'arguments', 'locations'];
132+
133+
SchemaExtension: ['directives', 'operationTypes'];
134+
135+
ScalarTypeExtension: ['name', 'directives'];
136+
ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'];
137+
InterfaceTypeExtension: ['name', 'directives', 'fields'];
138+
UnionTypeExtension: ['name', 'directives', 'types'];
139+
EnumTypeExtension: ['name', 'directives', 'values'];
140+
InputObjectTypeExtension: ['name', 'directives', 'fields'];
141+
};
50142

51-
export const BREAK: any;
143+
export const BREAK: {};
52144

53145
/**
54146
* visit() will walk through an AST using a depth first traversal, calling
@@ -149,7 +241,7 @@ export function visit(
149241
* If a prior visitor edits a node, no following visitors will see that node.
150242
*/
151243
export function visitInParallel(
152-
visitors: Array<Visitor<ASTKindToNode>>,
244+
visitors: ReadonlyArray<Visitor<ASTKindToNode>>,
153245
): Visitor<ASTKindToNode>;
154246

155247
/**

0 commit comments

Comments
 (0)