From aec89b8c0164361dc53d29b23132ac3aa9886ba8 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Sun, 26 Feb 2023 15:30:39 +0200 Subject: [PATCH] Use `replaceAll` instead of RegExp with global flag Motivation: since we droped support for node14 we can finally use this function. --- integrationTests/node/test.js | 3 ++- src/__testUtils__/dedent.ts | 2 +- src/__testUtils__/inspectStr.ts | 4 ++-- src/language/blockString.ts | 2 +- src/language/printer.ts | 2 +- src/utilities/printSchema.ts | 2 +- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/integrationTests/node/test.js b/integrationTests/node/test.js index 544a9390e7..2d4830aa7a 100644 --- a/integrationTests/node/test.js +++ b/integrationTests/node/test.js @@ -6,8 +6,9 @@ const graphqlPackageJSON = JSON.parse( ); const nodeVersions = graphqlPackageJSON.engines.node + .replaceAll('^', '') + .replaceAll('>=', '') .split(' || ') - .map((version) => version.replace('^', '').replace('>=', '')) .sort((a, b) => b.localeCompare(a)); for (const version of nodeVersions) { diff --git a/src/__testUtils__/dedent.ts b/src/__testUtils__/dedent.ts index 7fc6b46345..2085286165 100644 --- a/src/__testUtils__/dedent.ts +++ b/src/__testUtils__/dedent.ts @@ -12,7 +12,7 @@ export function dedentString(string: string): string { indent += char; } - return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent + return trimmedStr.replaceAll(RegExp('^' + indent, 'mg'), ''); // remove indent } /** diff --git a/src/__testUtils__/inspectStr.ts b/src/__testUtils__/inspectStr.ts index 74c40e3145..1e22da0042 100644 --- a/src/__testUtils__/inspectStr.ts +++ b/src/__testUtils__/inspectStr.ts @@ -9,6 +9,6 @@ export function inspectStr(str: Maybe): string { } return JSON.stringify(str) .replace(/^"|"$/g, '`') - .replace(/\\"/g, '"') - .replace(/\\\\/g, '\\'); + .replaceAll('\\"', '"') + .replaceAll('\\\\', '\\'); } diff --git a/src/language/blockString.ts b/src/language/blockString.ts index 5f8d5f6127..3d7915b781 100644 --- a/src/language/blockString.ts +++ b/src/language/blockString.ts @@ -122,7 +122,7 @@ export function printBlockString( value: string, options?: { minimize?: boolean }, ): string { - const escapedValue = value.replace(/"""/g, '\\"""'); + const escapedValue = value.replaceAll('"""', '\\"""'); // Expand a block string's raw value into independent lines. const lines = escapedValue.split(/\r\n|[\n\r]/g); diff --git a/src/language/printer.ts b/src/language/printer.ts index a07decc11d..6ccaa7d9b4 100644 --- a/src/language/printer.ts +++ b/src/language/printer.ts @@ -370,7 +370,7 @@ function wrap( } function indent(str: string): string { - return wrap(' ', str.replace(/\n/g, '\n ')); + return wrap(' ', str.replaceAll('\n', '\n ')); } function hasMultilineItems(maybeArray: Maybe>): boolean { diff --git a/src/utilities/printSchema.ts b/src/utilities/printSchema.ts index 0b9b1638d7..37cb09eb2e 100644 --- a/src/utilities/printSchema.ts +++ b/src/utilities/printSchema.ts @@ -317,5 +317,5 @@ function printDescription( const prefix = indentation && !firstInBlock ? '\n' + indentation : indentation; - return prefix + blockString.replace(/\n/g, '\n' + indentation) + '\n'; + return prefix + blockString.replaceAll('\n', '\n' + indentation) + '\n'; }