@openrewrite/rewrite 8.83.4 → 8.83.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/index.d.ts.map +1 -1
  2. package/dist/index.js +1 -5
  3. package/dist/index.js.map +1 -1
  4. package/dist/java/rpc.d.ts.map +1 -1
  5. package/dist/java/rpc.js +2 -0
  6. package/dist/java/rpc.js.map +1 -1
  7. package/dist/java/tree.d.ts +1 -0
  8. package/dist/java/tree.d.ts.map +1 -1
  9. package/dist/java/tree.js.map +1 -1
  10. package/dist/java/visitor.d.ts.map +1 -1
  11. package/dist/java/visitor.js +1 -0
  12. package/dist/java/visitor.js.map +1 -1
  13. package/dist/javascript/parser.d.ts.map +1 -1
  14. package/dist/javascript/parser.js +8 -0
  15. package/dist/javascript/parser.js.map +1 -1
  16. package/dist/javascript/recipes/dependencies.d.ts +63 -0
  17. package/dist/javascript/recipes/dependencies.d.ts.map +1 -0
  18. package/dist/javascript/recipes/dependencies.js +49 -0
  19. package/dist/javascript/recipes/dependencies.js.map +1 -0
  20. package/dist/javascript/recipes/index.d.ts +1 -4
  21. package/dist/javascript/recipes/index.d.ts.map +1 -1
  22. package/dist/javascript/recipes/index.js +1 -4
  23. package/dist/javascript/recipes/index.js.map +1 -1
  24. package/dist/rewrite-javascript-version.txt +1 -1
  25. package/package.json +1 -1
  26. package/src/index.ts +0 -8
  27. package/src/java/rpc.ts +2 -0
  28. package/src/java/tree.ts +1 -0
  29. package/src/java/visitor.ts +1 -0
  30. package/src/javascript/parser.ts +8 -0
  31. package/src/javascript/recipes/dependencies.ts +108 -0
  32. package/src/javascript/recipes/index.ts +1 -4
  33. package/dist/javascript/recipes/add-dependency.d.ts +0 -61
  34. package/dist/javascript/recipes/add-dependency.d.ts.map +0 -1
  35. package/dist/javascript/recipes/add-dependency.js +0 -430
  36. package/dist/javascript/recipes/add-dependency.js.map +0 -1
  37. package/dist/javascript/recipes/remove-dependency.d.ts +0 -29
  38. package/dist/javascript/recipes/remove-dependency.d.ts.map +0 -1
  39. package/dist/javascript/recipes/remove-dependency.js +0 -261
  40. package/dist/javascript/recipes/remove-dependency.js.map +0 -1
  41. package/dist/javascript/recipes/upgrade-dependency-version.d.ts +0 -74
  42. package/dist/javascript/recipes/upgrade-dependency-version.d.ts.map +0 -1
  43. package/dist/javascript/recipes/upgrade-dependency-version.js +0 -387
  44. package/dist/javascript/recipes/upgrade-dependency-version.js.map +0 -1
  45. package/dist/javascript/recipes/upgrade-transitive-dependency-version.d.ts +0 -68
  46. package/dist/javascript/recipes/upgrade-transitive-dependency-version.d.ts.map +0 -1
  47. package/dist/javascript/recipes/upgrade-transitive-dependency-version.js +0 -307
  48. package/dist/javascript/recipes/upgrade-transitive-dependency-version.js.map +0 -1
  49. package/src/javascript/recipes/add-dependency.ts +0 -549
  50. package/src/javascript/recipes/remove-dependency.ts +0 -345
  51. package/src/javascript/recipes/upgrade-dependency-version.ts +0 -486
  52. package/src/javascript/recipes/upgrade-transitive-dependency-version.ts +0 -403
