@openrewrite/recipes-nodejs 0.56.0 → 0.57.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.
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -1
- package/dist/migrate/casts.d.ts +4 -0
- package/dist/migrate/casts.d.ts.map +1 -0
- package/dist/migrate/casts.js +53 -0
- package/dist/migrate/casts.js.map +1 -0
- package/dist/migrate/exports-folder-mappings.d.ts +9 -0
- package/dist/migrate/exports-folder-mappings.d.ts.map +1 -0
- package/dist/migrate/exports-folder-mappings.js +118 -0
- package/dist/migrate/exports-folder-mappings.js.map +1 -0
- package/dist/migrate/find-dns-lookup-options-coercion.d.ts +9 -0
- package/dist/migrate/find-dns-lookup-options-coercion.d.ts.map +1 -0
- package/dist/migrate/find-dns-lookup-options-coercion.js +270 -0
- package/dist/migrate/find-dns-lookup-options-coercion.js.map +1 -0
- package/dist/migrate/find-err-invalid-callback.d.ts +9 -0
- package/dist/migrate/find-err-invalid-callback.d.ts.map +1 -0
- package/dist/migrate/find-err-invalid-callback.js +61 -0
- package/dist/migrate/find-err-invalid-callback.js.map +1 -0
- package/dist/migrate/find-process-multiple-resolves.d.ts +9 -0
- package/dist/migrate/find-process-multiple-resolves.d.ts.map +1 -0
- package/dist/migrate/find-process-multiple-resolves.js +58 -0
- package/dist/migrate/find-process-multiple-resolves.js.map +1 -0
- package/dist/migrate/find-stream-thenable-implementations.d.ts +9 -0
- package/dist/migrate/find-stream-thenable-implementations.d.ts.map +1 -0
- package/dist/migrate/find-stream-thenable-implementations.js +123 -0
- package/dist/migrate/find-stream-thenable-implementations.js.map +1 -0
- package/dist/migrate/find-tls-parse-cert-string.d.ts +9 -0
- package/dist/migrate/find-tls-parse-cert-string.d.ts.map +1 -0
- package/dist/migrate/find-tls-parse-cert-string.js +206 -0
- package/dist/migrate/find-tls-parse-cert-string.js.map +1 -0
- package/dist/migrate/hpagent/hpagent-support.d.ts.map +1 -1
- package/dist/migrate/hpagent/hpagent-support.js +1 -1
- package/dist/migrate/hpagent/hpagent-support.js.map +1 -1
- package/dist/migrate/module-bindings.d.ts.map +1 -1
- package/dist/migrate/module-bindings.js +1 -1
- package/dist/migrate/module-bindings.js.map +1 -1
- package/dist/migrate/source-map-support/replace-source-map-support-api.d.ts.map +1 -1
- package/dist/migrate/source-map-support/replace-source-map-support-api.js +1 -1
- package/dist/migrate/source-map-support/replace-source-map-support-api.js.map +1 -1
- package/dist/migrate/upgrade-node-18.d.ts +8 -0
- package/dist/migrate/upgrade-node-18.d.ts.map +1 -0
- package/dist/migrate/upgrade-node-18.js +58 -0
- package/dist/migrate/upgrade-node-18.js.map +1 -0
- package/dist/migrate/upgrade-node-20.d.ts +1 -1
- package/dist/migrate/upgrade-node-20.d.ts.map +1 -1
- package/dist/migrate/upgrade-node-20.js +3 -6
- package/dist/migrate/upgrade-node-20.js.map +1 -1
- package/dist/resources/advisories-npm.csv +57 -14
- package/package.json +2 -2
- package/src/index.ts +14 -0
- package/src/migrate/casts.ts +57 -0
- package/src/migrate/exports-folder-mappings.ts +113 -0
- package/src/migrate/find-dns-lookup-options-coercion.ts +292 -0
- package/src/migrate/find-err-invalid-callback.ts +43 -0
- package/src/migrate/find-process-multiple-resolves.ts +42 -0
- package/src/migrate/find-stream-thenable-implementations.ts +116 -0
- package/src/migrate/find-tls-parse-cert-string.ts +179 -0
- package/src/migrate/hpagent/hpagent-support.ts +2 -2
- package/src/migrate/module-bindings.ts +2 -2
- package/src/migrate/source-map-support/replace-source-map-support-api.ts +2 -1
- package/src/migrate/upgrade-node-18.ts +52 -0
- package/src/migrate/upgrade-node-20.ts +3 -8
|
@@ -0,0 +1,292 @@
|
|
|
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
|
+
import {check, ExecutionContext, foundSearchResult, or, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
7
|
+
import {JavaScriptVisitor, JS, MethodMatcher, usesMethod, usesType} from "@openrewrite/rewrite/javascript";
|
|
8
|
+
import {isIdentifier, J, Type} from "@openrewrite/rewrite/java";
|
|
9
|
+
import {isObjectLiteral, propertyNameOf} from "./fs-extra/fs-extra-support";
|
|
10
|
+
import {mapUnderCasts, unwrapCasts} from "./casts";
|
|
11
|
+
|
|
12
|
+
const CALLBACK_PATTERNS = ["dns lookup(..)", "node:dns lookup(..)"];
|
|
13
|
+
const PROMISE_PATTERNS = ["dns/promises lookup(..)", "node:dns/promises lookup(..)"];
|
|
14
|
+
|
|
15
|
+
// Calls that resolve the host with dns.lookup(), passing it their family and hints options, unless given a `lookup`.
|
|
16
|
+
const CONNECTING_CALLS: [string, string, string][] = [
|
|
17
|
+
...["net", "node:net"].flatMap(m => ["connect", "createConnection"].map(f => [`${m} ${f}(..)`, `net.${f}()`, m] as [string, string, string])),
|
|
18
|
+
["net.Socket connect(..)", "socket.connect()", "net"],
|
|
19
|
+
...["tls", "node:tls"].map(m => [`${m} connect(..)`, "tls.connect()", m] as [string, string, string]),
|
|
20
|
+
...["http", "node:http", "https", "node:https"].flatMap(m => ["request", "get"].map(f =>
|
|
21
|
+
[`${m} ${f}(..)`, `${m.replace("node:", "")}.${f}()`, m] as [string, string, string])),
|
|
22
|
+
];
|
|
23
|
+
const AGENTS = new Set(["http.Agent", "https.Agent"]);
|
|
24
|
+
const PASSED_TO_LOOKUP = new Set(["family", "hints"]);
|
|
25
|
+
|
|
26
|
+
// Literal values each option throws for since Node.js 18 but older versions accepted.
|
|
27
|
+
const NUMBER_LIKE = (value: unknown) => typeof value === "string" || value === false;
|
|
28
|
+
const BOOLEAN_LIKE = (value: unknown) => typeof value === "string" || typeof value === "number";
|
|
29
|
+
const REJECTED: Record<string, (value: unknown) => boolean> = {
|
|
30
|
+
family: NUMBER_LIKE,
|
|
31
|
+
hints: NUMBER_LIKE,
|
|
32
|
+
all: BOOLEAN_LIKE,
|
|
33
|
+
verbatim: BOOLEAN_LIKE,
|
|
34
|
+
};
|
|
35
|
+
// net only passes `hints || 0` on, so an empty string or false never reached dns.lookup().
|
|
36
|
+
const REJECTED_WHEN_PASSED: Record<string, (value: unknown) => boolean> = {
|
|
37
|
+
family: NUMBER_LIKE,
|
|
38
|
+
hints: value => typeof value === "string" && value !== "",
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const FIXES: Record<string, string> = {
|
|
42
|
+
family: "Pass 0, 4 or 6 for family.",
|
|
43
|
+
hints: "Pass a number for hints.",
|
|
44
|
+
all: "Older Node.js versions read any all but true as false, so pass false to keep that behavior.",
|
|
45
|
+
verbatim: "Older Node.js versions ignored a verbatim that is not a boolean and used the default, so remove it to keep that behavior.",
|
|
46
|
+
};
|
|
47
|
+
const EXPECTED: Record<string, string> = {family: "number", hints: "number", all: "boolean", verbatim: "boolean"};
|
|
48
|
+
|
|
49
|
+
/** One option as passed: a literal value, or else the type of the expression. */
|
|
50
|
+
interface Option {
|
|
51
|
+
expression?: J;
|
|
52
|
+
type?: Type;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export class FindDnsLookupOptionsCoercion extends Recipe {
|
|
56
|
+
readonly name = "org.openrewrite.node.migrate.dns.find-lookup-options-coercion"
|
|
57
|
+
readonly displayName: string = "Find `dns.lookup()` options of the wrong type, and fix numeric strings";
|
|
58
|
+
readonly description: string = "Since Node.js 18 (DEP0153), `dns.lookup()` throws for option values that older versions " +
|
|
59
|
+
"silently converted, such as a string `family` or a non-boolean `all`. Finds literal values of the wrong type, " +
|
|
60
|
+
"and values typed as the wrong type, such as a `process.env` variable or an options object declared elsewhere, " +
|
|
61
|
+
"so they can be fixed by hand. A `family` or `hints` given as a string of digits, such as `'4'`, is " +
|
|
62
|
+
"replaced with the number, which older versions converted it to. Also checks the `family` and `hints` options of `net.connect()`, `tls.connect()`, " +
|
|
63
|
+
"`http.request()`, `https.request()` and `new http.Agent()`, which pass them on to `dns.lookup()`.";
|
|
64
|
+
readonly tags: string[] = ["DEP0153"];
|
|
65
|
+
|
|
66
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
67
|
+
const patterns = [...CALLBACK_PATTERNS, ...PROMISE_PATTERNS, ...CONNECTING_CALLS.map(([pattern]) => pattern)];
|
|
68
|
+
const preconditions = [...patterns.map(pattern => usesMethod(pattern)), ...[...AGENTS].map(agent => usesType(agent))];
|
|
69
|
+
return check(or(...preconditions), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
70
|
+
callbackMatchers = CALLBACK_PATTERNS.map(pattern => new MethodMatcher(pattern));
|
|
71
|
+
promiseMatchers = PROMISE_PATTERNS.map(pattern => new MethodMatcher(pattern));
|
|
72
|
+
connectingMatchers = CONNECTING_CALLS.map(([pattern, label]) => ({matcher: new MethodMatcher(pattern), label}));
|
|
73
|
+
|
|
74
|
+
protected async visitMethodInvocation(m: J.MethodInvocation, p: ExecutionContext): Promise<J | undefined> {
|
|
75
|
+
let method = await super.visitMethodInvocation(m, p) as J.MethodInvocation;
|
|
76
|
+
const connecting = this.connectingMatchers.find(({matcher}) => matcher.matches(method.methodType));
|
|
77
|
+
if (connecting) {
|
|
78
|
+
return markPassedOptions(method, connecting.label, method.arguments.elements.map(rp => rp.element));
|
|
79
|
+
}
|
|
80
|
+
const callback = this.callbackMatchers.some(matcher => matcher.matches(method.methodType));
|
|
81
|
+
if (!callback && !this.promiseMatchers.some(matcher => matcher.matches(method.methodType))) {
|
|
82
|
+
return method;
|
|
83
|
+
}
|
|
84
|
+
const api = callback ? "dns.lookup()" : "dnsPromises.lookup()";
|
|
85
|
+
method = withNumericOptions(method);
|
|
86
|
+
const options = unwrapCasts(method.arguments.elements[1]?.element);
|
|
87
|
+
if (options?.kind === J.Kind.Literal && NUMBER_LIKE((options as J.Literal).value)) {
|
|
88
|
+
return foundSearchResult(method, `${api} throws for a string or boolean options argument ` +
|
|
89
|
+
`since Node.js 18. Pass 0, 4 or 6, or an options object (DEP0153).`);
|
|
90
|
+
}
|
|
91
|
+
const effective = optionsOf(options);
|
|
92
|
+
if (!effective) {
|
|
93
|
+
return method;
|
|
94
|
+
}
|
|
95
|
+
const sentences = describe(api, effective, REJECTED, callback);
|
|
96
|
+
return sentences.length === 0 ? method : foundSearchResult(method, `${sentences.join(" ")} (DEP0153)`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
protected async visitNewClass(newClass: J.NewClass, p: ExecutionContext): Promise<J | undefined> {
|
|
100
|
+
const n = await super.visitNewClass(newClass, p) as J.NewClass;
|
|
101
|
+
const type = (n.class as J.Identifier | J.FieldAccess | undefined)?.type;
|
|
102
|
+
if (type?.kind !== Type.Kind.Class || !AGENTS.has((type as Type.Class).fullyQualifiedName)) {
|
|
103
|
+
return n;
|
|
104
|
+
}
|
|
105
|
+
return markPassedOptions(n, `Requests through this ${(type as Type.Class).fullyQualifiedName}`, n.arguments.elements.map(rp => rp.element), "fail");
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Marks a call that passes the family and hints options in `args` on to dns.lookup(). */
|
|
112
|
+
function markPassedOptions<T extends J>(call: T, api: string, args: J[], verb = "fails"): T {
|
|
113
|
+
for (const arg of args) {
|
|
114
|
+
const effective = optionsOf(unwrapCasts(arg));
|
|
115
|
+
if (!effective || effective.has("lookup")) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const passed = new Map([...effective].filter(([name]) => PASSED_TO_LOOKUP.has(name)));
|
|
119
|
+
const sentences = describe(api, passed, REJECTED_WHEN_PASSED, true, verb);
|
|
120
|
+
if (sentences.length > 0) {
|
|
121
|
+
return foundSearchResult(call, `${sentences.join(" ")} (DEP0153)`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return call;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The sentences describing the options in `effective` that Node.js 18 rejects. A `verb` other than "throws"
|
|
129
|
+
* says `api` passes the options on to dns.lookup().
|
|
130
|
+
*/
|
|
131
|
+
function describe(api: string, effective: Map<string, Option>, rejected: Record<string, (value: unknown) => boolean>,
|
|
132
|
+
callback: boolean, verb = "throws"): string[] {
|
|
133
|
+
const passed = verb !== "throws";
|
|
134
|
+
const familyName = callback && ["IPv4", "IPv6"].includes(literalValue(effective.get("family")?.expression) as string);
|
|
135
|
+
const wrong: string[] = [];
|
|
136
|
+
const typed: [string, string][] = [];
|
|
137
|
+
for (const [name, option] of effective) {
|
|
138
|
+
if (!rejected[name] || (familyName && name === "family")) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const value = unwrapCasts(option.expression);
|
|
142
|
+
if (value?.kind === J.Kind.Literal) {
|
|
143
|
+
if (rejected[name]((value as J.Literal).value)) {
|
|
144
|
+
wrong.push(name);
|
|
145
|
+
}
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const actual = wrongType(option.type, EXPECTED[name]);
|
|
149
|
+
if (actual) {
|
|
150
|
+
typed.push([name, actual]);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const via = (count: number) => passed ? `, as it passes ${count > 1 ? "them" : "it"} to dns.lookup()` : "";
|
|
154
|
+
const sentences: string[] = [];
|
|
155
|
+
if (wrong.length > 0) {
|
|
156
|
+
sentences.push(
|
|
157
|
+
`${api} ${verb} for the ${wrong.join(" and ")} option${wrong.length > 1 ? "s" : ""} since Node.js 18${via(wrong.length)}.`,
|
|
158
|
+
...wrong.map(name => FIXES[name]));
|
|
159
|
+
}
|
|
160
|
+
for (const [name, actual] of typed) {
|
|
161
|
+
sentences.push(`${api} ${verb} since Node.js 18 when the ${name} option is not a ${EXPECTED[name]}${via(1)}. ` +
|
|
162
|
+
`Here it is ${actual}.`, FIXES[name]);
|
|
163
|
+
}
|
|
164
|
+
if (familyName) {
|
|
165
|
+
sentences.push("Before Node.js 18, a family of 'IPv4' or 'IPv6' was read as 0 (any family); 18.0 to 18.3 " +
|
|
166
|
+
"throw for it, and 18.4 and later read it as 4 or 6, so check that the narrower lookup is intended.");
|
|
167
|
+
}
|
|
168
|
+
return sentences;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// A string older versions read as the same number, as `value >>> 0` does for a decimal string.
|
|
172
|
+
const DIGITS = /^\d{1,9}$/;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* `call` with a family or hints given as a string of digits, which dns.lookup() read as that number before
|
|
176
|
+
* Node.js 18, replaced with the number. So is an options argument that is such a string, which was the family.
|
|
177
|
+
* Callers like net.connect() are left alone, as they checked the family before converting it.
|
|
178
|
+
*/
|
|
179
|
+
function withNumericOptions(call: J.MethodInvocation): J.MethodInvocation {
|
|
180
|
+
const arg = call.arguments.elements[1]?.element;
|
|
181
|
+
if (!arg) {
|
|
182
|
+
return call;
|
|
183
|
+
}
|
|
184
|
+
const options = mapUnderCasts(arg, inner => isDigitString(inner)
|
|
185
|
+
? toNumber(inner as J.Literal)
|
|
186
|
+
: isObjectLiteral(inner) ? withNumericMembers(inner) : inner);
|
|
187
|
+
if (options === arg) {
|
|
188
|
+
return call;
|
|
189
|
+
}
|
|
190
|
+
const elements = call.arguments.elements.map((rp, i) => i === 1 ? {...rp, element: options} : rp);
|
|
191
|
+
return {...call, arguments: {...call.arguments, elements}} as J.MethodInvocation;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** An options object with the family and hints it ends up with replaced with numbers, where they are digit strings. */
|
|
195
|
+
function withNumericMembers(options: J.NewClass): J.NewClass {
|
|
196
|
+
const effective = effectiveOptions(options);
|
|
197
|
+
let changed = false;
|
|
198
|
+
const statements = options.body!.statements.map(rp => {
|
|
199
|
+
const member = rp.element;
|
|
200
|
+
if (member.kind !== JS.Kind.PropertyAssignment) {
|
|
201
|
+
return rp;
|
|
202
|
+
}
|
|
203
|
+
const assignment = member as JS.PropertyAssignment;
|
|
204
|
+
const name = propertyNameOf(assignment);
|
|
205
|
+
const value = assignment.initializer;
|
|
206
|
+
// Only the value the option ends up with; one a later key or spread overrides was never read.
|
|
207
|
+
if ((name !== "family" && name !== "hints") || !value || effective.get(name) !== value) {
|
|
208
|
+
return rp;
|
|
209
|
+
}
|
|
210
|
+
const numeric = mapUnderCasts(value, inner => isDigitString(inner) ? toNumber(inner as J.Literal) : inner);
|
|
211
|
+
if (numeric === value) {
|
|
212
|
+
return rp;
|
|
213
|
+
}
|
|
214
|
+
changed = true;
|
|
215
|
+
return {...rp, element: {...assignment, initializer: numeric}};
|
|
216
|
+
});
|
|
217
|
+
return changed ? {...options, body: {...options.body!, statements}} as J.NewClass : options;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function isDigitString(e: J | undefined): boolean {
|
|
221
|
+
return e?.kind === J.Kind.Literal && typeof (e as J.Literal).value === "string" && DIGITS.test((e as J.Literal).value as string);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function toNumber(literal: J.Literal): J.Literal {
|
|
225
|
+
const value = Number(literal.value);
|
|
226
|
+
return {...literal, value, valueSource: String(value), unicodeEscapes: undefined, type: Type.Primitive.Double};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** The value of a literal under any casts, or undefined when `e` is not one. */
|
|
230
|
+
function literalValue(e: J | undefined): unknown {
|
|
231
|
+
const value = unwrapCasts(e);
|
|
232
|
+
return value?.kind === J.Kind.Literal ? (value as J.Literal).value : undefined;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* The options an argument passes: the members of an object literal, or of the type of any other expression,
|
|
237
|
+
* such as a variable holding an options object. Undefined when neither is known.
|
|
238
|
+
*/
|
|
239
|
+
function optionsOf(e: J | undefined): Map<string, Option> | undefined {
|
|
240
|
+
if (isObjectLiteral(e)) {
|
|
241
|
+
return new Map([...effectiveOptions(e)].map(([name, value]) => [name, {expression: value, type: typeOf(value)}]));
|
|
242
|
+
}
|
|
243
|
+
const type = typeOf(e);
|
|
244
|
+
if (type?.kind !== Type.Kind.Class || !(type as Type.Class).members?.length) {
|
|
245
|
+
return undefined;
|
|
246
|
+
}
|
|
247
|
+
return new Map((type as Type.Class).members.map(member => [member.name, {type: member.type}]));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function typeOf(e: J | undefined): Type | undefined {
|
|
251
|
+
return (unwrapCasts(e) as J & { type?: Type } | undefined)?.type;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* How a value of `type` breaks an option expecting `expected`: "a string" when it is one, and "possibly a string"
|
|
256
|
+
* when it may be one. Only strings, and numbers where a boolean is expected, are reported, as older versions
|
|
257
|
+
* converted them; other types failed before too, or are not known.
|
|
258
|
+
*/
|
|
259
|
+
function wrongType(type: Type | undefined, expected: string): string | undefined {
|
|
260
|
+
const kinds = (type as Type.Union | undefined)?.bounds ? (type as Type.Union).bounds : type ? [type] : [];
|
|
261
|
+
const rejected = (t: Type) => isPrimitive(t, "String") ||
|
|
262
|
+
(expected === "boolean" && ["double", "int", "long", "float", "short", "byte"].some(k => isPrimitive(t, k)));
|
|
263
|
+
const wrong = kinds.filter(rejected);
|
|
264
|
+
if (wrong.length === 0) {
|
|
265
|
+
return undefined;
|
|
266
|
+
}
|
|
267
|
+
const name = isPrimitive(wrong[0], "String") ? "a string" : "a number";
|
|
268
|
+
return wrong.length === kinds.length ? name : `possibly ${name}`;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function isPrimitive(type: Type, keyword: string): boolean {
|
|
272
|
+
return type.kind === Type.Kind.Primitive && (type as Type.Primitive).keyword === keyword;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** The value each option ends up with, where a later key or a spread overrides an earlier one. */
|
|
276
|
+
function effectiveOptions(options: J.NewClass): Map<string, J | undefined> {
|
|
277
|
+
const effective = new Map<string, J | undefined>();
|
|
278
|
+
for (const statement of options.body?.statements ?? []) {
|
|
279
|
+
const member = statement.element;
|
|
280
|
+
const name = member.kind === JS.Kind.PropertyAssignment ? propertyNameOf(member as JS.PropertyAssignment) : undefined;
|
|
281
|
+
if (name) {
|
|
282
|
+
effective.delete(name);
|
|
283
|
+
effective.set(name, (member as JS.PropertyAssignment).initializer);
|
|
284
|
+
} else if (isIdentifier(member)) {
|
|
285
|
+
effective.delete(member.simpleName);
|
|
286
|
+
effective.set(member.simpleName, member);
|
|
287
|
+
} else {
|
|
288
|
+
effective.clear();
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return effective;
|
|
292
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
import {ExecutionContext, foundSearchResult, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
7
|
+
import {JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
8
|
+
import {J} from "@openrewrite/rewrite/java";
|
|
9
|
+
|
|
10
|
+
const OLD_CODE = /\bERR_INVALID_CALLBACK\b/;
|
|
11
|
+
const MESSAGE = "ERR_INVALID_CALLBACK was removed in Node.js 18 (DEP0159). Invalid callbacks throw ERR_INVALID_ARG_TYPE instead.";
|
|
12
|
+
const TEMPLATE_PARTS = new Set<string>([JS.Kind.TemplateExpression, JS.Kind.TemplateExpressionSpan]);
|
|
13
|
+
|
|
14
|
+
const namesOldCode = (lit: J.Literal) => !!lit.valueSource && OLD_CODE.test(lit.valueSource);
|
|
15
|
+
|
|
16
|
+
export class FindErrInvalidCallback extends Recipe {
|
|
17
|
+
readonly name = "org.openrewrite.node.migrate.find-err-invalid-callback"
|
|
18
|
+
readonly displayName: string = "Find removed `ERR_INVALID_CALLBACK` error code usage";
|
|
19
|
+
readonly description: string = "The `ERR_INVALID_CALLBACK` error code was removed in Node.js 18 (DEP0159). Invalid " +
|
|
20
|
+
"callbacks now throw `ERR_INVALID_ARG_TYPE`, which Node.js also throws for every other argument of the wrong " +
|
|
21
|
+
"type. Finds strings and regular expressions that name the old code, so checks against it can be updated by " +
|
|
22
|
+
"hand. Code that creates the error itself, such as a polyfill, may need to keep it.";
|
|
23
|
+
readonly tags: string[] = ["DEP0159"];
|
|
24
|
+
|
|
25
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
26
|
+
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
27
|
+
protected async visitTemplateExpression(template: JS.TemplateExpression, p: ExecutionContext): Promise<J | undefined> {
|
|
28
|
+
const t = await super.visitTemplateExpression(template, p) as JS.TemplateExpression;
|
|
29
|
+
return namesOldCode(t.head) || t.spans.some(span => namesOldCode(span.element.tail))
|
|
30
|
+
? foundSearchResult(t, MESSAGE)
|
|
31
|
+
: t;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
protected async visitLiteral(literal: J.Literal, p: ExecutionContext): Promise<J | undefined> {
|
|
35
|
+
const lit = await super.visitLiteral(literal, p) as J.Literal;
|
|
36
|
+
// Template text is marked on the whole template, since a marker on a span's text prints inside `${}`.
|
|
37
|
+
return namesOldCode(lit) && !TEMPLATE_PARTS.has(this.cursor.parentTree()?.value.kind)
|
|
38
|
+
? foundSearchResult(lit, MESSAGE)
|
|
39
|
+
: lit;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
import {check, ExecutionContext, foundSearchResult, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
7
|
+
import {JavaScriptVisitor, usesType} from "@openrewrite/rewrite/javascript";
|
|
8
|
+
import {J, Type} from "@openrewrite/rewrite/java";
|
|
9
|
+
import {stringLiteral} from "./module-bindings";
|
|
10
|
+
import {unwrapCasts} from "./casts";
|
|
11
|
+
|
|
12
|
+
const LISTENER_METHODS = new Set(["on", "once", "addListener", "prependListener", "prependOnceListener"]);
|
|
13
|
+
const MESSAGE = "The 'multipleResolves' event is deprecated since Node.js 18 (DEP0160) and never fires from Node.js 25, " +
|
|
14
|
+
"since it did not work with promise combinators. Remove the listener, or track settled promises in the code itself.";
|
|
15
|
+
|
|
16
|
+
export class FindProcessMultipleResolves extends Recipe {
|
|
17
|
+
readonly name = "org.openrewrite.node.migrate.process.find-multiple-resolves"
|
|
18
|
+
readonly displayName: string = "Find `process.on('multipleResolves')` listeners";
|
|
19
|
+
readonly description: string = "The `process` `'multipleResolves'` event was runtime-deprecated in Node.js 18 " +
|
|
20
|
+
"(DEP0160) and removed in Node.js 25, because it did not work with promise combinators such as `Promise.race()`. " +
|
|
21
|
+
"Finds listeners for it, which emit a deprecation warning and later never run, so they can be removed.";
|
|
22
|
+
readonly tags: string[] = ["DEP0160"];
|
|
23
|
+
|
|
24
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
25
|
+
return check(usesType("NodeJS.Process"), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
26
|
+
protected async visitMethodInvocation(m: J.MethodInvocation, p: ExecutionContext): Promise<J | undefined> {
|
|
27
|
+
const method = await super.visitMethodInvocation(m, p) as J.MethodInvocation;
|
|
28
|
+
if (!LISTENER_METHODS.has(method.name.simpleName) ||
|
|
29
|
+
stringLiteral(method.arguments.elements[0]?.element) !== "multipleResolves" ||
|
|
30
|
+
!isProcess(unwrapCasts(method.select?.element))) {
|
|
31
|
+
return method;
|
|
32
|
+
}
|
|
33
|
+
return foundSearchResult(method, MESSAGE);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function isProcess(e: J | undefined): boolean {
|
|
40
|
+
const type = (e as J.Identifier | J.FieldAccess | undefined)?.type;
|
|
41
|
+
return type?.kind === Type.Kind.Class && (type as Type.Class).fullyQualifiedName === "NodeJS.Process";
|
|
42
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
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
|
+
import {ExecutionContext, foundSearchResult, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
7
|
+
import {JavaScriptVisitor, JS} from "@openrewrite/rewrite/javascript";
|
|
8
|
+
import {J, Type} from "@openrewrite/rewrite/java";
|
|
9
|
+
import {isObjectLiteral, propertyNameOf} from "./fs-extra/fs-extra-support";
|
|
10
|
+
|
|
11
|
+
// The implementation methods whose returned promise Node.js 16 and 17 waited on, and Node.js 18 ignores.
|
|
12
|
+
const OPTIONS = new Set(["construct", "destroy", "final", "transform", "flush"]);
|
|
13
|
+
const METHODS = new Set([...OPTIONS].map(name => "_" + name));
|
|
14
|
+
const STREAM_CLASS = /^(node:)?[Ss]tream\.(Readable|Writable|Duplex|Transform|PassThrough)$/;
|
|
15
|
+
|
|
16
|
+
const message = (name: string) => `Node.js 18 ignores the promise ${name}() returns (DEP0157), so the stream waits ` +
|
|
17
|
+
"until the callback is called, and a rejection is no longer passed to the stream. Call the callback, with any " +
|
|
18
|
+
"error, instead of relying on the promise.";
|
|
19
|
+
|
|
20
|
+
export class FindStreamThenableImplementations extends Recipe {
|
|
21
|
+
readonly name = "org.openrewrite.node.migrate.stream.find-thenable-implementations";
|
|
22
|
+
readonly displayName: string = "Find `async` stream implementation methods";
|
|
23
|
+
readonly description: string = "Before Node.js 18, a stream treated the promise returned by `_construct()`, " +
|
|
24
|
+
"`_destroy()`, `_final()`, `_transform()` or `_flush()` as if the callback had been called, and pushed the " +
|
|
25
|
+
"value a `_transform()` or `_flush()` promise resolved to. Node.js 18 removed this (DEP0157), so an `async` " +
|
|
26
|
+
"implementation that never calls its callback leaves the stream hanging. Finds `async` implementations in " +
|
|
27
|
+
"stream subclasses and in the options passed to stream constructors, so they can be changed to call the callback.";
|
|
28
|
+
readonly tags: string[] = ["DEP0157"];
|
|
29
|
+
|
|
30
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
31
|
+
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
32
|
+
protected async visitMethodDeclaration(method: J.MethodDeclaration, p: ExecutionContext): Promise<J | undefined> {
|
|
33
|
+
const m = await super.visitMethodDeclaration(method, p) as J.MethodDeclaration;
|
|
34
|
+
const name = m.name.simpleName;
|
|
35
|
+
if (!METHODS.has(name) || !isAsync(m.modifiers)) {
|
|
36
|
+
return m;
|
|
37
|
+
}
|
|
38
|
+
// A class member sits in the class body block, directly under the class declaration.
|
|
39
|
+
const owner = this.cursor.parentTree()?.parentTree()?.value;
|
|
40
|
+
return owner?.kind === J.Kind.ClassDeclaration && extendsStream((owner as J.ClassDeclaration).type)
|
|
41
|
+
? foundSearchResult(m, message(name))
|
|
42
|
+
: m;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
protected async visitNewClass(newClass: J.NewClass, p: ExecutionContext): Promise<J | undefined> {
|
|
46
|
+
const n = await super.visitNewClass(newClass, p) as J.NewClass;
|
|
47
|
+
if (!n.class || !extendsStream((n.class as any).type)) {
|
|
48
|
+
return n;
|
|
49
|
+
}
|
|
50
|
+
const options = n.arguments.elements[0]?.element;
|
|
51
|
+
if (!isObjectLiteral(options)) {
|
|
52
|
+
return n;
|
|
53
|
+
}
|
|
54
|
+
let changed = false;
|
|
55
|
+
const statements = options.body!.statements.map(rp => {
|
|
56
|
+
const member = rp.element;
|
|
57
|
+
const name = optionName(member);
|
|
58
|
+
if (!name || !OPTIONS.has(name) || !isAsyncImplementation(member)) {
|
|
59
|
+
return rp;
|
|
60
|
+
}
|
|
61
|
+
// Already marked in an earlier cycle: keep the same node so the tree stays unchanged.
|
|
62
|
+
const marked = foundSearchResult(member, message(name));
|
|
63
|
+
if (marked === member) {
|
|
64
|
+
return rp;
|
|
65
|
+
}
|
|
66
|
+
changed = true;
|
|
67
|
+
return {...rp, element: marked};
|
|
68
|
+
});
|
|
69
|
+
if (!changed) {
|
|
70
|
+
return n;
|
|
71
|
+
}
|
|
72
|
+
const marked = {...options, body: {...options.body!, statements}} as J.NewClass;
|
|
73
|
+
return {
|
|
74
|
+
...n,
|
|
75
|
+
arguments: {...n.arguments, elements: n.arguments.elements.map((rp, i) => i === 0 ? {...rp, element: marked} : rp)}
|
|
76
|
+
} as J.NewClass;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function isAsync(modifiers: J.Modifier[] | undefined): boolean {
|
|
83
|
+
return !!modifiers?.some(m => m.keyword === "async");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Whether `type` is one of the stream classes, or a class extending one. */
|
|
87
|
+
function extendsStream(type: Type | undefined): boolean {
|
|
88
|
+
for (let t = type as Type.Class | undefined, depth = 0; t && depth < 20; t = t.supertype, depth++) {
|
|
89
|
+
if (t.kind === Type.Kind.Class && STREAM_CLASS.test(t.fullyQualifiedName)) {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The name of an object-literal method or property. */
|
|
97
|
+
function optionName(member: J): string | undefined {
|
|
98
|
+
if (member.kind === J.Kind.MethodDeclaration) {
|
|
99
|
+
// A quoted method name, like `'flush'() {}`, keeps its quotes.
|
|
100
|
+
return (member as J.MethodDeclaration).name.simpleName.replace(/^(['"])(.*)\1$/, "$2");
|
|
101
|
+
}
|
|
102
|
+
return member.kind === JS.Kind.PropertyAssignment ? propertyNameOf(member as JS.PropertyAssignment) : undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Whether an object-literal member is an `async` method, arrow function or function expression. */
|
|
106
|
+
function isAsyncImplementation(member: J): boolean {
|
|
107
|
+
if (member.kind === J.Kind.MethodDeclaration) {
|
|
108
|
+
return isAsync((member as J.MethodDeclaration).modifiers);
|
|
109
|
+
}
|
|
110
|
+
let value: J | undefined = (member as JS.PropertyAssignment).initializer;
|
|
111
|
+
if (value?.kind === JS.Kind.StatementExpression) {
|
|
112
|
+
value = (value as JS.StatementExpression).statement;
|
|
113
|
+
}
|
|
114
|
+
return (value?.kind === JS.Kind.ArrowFunction || value?.kind === J.Kind.MethodDeclaration) &&
|
|
115
|
+
isAsync((value as JS.ArrowFunction | J.MethodDeclaration).modifiers);
|
|
116
|
+
}
|