From 0669767e1e2196a78cbefe3679a52bcbb341e913 Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Mon, 9 Jan 2023 20:11:15 +0100 Subject: [PATCH] enable `unicorn/prefer-optional-catch-binding` rule (#2965) --- .changeset/poor-mirrors-learn.md | 10 ++++++++++ .eslintrc.js | 1 + .../src/editor/components/image-preview.tsx | 2 +- packages/graphiql-react/src/editor/tabs.ts | 2 +- .../graphiql-react/src/explorer/components/search.tsx | 2 +- packages/graphiql-react/src/schema.tsx | 2 +- .../src/graphql-helpers/auto-complete.ts | 2 +- packages/graphiql-toolkit/src/storage/history.ts | 2 +- .../src/GraphQLCache.ts | 10 +++++----- .../src/GraphQLLanguageService.ts | 4 ++-- packages/graphql-language-service-server/src/Logger.ts | 2 +- .../src/MessageProcessor.ts | 6 +++--- .../src/interface/getOutline.ts | 2 +- .../src/utils/fragmentDependencies.ts | 2 +- packages/monaco-graphql/src/LanguageService.ts | 4 ++-- packages/monaco-graphql/src/graphqlMode.ts | 2 +- .../vscode-graphql-execution/src/helpers/source.ts | 8 ++++---- 17 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 .changeset/poor-mirrors-learn.md diff --git a/.changeset/poor-mirrors-learn.md b/.changeset/poor-mirrors-learn.md new file mode 100644 index 00000000000..56ba57d3865 --- /dev/null +++ b/.changeset/poor-mirrors-learn.md @@ -0,0 +1,10 @@ +--- +'@graphiql/react': patch +'@graphiql/toolkit': patch +'graphql-language-service': patch +'graphql-language-service-server': patch +'monaco-graphql': patch +'vscode-graphql-execution': patch +--- + +enable `unicorn/prefer-optional-catch-binding` rule diff --git a/.eslintrc.js b/.eslintrc.js index cdc11dcfa56..e1d354ca99e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -268,6 +268,7 @@ module.exports = { 'unicorn/prefer-includes': 'error', 'no-lonely-if': 'error', 'unicorn/no-lonely-if': 'error', + 'unicorn/prefer-optional-catch-binding': 'error', 'no-unused-expressions': 'off', '@typescript-eslint/no-unused-expressions': 'error', 'sonarjs/no-small-switch': 'error', diff --git a/packages/graphiql-react/src/editor/components/image-preview.tsx b/packages/graphiql-react/src/editor/components/image-preview.tsx index c5662a773d6..76c5536f7c4 100644 --- a/packages/graphiql-react/src/editor/components/image-preview.tsx +++ b/packages/graphiql-react/src/editor/components/image-preview.tsx @@ -78,7 +78,7 @@ function tokenToURL(token: Token) { try { const location = window.location; return new URL(value, location.protocol + '//' + location.host); - } catch (err) { + } catch { return; } } diff --git a/packages/graphiql-react/src/editor/tabs.ts b/packages/graphiql-react/src/editor/tabs.ts index d99cc6bc7aa..6a2fce75ef1 100644 --- a/packages/graphiql-react/src/editor/tabs.ts +++ b/packages/graphiql-react/src/editor/tabs.ts @@ -123,7 +123,7 @@ export function getDefaultTabState({ return parsed; } throw new Error('Storage for tabs is invalid'); - } catch (err) { + } catch { return { activeTabIndex: 0, tabs: ( diff --git a/packages/graphiql-react/src/explorer/components/search.tsx b/packages/graphiql-react/src/explorer/components/search.tsx index 32f7662cf7e..bb8c8eff540 100644 --- a/packages/graphiql-react/src/explorer/components/search.tsx +++ b/packages/graphiql-react/src/explorer/components/search.tsx @@ -286,7 +286,7 @@ function isMatch(sourceText: string, searchValue: string) { try { const escaped = searchValue.replace(/[^_0-9A-Za-z]/g, ch => '\\' + ch); return sourceText.search(new RegExp(escaped, 'i')) !== -1; - } catch (e) { + } catch { return sourceText.toLowerCase().includes(searchValue.toLowerCase()); } } diff --git a/packages/graphiql-react/src/schema.tsx b/packages/graphiql-react/src/schema.tsx index b9fa64b34ce..faecd072894 100644 --- a/packages/graphiql-react/src/schema.tsx +++ b/packages/graphiql-react/src/schema.tsx @@ -417,7 +417,7 @@ function parseHeaderString(headersString: string | undefined) { if (headersString) { headers = JSON.parse(headersString); } - } catch (err) { + } catch { isValidJSON = false; } return { headers, isValidJSON }; diff --git a/packages/graphiql-toolkit/src/graphql-helpers/auto-complete.ts b/packages/graphiql-toolkit/src/graphql-helpers/auto-complete.ts index 3c169676827..e67cf82a900 100644 --- a/packages/graphiql-toolkit/src/graphql-helpers/auto-complete.ts +++ b/packages/graphiql-toolkit/src/graphql-helpers/auto-complete.ts @@ -44,7 +44,7 @@ export function fillLeafs( let ast: DocumentNode; try { ast = parse(docString); - } catch (error) { + } catch { return { insertions, result: docString }; } diff --git a/packages/graphiql-toolkit/src/storage/history.ts b/packages/graphiql-toolkit/src/storage/history.ts index 89934986382..35d774fb8df 100644 --- a/packages/graphiql-toolkit/src/storage/history.ts +++ b/packages/graphiql-toolkit/src/storage/history.ts @@ -34,7 +34,7 @@ export class HistoryStore { try { parse(query); - } catch (e) { + } catch { return false; } diff --git a/packages/graphql-language-service-server/src/GraphQLCache.ts b/packages/graphql-language-service-server/src/GraphQLCache.ts index db75aa6d27e..f8cdbbafe76 100644 --- a/packages/graphql-language-service-server/src/GraphQLCache.ts +++ b/packages/graphql-language-service-server/src/GraphQLCache.ts @@ -142,7 +142,7 @@ export class GraphQLCache implements GraphQLCacheInterface { let parsedQuery; try { parsedQuery = parse(query); - } catch (error) { + } catch { return []; } return this.getFragmentDependenciesForAST(parsedQuery, fragmentDefinitions); @@ -234,7 +234,7 @@ export class GraphQLCache implements GraphQLCacheInterface { let parsedQuery; try { parsedQuery = parse(query); - } catch (error) { + } catch { return []; } return this.getObjectTypeDependenciesForAST( @@ -426,7 +426,7 @@ export class GraphQLCache implements GraphQLCacheInterface { ast: parse(query), query, }; - } catch (error) { + } catch { return { ast: null, query }; } }); @@ -488,7 +488,7 @@ export class GraphQLCache implements GraphQLCacheInterface { ast: parse(query), query, }; - } catch (error) { + } catch { return { ast: null, query }; } }); @@ -827,7 +827,7 @@ export class GraphQLCache implements GraphQLCacheInterface { mtime: 0, size: 0, }); - } catch (_) { + } catch { // If query has syntax errors, go ahead and still resolve // the filePath and the content, but leave ast empty. resolve({ diff --git a/packages/graphql-language-service-server/src/GraphQLLanguageService.ts b/packages/graphql-language-service-server/src/GraphQLLanguageService.ts index 9d7546762d4..02c3e223196 100644 --- a/packages/graphql-language-service-server/src/GraphQLLanguageService.ts +++ b/packages/graphql-language-service-server/src/GraphQLLanguageService.ts @@ -204,7 +204,7 @@ export class GraphQLLanguageService { let validationAst = null; try { validationAst = parse(source); - } catch (error) { + } catch { // the query string is already checked to be parsed properly - errors // from this parse must be from corrupted fragment dependencies. // For IDEs we don't care for errors outside of the currently edited @@ -306,7 +306,7 @@ export class GraphQLLanguageService { let ast; try { ast = parse(query); - } catch (error) { + } catch { return null; } diff --git a/packages/graphql-language-service-server/src/Logger.ts b/packages/graphql-language-service-server/src/Logger.ts index 5855b12f634..0c7a7f230cd 100644 --- a/packages/graphql-language-service-server/src/Logger.ts +++ b/packages/graphql-language-service-server/src/Logger.ts @@ -31,7 +31,7 @@ export class Logger implements VSCodeLogger { if (!fs.existsSync(dir)) { fs.mkdirSync(dir); } - } catch (_) { + } catch { // intentionally no-op. Don't block the language server even if // the necessary setup cannot be completed for logger. } diff --git a/packages/graphql-language-service-server/src/MessageProcessor.ts b/packages/graphql-language-service-server/src/MessageProcessor.ts index 7f63dff6932..fb80dd0aea6 100644 --- a/packages/graphql-language-service-server/src/MessageProcessor.ts +++ b/packages/graphql-language-service-server/src/MessageProcessor.ts @@ -783,7 +783,7 @@ export class MessageProcessor { toPosition(position), textDocument.uri, ); - } catch (err) { + } catch { // these thrown errors end up getting fired before the service is initialized, so lets cool down on that } @@ -983,11 +983,11 @@ export class MessageProcessor { } else { try { this._cacheSchemaFile(uri, project); - } catch (err) { + } catch { // this string may be an SDL string even, how do we even evaluate this? } } - } catch (err) {} + } catch {} } async _cacheObjectSchema( pointer: { [key: string]: any }, diff --git a/packages/graphql-language-service/src/interface/getOutline.ts b/packages/graphql-language-service/src/interface/getOutline.ts index b06c914d1ad..da5a36c9ac5 100644 --- a/packages/graphql-language-service/src/interface/getOutline.ts +++ b/packages/graphql-language-service/src/interface/getOutline.ts @@ -85,7 +85,7 @@ export function getOutline(documentText: string): Outline | null { let ast; try { ast = parse(documentText); - } catch (error) { + } catch { return null; } diff --git a/packages/graphql-language-service/src/utils/fragmentDependencies.ts b/packages/graphql-language-service/src/utils/fragmentDependencies.ts index 80dba30ac74..78a1a35e72e 100644 --- a/packages/graphql-language-service/src/utils/fragmentDependencies.ts +++ b/packages/graphql-language-service/src/utils/fragmentDependencies.ts @@ -22,7 +22,7 @@ export const getFragmentDependencies = ( let parsedOperation; try { parsedOperation = parse(operationString); - } catch (error) { + } catch { return []; } return getFragmentDependenciesForAST(parsedOperation, fragmentDefinitions); diff --git a/packages/monaco-graphql/src/LanguageService.ts b/packages/monaco-graphql/src/LanguageService.ts index b6bca7cce06..1339d7c3ceb 100644 --- a/packages/monaco-graphql/src/LanguageService.ts +++ b/packages/monaco-graphql/src/LanguageService.ts @@ -139,7 +139,7 @@ export class LanguageService { definitionNodes.push(node); }, }); - } catch (err) { + } catch { throw new Error( `Failed parsing externalFragmentDefinitions string:\n${this._externalFragmentDefinitionsString}`, ); @@ -278,7 +278,7 @@ export class LanguageService { if (operationFacts?.variableToType) { return getVariablesJSONSchema(operationFacts.variableToType, options); } - } catch (err) {} + } catch {} } return null; }; diff --git a/packages/monaco-graphql/src/graphqlMode.ts b/packages/monaco-graphql/src/graphqlMode.ts index 4c11b406011..12643a8877b 100644 --- a/packages/monaco-graphql/src/graphqlMode.ts +++ b/packages/monaco-graphql/src/graphqlMode.ts @@ -25,7 +25,7 @@ export function setupMode(defaults: MonacoGraphQLAPI): IDisposable { ): Promise => { try { return client!.getLanguageServiceWorker(...uris); - } catch (err) { + } catch { throw new Error('Error fetching graphql language service worker'); } }; diff --git a/packages/vscode-graphql-execution/src/helpers/source.ts b/packages/vscode-graphql-execution/src/helpers/source.ts index 04a66af851f..633109d8493 100644 --- a/packages/vscode-graphql-execution/src/helpers/source.ts +++ b/packages/vscode-graphql-execution/src/helpers/source.ts @@ -114,7 +114,7 @@ export class SourceHelper { // Object type try { return JSON.parse(value); - } catch (e) { + } catch { this.outputChannel.appendLine( `Failed to parse user input as JSON, please use double quotes.`, ); @@ -157,7 +157,7 @@ export class SourceHelper { const documentText = document.getText(); processGraphQLString(documentText, 0); return documents; - } catch (err) {} + } catch {} } tags.forEach(tag => { @@ -177,7 +177,7 @@ export class SourceHelper { processGraphQLString(contents, result.index + tag.length + 1); // no-op on exception, so that non-parse-able source files // don't break the extension while editing - } catch (e) {} + } catch {} } }); return documents; @@ -236,7 +236,7 @@ export const getFragmentDependencies = async ( let parsedQuery; try { parsedQuery = parse(query); - } catch (error) { + } catch { return []; } return getFragmentDependenciesForAST(parsedQuery, fragmentDefinitions);