Skip to content

Commit

Permalink
fix: resolve TypeScript errors
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed May 25, 2022
1 parent f59f532 commit f45dcfa
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 37 deletions.
12 changes: 11 additions & 1 deletion packages/codemirror-graphql/src/utils/jsonParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,18 @@ function expect(str: string) {
throw syntaxError(`Expected ${str} but found ${found}.`);
}

type SyntaxErrorPosition = { start: number; end: number };

export class JSONSyntaxError extends Error {
readonly position: SyntaxErrorPosition;
constructor(message: string, position: SyntaxErrorPosition) {
super(message);
this.position = position;
}
}

function syntaxError(message: string) {
return { message, start, end };
return new JSONSyntaxError(message, { start, end });
}

function skip(k: string) {
Expand Down
9 changes: 5 additions & 4 deletions packages/codemirror-graphql/src/variables/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from 'graphql';

import jsonParse, {
JSONSyntaxError,
ParseArrayOutput,
ParseObjectOutput,
ParseValueOutput,
Expand Down Expand Up @@ -57,11 +58,11 @@ CodeMirror.registerHelper(
let ast;
try {
ast = jsonParse(text);
} catch (syntaxError) {
if (syntaxError.stack) {
throw syntaxError;
} catch (error) {
if (error instanceof JSONSyntaxError) {
return [lintError(editor, error.position, error.message)];
}
return [lintError(editor, syntaxError, syntaxError.message)];
throw error;
}

// If there are not yet known variables, do nothing.
Expand Down
6 changes: 5 additions & 1 deletion packages/graphiql/src/components/ToolbarButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ export class ToolbarButton extends React.Component<
this.props.onClick();
this.setState({ error: null });
} catch (error) {
this.setState({ error });
if (error instanceof Error) {
this.setState({ error });
return;
}
throw error;
}
};
}
2 changes: 1 addition & 1 deletion packages/graphql-language-service-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ switch (command) {
startServer(options);
} catch (error) {
const logger = new Logger();
logger.error(error);
logger.error(String(error));
}
break;
default: {
Expand Down
16 changes: 12 additions & 4 deletions packages/graphql-language-service-cli/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ interface AutocompleteResultsMap {
[i: number]: CompletionItem;
}

function formatUnknownError(error: unknown) {
let message: string | undefined;
if (error instanceof Error) {
message = error.stack;
}
return message ?? String(error);
}

function _getAutocompleteSuggestions(
queryText: string,
point: Position,
Expand All @@ -104,7 +112,7 @@ function _getAutocompleteSuggestions(
process.stdout.write(JSON.stringify(resultObject, null, 2));
return GRAPHQL_SUCCESS_CODE;
} catch (error) {
process.stderr.write((error?.stack ?? String(error)) + '\n');
process.stderr.write(formatUnknownError(error) + '\n');
return GRAPHQL_FAILURE_CODE;
}
}
Expand Down Expand Up @@ -133,7 +141,7 @@ function _getDiagnostics(
process.stdout.write(JSON.stringify(resultObject, null, 2));
return GRAPHQL_SUCCESS_CODE;
} catch (error) {
process.stderr.write((error?.stack ?? String(error)) + '\n');
process.stderr.write(formatUnknownError(error) + '\n');
return GRAPHQL_FAILURE_CODE;
}
}
Expand All @@ -147,7 +155,7 @@ function _getOutline(queryText: string): EXIT_CODE {
throw Error('Error parsing or no outline tree found');
}
} catch (error) {
process.stderr.write((error?.stack ?? String(error)) + '\n');
process.stderr.write(formatUnknownError(error) + '\n');
return GRAPHQL_FAILURE_CODE;
}
return GRAPHQL_SUCCESS_CODE;
Expand All @@ -161,7 +169,7 @@ function ensureText(queryText: string, filePath: string): string {
try {
text = fs.readFileSync(filePath, 'utf8');
} catch (error) {
throw new Error(error);
throw new Error(String(error));
}
}
return text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
TypeDefinitionNode,
NamedTypeNode,
ValidationRule,
GraphQLError,
} from 'graphql';

import {
Expand Down Expand Up @@ -153,15 +154,22 @@ export class GraphQLLanguageService {
});
}
} catch (error) {
const range = getRange(error.locations[0], document);
return [
{
severity: DIAGNOSTIC_SEVERITY.Error,
message: error.message,
source: 'GraphQL: Syntax',
range,
},
];
if (error instanceof GraphQLError) {
const range = getRange(
error.locations?.[0] ?? { column: 0, line: 0 },
document,
);
return [
{
severity: DIAGNOSTIC_SEVERITY.Error,
message: error.message,
source: 'GraphQL: Syntax',
range,
},
];
}

throw error;
}

// If there's a matching config, proceed to prepare to run validation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export class MessageProcessor {
}
}
} catch (err) {
this._logger.warn(err);
this._logger.warn(String(err));
}

// Here, we set the workspace settings in memory,
Expand Down Expand Up @@ -834,7 +834,7 @@ export class MessageProcessor {
await this._updateObjectTypeDefinition(uri, contents);
}
} catch (err) {
this._logger.error(err);
this._logger.error(String(err));
}
}
async _cacheSchemaFile(
Expand Down Expand Up @@ -1002,7 +1002,7 @@ export class MessageProcessor {
}
}
} catch (err) {
this._logger.error(err);
this._logger.error(String(err));
}
}
/**
Expand Down Expand Up @@ -1043,7 +1043,7 @@ export class MessageProcessor {
this._logger.error(
`invalid/unknown file in graphql config documents entry:\n '${project.documents}'`,
);
this._logger.error(err);
this._logger.error(String(err));
}
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function findGraphQLTags(
logger.error(
`Could not parse the ${type} file at ${uri} to extract the graphql tags:`,
);
logger.error(error);
logger.error(String(error));
return [];
}
const ast = parsedAST!;
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-language-service-server/src/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export default async function startServer(
serverWithHandlers.listen();
} catch (err) {
logger.error('There was a Graphql LSP handler exception:');
logger.error(err);
logger.error(String(err));
}
}
}
Expand All @@ -236,7 +236,7 @@ async function initializeHandlers({
return connection;
} catch (err) {
logger.error('There was an error initializing the server connection');
logger.error(err);
logger.error(String(err));
process.exit(1);
}
}
Expand Down
25 changes: 16 additions & 9 deletions packages/graphql-language-service/src/interface/getDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,22 @@ export function getDiagnostics(
try {
ast = parse(query);
} catch (error) {
const range = getRange(error.locations[0], query);
return [
{
severity: DIAGNOSTIC_SEVERITY.Error as DiagnosticSeverity,
message: error.message,
source: 'GraphQL: Syntax',
range,
},
];
if (error instanceof GraphQLError) {
const range = getRange(
error.locations?.[0] ?? { line: 0, column: 0 },
query,
);

return [
{
severity: DIAGNOSTIC_SEVERITY.Error as DiagnosticSeverity,
message: error.message,
source: 'GraphQL: Syntax',
range,
},
];
}
throw error;
}

return validateQuery(ast, schema, customRules, isRelayCompatMode);
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-language-service/src/parser/Rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export const ParseRules: { [name: string]: ParseRule } = {
StringValue: [
{
style: 'string',
match: token => token.kind === 'String',
match: (token: Token) => token.kind === 'String',
update(state: State, token: Token) {
if (token.value.startsWith('"""')) {
state.inBlockstring = !token.value.slice(3).endsWith('"""');
Expand Down

0 comments on commit f45dcfa

Please sign in to comment.