@@ -1,403 +0,0 @@
1
- /*
2
- * Copyright 2025 the original author or authors.
3
- * <p>
4
- * Licensed under the Moderne Source Available License (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- * <p>
8
- * https://docs.moderne.io/licensing/moderne-source-available-license
9
- * <p>
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- import {Option, ScanningRecipe} from "../../recipe";
18
- import {ExecutionContext} from "../../execution";
19
- import {TreeVisitor} from "../../visitor";
20
- import {Tree} from "../../tree";
21
- import {isJson, Json, JsonParser, JsonVisitor} from "../../json";
22
- import {isDocuments, isYaml, Yaml} from "../../yaml";
23
- import {isPlainText, PlainText} from "../../text";
24
- import {
25
- allDependencyScopes,
26
- findNodeResolutionResult,
27
- NodeResolutionResultQueries,
28
- PackageManager,
29
- serializeNpmrcConfigs
30
- } from "../node-resolution-result";
31
- import * as path from "path";
32
- import * as semver from "semver";
33
- import {markupWarn} from "../../markers";
34
- import {TreePrinters} from "../../print";
35
- import {
36
- createDependencyRecipeAccumulator,
37
- createLockFileEditor,
38
- DependencyRecipeAccumulator,
39
- getAllLockFileNames,
40
- getLockFileName,
41
- parseLockFileContent,
42
- runInstallIfNeeded,
43
- runInstallInTempDir,
44
- storeInstallResult,
45
- updateNodeResolutionMarker
46
- } from "../package-manager";
47
- import {applyOverrideToPackageJson, DependencyPathSegment, parseDependencyPath} from "../dependency-manager";
48
-
49
- /**
50
- * Information about a project that needs updating
51
- */
52
- interface ProjectUpdateInfo {
53
- /** Relative path to package.json (from source root) */
54
- packageJsonPath: string;
55
- /** Original package.json content */
56
- originalPackageJson: string;
57
- /** New version constraint to apply */
58
- newVersion: string;
59
- /** The package manager used by this project */
60
- packageManager: PackageManager;
61
- /**
62
- * If true, skip running the package manager because the resolved version
63
- * already satisfies the new constraint. Only package.json needs updating.
64
- */
65
- skipInstall: boolean;
66
- /** Parsed dependency path for scoped overrides (if specified) */
67
- dependencyPathSegments?: DependencyPathSegment[];
68
- /** Config file contents extracted from the project (e.g., .npmrc) */
69
- configFiles?: Record<string, string>;
70
- }
71
-
72
- interface Accumulator extends DependencyRecipeAccumulator<ProjectUpdateInfo> {
73
- /** Original lock file content, keyed by lock file path */
74
- originalLockFiles: Map<string, string>;
75
- }
76
-
77
- /**
78
- * Upgrades the version of a transitive dependency by adding override entries to package.json.
79
- *
80
- * This recipe is used when you need to upgrade a dependency that is not directly declared
81
- * in your package.json, but is pulled in transitively by one of your direct dependencies.
82
- * This is commonly needed for security vulnerability remediation.
83
- *
84
- * The recipe adds entries to:
85
- * - `overrides` for npm and Bun
86
- * - `resolutions` for Yarn (Classic and Berry)
87
- * - `pnpm.overrides` for pnpm
88
- *
89
- * @see UpgradeDependencyVersion for upgrading direct dependencies
90
- */
91
- export class UpgradeTransitiveDependencyVersion extends ScanningRecipe<Accumulator> {
92
- readonly name = "org.openrewrite.javascript.dependencies.upgrade-transitive-dependency-version";
93
- readonly displayName = "Upgrade transitive npm dependency version";
94
- readonly description = "Upgrades the version of a transitive dependency by adding override/resolution entries to `package.json` and updates the lock file by running the package manager.";
95
-
96
- @Option({
97
- displayName: "Package name",
98
- description: "The name of the npm package to upgrade (e.g., `lodash`, `@types/node`)",
99
- example: "lodash"
100
- })
101
- packageName!: string;
102
-
103
- @Option({
104
- displayName: "Version",
105
- description: "The version constraint to set (e.g., `^5.0.0`, `~2.1.0`, `3.0.0`)",
106
- example: "^5.0.0"
107
- })
108
- newVersion!: string;
109
-
110
- @Option({
111
- displayName: "Dependency path",
112
- description: "Optional path to scope the override to a specific dependency chain. Use '>' as separator (e.g., 'express>accepts'). When not specified, applies globally to all transitive occurrences.",
113
- required: false,
114
- example: "express>accepts"
115
- })
116
- dependencyPath?: string;
117
-
118
- initialValue(_ctx: ExecutionContext): Accumulator {
119
- return {
120
- ...createDependencyRecipeAccumulator<ProjectUpdateInfo>(),
121
- originalLockFiles: new Map()
122
- };
123
- }
124
-
125
- async scanner(acc: Accumulator): Promise<TreeVisitor<any, ExecutionContext>> {
126
- const recipe = this;
127
- const LOCK_FILE_NAMES = getAllLockFileNames();
128
-
129
- return new class extends TreeVisitor<Tree, ExecutionContext> {
130
- protected async accept(tree: Tree, ctx: ExecutionContext): Promise<Tree | undefined> {
131
- // Handle JSON documents (package.json and JSON lock files)
132
- if (isJson(tree) && tree.kind === Json.Kind.Document) {
133
- return this.handleJsonDocument(tree as Json.Document, ctx);
134
- }
135
-
136
- // Handle YAML documents (pnpm-lock.yaml)
137
- if (isYaml(tree) && isDocuments(tree)) {
138
- return this.handleYamlDocument(tree, ctx);
139
- }
140
-
141
- // Handle PlainText files (yarn.lock for Yarn Classic)
142
- if (isPlainText(tree)) {
143
- return this.handlePlainTextDocument(tree as PlainText, ctx);
144
- }
145
-
146
- return tree;
147
- }
148
-
149
- private async handleJsonDocument(doc: Json.Document, _ctx: ExecutionContext): Promise<Json | undefined> {
150
- const basename = path.basename(doc.sourcePath);
151
-
152
- // Capture JSON lock file content (package-lock.json, bun.lock)
153
- if (LOCK_FILE_NAMES.includes(basename)) {
154
- acc.originalLockFiles.set(doc.sourcePath, await TreePrinters.print(doc));
155
- return doc;
156
- }
157
-
158
- // Only process package.json files for dependency analysis
159
- if (!doc.sourcePath.endsWith('package.json')) {
160
- return doc;
161
- }
162
-
163
- const marker = findNodeResolutionResult(doc);
164
- if (!marker) {
165
- return doc;
166
- }
167
-
168
- const pm = marker.packageManager ?? PackageManager.Npm;
169
-
170
- // Check if package is a direct dependency - if so, skip (use UpgradeDependencyVersion instead)
171
- for (const scope of allDependencyScopes) {
172
- const deps = marker[scope];
173
- if (deps?.find(d => d.name === recipe.packageName)) {
174
- // Package is a direct dependency, don't add override
175
- return doc;
176
- }
177
- }
178
-
179
- // Check if package exists as a transitive dependency (in resolvedDependencies)
180
- // Note: There may be multiple versions of the same package installed
181
- const resolvedVersions = NodeResolutionResultQueries.getAllResolvedVersions(
182
- marker,
183
- recipe.packageName
184
- );
185
-
186
- if (resolvedVersions.length === 0) {
187
- // Package not found in resolved dependencies at all
188
- return doc;
189
- }
190
-
191
- // Check if ANY resolved version needs upgrading
192
- // We need an override if at least one installed version doesn't satisfy the constraint
193
- const anyVersionNeedsUpgrade = resolvedVersions.some(
194
- rd => !semver.satisfies(rd.version, recipe.newVersion)
195
- );
196
-
197
- if (!anyVersionNeedsUpgrade) {
198
- // All installed versions already satisfy the constraint
199
- return doc;
200
- }
201
-
202
- // Parse dependency path if specified
203
- const dependencyPathSegments = recipe.dependencyPath
204
- ? parseDependencyPath(recipe.dependencyPath)
205
- : undefined;
206
-
207
- // Serialize npmrc configs from marker using requested scopes
208
- const configFiles: Record<string, string> = {};
209
- const npmrcContent = serializeNpmrcConfigs(marker.npmrcConfigs);
210
- if (npmrcContent) {
211
- configFiles['.npmrc'] = npmrcContent;
212
- }
213
-
214
- acc.projectsToUpdate.set(doc.sourcePath, {
215
- packageJsonPath: doc.sourcePath,
216
- originalPackageJson: await TreePrinters.print(doc),
217
- newVersion: recipe.newVersion,
218
- packageManager: pm,
219
- skipInstall: false, // Always need to run install for overrides
220
- dependencyPathSegments,
221
- configFiles: Object.keys(configFiles).length > 0 ? configFiles : undefined
222
- });
223
-
224
- return doc;
225
- }
226
-
227
- private async handleYamlDocument(docs: Yaml.Documents, _ctx: ExecutionContext): Promise<Yaml.Documents | undefined> {
228
- const basename = path.basename(docs.sourcePath);
229
- if (LOCK_FILE_NAMES.includes(basename)) {
230
- acc.originalLockFiles.set(docs.sourcePath, await TreePrinters.print(docs));
231
- }
232
- return docs;
233
- }
234
-
235
- private async handlePlainTextDocument(text: PlainText, _ctx: ExecutionContext): Promise<PlainText | undefined> {
236
- const basename = path.basename(text.sourcePath);
237
- if (LOCK_FILE_NAMES.includes(basename)) {
238
- acc.originalLockFiles.set(text.sourcePath, await TreePrinters.print(text));
239
- }
240
- return text;
241
- }
242
- };
243
- }
244
-
245
- async editorWithData(acc: Accumulator): Promise<TreeVisitor<any, ExecutionContext>> {
246
- const recipe = this;
247
-
248
- // Create JSON visitor that handles both package.json and JSON lock files
249
- const jsonEditor = new class extends JsonVisitor<ExecutionContext> {
250
- protected async visitDocument(doc: Json.Document, ctx: ExecutionContext): Promise<Json | undefined> {
251
- const sourcePath = doc.sourcePath;
252
-
253
- // Handle package.json files
254
- if (sourcePath.endsWith('package.json')) {
255
- const updateInfo = acc.projectsToUpdate.get(sourcePath);
256
- if (!updateInfo) {
257
- return doc; // This package.json doesn't need updating
258
- }
259
-
260
- // Run package manager install if needed, check for failure
261
- const failureMessage = await runInstallIfNeeded(sourcePath, acc, () =>
262
- recipe.runPackageManagerInstall(acc, updateInfo, ctx)
263
- );
264
- if (failureMessage) {
265
- return markupWarn(
266
- doc,
267
- `Failed to add override for ${recipe.packageName} to ${recipe.newVersion}`,
268
- failureMessage
269
- );
270
- }
271
-
272
- // Add override entries
273
- const modifiedDoc = await this.addOverrideEntry(doc, updateInfo);
274
-
275
- // Update the NodeResolutionResult marker
276
- return updateNodeResolutionMarker(modifiedDoc, updateInfo, acc);
277
- }
278
-
279
- // Handle JSON lock files (package-lock.json, bun.lock)
280
- const lockFileName = path.basename(sourcePath);
281
- if (getAllLockFileNames().includes(lockFileName)) {
282
- const updatedLockContent = acc.updatedLockFiles.get(sourcePath);
283
- if (updatedLockContent) {
284
- const parsed = await parseLockFileContent(updatedLockContent, sourcePath, lockFileName) as Json.Document;
285
- // Preserve original ID for RPC compatibility
286
- return {
287
- ...doc,
288
- value: parsed.value,
289
- eof: parsed.eof
290
- } as Json.Document;
291
- }
292
- }
293
-
294
- return doc;
295
- }
296
-
297
- /**
298
- * Adds override entry to package.json for transitive dependency upgrade.
299
- */
300
- private async addOverrideEntry(
301
- doc: Json.Document,
302
- updateInfo: ProjectUpdateInfo
303
- ): Promise<Json.Document> {
304
- // Parse current package.json content
305
- const currentContent = await TreePrinters.print(doc);
306
- let packageJson: Record<string, any>;
307
- try {
308
- packageJson = JSON.parse(currentContent);
309
- } catch {
310
- return doc; // Can't parse, return unchanged
311
- }
312
-
313
- // Apply override
314
- const modifiedPackageJson = applyOverrideToPackageJson(
315
- packageJson,
316
- updateInfo.packageManager,
317
- recipe.packageName,
318
- updateInfo.newVersion,
319
- updateInfo.dependencyPathSegments
320
- );
321
-
322
- // Serialize back to JSON, preserving indentation
323
- const indentMatch = currentContent.match(/^(\s+)"/m);
324
- const indent = indentMatch ? indentMatch[1].length : 2;
325
- const newContent = JSON.stringify(modifiedPackageJson, null, indent);
326
-
327
- // Re-parse with JsonParser to get proper AST
328
- const parsed = await new JsonParser({}).parseOne({
329
- text: newContent,
330
- sourcePath: doc.sourcePath
331
- }) as Json.Document;
332
-
333
- // Preserve original ID for RPC compatibility
334
- return {
335
- ...doc,
336
- value: parsed.value,
337
- eof: parsed.eof
338
- } as Json.Document;
339
- }
340
- };
341
-
342
- // Return composite visitor that handles both JSON and YAML lock files
343
- return createLockFileEditor(jsonEditor, acc);
344
- }
345
-
346
- /**
347
- * Runs the package manager in a temporary directory to update the lock file.
348
- * All file contents are provided from in-memory sources (SourceFiles), not read from disk.
349
- */
350
- private async runPackageManagerInstall(
351
- acc: Accumulator,
352
- updateInfo: ProjectUpdateInfo,
353
- _ctx: ExecutionContext
354
- ): Promise<void> {
355
- // Create modified package.json with the override
356
- const modifiedPackageJson = this.createModifiedPackageJson(
357
- updateInfo.originalPackageJson,
358
- updateInfo
359
- );
360
-
361
- // Get the lock file path based on package manager
362
- const lockFileName = getLockFileName(updateInfo.packageManager);
363
- const packageJsonDir = path.dirname(updateInfo.packageJsonPath);
364
- const lockFilePath = packageJsonDir === '.'
365
- ? lockFileName
366
- : path.join(packageJsonDir, lockFileName);
367
-
368
- // Look up the original lock file content from captured SourceFiles
369
- const originalLockFileContent = acc.originalLockFiles.get(lockFilePath);
370
-
371
- const result = await runInstallInTempDir(
372
- updateInfo.packageManager,
373
- modifiedPackageJson,
374
- {
375
- originalLockFileContent,
376
- configFiles: updateInfo.configFiles
377
- }
378
- );
379
-
380
- storeInstallResult(result, acc, updateInfo, modifiedPackageJson);
381
- }
382
-
383
- /**
384
- * Creates a modified package.json with the override.
385
- */
386
- private createModifiedPackageJson(
387
- originalContent: string,
388
- updateInfo: ProjectUpdateInfo
389
- ): string {
390
- let packageJson = JSON.parse(originalContent);
391
-
392
- packageJson = applyOverrideToPackageJson(
393
- packageJson,
394
- updateInfo.packageManager,
395
- this.packageName,
396
- updateInfo.newVersion,
397
- updateInfo.dependencyPathSegments
398
- );
399
-
400
- return JSON.stringify(packageJson, null, 2);
401
- }
402
-
403
- }