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 4bcd3f2 commit 6e089f2
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 60 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
40 changes: 20 additions & 20 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 @@ -62,7 +62,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 +93,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 +122,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 @@ -279,7 +279,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 +317,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 +352,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 +389,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 +438,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 +469,7 @@ describe('Visitor', () => {
noLocation: true,
allowLegacyFragmentVariables: true,
});
const visited = [];
const visited: Array<any> = [];

visit(ast, {
enter(node) {
Expand Down Expand Up @@ -516,8 +516,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 +895,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 +938,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 +1014,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 +1054,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 +1116,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 +1157,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 +1233,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 +1297,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
5 changes: 2 additions & 3 deletions src/language/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,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
12 changes: 6 additions & 6 deletions src/language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import type {
ConstDirectiveNode,
TypeNode,
NamedTypeNode,
ListTypeNode,
NonNullTypeNode,
TypeSystemDefinitionNode,
SchemaDefinitionNode,
OperationTypeDefinitionNode,
Expand Down Expand Up @@ -727,8 +729,7 @@ export class Parser {
if (this.expectOptionalToken(TokenKind.BRACKET_L)) {
const innerType = this.parseTypeReference();
this.expectToken(TokenKind.BRACKET_R);
type = this.node(start, {
// @ts-expect-error FIXME: TS Conversion
type = this.node<ListTypeNode>(start, {
kind: Kind.LIST_TYPE,
type: innerType,
});
Expand All @@ -737,13 +738,12 @@ export class Parser {
}

if (this.expectOptionalToken(TokenKind.BANG)) {
// @ts-expect-error FIXME: TS Conversion
return this.node(start, {
// @ts-expect-error FIXME: TS Conversion
return this.node<NonNullTypeNode>(start, {
kind: Kind.NON_NULL_TYPE,
type,
});
}

return type;
}

Expand Down Expand Up @@ -1420,7 +1420,7 @@ export class Parser {
parseDirectiveLocation(): NameNode {
const start = this._lexer.token;
const name = this.parseName();
if (DirectiveLocation[name.value] !== undefined) {
if (Object.prototype.hasOwnProperty.call(DirectiveLocation, name.value)) {
return name;
}
throw this.unexpected(start);
Expand Down
14 changes: 8 additions & 6 deletions src/language/printer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Maybe } from '../jsutils/Maybe';

import type { ASTNode } from './ast';

import type { ASTReducer } from './visitor';
import { visit } from './visitor';
import { printBlockString } from './blockString';

Expand All @@ -15,8 +15,7 @@ export function print(ast: ASTNode): string {

const MAX_LINE_LENGTH = 80;

// TODO: provide better type coverage in future
const printDocASTReducer: any = {
const printDocASTReducer: ASTReducer<string> = {
Name: { leave: (node) => node.value },
Variable: { leave: (node) => '$' + node.name },

Expand Down Expand Up @@ -309,15 +308,18 @@ const printDocASTReducer: any = {
* Given maybeArray, print an empty string if it is null or empty, otherwise
* print all items together separated by separator if provided
*/
function join(maybeArray: Maybe<Array<string>>, separator = ''): string {
function join(
maybeArray: Maybe<ReadonlyArray<string | undefined>>,
separator = '',
): string {
return maybeArray?.filter((x) => x).join(separator) ?? '';
}

/**
* Given array, print each item on its own line, wrapped in an
* indented "{ }" block.
*/
function block(array: Maybe<Array<string>>): string {
function block(array: Maybe<ReadonlyArray<string | undefined>>): string {
return wrap('{\n', indent(join(array, '\n')), '\n}');
}

Expand All @@ -338,7 +340,7 @@ function indent(str: string): string {
return wrap(' ', str.replace(/\n/g, '\n '));
}

function hasMultilineItems(maybeArray: Maybe<Array<string>>): boolean {
function hasMultilineItems(maybeArray: Maybe<ReadonlyArray<string>>): boolean {
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
return maybeArray?.some((str) => str.includes('\n')) ?? false;
}

0 comments on commit 6e089f2

Please sign in to comment.