@openrewrite/recipes-nodejs 0.55.0 → 0.56.1
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 +22 -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/child-process-channel.d.ts +9 -0
- package/dist/migrate/child-process-channel.d.ts.map +1 -0
- package/dist/migrate/child-process-channel.js +65 -0
- package/dist/migrate/child-process-channel.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 +6 -2
- 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/process-exit-code.d.ts.map +1 -1
- package/dist/migrate/process-exit-code.js +16 -11
- package/dist/migrate/process-exit-code.js.map +1 -1
- package/dist/migrate/process-multiple-resolves.d.ts +9 -0
- package/dist/migrate/process-multiple-resolves.d.ts.map +1 -0
- package/dist/migrate/process-multiple-resolves.js +72 -0
- package/dist/migrate/process-multiple-resolves.js.map +1 -0
- package/dist/migrate/prop-types/remove-prop-types.d.ts +8 -4
- package/dist/migrate/prop-types/remove-prop-types.d.ts.map +1 -1
- package/dist/migrate/prop-types/remove-prop-types.js +62 -40
- package/dist/migrate/prop-types/remove-prop-types.js.map +1 -1
- package/dist/migrate/repl-classes-with-new.d.ts +9 -0
- package/dist/migrate/repl-classes-with-new.d.ts.map +1 -0
- package/dist/migrate/repl-classes-with-new.js +74 -0
- package/dist/migrate/repl-classes-with-new.js.map +1 -0
- 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/migrate/upgrade-node-22.d.ts.map +1 -1
- package/dist/migrate/upgrade-node-22.js +2 -0
- package/dist/migrate/upgrade-node-22.js.map +1 -1
- package/dist/migrate/upgrade-node-24.d.ts.map +1 -1
- package/dist/migrate/upgrade-node-24.js +8 -0
- package/dist/migrate/upgrade-node-24.js.map +1 -1
- package/dist/migrate/upgrade-node-26.d.ts.map +1 -1
- package/dist/migrate/upgrade-node-26.js +4 -0
- package/dist/migrate/upgrade-node-26.js.map +1 -1
- package/dist/migrate/util-type-checking.d.ts.map +1 -1
- package/dist/migrate/util-type-checking.js +24 -2
- package/dist/migrate/util-type-checking.js.map +1 -1
- package/dist/resources/advisories-npm.csv +207 -27
- package/package.json +2 -2
- package/src/index.ts +22 -0
- package/src/migrate/casts.ts +57 -0
- package/src/migrate/child-process-channel.ts +61 -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 +8 -4
- package/src/migrate/module-bindings.ts +2 -2
- package/src/migrate/process-exit-code.ts +19 -14
- package/src/migrate/process-multiple-resolves.ts +72 -0
- package/src/migrate/prop-types/remove-prop-types.ts +77 -45
- package/src/migrate/repl-classes-with-new.ts +66 -0
- 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
- package/src/migrate/upgrade-node-22.ts +2 -0
- package/src/migrate/upgrade-node-24.ts +8 -0
- package/src/migrate/upgrade-node-26.ts +4 -0
- package/src/migrate/util-type-checking.ts +32 -6
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
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 {declarationsOf, JavaScriptVisitor, JS, MethodMatcher, usesMethod, usesType} from "@openrewrite/rewrite/javascript";
|
|
8
|
+
import {isIdentifier, J, Type} from "@openrewrite/rewrite/java";
|
|
9
|
+
import {unwrapCasts} from "./casts";
|
|
10
|
+
import {bindingsOf, isModuleScoped, requiredModule, stringLiteral} from "./module-bindings";
|
|
11
|
+
|
|
12
|
+
const TLS_MODULES = new Set(["tls", "node:tls"]);
|
|
13
|
+
const NAME = "parseCertString";
|
|
14
|
+
const ADVICE = "Use the subject/issuer objects from getPeerCertificate() instead.";
|
|
15
|
+
const CALL_MESSAGE = `tls.parseCertString() was removed in Node.js 18 (DEP0076). ${ADVICE}`;
|
|
16
|
+
const REFERENCE_MESSAGE = `tls.parseCertString was removed in Node.js 18 (DEP0076), so this is undefined. ${ADVICE}`;
|
|
17
|
+
|
|
18
|
+
export class FindTlsParseCertString extends Recipe {
|
|
19
|
+
readonly name = "org.openrewrite.node.migrate.tls.find-tls-parse-cert-string"
|
|
20
|
+
readonly displayName: string = "Find deprecated `tls.parseCertString()` usage";
|
|
21
|
+
readonly description: string = "`tls.parseCertString()` was runtime-deprecated in Node.js 9 (DEP0076) and removed " +
|
|
22
|
+
"in Node.js 18. It never parsed multi-value Relative Distinguished Names correctly, and `querystring.parse()` " +
|
|
23
|
+
"is not a safe replacement either. Read the parsed `subject` and `issuer` objects from " +
|
|
24
|
+
"`tlsSocket.getPeerCertificate()` instead. Finds calls, and references that are not calls, such as " +
|
|
25
|
+
"`tls.parseCertString` passed as a callback or checked for feature detection, and calls through names it was imported, required or destructured as.";
|
|
26
|
+
readonly tags: string[] = ["DEP0076"];
|
|
27
|
+
|
|
28
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
29
|
+
const patterns = [...TLS_MODULES].map(module => `${module} ${NAME}(..)`);
|
|
30
|
+
return check(or(bindsTls(), ...[...TLS_MODULES].map(module => usesType(module)), ...patterns.map(pattern => usesMethod(pattern))), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
31
|
+
matchers = patterns.map(pattern => new MethodMatcher(pattern));
|
|
32
|
+
/** Module-scoped names bound to the tls module itself. */
|
|
33
|
+
modules = new Set<string>();
|
|
34
|
+
/** Module-scoped names bound to parseCertString. */
|
|
35
|
+
functions = new Set<string>();
|
|
36
|
+
|
|
37
|
+
protected async visitJsCompilationUnit(cu: JS.CompilationUnit, p: ExecutionContext): Promise<J | undefined> {
|
|
38
|
+
const bindings = bindingsOf(cu, TLS_MODULES);
|
|
39
|
+
this.modules = new Set([...bindings].filter(([, member]) => member === undefined).map(([local]) => local));
|
|
40
|
+
this.functions = new Set([...bindings].filter(([, member]) => member === NAME).map(([local]) => local));
|
|
41
|
+
for (const [local, member] of destructuredFromModule(cu, this.modules)) {
|
|
42
|
+
if (member === NAME) {
|
|
43
|
+
this.functions.add(local);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return super.visitJsCompilationUnit(cu, p);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
protected async visitMethodInvocation(m: J.MethodInvocation, p: ExecutionContext): Promise<J | undefined> {
|
|
50
|
+
const method = await super.visitMethodInvocation(m, p) as J.MethodInvocation;
|
|
51
|
+
if (this.matchers.some(matcher => matcher.matches(method.methodType)) ||
|
|
52
|
+
(method.name.simpleName === NAME && this.isTls(method.select?.element)) ||
|
|
53
|
+
(!method.select && this.functions.has(method.name.simpleName) && isModuleScoped(this.cursor, method.name.simpleName))) {
|
|
54
|
+
return foundSearchResult(method, CALL_MESSAGE);
|
|
55
|
+
}
|
|
56
|
+
return method;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected async visitFieldAccess(fieldAccess: J.FieldAccess, p: ExecutionContext): Promise<J | undefined> {
|
|
60
|
+
const f = await super.visitFieldAccess(fieldAccess, p) as J.FieldAccess;
|
|
61
|
+
return f.name.element.simpleName === NAME && this.isTls(f.target)
|
|
62
|
+
? foundSearchResult(f, REFERENCE_MESSAGE)
|
|
63
|
+
: f;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
protected async visitArrayAccess(arrayAccess: J.ArrayAccess, p: ExecutionContext): Promise<J | undefined> {
|
|
67
|
+
const a = await super.visitArrayAccess(arrayAccess, p) as J.ArrayAccess;
|
|
68
|
+
// Called right away, as in `tls['parseCertString'](s)`, the call is marked instead.
|
|
69
|
+
const parent = this.cursor.parentTree()?.value as J | undefined;
|
|
70
|
+
const called = parent?.kind === JS.Kind.FunctionCall &&
|
|
71
|
+
(parent as JS.FunctionCall).function?.element === arrayAccess;
|
|
72
|
+
return !called && this.isTlsBracketAccess(a) ? foundSearchResult(a, REFERENCE_MESSAGE) : a;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
protected async visitFunctionCall(functionCall: JS.FunctionCall, p: ExecutionContext): Promise<J | undefined> {
|
|
76
|
+
const f = await super.visitFunctionCall(functionCall, p) as JS.FunctionCall;
|
|
77
|
+
const callee = f.function?.element;
|
|
78
|
+
return callee?.kind === J.Kind.ArrayAccess && this.isTlsBracketAccess(callee as J.ArrayAccess)
|
|
79
|
+
? foundSearchResult(f, CALL_MESSAGE)
|
|
80
|
+
: f;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
protected async visitIdentifier(ident: J.Identifier, p: ExecutionContext): Promise<J | undefined> {
|
|
84
|
+
const id = await super.visitIdentifier(ident, p) as J.Identifier;
|
|
85
|
+
return this.functions.has(id.simpleName) && !declaresOrNames(this.cursor.parentTree()?.value, ident) &&
|
|
86
|
+
isModuleScoped(this.cursor, id.simpleName)
|
|
87
|
+
? foundSearchResult(id, REFERENCE_MESSAGE)
|
|
88
|
+
: id;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private isTlsBracketAccess(a: J.ArrayAccess): boolean {
|
|
92
|
+
return stringLiteral(unwrapCasts(a.dimension.index.element)) === NAME && this.isTls(a.indexed);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Whether `e` is the tls module: typed as it, a name bound to it, or a `require` of it. */
|
|
96
|
+
private isTls(e: J | undefined): boolean {
|
|
97
|
+
const target = unwrapCasts(e);
|
|
98
|
+
if (target?.kind === J.Kind.Identifier) {
|
|
99
|
+
const id = target as J.Identifier;
|
|
100
|
+
const type = id.type;
|
|
101
|
+
return (type?.kind === Type.Kind.Class && TLS_MODULES.has((type as Type.Class).fullyQualifiedName)) ||
|
|
102
|
+
(this.modules.has(id.simpleName) && isModuleScoped(this.cursor, id.simpleName));
|
|
103
|
+
}
|
|
104
|
+
return TLS_MODULES.has(requiredModule(target) ?? "");
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Finds files that import or require tls, which can refer to parseCertString through a local name without
|
|
112
|
+
* anything typed as the tls module, as `import {parseCertString as parse} from 'tls'; [].map(parse)` does.
|
|
113
|
+
*/
|
|
114
|
+
function bindsTls(): TreeVisitor<any, ExecutionContext> {
|
|
115
|
+
return new class extends JavaScriptVisitor<ExecutionContext> {
|
|
116
|
+
protected async visitJsCompilationUnit(cu: JS.CompilationUnit): Promise<J | undefined> {
|
|
117
|
+
return bindingsOf(cu, TLS_MODULES).size > 0 ? foundSearchResult(cu) : cu;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Whether `ident` under `parent` is not a use of the name it holds: the name a call, property access, property or
|
|
124
|
+
* declaration spells out, such as `p` in `p(s)`, `x.p`, `{p: 1}`, `const {p} = tls` or `import {p} from 'tls'`.
|
|
125
|
+
* Calls are marked as calls, and the rest are not references to parseCertString.
|
|
126
|
+
*/
|
|
127
|
+
function declaresOrNames(parent: any, ident: J.Identifier): boolean {
|
|
128
|
+
switch (parent?.kind) {
|
|
129
|
+
case J.Kind.MethodInvocation:
|
|
130
|
+
return (parent as J.MethodInvocation).name === ident;
|
|
131
|
+
case J.Kind.FieldAccess:
|
|
132
|
+
return (parent as J.FieldAccess).name.element === ident;
|
|
133
|
+
case JS.Kind.PropertyAssignment:
|
|
134
|
+
// The shorthand `{p}` has no initializer and uses `p` as its value.
|
|
135
|
+
return (parent as JS.PropertyAssignment).name.element === ident && !!(parent as JS.PropertyAssignment).initializer;
|
|
136
|
+
case J.Kind.NamedVariable:
|
|
137
|
+
return (parent as J.VariableDeclarations.NamedVariable).name === ident;
|
|
138
|
+
case J.Kind.MethodDeclaration:
|
|
139
|
+
return (parent as J.MethodDeclaration).name === ident;
|
|
140
|
+
case JS.Kind.ObjectBindingPattern:
|
|
141
|
+
case JS.Kind.BindingElement:
|
|
142
|
+
case JS.Kind.ImportSpecifier:
|
|
143
|
+
case JS.Kind.Alias:
|
|
144
|
+
case JS.Kind.ImportClause:
|
|
145
|
+
return true;
|
|
146
|
+
default:
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The local and property names of top-level `const {a: b} = tls` destructurings of the module names in `modules`. */
|
|
152
|
+
function destructuredFromModule(cu: JS.CompilationUnit, modules: ReadonlySet<string>): [string, string][] {
|
|
153
|
+
const found: [string, string][] = [];
|
|
154
|
+
for (const padded of cu.statements) {
|
|
155
|
+
for (const declaration of declarationsOf(padded.element)) {
|
|
156
|
+
for (const v of declaration.variables) {
|
|
157
|
+
const initializer = unwrapCasts(v.element.initializer?.element);
|
|
158
|
+
const pattern = v.element.name as J;
|
|
159
|
+
if (!isIdentifier(initializer) || !modules.has(initializer.simpleName) ||
|
|
160
|
+
pattern.kind !== JS.Kind.ObjectBindingPattern) {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
for (const el of (pattern as JS.ObjectBindingPattern).bindings.elements) {
|
|
164
|
+
const b = el.element;
|
|
165
|
+
if (isIdentifier(b)) {
|
|
166
|
+
found.push([b.simpleName, b.simpleName]);
|
|
167
|
+
} else if (b.kind === JS.Kind.BindingElement) {
|
|
168
|
+
const be = b as JS.BindingElement;
|
|
169
|
+
const property = be.propertyName?.element ?? be.name;
|
|
170
|
+
if (isIdentifier(be.name as J) && isIdentifier(property as J)) {
|
|
171
|
+
found.push([(be.name as J.Identifier).simpleName, (property as J.Identifier).simpleName]);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return found;
|
|
179
|
+
}
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import {Column, Cursor, DataTable, emptyMarkers, randomId} from "@openrewrite/rewrite";
|
|
8
|
-
import {emptySpace, isIdentifier, J, rightPadded, space} from "@openrewrite/rewrite/java";
|
|
9
|
-
import {bindingNames, declarationsOf, JS, scopeOf, walk} from "@openrewrite/rewrite/javascript";
|
|
8
|
+
import {emptySpace, isIdentifier, J, rightPadded, space, Type} from "@openrewrite/rewrite/java";
|
|
9
|
+
import {bindingNames, declarationsOf, JS, moduleExportName, scopeOf, walk} from "@openrewrite/rewrite/javascript";
|
|
10
10
|
import {CodeStyle, DEFAULT_CODE_STYLE, indented} from "../code-style";
|
|
11
11
|
import {bindingsOf, importModule, ModuleBindings, requiredModule, stringLiteral} from "../module-bindings";
|
|
12
12
|
|
|
@@ -92,7 +92,7 @@ export function hpagentBindings(cu: JS.CompilationUnit): HpagentBindings {
|
|
|
92
92
|
typeOnly.add(spec.specifier.simpleName);
|
|
93
93
|
} else if (spec.specifier.kind === JS.Kind.Alias && isIdentifier((spec.specifier as JS.Alias).alias)) {
|
|
94
94
|
const alias = spec.specifier as JS.Alias;
|
|
95
|
-
bindings.set((alias.alias as J.Identifier).simpleName, alias.propertyName.element
|
|
95
|
+
bindings.set((alias.alias as J.Identifier).simpleName, moduleExportName(alias.propertyName.element));
|
|
96
96
|
typeOnly.add((alias.alias as J.Identifier).simpleName);
|
|
97
97
|
}
|
|
98
98
|
}
|
|
@@ -495,7 +495,11 @@ export function builtinAgentType(local: string | undefined, specifier: string, p
|
|
|
495
495
|
}
|
|
496
496
|
const module = {
|
|
497
497
|
kind: JS.Kind.LiteralType, id: randomId(), prefix: emptySpace, markers: emptyMarkers,
|
|
498
|
-
literal: {
|
|
498
|
+
literal: {
|
|
499
|
+
kind: J.Kind.Literal, id: randomId(), prefix: emptySpace, markers: emptyMarkers, value: specifier,
|
|
500
|
+
valueSource: `'${specifier}'`, type: Type.Primitive.String
|
|
501
|
+
},
|
|
502
|
+
type: Type.Primitive.String
|
|
499
503
|
};
|
|
500
504
|
return {
|
|
501
505
|
kind: JS.Kind.ImportType, id: randomId(), prefix, markers: emptyMarkers,
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import {Cursor} from "@openrewrite/rewrite";
|
|
8
8
|
import {isIdentifier, J} from "@openrewrite/rewrite/java";
|
|
9
|
-
import {declarationsOf, JS, scopeOf} from "@openrewrite/rewrite/javascript";
|
|
9
|
+
import {declarationsOf, JS, moduleExportName, scopeOf} from "@openrewrite/rewrite/javascript";
|
|
10
10
|
|
|
11
11
|
// Resolution is syntactic rather than by type attribution: JavaScript without type declarations for
|
|
12
12
|
// a library (express-request-id 1.x, fs-extra without @types) attributes its calls to `<unknown>`.
|
|
@@ -58,7 +58,7 @@ export function bindingsOf(cu: JS.CompilationUnit, modules: ReadonlySet<string>)
|
|
|
58
58
|
} else if (spec.specifier.kind === JS.Kind.Alias) {
|
|
59
59
|
const alias = spec.specifier as JS.Alias;
|
|
60
60
|
if (isIdentifier(alias.alias)) {
|
|
61
|
-
bindings.set(alias.alias.simpleName, alias.propertyName.element
|
|
61
|
+
bindings.set(alias.alias.simpleName, moduleExportName(alias.propertyName.element));
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Moderne Proprietary. Only for use by Moderne customers under the terms of a commercial contract.
|
|
5
5
|
*/
|
|
6
6
|
import {check, emptyMarkers, ExecutionContext, produceAsync, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
7
|
-
import {JavaScriptVisitor, MethodMatcher, template, usesType} from "@openrewrite/rewrite/javascript";
|
|
7
|
+
import {JavaScriptVisitor, MethodMatcher, raw, template, usesType} from "@openrewrite/rewrite/javascript";
|
|
8
8
|
import {Expression, J, TextComment, Type} from "@openrewrite/rewrite/java";
|
|
9
9
|
|
|
10
10
|
|
|
@@ -40,15 +40,23 @@ export class AvoidImplicitCoercionOfExitCode extends Recipe {
|
|
|
40
40
|
return false;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
private
|
|
44
|
-
|
|
43
|
+
private isIntegerLiteral(expr: Expression): boolean {
|
|
44
|
+
if (expr.kind === J.Kind.Unary) {
|
|
45
|
+
const unary = expr as J.Unary;
|
|
46
|
+
const operator = unary.operator.element;
|
|
47
|
+
return (operator === J.Unary.Type.Negative || operator === J.Unary.Type.Positive) &&
|
|
48
|
+
this.isIntegerLiteral(unary.expression);
|
|
49
|
+
}
|
|
45
50
|
if (expr.kind === J.Kind.Literal) {
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
const value = (expr as J.Literal).value;
|
|
52
|
+
return typeof value === 'number' && Number.isInteger(value);
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private needsCoercion(expr: Expression): boolean {
|
|
58
|
+
if (this.isIntegerLiteral(expr)) {
|
|
59
|
+
return false;
|
|
52
60
|
}
|
|
53
61
|
|
|
54
62
|
const exprType = expr.type;
|
|
@@ -74,11 +82,8 @@ export class AvoidImplicitCoercionOfExitCode extends Recipe {
|
|
|
74
82
|
if (expr.kind === J.Kind.Literal) {
|
|
75
83
|
const literal = expr as J.Literal;
|
|
76
84
|
if (literal && Number.isInteger(Number(literal.value))) {
|
|
77
|
-
|
|
78
|
-
return (await
|
|
79
|
-
draft.value = numberValue;
|
|
80
|
-
draft.valueSource = (numberValue).toString();
|
|
81
|
-
}))!;
|
|
85
|
+
// A template makes the number as the parser does: a number literal, under a unary minus when negative
|
|
86
|
+
return (await template`${raw(Number(literal.value).toString())}`.apply(expr, this.cursor))! as Expression;
|
|
82
87
|
} else {
|
|
83
88
|
const justZero = await template`0`.apply(expr, this.cursor) as J.MethodInvocation;
|
|
84
89
|
return (await produceAsync(justZero, async draft => {
|
|
@@ -0,0 +1,72 @@
|
|
|
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, Recipe, TreeVisitor} from "@openrewrite/rewrite";
|
|
7
|
+
import {JavaScriptVisitor, JS, usesType} from "@openrewrite/rewrite/javascript";
|
|
8
|
+
import {J, Type} from "@openrewrite/rewrite/java";
|
|
9
|
+
|
|
10
|
+
/** The `EventEmitter` methods that take an event name as their first argument. */
|
|
11
|
+
const LISTENER_METHODS = [
|
|
12
|
+
"on", "once", "addListener", "prependListener", "prependOnceListener",
|
|
13
|
+
"off", "removeListener", "removeAllListeners",
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
const PROCESS_TYPE = "NodeJS.Process";
|
|
17
|
+
|
|
18
|
+
export class RemoveProcessMultipleResolves extends Recipe {
|
|
19
|
+
readonly name = "org.openrewrite.node.migrate.process.remove-multiple-resolves"
|
|
20
|
+
readonly displayName: string = "Remove `process.on('multipleResolves')` listeners";
|
|
21
|
+
readonly description: string = "The `'multipleResolves'` process event was removed in Node.js 25 (DEP0160) because it " +
|
|
22
|
+
"did not work with the V8 promise combinators, which left it of little use. Node.js offers no replacement and " +
|
|
23
|
+
"never emits the event, so registering a listener for it is dead code and the registration is removed. The " +
|
|
24
|
+
"handler itself is left in place, since it may be referenced elsewhere. Only a registration written as its own " +
|
|
25
|
+
"statement is removed; one whose result is used, such as a chained or assigned call, is left alone.";
|
|
26
|
+
readonly tags: string[] = ["DEP0160"];
|
|
27
|
+
|
|
28
|
+
async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
|
|
29
|
+
return check(usesType(PROCESS_TYPE), new class extends JavaScriptVisitor<ExecutionContext> {
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Whether the call registers (or removes) a listener on `process`. The receiver's type
|
|
33
|
+
* is what is checked, not the method's declaring type: `process` overrides `on()` and
|
|
34
|
+
* `once()` but inherits `off()`, `removeListener()` and `removeAllListeners()` from
|
|
35
|
+
* `EventEmitter`, which every other emitter declares too.
|
|
36
|
+
*/
|
|
37
|
+
private isProcessListenerCall(m: J.MethodInvocation): boolean {
|
|
38
|
+
if (!LISTENER_METHODS.includes(m.name.simpleName)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
const receiver = m.select?.element.type;
|
|
42
|
+
return receiver?.kind === Type.Kind.Class
|
|
43
|
+
&& (receiver as Type.Class).fullyQualifiedName === PROCESS_TYPE;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Whether the first argument is the string `'multipleResolves'`. */
|
|
47
|
+
private isMultipleResolves(m: J.MethodInvocation): boolean {
|
|
48
|
+
const first = m.arguments.elements[0]?.element;
|
|
49
|
+
return first?.kind === J.Kind.Literal && (first as J.Literal).value === "multipleResolves";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Whether the call stands alone as a statement, so dropping it cannot strand an
|
|
54
|
+
* expression. A chained `process.on(..).on(..)` or an assigned result does not.
|
|
55
|
+
*/
|
|
56
|
+
private isOwnStatement(): boolean {
|
|
57
|
+
const parent = this.cursor.parentTree()?.value as J | undefined;
|
|
58
|
+
return parent?.kind === J.Kind.Block || parent?.kind === JS.Kind.CompilationUnit;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
protected async visitMethodInvocation(method: J.MethodInvocation, p: ExecutionContext): Promise<J | undefined> {
|
|
62
|
+
const m = await super.visitMethodInvocation(method, p) as J.MethodInvocation;
|
|
63
|
+
|
|
64
|
+
if (this.isProcessListenerCall(m) && this.isMultipleResolves(m) && this.isOwnStatement()) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return m;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|