Skip to content

Commit fdb74f9

Browse files
n1ru4lacao
authored andcommitted
fix: resolve TypeScript errors
1 parent 5cc91cc commit fdb74f9

File tree

13 files changed

+87
-42
lines changed

13 files changed

+87
-42
lines changed

packages/codemirror-graphql/src/utils/jsonParse.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,18 @@ function expect(str: string) {
155155
throw syntaxError(`Expected ${str} but found ${found}.`);
156156
}
157157

158+
type SyntaxErrorPosition = { start: number; end: number };
159+
160+
export class JSONSyntaxError extends Error {
161+
readonly position: SyntaxErrorPosition;
162+
constructor(message: string, position: SyntaxErrorPosition) {
163+
super(message);
164+
this.position = position;
165+
}
166+
}
167+
158168
function syntaxError(message: string) {
159-
return { message, start, end };
169+
return new JSONSyntaxError(message, { start, end });
160170
}
161171

162172
function skip(k: string) {

packages/codemirror-graphql/src/variables/lint.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from 'graphql';
1919

2020
import jsonParse, {
21+
JSONSyntaxError,
2122
ParseArrayOutput,
2223
ParseObjectOutput,
2324
ParseValueOutput,
@@ -57,11 +58,11 @@ CodeMirror.registerHelper(
5758
let ast;
5859
try {
5960
ast = jsonParse(text);
60-
} catch (syntaxError) {
61-
if (syntaxError.stack) {
62-
throw syntaxError;
61+
} catch (error) {
62+
if (error instanceof JSONSyntaxError) {
63+
return [lintError(editor, error.position, error.message)];
6364
}
64-
return [lintError(editor, syntaxError, syntaxError.message)];
65+
throw error;
6566
}
6667

6768
// If there are not yet known variables, do nothing.

packages/graphiql-react/src/history/context.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ export function HistoryContextProvider(props: HistoryContextProviderProps) {
154154
);
155155
}
156156

157-
export const useHistoryContext = createContextHook(HistoryContext);
157+
export const useHistoryContext = createContextHook<HistoryContextType>(
158+
HistoryContext,
159+
);
158160

159161
const DEFAULT_HISTORY_LENGTH = 20;
160162
const STORAGE_KEY = 'historyPaneOpen';

packages/graphiql/src/components/QueryHistory.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { useHistoryContext, useSelectHistoryItem } from '@graphiql/react';
8+
import {
9+
HistoryContextType,
10+
useHistoryContext,
11+
useSelectHistoryItem,
12+
} from '@graphiql/react';
913
import { QueryStoreItem } from '@graphiql/toolkit';
1014
import React, { useEffect, useRef, useState } from 'react';
1115

1216
export function QueryHistory() {
13-
const { hide, items } = useHistoryContext({ nonNull: true });
17+
const { hide, items } = useHistoryContext({
18+
nonNull: true,
19+
}) as HistoryContextType;
1420

1521
return (
1622
<section aria-label="History">

packages/graphiql/src/components/ToolbarButton.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ export class ToolbarButton extends React.Component<
4949
this.props.onClick();
5050
this.setState({ error: null });
5151
} catch (error) {
52-
this.setState({ error });
52+
if (error instanceof Error) {
53+
this.setState({ error });
54+
return;
55+
}
56+
throw error;
5357
}
5458
};
5559
}

packages/graphql-language-service-cli/src/cli.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ switch (command) {
130130
startServer(options);
131131
} catch (error) {
132132
const logger = new Logger();
133-
logger.error(error);
133+
logger.error(String(error));
134134
}
135135
break;
136136
default: {

packages/graphql-language-service-cli/src/client.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ interface AutocompleteResultsMap {
7979
[i: number]: CompletionItem;
8080
}
8181

82+
function formatUnknownError(error: unknown) {
83+
let message: string | undefined;
84+
if (error instanceof Error) {
85+
message = error.stack;
86+
}
87+
return message ?? String(error);
88+
}
89+
8290
function _getAutocompleteSuggestions(
8391
queryText: string,
8492
point: Position,
@@ -104,7 +112,7 @@ function _getAutocompleteSuggestions(
104112
process.stdout.write(JSON.stringify(resultObject, null, 2));
105113
return GRAPHQL_SUCCESS_CODE;
106114
} catch (error) {
107-
process.stderr.write((error?.stack ?? String(error)) + '\n');
115+
process.stderr.write(formatUnknownError(error) + '\n');
108116
return GRAPHQL_FAILURE_CODE;
109117
}
110118
}
@@ -133,7 +141,7 @@ function _getDiagnostics(
133141
process.stdout.write(JSON.stringify(resultObject, null, 2));
134142
return GRAPHQL_SUCCESS_CODE;
135143
} catch (error) {
136-
process.stderr.write((error?.stack ?? String(error)) + '\n');
144+
process.stderr.write(formatUnknownError(error) + '\n');
137145
return GRAPHQL_FAILURE_CODE;
138146
}
139147
}
@@ -147,7 +155,7 @@ function _getOutline(queryText: string): EXIT_CODE {
147155
throw Error('Error parsing or no outline tree found');
148156
}
149157
} catch (error) {
150-
process.stderr.write((error?.stack ?? String(error)) + '\n');
158+
process.stderr.write(formatUnknownError(error) + '\n');
151159
return GRAPHQL_FAILURE_CODE;
152160
}
153161
return GRAPHQL_SUCCESS_CODE;
@@ -161,7 +169,7 @@ function ensureText(queryText: string, filePath: string): string {
161169
try {
162170
text = fs.readFileSync(filePath, 'utf8');
163171
} catch (error) {
164-
throw new Error(error);
172+
throw new Error(String(error));
165173
}
166174
}
167175
return text;

packages/graphql-language-service-server/src/GraphQLLanguageService.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
NamedTypeNode,
1616
ValidationRule,
1717
FieldNode,
18+
GraphQLError,
1819
} from 'graphql';
1920

