From 69f6f0603ede51b7700232e2919818a92778d455 Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Sat, 25 Feb 2023 01:23:55 +0100 Subject: [PATCH] enable `unicorn/prefer-string-replace-all` rule --- .eslintrc.js | 1 + examples/graphiql-webpack/src/index.jsx | 2 +- packages/codemirror-graphql/src/utils/hintList.ts | 2 +- packages/graphiql-react/src/explorer/components/search.tsx | 2 +- packages/graphiql-react/src/history/components.tsx | 6 +++--- .../src/graphql-helpers/__tests__/merge-ast.spec.ts | 2 +- .../graphiql-toolkit/src/graphql-helpers/auto-complete.ts | 2 +- packages/graphiql/cypress/support/commands.ts | 4 ++-- packages/graphiql/cypress/tsconfig.json | 2 +- .../src/interface/autocompleteUtils.ts | 2 +- .../src/parser/__tests__/OnlineParserUtils.ts | 2 +- 11 files changed, 14 insertions(+), 13 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 2394e895bc4..6f36a2c108b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -267,6 +267,7 @@ module.exports = { 'sonarjs/no-duplicated-branches': 'error', 'unicorn/prefer-node-protocol': 'error', 'import/no-unresolved': ['error', { ignore: ['^node:'] }], + 'unicorn/prefer-string-replace-all': 'error', // TODO: Fix all errors for the following rules included in recommended config '@typescript-eslint/no-explicit-any': 'off', diff --git a/examples/graphiql-webpack/src/index.jsx b/examples/graphiql-webpack/src/index.jsx index 52cfeb6a54f..e955f2693ff 100644 --- a/examples/graphiql-webpack/src/index.jsx +++ b/examples/graphiql-webpack/src/index.jsx @@ -21,7 +21,7 @@ const getQuery = (arg, spaceCount) => { const anonymousQuery = removeQueryName(query); return ( ` `.repeat(spaceCount) + - anonymousQuery.replace(/\n/g, `\n` + ` `.repeat(spaceCount)) + anonymousQuery.replaceAll('\n', `\n` + ` `.repeat(spaceCount)) ); }; diff --git a/packages/codemirror-graphql/src/utils/hintList.ts b/packages/codemirror-graphql/src/utils/hintList.ts index d5f9c7e74a8..dc842ab04fa 100644 --- a/packages/codemirror-graphql/src/utils/hintList.ts +++ b/packages/codemirror-graphql/src/utils/hintList.ts @@ -68,7 +68,7 @@ function filterNonEmpty(array: T[], predicate: (item: T) => boolean) { } function normalizeText(text: string) { - return text.toLowerCase().replace(/\W/g, ''); + return text.toLowerCase().replaceAll(/\W/g, ''); } // Determine a numeric proximity for a suggestion based on current text. diff --git a/packages/graphiql-react/src/explorer/components/search.tsx b/packages/graphiql-react/src/explorer/components/search.tsx index bb8c8eff540..befc8332edd 100644 --- a/packages/graphiql-react/src/explorer/components/search.tsx +++ b/packages/graphiql-react/src/explorer/components/search.tsx @@ -284,7 +284,7 @@ export function useSearchResults(caller?: Function) { function isMatch(sourceText: string, searchValue: string) { try { - const escaped = searchValue.replace(/[^_0-9A-Za-z]/g, ch => '\\' + ch); + const escaped = searchValue.replaceAll(/[^_0-9A-Za-z]/g, ch => '\\' + ch); return sourceText.search(new RegExp(escaped, 'i')) !== -1; } catch { return sourceText.toLowerCase().includes(searchValue.toLowerCase()); diff --git a/packages/graphiql-react/src/history/components.tsx b/packages/graphiql-react/src/history/components.tsx index ca2c95ed3bc..b5ad56b52a8 100644 --- a/packages/graphiql-react/src/history/components.tsx +++ b/packages/graphiql-react/src/history/components.tsx @@ -164,7 +164,7 @@ export function formatQuery(query?: string) { ?.split('\n') .map(line => line.replace(/#(.*)/, '')) .join(' ') - .replace(/{/g, ' { ') - .replace(/}/g, ' } ') - .replace(/[\s]{2,}/g, ' '); + .replaceAll('{', ' { ') + .replaceAll('}', ' } ') + .replaceAll(/[\s]{2,}/g, ' '); } diff --git a/packages/graphiql-toolkit/src/graphql-helpers/__tests__/merge-ast.spec.ts b/packages/graphiql-toolkit/src/graphql-helpers/__tests__/merge-ast.spec.ts index 60391053208..85d59054320 100644 --- a/packages/graphiql-toolkit/src/graphql-helpers/__tests__/merge-ast.spec.ts +++ b/packages/graphiql-toolkit/src/graphql-helpers/__tests__/merge-ast.spec.ts @@ -160,5 +160,5 @@ function parseMergeAndPrint(query: string, maybeSchema?: GraphQLSchema) { } function stripWhitespace(str: string) { - return str.replace(/\s/g, ''); + return str.replaceAll(/\s/g, ''); } diff --git a/packages/graphiql-toolkit/src/graphql-helpers/auto-complete.ts b/packages/graphiql-toolkit/src/graphql-helpers/auto-complete.ts index e67cf82a900..cf8127c963e 100644 --- a/packages/graphiql-toolkit/src/graphql-helpers/auto-complete.ts +++ b/packages/graphiql-toolkit/src/graphql-helpers/auto-complete.ts @@ -66,7 +66,7 @@ export function fillLeafs( const indent = getIndentation(docString, node.loc.start); insertions.push({ index: node.loc.end, - string: ' ' + print(selectionSet).replace(/\n/g, '\n' + indent), + string: ' ' + print(selectionSet).replaceAll('\n', '\n' + indent), }); } } diff --git a/packages/graphiql/cypress/support/commands.ts b/packages/graphiql/cypress/support/commands.ts index dd2163a0c4a..5b7181edac2 100644 --- a/packages/graphiql/cypress/support/commands.ts +++ b/packages/graphiql/cypress/support/commands.ts @@ -137,11 +137,11 @@ function codeWithLineNumbers(code: string): string { } function normalize(str: string) { - return str.replace(/\u200b/g, ''); + return str.replaceAll('​', ''); } function normalizeWhitespace(str: string) { - return str.replace(/\u00a0/g, ' '); + return str.replaceAll('\xA0', ' '); } Cypress.Commands.add( diff --git a/packages/graphiql/cypress/tsconfig.json b/packages/graphiql/cypress/tsconfig.json index 18edb199acd..d103d9147dc 100644 --- a/packages/graphiql/cypress/tsconfig.json +++ b/packages/graphiql/cypress/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "target": "es5", - "lib": ["es5", "dom"], + "lib": ["es2021", "dom"], "types": ["cypress", "node"] }, "include": ["**/*.ts"] diff --git a/packages/graphql-language-service/src/interface/autocompleteUtils.ts b/packages/graphql-language-service/src/interface/autocompleteUtils.ts index cf7c192ab07..7e9eb917c13 100644 --- a/packages/graphql-language-service/src/interface/autocompleteUtils.ts +++ b/packages/graphql-language-service/src/interface/autocompleteUtils.ts @@ -140,7 +140,7 @@ function filterNonEmpty( } function normalizeText(text: string): string { - return text.toLowerCase().replace(/\W/g, ''); + return text.toLowerCase().replaceAll(/\W/g, ''); } // Determine a numeric proximity for a suggestion based on current text. diff --git a/packages/graphql-language-service/src/parser/__tests__/OnlineParserUtils.ts b/packages/graphql-language-service/src/parser/__tests__/OnlineParserUtils.ts index 6ba7cb24512..3f5d6e7fe80 100644 --- a/packages/graphql-language-service/src/parser/__tests__/OnlineParserUtils.ts +++ b/packages/graphql-language-service/src/parser/__tests__/OnlineParserUtils.ts @@ -130,7 +130,7 @@ export const performForEachType = (source, test) => { Object.keys(typesMap).map(type => { const { value, kind, valueType } = typesMap[type]; const utils = getUtils( - source.replace(/__VALUE__/g, value).replace(/__TYPE__/g, type), + source.replaceAll('__VALUE__', value).replaceAll('__TYPE__', type), ); test(utils, { type, value, kind, valueType }); });