Skip to content

Commit bd2aa7f

Browse files
author
Jackson Kearl
committed
Sync language TS definitions with Flow
1 parent 7b389be commit bd2aa7f

File tree

7 files changed

+172
-86
lines changed

7 files changed

+172
-86
lines changed

tstypes/language/blockString.d.ts

Lines changed: 14 additions & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 40 deletions
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

Lines changed: 1 addition & 27 deletions
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 35 additions & 0 deletions
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

Lines changed: 100 additions & 16 deletions
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,88 @@ 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+
export const QueryDocumentKeys: {
55+
Name: [];
56+
57+
Document: ['definitions'];
58+
OperationDefinition: [
59+
'name',
60+
'variableDefinitions',
61+
'directives',
62+
'selectionSet',
63+
];
64+
VariableDefinition: ['variable', 'type', 'defaultValue', 'directives'];
65+
Variable: ['name'];
66+
SelectionSet: ['selections'];
67+
Field: ['alias', 'name', 'arguments', 'directives', 'selectionSet'];
68+
Argument: ['name', 'value'];
69+
70+
FragmentSpread: ['name', 'directives'];
71+
InlineFragment: ['typeCondition', 'directives', 'selectionSet'];
72+
FragmentDefinition: [
73+
'name',
74+
// Note: fragment variable definitions are experimental and may be changed
75+
// or removed in the future.
76+
'variableDefinitions',
77+
'typeCondition',
78+
'directives',
79+
'selectionSet',
80+
];
81+
82+
IntValue: [];
83+
FloatValue: [];
84+
StringValue: [];
85+
BooleanValue: [];
86+
NullValue: [];
87+
EnumValue: [];
88+
ListValue: ['values'];
89+
ObjectValue: ['fields'];
90+
ObjectField: ['name', 'value'];
91+
92+
Directive: ['name', 'arguments'];
93+
94+
NamedType: ['name'];
95+
ListType: ['type'];
96+
NonNullType: ['type'];
97+
98+
SchemaDefinition: ['directives', 'operationTypes'];
99+
OperationTypeDefinition: ['type'];
100+
101+
ScalarTypeDefinition: ['description', 'name', 'directives'];
102+
ObjectTypeDefinition: [
103+
'description',
104+
'name',
105+
'interfaces',
106+
'directives',
107+
'fields',
108+
];
109+
FieldDefinition: ['description', 'name', 'arguments', 'type', 'directives'];
110+
InputValueDefinition: [
111+
'description',
112+
'name',
113+
'type',
114+
'defaultValue',
115+
'directives',
116+
];
117+
InterfaceTypeDefinition: ['description', 'name', 'directives', 'fields'];
118+
UnionTypeDefinition: ['description', 'name', 'directives', 'types'];
119+
EnumTypeDefinition: ['description', 'name', 'directives', 'values'];
120+
EnumValueDefinition: ['description', 'name', 'directives'];
121+
InputObjectTypeDefinition: ['description', 'name', 'directives', 'fields'];
122+
123+
DirectiveDefinition: ['description', 'name', 'arguments', 'locations'];
124+
125+
SchemaExtension: ['directives', 'operationTypes'];
126+
127+
ScalarTypeExtension: ['name', 'directives'];
128+
ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'];
129+
InterfaceTypeExtension: ['name', 'directives', 'fields'];
130+
UnionTypeExtension: ['name', 'directives', 'types'];
131+
EnumTypeExtension: ['name', 'directives', 'values'];
132+
InputObjectTypeExtension: ['name', 'directives', 'fields'];
133+
};
50134

51-
export const BREAK: any;
135+
export const BREAK: {};
52136

53137
/**
54138
* visit() will walk through an AST using a depth first traversal, calling
@@ -149,7 +233,7 @@ export function visit(
149233
* If a prior visitor edits a node, no following visitors will see that node.
150234
*/
151235
export function visitInParallel(
152-
visitors: Array<Visitor<ASTKindToNode>>,
236+
visitors: ReadonlyArray<Visitor<ASTKindToNode>>,
153237
): Visitor<ASTKindToNode>;
154238

155239
/**

0 commit comments

Comments
 (0)