Skip to content

Commit

Permalink
TS: Fix strict issues in src/language
Browse files Browse the repository at this point in the history
  • Loading branch information
leebyron committed May 27, 2021
1 parent f41145b commit bf47ef8
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 200 deletions.
3 changes: 2 additions & 1 deletion src/language/__tests__/lexer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { inspect } from '../../jsutils/inspect';

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

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

const tokens = [];
for (let tok = startToken; tok; tok = tok.next) {
for (let tok: Token | null = startToken; tok; tok = tok.next) {
if (tokens.length) {
// Tokens are double-linked, prev should point to last seen token.
expect(tok.prev).to.equal(tokens[tokens.length - 1]);
Expand Down
46 changes: 22 additions & 24 deletions src/language/__tests__/visitor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, it } from 'mocha';

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

import type { ASTNode } from '../ast';
import type { ASTNode, SelectionSetNode } from '../ast';
import { isNode } from '../ast';
import { Kind } from '../kinds';
import { parse } from '../parser';
Expand Down Expand Up @@ -51,8 +51,7 @@ function checkVisitorFnArgs(ast: any, args: any, isEdited: boolean = false) {
}

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

describe('Visitor', () => {
Expand All @@ -62,7 +61,7 @@ describe('Visitor', () => {
});

it('validates path argument', () => {
const visited = [];
const visited: Array<any> = [];

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

Expand Down Expand Up @@ -93,7 +92,7 @@ describe('Visitor', () => {

it('validates ancestors argument', () => {
const ast = parse('{ a }', { noLocation: true });
const visitedNodes = [];
const visitedNodes: Array<any> = [];

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

let selectionSet;
let selectionSet: SelectionSetNode;

const editedAST = visit(ast, {
OperationDefinition: {
Expand Down Expand Up @@ -265,8 +264,7 @@ describe('Visitor', () => {
if (node.kind === 'Field' && node.name.value === 'a') {
return {
kind: 'Field',
// @ts-expect-error FIXME: TS Conversion
selectionSet: [addedField].concat(node.selectionSet),
selectionSet: [addedField, node.selectionSet],
};
}
if (node === addedField) {
Expand All @@ -279,7 +277,7 @@ describe('Visitor', () => {
});

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

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

it('allows early exit while visiting', () => {
const visited = [];
const visited: Array<any> = [];

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

it('allows early exit while leaving', () => {
const visited = [];
const visited: Array<any> = [];

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

it('allows a named functions visitor API', () => {
const visited = [];
const visited: Array<any> = [];

const ast = parse('{ a, b { x }, c }', { noLocation: true });
visit(ast, {
Expand Down Expand Up @@ -438,7 +436,7 @@ describe('Visitor', () => {
},
});

const visited = [];
const visited: Array<any> = [];
visit(customAST, {
enter(node) {
visited.push(['enter', node.kind, getValue(node)]);
Expand Down Expand Up @@ -469,7 +467,7 @@ describe('Visitor', () => {
noLocation: true,
allowLegacyFragmentVariables: true,
});
const visited = [];
const visited: Array<any> = [];

visit(ast, {
enter(node) {
Expand Down Expand Up @@ -516,8 +514,8 @@ describe('Visitor', () => {

it('visits kitchen sink', () => {
const ast = parse(kitchenSinkQuery);
const visited = [];
const argsStack = [];
const visited: Array<any> = [];
const argsStack: Array<any> = [];

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

const ast = parse('{ a, b { x }, c }');
visit(
Expand Down Expand Up @@ -938,7 +936,7 @@ describe('Visitor', () => {
});

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

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

const ast = parse('{ a, b { x }, c }');
visit(
Expand Down Expand Up @@ -1054,7 +1052,7 @@ describe('Visitor', () => {
});

it('allows early exit from different points', () => {
const visited = [];
const visited: Array<any> = [];

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

const ast = parse('{ a, b { x }, c }');
visit(
Expand Down Expand Up @@ -1157,7 +1155,7 @@ describe('Visitor', () => {
});

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

const ast = parse('{ a { y }, b { x } }');
visit(
Expand Down Expand Up @@ -1233,7 +1231,7 @@ describe('Visitor', () => {
});

it('allows for editing on enter', () => {
const visited = [];
const visited: Array<any> = [];

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

it('allows for editing on leave', () => {
const visited = [];
const visited: Array<any> = [];

const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true });
const editedAST = visit(
Expand Down
10 changes: 6 additions & 4 deletions src/language/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ export class Token {

/**
* For non-punctuation tokens, represents the interpreted value of the token.
*
* Note: is undefined for punctuation tokens, but typed as string for
* convenience in the parser.
*/
readonly value?: string;
readonly value: string;

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

/**
Expand Down
3 changes: 3 additions & 0 deletions src/language/location.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { invariant } from '../jsutils/invariant';

import type { Source } from './source';

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

for (const match of source.body.matchAll(LineRegExp)) {
invariant(typeof match.index === 'number');
if (match.index >= position) {
break;
}
Expand Down
Loading

0 comments on commit bf47ef8

Please sign in to comment.