@openrewrite/recipes-nodejs 0.65.0 → 0.67.0

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 (79) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +12 -1
  4. package/dist/index.js.map +1 -1
  5. package/dist/migrate/aws-sdk-v3/add-aws-sdk-v3-dependencies.d.ts +16 -0
  6. package/dist/migrate/aws-sdk-v3/add-aws-sdk-v3-dependencies.d.ts.map +1 -0
  7. package/dist/migrate/aws-sdk-v3/add-aws-sdk-v3-dependencies.js +89 -0
  8. package/dist/migrate/aws-sdk-v3/add-aws-sdk-v3-dependencies.js.map +1 -0
  9. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-node-support.d.ts +33 -0
  10. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-node-support.d.ts.map +1 -0
  11. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-node-support.js +190 -0
  12. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-node-support.js.map +1 -0
  13. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-plan.d.ts +70 -0
  14. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-plan.d.ts.map +1 -0
  15. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-plan.js +315 -0
  16. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-plan.js.map +1 -0
  17. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-support.d.ts +181 -0
  18. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-support.d.ts.map +1 -0
  19. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-support.js +1504 -0
  20. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-support.js.map +1 -0
  21. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-tables.d.ts +3 -0
  22. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-tables.d.ts.map +1 -0
  23. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-tables.js +421 -0
  24. package/dist/migrate/aws-sdk-v3/aws-sdk-v3-tables.js.map +1 -0
  25. package/dist/migrate/aws-sdk-v3/find-aws-sdk-v2-manual-migrations.d.ts +20 -0
  26. package/dist/migrate/aws-sdk-v3/find-aws-sdk-v2-manual-migrations.d.ts.map +1 -0
  27. package/dist/migrate/aws-sdk-v3/find-aws-sdk-v2-manual-migrations.js +146 -0
  28. package/dist/migrate/aws-sdk-v3/find-aws-sdk-v2-manual-migrations.js.map +1 -0
  29. package/dist/migrate/aws-sdk-v3/migrate-aws-sdk-v2-to-v3.d.ts +13 -0
  30. package/dist/migrate/aws-sdk-v3/migrate-aws-sdk-v2-to-v3.d.ts.map +1 -0
  31. package/dist/migrate/aws-sdk-v3/migrate-aws-sdk-v2-to-v3.js +69 -0
  32. package/dist/migrate/aws-sdk-v3/migrate-aws-sdk-v2-to-v3.js.map +1 -0
  33. package/dist/migrate/aws-sdk-v3/remove-unused-aws-sdk-v2-dependency.d.ts +35 -0
  34. package/dist/migrate/aws-sdk-v3/remove-unused-aws-sdk-v2-dependency.d.ts.map +1 -0
  35. package/dist/migrate/aws-sdk-v3/remove-unused-aws-sdk-v2-dependency.js +132 -0
  36. package/dist/migrate/aws-sdk-v3/remove-unused-aws-sdk-v2-dependency.js.map +1 -0
  37. package/dist/migrate/aws-sdk-v3/replace-aws-sdk-v2-clients.d.ts +18 -0
  38. package/dist/migrate/aws-sdk-v3/replace-aws-sdk-v2-clients.d.ts.map +1 -0
  39. package/dist/migrate/aws-sdk-v3/replace-aws-sdk-v2-clients.js +562 -0
  40. package/dist/migrate/aws-sdk-v3/replace-aws-sdk-v2-clients.js.map +1 -0
  41. package/dist/migrate/builtin-binding-visitor.d.ts +1 -6
  42. package/dist/migrate/builtin-binding-visitor.d.ts.map +1 -1
  43. package/dist/migrate/builtin-binding-visitor.js +6 -69
  44. package/dist/migrate/builtin-binding-visitor.js.map +1 -1
  45. package/dist/migrate/express-request-id/replace-express-request-id.d.ts.map +1 -1
  46. package/dist/migrate/express-request-id/replace-express-request-id.js +1 -2
  47. package/dist/migrate/express-request-id/replace-express-request-id.js.map +1 -1
  48. package/dist/migrate/fs-extra/fs-extra-support.d.ts +0 -1
  49. package/dist/migrate/fs-extra/fs-extra-support.d.ts.map +1 -1
  50. package/dist/migrate/fs-extra/fs-extra-support.js +1 -2
  51. package/dist/migrate/fs-extra/fs-extra-support.js.map +1 -1
  52. package/dist/migrate/increase-node-engine-version.d.ts +1 -0
  53. package/dist/migrate/increase-node-engine-version.d.ts.map +1 -1
  54. package/dist/migrate/increase-node-engine-version.js +12 -7
  55. package/dist/migrate/increase-node-engine-version.js.map +1 -1
  56. package/dist/migrate/source-map-support/replace-source-map-support-api.js +1 -1
  57. package/dist/migrate/source-map-support/replace-source-map-support-api.js.map +1 -1
  58. package/dist/migrate/source-map-support/source-map-support-support.d.ts +0 -1
  59. package/dist/migrate/source-map-support/source-map-support-support.d.ts.map +1 -1
  60. package/dist/migrate/source-map-support/source-map-support-support.js +0 -1
  61. package/dist/migrate/source-map-support/source-map-support-support.js.map +1 -1
  62. package/dist/resources/advisories-npm.csv +97 -3
  63. package/package.json +2 -2
  64. package/src/index.ts +11 -0
  65. package/src/migrate/aws-sdk-v3/add-aws-sdk-v3-dependencies.ts +71 -0
  66. package/src/migrate/aws-sdk-v3/aws-sdk-v3-node-support.ts +172 -0
  67. package/src/migrate/aws-sdk-v3/aws-sdk-v3-plan.ts +399 -0
  68. package/src/migrate/aws-sdk-v3/aws-sdk-v3-support.ts +1480 -0
  69. package/src/migrate/aws-sdk-v3/aws-sdk-v3-tables.ts +432 -0
  70. package/src/migrate/aws-sdk-v3/find-aws-sdk-v2-manual-migrations.ts +126 -0
  71. package/src/migrate/aws-sdk-v3/migrate-aws-sdk-v2-to-v3.ts +56 -0
  72. package/src/migrate/aws-sdk-v3/remove-unused-aws-sdk-v2-dependency.ts +119 -0
  73. package/src/migrate/aws-sdk-v3/replace-aws-sdk-v2-clients.ts +523 -0
  74. package/src/migrate/builtin-binding-visitor.ts +9 -94
  75. package/src/migrate/express-request-id/replace-express-request-id.ts +1 -2
  76. package/src/migrate/fs-extra/fs-extra-support.ts +1 -2
  77. package/src/migrate/increase-node-engine-version.ts +11 -7
  78. package/src/migrate/source-map-support/replace-source-map-support-api.ts +1 -1
  79. package/src/migrate/source-map-support/source-map-support-support.ts +0 -1
