From c70d9165cc1ef8eb1cd0d6b506ced98c626597f9 Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Thu, 8 Dec 2022 12:19:37 +0100 Subject: [PATCH] enable `unicorn/prefer-includes` (#2937) * enable `unicorn/prefer-includes` * fix build --- .changeset/modern-vans-warn.md | 11 +++++ .../src/utils/jump-addon.ts | 2 +- .../src/explorer/components/search.tsx | 2 +- .../src/graphql-helpers/merge-ast.ts | 4 +- .../src/graphql-helpers/operation-name.ts | 5 +-- .../src/MessageProcessor.ts | 3 +- .../src/findGraphQLTags.ts | 5 +-- .../src/parseDocument.ts | 6 +-- .../getAutocompleteSuggestions-test.ts | 4 +- .../interface/getAutocompleteSuggestions.ts | 40 +++++++++---------- .../src/utils/validateWithCustomRules.ts | 2 +- packages/vscode-graphql/src/apis/statusBar.ts | 2 +- scripts/pretty.js | 2 +- 13 files changed, 43 insertions(+), 45 deletions(-) create mode 100644 .changeset/modern-vans-warn.md diff --git a/.changeset/modern-vans-warn.md b/.changeset/modern-vans-warn.md new file mode 100644 index 00000000000..70caccd5013 --- /dev/null +++ b/.changeset/modern-vans-warn.md @@ -0,0 +1,11 @@ +--- +'codemirror-graphql': patch +'@graphiql/react': patch +'@graphiql/toolkit': patch +'graphql-language-service': patch +'graphql-language-service-server': patch +'vscode-graphql': patch +'vscode-graphql-execution': patch +--- + +enable `unicorn/prefer-includes` diff --git a/packages/codemirror-graphql/src/utils/jump-addon.ts b/packages/codemirror-graphql/src/utils/jump-addon.ts index d9cca3bc9ed..742741a91cf 100644 --- a/packages/codemirror-graphql/src/utils/jump-addon.ts +++ b/packages/codemirror-graphql/src/utils/jump-addon.ts @@ -123,7 +123,7 @@ function onKeyDown(cm: CodeMirror.Editor, event: KeyboardEvent) { const isMac = typeof navigator !== 'undefined' && navigator && - navigator.appVersion.indexOf('Mac') !== -1; + navigator.appVersion.includes('Mac'); function isJumpModifier(key: string) { return key === (isMac ? 'Meta' : 'Control'); diff --git a/packages/graphiql-react/src/explorer/components/search.tsx b/packages/graphiql-react/src/explorer/components/search.tsx index ff8f6d88e81..32f7662cf7e 100644 --- a/packages/graphiql-react/src/explorer/components/search.tsx +++ b/packages/graphiql-react/src/explorer/components/search.tsx @@ -287,7 +287,7 @@ function isMatch(sourceText: string, searchValue: string) { const escaped = searchValue.replace(/[^_0-9A-Za-z]/g, ch => '\\' + ch); return sourceText.search(new RegExp(escaped, 'i')) !== -1; } catch (e) { - return sourceText.toLowerCase().indexOf(searchValue.toLowerCase()) !== -1; + return sourceText.toLowerCase().includes(searchValue.toLowerCase()); } } diff --git a/packages/graphiql-toolkit/src/graphql-helpers/merge-ast.ts b/packages/graphiql-toolkit/src/graphql-helpers/merge-ast.ts index 288a48973d6..ecb0a9cca32 100644 --- a/packages/graphiql-toolkit/src/graphql-helpers/merge-ast.ts +++ b/packages/graphiql-toolkit/src/graphql-helpers/merge-ast.ts @@ -56,12 +56,12 @@ function inlineRelevantFragmentSpreads( ? getNamedType(selectionSetType).name : null; const outputSelections = []; - const seenSpreads = []; + const seenSpreads: string[] = []; for (let selection of selections) { if (selection.kind === 'FragmentSpread') { const fragmentName = selection.name.value; if (!selection.directives || selection.directives.length === 0) { - if (seenSpreads.indexOf(fragmentName) >= 0) { + if (seenSpreads.includes(fragmentName)) { /* It's a duplicate - skip it! */ continue; } else { diff --git a/packages/graphiql-toolkit/src/graphql-helpers/operation-name.ts b/packages/graphiql-toolkit/src/graphql-helpers/operation-name.ts index 4bdd1a912f9..041e31ffcf4 100644 --- a/packages/graphiql-toolkit/src/graphql-helpers/operation-name.ts +++ b/packages/graphiql-toolkit/src/graphql-helpers/operation-name.ts @@ -16,10 +16,7 @@ export function getSelectedOperationName( // If a previous selection still exists, continue to use it. const names = operations.map(op => op.name?.value); - if ( - prevSelectedOperationName && - names.indexOf(prevSelectedOperationName) !== -1 - ) { + if (prevSelectedOperationName && names.includes(prevSelectedOperationName)) { return prevSelectedOperationName; } diff --git a/packages/graphql-language-service-server/src/MessageProcessor.ts b/packages/graphql-language-service-server/src/MessageProcessor.ts index d77e95f60f1..b8b022cf74c 100644 --- a/packages/graphql-language-service-server/src/MessageProcessor.ts +++ b/packages/graphql-language-service-server/src/MessageProcessor.ts @@ -1160,8 +1160,7 @@ export class MessageProcessor { } _isRelayCompatMode(query: string): boolean { return ( - query.indexOf('RelayCompat') !== -1 || - query.indexOf('react-relay/compat') !== -1 + query.includes('RelayCompat') || query.includes('react-relay/compat') ); } diff --git a/packages/graphql-language-service-server/src/findGraphQLTags.ts b/packages/graphql-language-service-server/src/findGraphQLTags.ts index 86ac9050e22..31f5a118c2f 100644 --- a/packages/graphql-language-service-server/src/findGraphQLTags.ts +++ b/packages/graphql-language-service-server/src/findGraphQLTags.ts @@ -260,10 +260,7 @@ const IGNORED_KEYS: { [key: string]: boolean } = { }; function getGraphQLTagName(tag: Expression): string | null { - if ( - tag.type === 'Identifier' && - DEFAULT_STABLE_TAGS.some(t => t === tag.name) - ) { + if (tag.type === 'Identifier' && DEFAULT_STABLE_TAGS.includes(tag.name)) { return tag.name; } if ( diff --git a/packages/graphql-language-service-server/src/parseDocument.ts b/packages/graphql-language-service-server/src/parseDocument.ts index 442de4ef4d3..a251a3478d8 100644 --- a/packages/graphql-language-service-server/src/parseDocument.ts +++ b/packages/graphql-language-service-server/src/parseDocument.ts @@ -53,14 +53,14 @@ export function parseDocument( // Check if the text content includes a GraphQLV query. // If the text doesn't include GraphQL queries, do not proceed. const ext = extname(uri); - if (fileExtensions.some(e => e === ext)) { - if (DEFAULT_TAGS.some(t => t === text)) { + if (fileExtensions.includes(ext)) { + if (DEFAULT_TAGS.includes(text)) { return []; } const templates = findGraphQLTags(text, ext, uri, logger); return templates.map(({ template, range }) => ({ query: template, range })); } - if (graphQLFileExtensions.some(e => e === ext)) { + if (graphQLFileExtensions.includes(ext)) { const query = text; if (!query && query !== '') { return []; diff --git a/packages/graphql-language-service/src/interface/__tests__/getAutocompleteSuggestions-test.ts b/packages/graphql-language-service/src/interface/__tests__/getAutocompleteSuggestions-test.ts index 0237a95527b..81f6f0737e9 100644 --- a/packages/graphql-language-service/src/interface/__tests__/getAutocompleteSuggestions-test.ts +++ b/packages/graphql-language-service/src/interface/__tests__/getAutocompleteSuggestions-test.ts @@ -84,9 +84,7 @@ describe('getAutocompleteSuggestions', () => { externalFragments, options, ) - .filter( - field => !['__schema', '__type'].some(name => name === field.label), - ) + .filter(field => !['__schema', '__type'].includes(field.label)) .sort((a, b) => a.label.localeCompare(b.label)) .map(suggestion => { // TODO: A PR where we do `const { type, ..rest} = suggestion; return rest;` diff --git a/packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts b/packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts index b18c891509e..d76462f749a 100644 --- a/packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts +++ b/packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts @@ -1061,51 +1061,47 @@ export function canUseDirective( const locations = directive.locations; switch (kind) { case RuleKinds.QUERY: - return locations.indexOf(DirectiveLocation.QUERY) !== -1; + return locations.includes(DirectiveLocation.QUERY); case RuleKinds.MUTATION: - return locations.indexOf(DirectiveLocation.MUTATION) !== -1; + return locations.includes(DirectiveLocation.MUTATION); case RuleKinds.SUBSCRIPTION: - return locations.indexOf(DirectiveLocation.SUBSCRIPTION) !== -1; + return locations.includes(DirectiveLocation.SUBSCRIPTION); case RuleKinds.FIELD: case RuleKinds.ALIASED_FIELD: - return locations.indexOf(DirectiveLocation.FIELD) !== -1; + return locations.includes(DirectiveLocation.FIELD); case RuleKinds.FRAGMENT_DEFINITION: - return locations.indexOf(DirectiveLocation.FRAGMENT_DEFINITION) !== -1; + return locations.includes(DirectiveLocation.FRAGMENT_DEFINITION); case RuleKinds.FRAGMENT_SPREAD: - return locations.indexOf(DirectiveLocation.FRAGMENT_SPREAD) !== -1; + return locations.includes(DirectiveLocation.FRAGMENT_SPREAD); case RuleKinds.INLINE_FRAGMENT: - return locations.indexOf(DirectiveLocation.INLINE_FRAGMENT) !== -1; + return locations.includes(DirectiveLocation.INLINE_FRAGMENT); // Schema Definitions case RuleKinds.SCHEMA_DEF: - return locations.indexOf(DirectiveLocation.SCHEMA) !== -1; + return locations.includes(DirectiveLocation.SCHEMA); case RuleKinds.SCALAR_DEF: - return locations.indexOf(DirectiveLocation.SCALAR) !== -1; + return locations.includes(DirectiveLocation.SCALAR); case RuleKinds.OBJECT_TYPE_DEF: - return locations.indexOf(DirectiveLocation.OBJECT) !== -1; + return locations.includes(DirectiveLocation.OBJECT); case RuleKinds.FIELD_DEF: - return locations.indexOf(DirectiveLocation.FIELD_DEFINITION) !== -1; + return locations.includes(DirectiveLocation.FIELD_DEFINITION); case RuleKinds.INTERFACE_DEF: - return locations.indexOf(DirectiveLocation.INTERFACE) !== -1; + return locations.includes(DirectiveLocation.INTERFACE); case RuleKinds.UNION_DEF: - return locations.indexOf(DirectiveLocation.UNION) !== -1; + return locations.includes(DirectiveLocation.UNION); case RuleKinds.ENUM_DEF: - return locations.indexOf(DirectiveLocation.ENUM) !== -1; + return locations.includes(DirectiveLocation.ENUM); case RuleKinds.ENUM_VALUE: - return locations.indexOf(DirectiveLocation.ENUM_VALUE) !== -1; + return locations.includes(DirectiveLocation.ENUM_VALUE); case RuleKinds.INPUT_DEF: - return locations.indexOf(DirectiveLocation.INPUT_OBJECT) !== -1; + return locations.includes(DirectiveLocation.INPUT_OBJECT); case RuleKinds.INPUT_VALUE_DEF: const prevStateKind = state.prevState?.kind; switch (prevStateKind) { case RuleKinds.ARGUMENTS_DEF: - return ( - locations.indexOf(DirectiveLocation.ARGUMENT_DEFINITION) !== -1 - ); + return locations.includes(DirectiveLocation.ARGUMENT_DEFINITION); case RuleKinds.INPUT_DEF: - return ( - locations.indexOf(DirectiveLocation.INPUT_FIELD_DEFINITION) !== -1 - ); + return locations.includes(DirectiveLocation.INPUT_FIELD_DEFINITION); } } diff --git a/packages/graphql-language-service/src/utils/validateWithCustomRules.ts b/packages/graphql-language-service/src/utils/validateWithCustomRules.ts index aa955e0e837..4f4f2ecac69 100644 --- a/packages/graphql-language-service/src/utils/validateWithCustomRules.ts +++ b/packages/graphql-language-service/src/utils/validateWithCustomRules.ts @@ -83,7 +83,7 @@ export function validateWithCustomRules( } const errors = validate(schema, ast, rules); return errors.filter(error => { - if (error.message.indexOf('Unknown directive') !== -1 && error.nodes) { + if (error.message.includes('Unknown directive') && error.nodes) { const node = error.nodes[0]; if (node && node.kind === Kind.DIRECTIVE) { const name = node.name.value; diff --git a/packages/vscode-graphql/src/apis/statusBar.ts b/packages/vscode-graphql/src/apis/statusBar.ts index 02c0ca83640..4153d3cd309 100644 --- a/packages/vscode-graphql/src/apis/statusBar.ts +++ b/packages/vscode-graphql/src/apis/statusBar.ts @@ -139,7 +139,7 @@ function updateStatusBar( if ( editor && - statusBarActivationLanguageIds.indexOf(editor.document.languageId) > -1 + statusBarActivationLanguageIds.includes(editor.document.languageId) ) { statusBarItem.show(); } else { diff --git a/scripts/pretty.js b/scripts/pretty.js index b576034582a..ba0299fa20b 100755 --- a/scripts/pretty.js +++ b/scripts/pretty.js @@ -25,7 +25,7 @@ const executable = join( os.platform() === 'win32' ? 'prettier.cmd' : 'prettier', ); const ignorePath = ['--ignore-path', '.eslintignore']; -const check = process.argv.indexOf('--check') !== -1; +const check = process.argv.includes('--check'); const mode = check ? '--list-different' : '--write'; process.chdir(root);