Skip to content

Commit c10a670

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

File tree

8 files changed

+178
-87
lines changed

8 files changed

+178
-87
lines changed

tstypes/language/ast.d.ts

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

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

51-
export const BREAK: any;
140+
export const BREAK: {};
52141

53142
/**
54143
* visit() will walk through an AST using a depth first traversal, calling
@@ -149,7 +238,7 @@ export function visit(
149238
* If a prior visitor edits a node, no following visitors will see that node.
150239
*/
151240
export function visitInParallel(
152-
visitors: Array<Visitor<ASTKindToNode>>,
241+
visitors: ReadonlyArray<Visitor<ASTKindToNode>>,
153242
): Visitor<ASTKindToNode>;
154243

155244
/**

0 commit comments

Comments
 (0)