@@ -0,0 +1,399 @@
1
+ /*
2
+ * Copyright 2026 the original author or authors.
3
+ *
4
+ * Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
5
+ */
6
+
7
+ import {builtinModules} from "module";
8
+ import {Column, DataTable, ExecutionContext, Tree, TreePrinters, TreeVisitor} from "@openrewrite/rewrite";
9
+ import {Json} from "@openrewrite/rewrite/json";
10
+ import {findNodeResolutionResult, JS} from "@openrewrite/rewrite/javascript";
11
+ import {PlainText} from "@openrewrite/rewrite/text";
12
+ import {Yaml} from "@openrewrite/rewrite/yaml";
13
+ import {dirOf, isPackageJson, packageData} from "../package-json-support";
14
+ import {analyzeAwsSdk, Analysis, declaresAwsSdk, ownerOf, REASONS, SECTIONS, SMITHY_CLIENT} from "./aws-sdk-v3-support";
15
+ import {
16
+ AwsSdkLockGroupNodeVersion,
17
+ CURRENT_NODE,
18
+ NODE_VERSION_FILE,
19
+ nodeNeededBy,
20
+ NodeSignal,
21
+ packageJsonSignals,
22
+ OLDEST_NODE,
23
+ sourceRuntimes,
24
+ V3Versions,
25
+ v3VersionsFor,
26
+ versionFileSignal,
27
+ yamlSignals
28
+ } from "./aws-sdk-v3-node-support";
29
+ // A cycle, used only at run time: the plan dry-runs that recipe's editor on each source.
30
+ import {migrates} from "./replace-aws-sdk-v2-clients";
31
+
32
+ const PARSE_ERROR = "org.openrewrite.tree.ParseError";
33
+ const SOURCE_FILE = /\.([cm]?[jt]sx?|vue|svelte|astro)$/;
34
+ const REFERENCE = /['"`]aws-sdk(\/[^'"`]*)?['"`]/;
35
+
36
+ /** What keeps a source on v2, as the `AwsSdkManualMigrationSteps` table names it. */
37
+ export interface Blocker {
38
+ construct: string;
39
+ reason: string;
40
+ /** A v2 call on a value from another file, which only counts in a lock group that declares `aws-sdk`. */
41
+ orphan?: boolean;
42
+ /** The package the call's receiver comes from, which voids the blocker where the source's project declares it. */
43
+ package?: string;
44
+ }
45
+
46
+ /** What one source needs, judged alone. */
47
+ interface SourceScan {
48
+ /** It binds the SDK at the top level. */
49
+ bound: boolean;
50
+ /** What keeps it on v2 taken alone. */
51
+ blockers: Blocker[];
52
+ /** The v3 packages it imports once migrated. */
53
+ packages: string[];
54
+ /** It exports a client another file may call as v2. */
55
+ exports: boolean;
56
+ /** It calls a v2-only API on a value from another file. */
57
+ foreign: boolean;
58
+ /** The Lambda runtimes it names. */
59
+ runtimes: {declared: string; node: number}[];
60
+ }
61
+
62
+ interface ManifestScan {
63
+ declaresAwsSdk: boolean;
64
+ /** The packages it declares in any dependency section. */
65
+ dependencies: string[];
66
+ /** The `workspaces` globs of a workspace root, for an LST without the resolution marker. */
67
+ workspaces: string[];
68
+ }
69
+
70
+ /** What the steps of the migration scan, which decides what each of them edits. */
71
+ export interface AwsSdkScan {
72
+ sources: Map<string, SourceScan>;
73
+ /** By the directory of each `package.json`. */
74
+ manifests: Map<string, ManifestScan>;
75
+ /** Each workspace member's directory, to its workspace root's, from the resolution markers. */
76
+ workspaceRoots: Map<string, string>;
77
+ nodeSignals: NodeSignal[];
78
+ }
79
+
80
+ export function newAwsSdkScan(): AwsSdkScan {
81
+ return {sources: new Map(), manifests: new Map(), workspaceRoots: new Map(), nodeSignals: []};
82
+ }
83
+
84
+ // Each step scans the same trees, so a source is analyzed and dry-run once per cycle rather than once per step.
85
+ const scans = new WeakMap<object, Promise<SourceScan>>();
86
+
87
+ async function scanSource(cu: JS.CompilationUnit, ctx: ExecutionContext): Promise<SourceScan> {
88
+ let scan = scans.get(cu);
89
+ if (!scan) {
90
+ scan = (async () => {
91
+ const analysis: Analysis = await analyzeAwsSdk(cu);
92
+ const bound = analysis.bindings.statements.size > 0;
93
+ const blockers: Blocker[] = analysis.problems.map(p => ({construct: p.construct, reason: p.reason, orphan: p.orphan, package: p.package}));
94
+ if (bound && blockers.length === 0 && !await migrates(cu, ctx)) {
95
+ blockers.push({construct: 'aws-sdk import', reason: REASONS.unmovable});
96
+ }
97
+ const runtimes = await sourceRuntimes(cu);
98
+ return {bound, blockers, packages: [...analysis.packages], exports: analysis.exports.length > 0, foreign: analysis.foreign, runtimes};
99
+ })();
100
+ scans.set(cu, scan);
101
+ }
102
+ return scan;
103
+ }
104
+
105
+ export function normalizePath(path: string): string {
106
+ return path.replace(/\\/g, '/').replace(/^\.\//, '');
107
+ }
108
+
109
+ function workspaceGlobs(pkg: Record<string, any> | undefined): string[] {
110
+ const workspaces = pkg?.workspaces;
111
+ const globs = Array.isArray(workspaces) ? workspaces : Array.isArray(workspaces?.packages) ? workspaces.packages : [];
112
+ return globs.filter((g: unknown): g is string => typeof g === 'string');
113
+ }
114
+
115
+ /** `packages/*`, `apps/**`, `!packages/skip` as a test on a directory relative to the workspace root. */
116
+ function globMatcher(globs: string[]): (dir: string) => boolean {
117
+ const toRegex = (glob: string) => new RegExp('^' + normalizePath(glob).replace(/\/+$/, '').split('/').map(segment =>
118
+ segment === '**' ? '.*' : segment.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '[^/]*').replace(/\?/g, '[^/]')
119
+ ).join('/').replace(/\/\.\*$/, '(/.*)?') + '$');
120
+ const include = globs.filter(g => !g.startsWith('!')).map(toRegex);
121
+ const exclude = globs.filter(g => g.startsWith('!')).map(g => toRegex(g.slice(1)));
122
+ return dir => include.some(r => r.test(dir)) && !exclude.some(r => r.test(dir));
123
+ }
124
+
125
+ export function awsSdkScanner(scan: AwsSdkScan): TreeVisitor<any, ExecutionContext> {
126
+ return new class extends TreeVisitor<Tree, ExecutionContext> {
127
+ async visit<R extends Tree>(tree: Tree, ctx: ExecutionContext): Promise<R | undefined> {
128
+ const raw: string | undefined = (tree as any).sourcePath;
129
+ if (!raw || raw.includes('node_modules/')) return tree as R;
130
+ const sourcePath = normalizePath(raw);
131
+ if (tree.kind === JS.Kind.CompilationUnit) {
132
+ const source = await scanSource(tree as JS.CompilationUnit, ctx);
133
+ if (source.bound || source.blockers.length > 0 || source.foreign) scan.sources.set(sourcePath, source);
134
+ scan.nodeSignals.push(...source.runtimes.map(r => ({path: sourcePath, ...r, engines: false})));
135
+ } else if (tree.kind === Json.Kind.Document && isPackageJson(sourcePath)) {
136
+ const pkg = packageData(tree as Json.Document);
137
+ const dir = dirOf(sourcePath);
138
+ scan.manifests.set(dir, {declaresAwsSdk: declaresAwsSdk(pkg), dependencies: declaredPackages(pkg), workspaces: workspaceGlobs(pkg)});
139
+ for (const member of findNodeResolutionResult(tree as Json.Document)?.workspacePackagePaths ?? []) {
140
+ scan.workspaceRoots.set(dirOf(normalizePath(member)), dir);
141
+ }
142
+ scan.nodeSignals.push(...packageJsonSignals(sourcePath, pkg));
143
+ } else if (tree.kind === PlainText.Kind.PlainText && NODE_VERSION_FILE.test(sourcePath)) {
144
+ const signal = versionFileSignal(sourcePath, (tree as PlainText).text);
145
+ if (signal) scan.nodeSignals.push(signal);
146
+ } else if (tree.kind === Yaml.Kind.Documents) {
147
+ scan.nodeSignals.push(...yamlSignals(sourcePath, await TreePrinters.print(tree as any)));
148
+ } else if ((tree.kind === PARSE_ERROR || tree.kind === PlainText.Kind.PlainText) && SOURCE_FILE.test(sourcePath) &&
149
+ REFERENCE.test(await TreePrinters.print(tree as any))) {
150
+ scan.sources.set(sourcePath, {
151
+ bound: true, packages: [], exports: false, foreign: false, runtimes: [],
152
+ blockers: [{construct: 'unparsed source', reason: REASONS.unparsed}]
153
+ });
154
+ }
155
+ return tree as R;
156
+ }
157
+ };
158
+ }
159
+
160
+ /** A lock group the migration leaves unchanged, and the sources that keep it on v2. */
161
+ export interface SkippedGroup {
162
+ /** The `package.json` whose lock the group shares: its workspace root's, or its own. */
163
+ manifest: string;
164
+ blocked: Map<string, Blocker[]>;
165
+ }
166
+
167
+ /** How a lock group whose sources migrate is judged by the oldest Node.js it declares. */
168
+ interface NodeVerdict {
169
+ manifest: string;
170
+ signal?: NodeSignal;
171
+ /** Undefined where the group is left on v2 for its Node.js. */
172
+ versions?: V3Versions;
173
+ note: string;
174
+ }
175
+
176
+ /**
177
+ * Which sources and manifests the migration changes. The unit is a lock group, the directory whose lock file is
178
+ * regenerated: a workspace root with its members, or a standalone `package.json`. A group migrates only when every
179
+ * source in it that uses the SDK migrates; otherwise none of its sources, manifests or lock change. A source with no
180
+ * `package.json` above it has no lock and is judged alone.
181
+ */
182
+ export class AwsSdkPlan {
183
+ private readonly manifestDirs: string[];
184
+ private readonly sdkDirs: string[];
185
+ private readonly rootOf = new Map<string, string>();
186
+ /** By lock group, the sources that keep it on v2. */
187
+ readonly skipped = new Map<string, SkippedGroup>();
188
+ /** By source, what keeps it on v2. */
189
+ private readonly blockers = new Map<string, Blocker[]>();
190
+ /** By lock group whose sources migrate, what its Node.js lets it declare. */
191
+ private readonly nodes = new Map<string, NodeVerdict>();
192
+
193
+ /** `version` is the range the user asks to declare for each `@aws-sdk/*` package, which a group's Node.js may rule out. */
194
+ constructor(private readonly scan: AwsSdkScan, version?: string) {
195
+ this.manifestDirs = [...scan.manifests.keys()].sort((a, b) => b.length - a.length);
196
+ this.sdkDirs = [...scan.manifests].filter(([, m]) => m.declaresAwsSdk).map(([dir]) => dir);
197
+ for (const [member, root] of scan.workspaceRoots) this.rootOf.set(member, root);
198
+ for (const [root, manifest] of scan.manifests) {
199
+ if (manifest.workspaces.length === 0) continue;
200
+ const matches = globMatcher(manifest.workspaces);
201
+ for (const dir of this.manifestDirs) {
202
+ if (dir === root || this.rootOf.has(dir)) continue;
203
+ const relative = root === '' ? dir : dir.startsWith(root + '/') ? dir.substring(root.length + 1) : undefined;
204
+ if (relative !== undefined && matches(relative)) this.rootOf.set(dir, root);
205
+ }
206
+ }
207
+
208
+ const declaring = new Set<string>();
209
+ for (const dir of this.sdkDirs) declaring.add(this.groupOfDir(dir));
210
+ // Sources with no `package.json` above them share no lock, but may still hand each other a client.
211
+ const NO_GROUP = '\u0000';
212
+ const foreignByGroup = new Map<string, string[]>();
213
+ const standing = new Map<string, Blocker[]>();
214
+ for (const [path, source] of scan.sources) {
215
+ const group = this.sourceGroupOf(path) ?? NO_GROUP;
216
+ const kept = source.blockers.filter(b => b.package === undefined || !this.resolves(path, b.package));
217
+ standing.set(path, kept);
218
+ if (source.foreign || kept.some(b => b.package !== undefined)) foreignByGroup.set(group, [...foreignByGroup.get(group) ?? [], path]);
219
+ }
220
+ for (const [path, source] of scan.sources) {
221
+ const group = this.sourceGroupOf(path);
222
+ const blockers = standing.get(path)!.filter(b => !b.orphan || (group !== undefined && declaring.has(group)));
223
+ if (blockers.length === 0 && source.exports && (foreignByGroup.get(group ?? NO_GROUP) ?? []).some(f => f !== path)) {
224
+ blockers.push({construct: 'exported client', reason: REASONS.exportedClient});
225
+ }
226
+ if (blockers.length === 0) continue;
227
+ this.blockers.set(path, blockers);
228
+ if (group === undefined) continue;
229
+ this.skip(group, path, blockers);
230
+ }
231
+ this.judgeNode(version);
232
+ }
233
+
234
+ private skip(group: string, path: string, blockers: Blocker[]) {
235
+ const skipped = this.skipped.get(group) ?? {manifest: manifestOf(group), blocked: new Map()};
236
+ skipped.blocked.set(path, blockers);
237
+ this.skipped.set(group, skipped);
238
+ }
239
+
240
+ /** Leaves on v2 each group whose oldest Node.js no v3 release (or the `version` asked for) supports. */
241
+ private judgeNode(version: string | undefined) {
242
+ const smithy = new Set<string>();
243
+ const migrating = new Set<string>();
244
+ for (const [path, source] of this.scan.sources) {
245
+ const group = this.sourceGroupOf(path);
246
+ if (!source.bound || this.blockers.has(path) || group === undefined || this.skipped.has(group)) continue;
247
+ migrating.add(group);
248
+ if (source.packages.includes(SMITHY_CLIENT)) smithy.add(group);
249
+ }
250
+ for (const group of migrating) {
251
+ const signal = this.scan.nodeSignals
252
+ .filter(s => this.speaksFor(s, group))
253
+ .sort((a, b) => a.node - b.node || a.path.localeCompare(b.path))[0];
254
+ const found = v3VersionsFor(signal?.node, version);
255
+ const versions = found && (found.smithy || !smithy.has(group)) ? found : undefined;
256
+ const needs = version !== undefined ? nodeNeededBy(version) : found ? CURRENT_NODE : OLDEST_NODE;
257
+ let note = '';
258
+ if (!signal) {
259
+ note = `Node.js version undeclared; v3 needs >= ${needs}`;
260
+ } else if (!versions) {
261
+ note = found ? `Node.js ${signal.node} (${signal.declared}); no @smithy/smithy-client release supports it` :
262
+ `Node.js ${signal.node} (${signal.declared}); ${version !== undefined ? `version ${version}` : 'AWS SDK v3'} needs >= ${needs}`;
263
+ this.skip(group, signal.path, [{construct: note, reason: note}]);
264
+ } else if (version === undefined && signal.node < CURRENT_NODE) {
265
+ note = `Node.js ${signal.node} (${signal.declared}); newer v3 releases need >= ${CURRENT_NODE}, and a lock regenerated on a newer Node.js may pin @aws-sdk/util-locate-window >= 20 (npm update it on Node.js ${signal.node})`;
266
+ }
267
+ this.nodes.set(group, {manifest: manifestOf(group), signal, versions, note});
268
+ }
269
+ }
270
+
271
+ /** `engines.node` speaks for its lock group; any other file for its own group and every group beneath it. */
272
+ private speaksFor(signal: NodeSignal, group: string): boolean {
273
+ if (this.groupOf(signal.path) === group) return true;
274
+ if (signal.engines) return false;
275
+ const base = dirOf(signal.path);
276
+ return this.manifestDirs.some(dir => this.groupOfDir(dir) === group && (base === '' || dir === base || dir.startsWith(base + '/')));
277
+ }
278
+
279
+ /** Whether `pkg` is a Node.js built-in or declared by a `package.json` the source at `path` resolves packages from. */
280
+ private resolves(path: string, pkg: string): boolean {
281
+ if (pkg.startsWith('node:') || builtinModules.includes(pkg)) return true;
282
+ const p = normalizePath(path);
283
+ return [...this.scan.manifests].some(([dir, m]) => (dir === '' || p.startsWith(dir + '/')) && m.dependencies.includes(pkg));
284
+ }
285
+
286
+ private groupOfDir(dir: string): string {
287
+ return this.rootOf.get(dir) ?? dir;
288
+ }
289
+
290
+ /**
291
+ * The lock group of a source: that of the nearest `package.json` above it declaring `aws-sdk`, which is what it
292
+ * resolves the SDK from and what loses it, else that of its nearest `package.json`. A nested `package.json` that
293
+ * doesn't declare the SDK (`{"type": "module"}` in a test folder, an example app) doesn't split its sources off.
294
+ */
295
+ sourceGroupOf(path: string): string | undefined {
296
+ const owner = ownerOf(normalizePath(path), this.sdkDirs);
297
+ return owner === undefined ? this.groupOf(path) : this.groupOfDir(owner);
298
+ }
299
+
300
+ /** The directory of the lock `path` belongs to, or undefined where no `package.json` is above it. */
301
+ groupOf(path: string): string | undefined {
302
+ const p = normalizePath(path);
303
+ const dir = this.manifestDirs.find(d => d === '' || p.startsWith(d + '/'));
304
+ return dir === undefined ? undefined : this.groupOfDir(dir);
305
+ }
306
+
307
+ /** Whether the source at `path` is migrated: it uses the SDK, and nothing in it or its lock group stays on v2. */
308
+ migrates(path: string): boolean {
309
+ const p = normalizePath(path);
310
+ const source = this.scan.sources.get(p);
311
+ if (!source?.bound || this.blockers.has(p)) return false;
312
+ const group = this.sourceGroupOf(p);
313
+ return group === undefined || !this.skipped.has(group);
314
+ }
315
+
316
+ /** What keeps the source at `path` itself on v2, which `FindAwsSdkV2ManualMigrations` marks. */
317
+ blockersOf(path: string): Blocker[] {
318
+ return this.blockers.get(normalizePath(path)) ?? [];
319
+ }
320
+
321
+ /** The lock group the `package.json` at `path` belongs to, if the migration leaves it unchanged. */
322
+ skippedGroupOf(path: string): SkippedGroup | undefined {
323
+ const group = this.groupOf(path);
324
+ return group === undefined ? undefined : this.skipped.get(group);
325
+ }
326
+
327
+ /** The v3 packages the migrated sources of the `package.json` in `dir` import, which declares `aws-sdk`. */
328
+ packagesOf(dir: string): Set<string> {
329
+ const packages = new Set<string>();
330
+ if (!this.sdkDirs.includes(dir)) return packages;
331
+ for (const [path, source] of this.scan.sources) {
332
+ if (ownerOf(path, this.sdkDirs) === dir && this.migrates(path)) source.packages.forEach(p => packages.add(p));
333
+ }
334
+ return packages;
335
+ }
336
+
337
+ /** The ranges the `package.json` in `dir` declares for the v3 packages it gains. */
338
+ versionsOf(dir: string): V3Versions | undefined {
339
+ return this.nodes.get(this.groupOfDir(dir))?.versions;
340
+ }
341
+
342
+ /** Each lock group whose sources migrate, by its Node.js, for the `AwsSdkLockGroupNodeVersions` table. */
343
+ nodeRow(manifest: string): AwsSdkLockGroupNodeVersion | undefined {
344
+ const verdict = [...this.nodes.values()].find(v => v.manifest === manifest);
345
+ return verdict && {
346
+ lockGroup: verdict.manifest,
347
+ nodeVersion: verdict.signal ? String(verdict.signal.node) : '',
348
+ declaredIn: verdict.signal ? `${verdict.signal.path}: ${verdict.signal.declared}` : '',
349
+ awsSdkVersion: verdict.versions?.awsSdk ?? '',
350
+ note: verdict.note
351
+ };
352
+ }
353
+
354
+ /** Whether the `package.json` in `dir` loses `aws-sdk`: its sources move to v3, or its group's lock is regenerated, which can't be with `aws-sdk` set aside. */
355
+ dropsAwsSdk(dir: string): boolean {
356
+ if (this.packagesOf(dir).size > 0) return true;
357
+ const group = this.groupOfDir(dir);
358
+ return this.sdkDirs.includes(dir) && this.sdkDirs.some(other => this.groupOfDir(other) === group && this.packagesOf(other).size > 0);
359
+ }
360
+ }
361
+
362
+ function declaredPackages(pkg: Record<string, any> | undefined): string[] {
363
+ return SECTIONS.flatMap(section => pkg?.[section] && typeof pkg[section] === 'object' ? Object.keys(pkg[section]) : []);
364
+ }
365
+
366
+ function manifestOf(group: string): string {
367
+ return group === '' ? 'package.json' : `${group}/package.json`;
368
+ }
369
+
370
+ export class AwsSdkSkippedLockGroup {
371
+ @Column({displayName: "Lock group", description: "The `package.json` whose lock the group shares: a workspace root's, or a standalone project's."})
372
+ lockGroup!: string;
373
+
374
+ @Column({displayName: "Source path", description: "A source in the group that stays on AWS SDK v2, which keeps the whole group on v2, or the file declaring a Node.js no v3 release supports."})
375
+ sourcePath!: string;
376
+
377
+ @Column({displayName: "Constructs", description: "What keeps the source on v2, each listed with its reason in `AwsSdkManualMigrationSteps`, or the Node.js version and what v3 needs."})
378
+ constructs!: string;
379
+ }
380
+
381
+ export function awsSdkSkippedLockGroups(): DataTable<AwsSdkSkippedLockGroup> {
382
+ return new DataTable<AwsSdkSkippedLockGroup>(
383
+ "org.openrewrite.node.table.AwsSdkSkippedLockGroups",
384
+ "AWS SDK v2 lock groups left unchanged",
385
+ "Lock groups (a workspace root with its members, or a standalone `package.json`) the migration to v3 left " +
386
+ "wholly unchanged, one row for each source that keeps the group on v2. Migrate those by hand, then run the " +
387
+ "migration again.",
388
+ AwsSdkSkippedLockGroup,
389
+ );
390
+ }
391
+
392
+ /** One row per source that keeps the lock group on v2, each construct named once. */
393
+ export function skippedRows(group: SkippedGroup): AwsSdkSkippedLockGroup[] {
394
+ return [...group.blocked].sort(([a], [b]) => a.localeCompare(b)).map(([sourcePath, blockers]) => ({
395
+ lockGroup: group.manifest,
396
+ sourcePath,
397
+ constructs: [...new Set(blockers.map(b => b.construct))].join(', ')
398
+ }));
399
+ }