Skip to content

Commit 2fadef3

Browse files
committed
TS: Fix strict issues in src/language
1 parent a7f3cc6 commit 2fadef3

File tree

8 files changed

+167
-200
lines changed

8 files changed

+167
-200
lines changed

src/language/__tests__/lexer-test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { inspect } from '../../jsutils/inspect';
77

88
import { GraphQLError } from '../../error/GraphQLError';
99

10+
import type { Token } from '../ast';
1011
import { Source } from '../source';
1112
import { TokenKind } from '../tokenKind';
1213
import { Lexer, isPunctuatorTokenKind } from '../lexer';
@@ -876,7 +877,7 @@ describe('Lexer', () => {
876877
expect(endToken.next).to.equal(null);
877878

878879
const tokens = [];
879-
for (let tok = startToken; tok; tok = tok.next) {
880+
for (let tok: Token | null = startToken; tok; tok = tok.next) {
880881
if (tokens.length) {
881882
// Tokens are double-linked, prev should point to last seen token.
882883
expect(tok.prev).to.equal(tokens[tokens.length - 1]);

src/language/__tests__/visitor-test.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { describe, it } from 'mocha';
33

44
import { kitchenSinkQuery } from '../../__testUtils__/kitchenSinkQuery';
55

6-
import type { ASTNode } from '../ast';
6+
import type { ASTNode, SelectionSetNode } from '../ast';
77
import { isNode } from '../ast';
88
import { Kind } from '../kinds';
99
import { parse } from '../parser';
@@ -51,8 +51,7 @@ function checkVisitorFnArgs(ast: any, args: any, isEdited: boolean = false) {
5151
}
5252

5353
function getValue(node: ASTNode) {
54-
// @ts-expect-error FIXME: TS Conversion
55-
return node.value != null ? node.value : undefined;
54+
return 'value' in node ? node.value : undefined;
5655
}
5756

5857
describe('Visitor', () => {
@@ -62,7 +61,7 @@ describe('Visitor', () => {
6261
});
6362

6463
it('validates path argument', () => {
65-
const visited = [];
64+
const visited: Array<any> = [];
6665

6766
const ast = parse('{ a }', { noLocation: true });
6867

@@ -93,7 +92,7 @@ describe('Visitor', () => {
9392

9493
it('validates ancestors argument', () => {
9594
const ast = parse('{ a }', { noLocation: true });
96-
const visitedNodes = [];
95+
const visitedNodes: Array<any> = [];
9796

9897
visit(ast, {
9998
enter(node, key, parent, _path, ancestors) {
@@ -122,7 +121,7 @@ describe('Visitor', () => {
122121
it('allows editing a node both on enter and on leave', () => {
123122
const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true });
124123

125-
let selectionSet;
124+
let selectionSet: SelectionSetNode;
126125

127126
const editedAST = visit(ast, {
128127
OperationDefinition: {
@@ -265,8 +264,7 @@ describe('Visitor', () => {
265264
if (node.kind === 'Field' && node.name.value === 'a') {
266265
return {
267266
kind: 'Field',
268-
// @ts-expect-error FIXME: TS Conversion
269-
selectionSet: [addedField].concat(node.selectionSet),
267+
selectionSet: [addedField, node.selectionSet],
270268
};
271269
}
272270
if (node === addedField) {
@@ -279,7 +277,7 @@ describe('Visitor', () => {
279277
});
280278

281279
it('allows skipping a sub-tree', () => {
282-
const visited = [];
280+
const visited: Array<any> = [];
283281

284282
const ast = parse('{ a, b { x }, c }', { noLocation: true });
285283
visit(ast, {
@@ -317,7 +315,7 @@ describe('Visitor', () => {
317315
});
318316

319317
it('allows early exit while visiting', () => {
320-
const visited = [];
318+
const visited: Array<any> = [];
321319

322320
const ast = parse('{ a, b { x }, c }', { noLocation: true });
323321
visit(ast, {
@@ -352,7 +350,7 @@ describe('Visitor', () => {
352350
});
353351

354352
it('allows early exit while leaving', () => {
355-
const visited = [];
353+
const visited: Array<any> = [];
356354

357355
const ast = parse('{ a, b { x }, c }', { noLocation: true });
358356
visit(ast, {
@@ -389,7 +387,7 @@ describe('Visitor', () => {
389387
});
390388

391389
it('allows a named functions visitor API', () => {
392-
const visited = [];
390+
const visited: Array<any> = [];
393391

394392
const ast = parse('{ a, b { x }, c }', { noLocation: true });
395393
visit(ast, {
@@ -438,7 +436,7 @@ describe('Visitor', () => {
438436
},
439437
});
440438

441-
const visited = [];
439+
const visited: Array<any> = [];
442440
visit(customAST, {
443441
enter(node) {
444442
visited.push(['enter', node.kind, getValue(node)]);
@@ -469,7 +467,7 @@ describe('Visitor', () => {
469467
noLocation: true,
470468
allowLegacyFragmentVariables: true,
471469
});
472-
const visited = [];
470+
const visited: Array<any> = [];
473471

474472
visit(ast, {
475473
enter(node) {
@@ -516,8 +514,8 @@ describe('Visitor', () => {
516514

517515
it('visits kitchen sink', () => {
518516
const ast = parse(kitchenSinkQuery);
519-
const visited = [];
520-
const argsStack = [];
517+
const visited: Array<any> = [];
518+
const argsStack: Array<any> = [];
521519

522520
visit(ast, {
523521
enter(node, key, parent) {
@@ -895,7 +893,7 @@ describe('Visitor', () => {
895893
// Note: nearly identical to the above test of the same test but
896894
// using visitInParallel.
897895
it('allows skipping a sub-tree', () => {
898-
const visited = [];
896+
const visited: Array<any> = [];
899897

900898
const ast = parse('{ a, b { x }, c }');
901899
visit(
@@ -938,7 +936,7 @@ describe('Visitor', () => {
938936
});
939937

940938
it('allows skipping different sub-trees', () => {
941-
const visited = [];
939+
const visited: Array<any> = [];
942940

943941
const ast = parse('{ a { x }, b { y} }');
944942
visit(
@@ -1014,7 +1012,7 @@ describe('Visitor', () => {
10141012
// Note: nearly identical to the above test of the same test but
10151013
// using visitInParallel.
10161014
it('allows early exit while visiting', () => {
1017-
const visited = [];
1015+
const visited: Array<any> = [];
10181016

10191017
const ast = parse('{ a, b { x }, c }');
10201018
visit(
@@ -1054,7 +1052,7 @@ describe('Visitor', () => {
10541052
});
10551053

10561054
it('allows early exit from different points', () => {
1057-
const visited = [];
1055+
const visited: Array<any> = [];
10581056

10591057
const ast = parse('{ a { y }, b { x } }');
10601058
visit(
@@ -1116,7 +1114,7 @@ describe('Visitor', () => {
11161114
// Note: nearly identical to the above test of the same test but
11171115
// using visitInParallel.
11181116
it('allows early exit while leaving', () => {
1119-
const visited = [];
1117+
const visited: Array<any> = [];
11201118

11211119
const ast = parse('{ a, b { x }, c }');
11221120
visit(
@@ -1157,7 +1155,7 @@ describe('Visitor', () => {
11571155
});
11581156

11591157
it('allows early exit from leaving different points', () => {
1160-
const visited = [];
1158+
const visited: Array<any> = [];
11611159

11621160
const ast = parse('{ a { y }, b { x } }');
11631161
visit(
@@ -1233,7 +1231,7 @@ describe('Visitor', () => {
12331231
});
12341232

12351233
it('allows for editing on enter', () => {
1236-
const visited = [];
1234+
const visited: Array<any> = [];
12371235

12381236
const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true });
12391237
const editedAST = visit(
@@ -1297,7 +1295,7 @@ describe('Visitor', () => {
12971295
});
12981296

12991297
it('allows for editing on leave', () => {
1300-
const visited = [];
1298+
const visited: Array<any> = [];
13011299

13021300
const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true });
13031301
const editedAST = visit(

src/language/ast.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ export class Token {
7676

7777
/**
7878
* For non-punctuation tokens, represents the interpreted value of the token.
79+
*
80+
* Note: is undefined for punctuation tokens, but typed as string for
81+
* convenience in the parser.
7982
*/
80-
readonly value?: string;
83+
readonly value: string;
8184

8285
/**
8386
* Tokens exist as nodes in a double-linked-list amongst all tokens
@@ -124,9 +127,8 @@ export class Token {
124127
/**
125128
* @internal
126129
*/
127-
export function isNode(maybeNode: unknown): maybeNode is ASTNode {
128-
// eslint-disable-next-line @typescript-eslint/dot-notation
129-
return typeof maybeNode?.['kind'] === 'string';
130+
export function isNode(maybeNode: any): maybeNode is ASTNode {
131+
return typeof maybeNode?.kind === 'string';
130132
}
131133

132134
/**

src/language/location.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { invariant } from '../jsutils/invariant';
2+
13
import type { Source } from './source';
24

35
const LineRegExp = /\r\n|[\n\r]/g;
@@ -19,6 +21,7 @@ export function getLocation(source: Source, position: number): SourceLocation {
1921
let line = 1;
2022

2123
for (const match of source.body.matchAll(LineRegExp)) {
24+
invariant(typeof match.index === 'number');
2225
if (match.index >= position) {
2326
break;
2427
}

0 commit comments

Comments
 (0)