From 9e32ba36dda85c1983da44770799615c12322f68 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Tue, 18 Jun 2024 07:56:22 +0200 Subject: [PATCH] refactor: use `String.replaceAll()` instead of `.replace()` with regex (#2090) --- android/android-manifest.js | 2 +- example/test/config.test.mjs | 2 +- example/test/specs/wdio.config.mjs | 2 +- package.json | 2 +- scripts/configure.mjs | 4 ++-- scripts/generate-schema.mjs | 2 +- scripts/helpers.js | 2 +- scripts/release-notes.mjs | 5 ++++- test/configure/gatherConfig.test.mjs | 6 +++--- tsconfig.cjs.json | 11 +++++++++++ tsconfig.esm.json | 11 ----------- tsconfig.json | 9 +++++---- 12 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 tsconfig.cjs.json delete mode 100644 tsconfig.esm.json diff --git a/android/android-manifest.js b/android/android-manifest.js index 58656751..d7b3a656 100644 --- a/android/android-manifest.js +++ b/android/android-manifest.js @@ -107,7 +107,7 @@ function generateAndroidManifest(appManifestPath, manifestOutput, fs = nodefs) { exports.generateAndroidManifest = generateAndroidManifest; -if (isMain(pathToFileURL(__filename).toString())) { +if (isMain(pathToFileURL(__filename))) { const [, , appManifestPath, manifestOutput] = process.argv; process.exitCode = generateAndroidManifest(appManifestPath, manifestOutput); } diff --git a/example/test/config.test.mjs b/example/test/config.test.mjs index a60fe305..ea3d8667 100644 --- a/example/test/config.test.mjs +++ b/example/test/config.test.mjs @@ -41,7 +41,7 @@ async function getLoadConfig() { * @returns {RegExp} */ function regexp(p) { - return new RegExp(p.replace(/\\/g, "\\\\")); + return new RegExp(p.replaceAll("\\", "\\\\")); } test("react-native config", async (t) => { diff --git a/example/test/specs/wdio.config.mjs b/example/test/specs/wdio.config.mjs index a65de399..9845d3fd 100644 --- a/example/test/specs/wdio.config.mjs +++ b/example/test/specs/wdio.config.mjs @@ -104,7 +104,7 @@ const findLatestIPhoneSimulator = (() => { simulator?.name ?? "iPhone 15 Pro", latestRuntime .substring("com.apple.CoreSimulator.SimRuntime.iOS-".length) - .replace(/-/g, "."), + .replaceAll("-", "."), ]; } return result; diff --git a/package.json b/package.json index 67a84bd5..414b69f7 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "generate:docs": "node scripts/generate-manifest-docs.mjs", "generate:schema": "node scripts/generate-schema.mjs", "lint:commit": "git log --format='%s' origin/trunk..HEAD | tail -1 | npx @rnx-kit/commitlint-lite@1.0.1", - "lint:js": "eslint $(git ls-files '*.[cm]js' '*.[jt]s' '*.tsx' ':!:*.config.js' ':!:.yarn/releases') && tsc && tsc --project tsconfig.esm.json", + "lint:js": "eslint $(git ls-files '*.[cm]js' '*.[jt]s' '*.tsx' ':!:*.config.js' ':!:.yarn/releases') && tsc && tsc --project tsconfig.cjs.json", "lint:kt": "ktlint --relative 'android/app/src/**/*.kt'", "lint:rb": "bundle exec rubocop", "lint:swift": "swiftlint", diff --git a/scripts/configure.mjs b/scripts/configure.mjs index 5819a6cd..c3b08ffd 100755 --- a/scripts/configure.mjs +++ b/scripts/configure.mjs @@ -187,7 +187,7 @@ export function reactNativeConfig( } const config = path.join(testAppPath, "example", "react-native.config.js"); - return readTextFile(config, fs).replace(/Example/g, name); + return readTextFile(config, fs).replaceAll("Example", name); } /** @@ -279,7 +279,7 @@ export const getConfig = (() => { "package.json": readTextFile( path.join(templateDir, "package.json"), fs - ).replace(/HelloWorld/g, name), + ).replaceAll("HelloWorld", name), }), }, oldFiles: [], diff --git a/scripts/generate-schema.mjs b/scripts/generate-schema.mjs index 45a4837c..2910c42d 100644 --- a/scripts/generate-schema.mjs +++ b/scripts/generate-schema.mjs @@ -10,7 +10,7 @@ import { generateSchema } from "./schema.mjs"; /** @type {(str: string) => string} */ const stripCarriageReturn = - os.EOL === "\r\n" ? (str) => str.replace(/\r/g, "") : (str) => str; + os.EOL === "\r\n" ? (str) => str.replaceAll("\r", "") : (str) => str; /** * @returns {Promise>} diff --git a/scripts/helpers.js b/scripts/helpers.js index d0566f20..1a0a50aa 100644 --- a/scripts/helpers.js +++ b/scripts/helpers.js @@ -64,7 +64,7 @@ function findNearest( /** * Returns whether the current module is main. - * @param {string} url + * @param {string | URL} url * @param {string} script * @returns {boolean} */ diff --git a/scripts/release-notes.mjs b/scripts/release-notes.mjs index 8b66fab0..de3cb847 100644 --- a/scripts/release-notes.mjs +++ b/scripts/release-notes.mjs @@ -67,7 +67,10 @@ function main(lastRelease, nextRelease) { buffers.push(Buffer.from("]")); - const output = Buffer.concat(buffers).toString().trim().replace(/\n/g, ","); + const output = Buffer.concat(buffers) + .toString() + .trim() + .replaceAll("\n", ","); const commits = JSON.parse(output); if (commits.length === 0) { return; diff --git a/test/configure/gatherConfig.test.mjs b/test/configure/gatherConfig.test.mjs index 6803c19c..c63322dc 100644 --- a/test/configure/gatherConfig.test.mjs +++ b/test/configure/gatherConfig.test.mjs @@ -19,14 +19,14 @@ describe("gatherConfig()", () => { */ function gatherConfig(params) { /** @type {(p: string) => string} */ - const normalize = (p) => p.replace(/\\/g, "/"); + const normalize = (p) => p.replaceAll("\\", "/"); const config = gatherConfigActual(params, true); config.files = Object.fromEntries( Object.entries(config.files).map(([key, value]) => [ normalize(key), typeof value === "string" - ? value.replace(/\r/g, "") + ? value.replaceAll("\r", "") : { source: normalize(value.source) }, ]) ); @@ -36,7 +36,7 @@ describe("gatherConfig()", () => { const gradleWrapper = readTextFile( "example/android/gradle/wrapper/gradle-wrapper.properties" - ).replace(/\r/g, ""); + ).replaceAll("\r", ""); it("returns configuration for all platforms", () => { deepEqual(gatherConfig(mockParams()), { diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 00000000..e1a0d9dd --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,11 @@ +{ + "extends": "@rnx-kit/tsconfig/tsconfig.json", + "compilerOptions": { + "noEmit": true + }, + "include": [ + "plugins/**/*.js", + "react-native.config.js", + "scripts/**/*.js" + ] +} diff --git a/tsconfig.esm.json b/tsconfig.esm.json deleted file mode 100644 index b2291542..00000000 --- a/tsconfig.esm.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "extends": "@rnx-kit/tsconfig/tsconfig.esm.json", - "compilerOptions": { - "module": "ES2022", - "moduleResolution": "Node", - "noEmit": true - }, - "include": [ - "**/*.mjs" - ] -} diff --git a/tsconfig.json b/tsconfig.json index e1a0d9dd..69ad4f3b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,12 @@ { - "extends": "@rnx-kit/tsconfig/tsconfig.json", + "extends": "@rnx-kit/tsconfig/tsconfig.esm.json", "compilerOptions": { + "target": "ES2022", + "module": "ES2022", + "moduleResolution": "Node", "noEmit": true }, "include": [ - "plugins/**/*.js", - "react-native.config.js", - "scripts/**/*.js" + "**/*.mjs" ] }