diff --git a/package.json b/package.json index 816383161..c70df16d6 100644 --- a/package.json +++ b/package.json @@ -176,7 +176,7 @@ "jsx-test-suite": "0.0.2", "knip": "6.33.0", "less-test-suite": "0.0.1", - "magic-string": "1.2.3", + "magic-string": "1.4.1", "make-synchronized": "0.8.0", "nano-spawn": "2.1.0", "node-style-text": "2.1.2", diff --git a/scripts/build/hacks/modify-typescript-module.js b/scripts/build/hacks/modify-typescript-module.js index 31c4d9d62..00b46774f 100644 --- a/scripts/build/hacks/modify-typescript-module.js +++ b/scripts/build/hacks/modify-typescript-module.js @@ -1,6 +1,6 @@ import path from "node:path"; import escapeStringRegexp from "escape-string-regexp"; -import MagicString from "magic-string"; +import { MagicString } from "magic-string"; import { outdent } from "outdent"; import { PROJECT_ROOT, writeFile } from "../../utilities/index.js"; import UNUSED_SPECIFIERS from "./typescript-unused-specifiers.js"; @@ -38,23 +38,39 @@ class TypeScriptModuleSource { this.modules = [...getModules(text)]; } - replaceModule(module, replacement) { - if (typeof module === "string") { - const found = this.modules.find((searching) => searching.path === module); + replaceModule(moduleOrFilter, replacement) { + let modules; + if (typeof moduleOrFilter === "string") { + const modulePath = moduleOrFilter; + const found = this.modules.find( + (searching) => searching.path === modulePath, + ); if (!found) { - throw new Error(`Module '${module}' not found`); + throw new Error(`Module '${modulePath}' not found`); } - module = found; + modules = [found]; + } else if (typeof moduleOrFilter === "function") { + const filter = moduleOrFilter; + const matched = this.modules.filter((searching) => filter(searching)); + + if (matched.length === 0) { + throw new Error("No matched modules found"); + } + modules = matched; + } else { + modules = [moduleOrFilter]; } - this.#source.overwrite(module.start, module.end, replacement); + for (const { start, end } of modules) { + this.#source.overwrite(start, end, replacement); + } return this; } - removeModule(module) { - return this.replaceModule(module, ""); + removeModule(moduleOrFilter) { + return this.replaceModule(moduleOrFilter, ""); } hasModule(module) { @@ -105,12 +121,6 @@ class TypeScriptModuleSource { return this; } - applyChanges() { - const text = this.#source.toString(); - this.#source = new MagicString(text); - this.modules = getModules(text); - } - toString() { return this.#source.toString(); } @@ -176,18 +186,10 @@ function modifyTypescriptModule(text) { source.removeModule(entry); // Deprecated - for (const module of source.modules) { - if (module.path.startsWith("src/deprecatedCompat/")) { - source.removeModule(module); - } - } + source.removeModule(({ path }) => path.startsWith("src/deprecatedCompat/")); // jsTyping - for (const module of source.modules) { - if (module.path.startsWith("src/jsTyping/")) { - source.removeModule(module); - } - } + source.removeModule(({ path }) => path.startsWith("src/jsTyping/")); // services for (const module of source.modules) { @@ -211,26 +213,18 @@ function modifyTypescriptModule(text) { // server source.removeModule("src/typescript/_namespaces/ts.server.ts"); - for (const module of source.modules) { - if (module.path.startsWith("src/server/")) { - source.removeModule(module); - } - } + source.removeModule(({ path }) => path.startsWith("src/server/")); // `transformers` source.removeModule("src/compiler/transformer.ts"); - for (const module of source.modules) { - if (module.path.startsWith("src/compiler/transformers/")) { - source.removeModule(module); - } - } + source.removeModule(({ path }) => + path.startsWith("src/compiler/transformers/"), + ); // `typingsInstaller` - for (const module of source.modules) { - if (module.path.startsWith("src/typingsInstallerCore/")) { - source.removeModule(module); - } - } + source.removeModule(({ path }) => + path.startsWith("src/typingsInstallerCore/"), + ); // `ts.moduleSpecifiers` source.removeModule("src/compiler/_namespaces/ts.moduleSpecifiers.ts"); diff --git a/yarn.lock b/yarn.lock index 7625795c5..0ee49979f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1891,7 +1891,7 @@ __metadata: languageName: node linkType: hard -"@jridgewell/sourcemap-codec@npm:^1.4.14, @jridgewell/sourcemap-codec@npm:^1.5.0, @jridgewell/sourcemap-codec@npm:^1.5.5, @jridgewell/sourcemap-codec@npm:^1.6.0-beta.0": +"@jridgewell/sourcemap-codec@npm:^1.4.14, @jridgewell/sourcemap-codec@npm:^1.5.0, @jridgewell/sourcemap-codec@npm:^1.5.5, @jridgewell/sourcemap-codec@npm:^1.6.0, @jridgewell/sourcemap-codec@npm:^1.6.0-beta.0": version: 1.6.0 resolution: "@jridgewell/sourcemap-codec@npm:1.6.0" checksum: 10/eb53fbf7eb4051302a599d6b91425f381168b2856f321336fb00852cea1e0c1904af2610c157b1535e94a61caa98f0bd068d1d0394dd46adf9e9c45c4e11af91 @@ -6816,12 +6816,12 @@ __metadata: languageName: node linkType: hard -"magic-string@npm:1.2.3": - version: 1.2.3 - resolution: "magic-string@npm:1.2.3" +"magic-string@npm:1.4.1": + version: 1.4.1 + resolution: "magic-string@npm:1.4.1" dependencies: - "@jridgewell/sourcemap-codec": "npm:^1.5.5" - checksum: 10/d1a943913fad82733704cc6b4ff327a1ee9dcdbaa1475931f03368aa0ac54305eb37f591ff1478c36e7f9c62dabc335d6dc5d28744e632f62d42f350031d758f + "@jridgewell/sourcemap-codec": "npm:^1.6.0" + checksum: 10/e24599bdd37e2bd4d3bed3e52739791d47652ce16a13f31c2fc7cc760912549a3af4db2625c4a26d4d7cd9ef573fea96c9470fea5b9deddd296ada896b6c44a3 languageName: node linkType: hard @@ -8364,7 +8364,7 @@ __metadata: less-test-suite: "npm:0.0.1" leven: "npm:4.1.0" linguist-languages: "npm:9.5.0" - magic-string: "npm:1.2.3" + magic-string: "npm:1.4.1" make-synchronized: "npm:0.8.0" mdast-util-from-markdown: "npm:2.0.3" mdast-util-gfm: "npm:3.1.0"