2021
import {
@@ -161,17 +162,23 @@ export class GraphQLLanguageService {
161162
return false;
162163
});
163164
}
164-
} catch (err) {
165-
const error = err as any;
166-
const range = getRange(error.locations[0], document);
167-
return [
168-
{
169-
severity: DIAGNOSTIC_SEVERITY.Error,
170-
message: error.message,
171-
source: 'GraphQL: Syntax',
172-
range,
173-
},
174-
];
165+
} catch (error) {
166+
if (error instanceof GraphQLError) {
167+
const range = getRange(
168+
error.locations?.[0] ?? { column: 0, line: 0 },
169+
document,
170+
);
171+
return [
172+
{
173+
severity: DIAGNOSTIC_SEVERITY.Error,
174+
message: error.message,
175+
source: 'GraphQL: Syntax',
176+
range,
177+
},
178+
];
179+
}
180+
181+
throw error;
175182
}
176183

177184
// If there's a matching config, proceed to prepare to run validation

packages/graphql-language-service-server/src/MessageProcessor.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ export class MessageProcessor {
321321
}
322322
}
323323
} catch (err) {
324-
this._logger.error(err);
324+
this._logger.error(String(err));
325325
}
326326

327327
// Here, we set the workspace settings in memory,
@@ -910,7 +910,7 @@ export class MessageProcessor {
910910
await this._updateObjectTypeDefinition(uri, contents);
911911
}
912912
} catch (err) {
913-
this._logger.error(err);
913+
this._logger.error(String(err));
914914
}
915915
}
916916
async _cacheSchemaFile(
@@ -1078,7 +1078,7 @@ export class MessageProcessor {
10781078
}
10791079
}
10801080
} catch (err) {
1081-
this._logger.error(err);
1081+
this._logger.error(String(err));
10821082
}
10831083
}
10841084
/**
@@ -1119,7 +1119,7 @@ export class MessageProcessor {
11191119
this._logger.error(
11201120
`invalid/unknown file in graphql config documents entry:\n '${project.documents}'`,
11211121
);
1122-
this._logger.error(err);
1122+
this._logger.error(String(err));
11231123
}
11241124
}
11251125
/**

packages/graphql-language-service-server/src/findGraphQLTags.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export function findGraphQLTags(
9595
logger.error(
9696
`Could not parse the ${type} file at ${uri} to extract the graphql tags:`,
9797
);
98-
logger.error(error);
98+
logger.error(String(error));
9999
return [];
100100
}
101101
const ast = parsedAST!;

packages/graphql-language-service-server/src/startServer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export default async function startServer(
211211
serverWithHandlers.listen();
212212
} catch (err) {
213213
logger.error('There was a Graphql LSP handler exception:');
214-
logger.error(err);
214+
logger.error(String(err));
215215
}
216216
}
217217
}
@@ -236,7 +236,7 @@ async function initializeHandlers({
236236
return connection;
237237
} catch (err) {
238238
logger.error('There was an error initializing the server connection');
239-
logger.error(err);
239+
logger.error(String(err));
240240
process.exit(1);
241241
}
242242
}

packages/graphql-language-service/src/interface/getDiagnostics.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,22 @@ export function getDiagnostics(
8181
try {
8282
ast = parse(query);
8383
} catch (error) {
84-
const range = getRange(error.locations[0], query);
85-
return [
86-
{
87-
severity: DIAGNOSTIC_SEVERITY.Error as DiagnosticSeverity,
88-
message: error.message,
89-
source: 'GraphQL: Syntax',
90-
range,
91-
},
92-
];
84+
if (error instanceof GraphQLError) {
85+
const range = getRange(
86+
error.locations?.[0] ?? { line: 0, column: 0 },
87+
query,
88+
);
89+
90+
return [
91+
{
92+
severity: DIAGNOSTIC_SEVERITY.Error as DiagnosticSeverity,
93+
message: error.message,
94+
source: 'GraphQL: Syntax',
95+
range,
96+
},
97+
];
98+
}
99+
throw error;
93100
}
94101

95102
return validateQuery(ast, schema, customRules, isRelayCompatMode);

packages/graphql-language-service/src/parser/Rules.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export const ParseRules: { [name: string]: ParseRule } = {
196196
StringValue: [
197197
{
198198
style: 'string',
199-
match: token => token.kind === 'String',
199+
match: (token: Token) => token.kind === 'String',
200200
update(state: State, token: Token) {
201201
if (token.value.startsWith('"""')) {
202202
state.inBlockstring = !token.value.slice(3).endsWith('"""');

0 commit comments

Comments
 (0)