@openrewrite/rewrite 8.111.0 → 8.112.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.
@@ -0,0 +1,219 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MSBuildPackageSourceKind = exports.MSBuildPropertyValueKind = exports.MSBuildProjectReferenceKind = exports.MSBuildResolvedPackageKind = exports.MSBuildPackageReferenceKind = exports.MSBuildTargetFrameworkKind = exports.MSBuildProjectKind = void 0;
4
+ /*
5
+ * Copyright 2026 the original author or authors.
6
+ * <p>
7
+ * Licensed under the Moderne Source Available License (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ * <p>
11
+ * https://docs.moderne.io/licensing/moderne-source-available-license
12
+ * <p>
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ const mutative_1 = require("mutative");
20
+ const reference_1 = require("../reference");
21
+ const util_1 = require("../util");
22
+ const queue_1 = require("./queue");
23
+ /*
24
+ * The Java `org.openrewrite.csharp.marker.MSBuildProject` marker, which the Java host attaches to
25
+ * the Xml document of each .csproj file. A JavaScript recipe run over a repository that also holds
26
+ * .NET projects receives it on those files, and the Java codec streams it property by property, so
27
+ * this side needs a codec for it and every nested type to read exactly the messages Java sends. The
28
+ * field order in each codec is the wire contract and mirrors `rpcSend`/`rpcReceive` in the Java class.
29
+ */
30
+ exports.MSBuildProjectKind = "org.openrewrite.csharp.marker.MSBuildProject";
31
+ exports.MSBuildTargetFrameworkKind = "org.openrewrite.csharp.marker.MSBuildProject$TargetFramework";
32
+ exports.MSBuildPackageReferenceKind = "org.openrewrite.csharp.marker.MSBuildProject$PackageReference";
33
+ exports.MSBuildResolvedPackageKind = "org.openrewrite.csharp.marker.MSBuildProject$ResolvedPackage";
34
+ exports.MSBuildProjectReferenceKind = "org.openrewrite.csharp.marker.MSBuildProject$ProjectReference";
35
+ exports.MSBuildPropertyValueKind = "org.openrewrite.csharp.marker.MSBuildProject$PropertyValue";
36
+ exports.MSBuildPackageSourceKind = "org.openrewrite.csharp.marker.MSBuildProject$PackageSource";
37
+ const resolvedId = (rp) => `${rp.name}@${rp.resolvedVersion}`;
38
+ /**
39
+ * Java sends a map as two parallel lists, its keys and then its values. This zips them back, keeping
40
+ * the map it had before when neither list changed.
41
+ */
42
+ function zip(keys, values, before) {
43
+ const entries = [];
44
+ if (keys && values) {
45
+ for (let i = 0; i < keys.length && i < values.length; i++) {
46
+ entries.push([keys[i], values[i]]);
47
+ }
48
+ }
49
+ if (before) {
50
+ const beforeEntries = Object.entries(before);
51
+ if (beforeEntries.length === entries.length &&
52
+ beforeEntries.every(([k, v], i) => entries[i][0] === k && entries[i][1] === v)) {
53
+ return before;
54
+ }
55
+ }
56
+ // fromEntries defines own properties, so a key like `__proto__` stays an entry
57
+ return Object.fromEntries(entries);
58
+ }
59
+ function receiveStringList(q, before) {
60
+ return q.receiveListDefined(before, s => q.receive(s));
61
+ }
62
+ function sendStringList(q, after, values) {
63
+ q.getAndSendList(after, values, s => s, s => q.getAndSend(s, x => x));
64
+ }
65
+ queue_1.RpcCodecs.registerCodec(exports.MSBuildPropertyValueKind, {
66
+ rpcReceive(before, q) {
67
+ return (0, util_1.updateIfChanged)(before, {
68
+ value: q.receive(before.value),
69
+ definedIn: q.receive(before.definedIn),
70
+ });
71
+ },
72
+ rpcSend(after, q) {
73
+ q.getAndSend(after, a => a.value);
74
+ q.getAndSend(after, a => a.definedIn);
75
+ }
76
+ });
77
+ queue_1.RpcCodecs.registerCodec(exports.MSBuildPackageSourceKind, {
78
+ rpcReceive(before, q) {
79
+ return (0, util_1.updateIfChanged)(before, {
80
+ key: q.receive(before.key),
81
+ url: q.receive(before.url),
82
+ });
83
+ },
84
+ rpcSend(after, q) {
85
+ q.getAndSend(after, a => a.key);
86
+ q.getAndSend(after, a => a.url);
87
+ }
88
+ });
89
+ queue_1.RpcCodecs.registerCodec(exports.MSBuildPackageReferenceKind, {
90
+ rpcReceive(before, q) {
91
+ return (0, util_1.updateIfChanged)(before, {
92
+ include: q.receive(before.include),
93
+ requestedVersion: q.receive(before.requestedVersion),
94
+ resolvedVersion: q.receive(before.resolvedVersion),
95
+ });
96
+ },
97
+ rpcSend(after, q) {
98
+ q.getAndSend(after, a => a.include);
99
+ q.getAndSend(after, a => a.requestedVersion);
100
+ q.getAndSend(after, a => a.resolvedVersion);
101
+ }
102
+ });
103
+ queue_1.RpcCodecs.registerCodec(exports.MSBuildResolvedPackageKind, {
104
+ rpcReceive(before, q) {
105
+ var _a;
106
+ // Populated in place: Java sends resolved packages only by ref, so the receive queue
107
+ // registered this instance for its ref before calling the codec, and a back-reference
108
+ // in the dependency graph resolves to the finished object.
109
+ const rp = (0, mutative_1.castDraft)(before);
110
+ rp.name = q.receive(before.name);
111
+ rp.resolvedVersion = q.receive(before.resolvedVersion);
112
+ rp.dependencies = (0, mutative_1.castDraft)(q.receiveListDefined(before.dependencies));
113
+ rp.depth = q.receive(before.depth);
114
+ rp.type = q.receive(before.type);
115
+ rp.compileAssemblies = receiveStringList(q, before.compileAssemblies);
116
+ rp.runtimeAssemblies = receiveStringList(q, before.runtimeAssemblies);
117
+ rp.frameworkAssemblies = receiveStringList(q, before.frameworkAssemblies);
118
+ rp.buildFiles = receiveStringList(q, before.buildFiles);
119
+ rp.buildMultiTargetingFiles = receiveStringList(q, before.buildMultiTargetingFiles);
120
+ rp.contentFiles = receiveStringList(q, before.contentFiles);
121
+ rp.runtimeTargets = receiveStringList(q, before.runtimeTargets);
122
+ rp.resourceAssemblies = receiveStringList(q, before.resourceAssemblies);
123
+ rp.analyzerAssemblies = receiveStringList(q, before.analyzerAssemblies);
124
+ rp.hasInstallScripts = q.receive(before.hasInstallScripts);
125
+ rp.hasXdtTransforms = q.receive(before.hasXdtTransforms);
126
+ rp.hasLegacyContentFolder = q.receive(before.hasLegacyContentFolder);
127
+ const beforeRanges = (_a = before.dependencyRanges) !== null && _a !== void 0 ? _a : {};
128
+ const rangeKeys = receiveStringList(q, Object.keys(beforeRanges));
129
+ const rangeValues = receiveStringList(q, Object.values(beforeRanges));
130
+ rp.dependencyRanges = zip(rangeKeys, rangeValues, before.dependencyRanges);
131
+ return before;
132
+ },
133
+ rpcSend(after, q) {
134
+ q.getAndSend(after, a => a.name);
135
+ q.getAndSend(after, a => a.resolvedVersion);
136
+ q.getAndSendList(after, a => { var _a; return (_a = a.dependencies) === null || _a === void 0 ? void 0 : _a.map(d => (0, reference_1.asRef)(d)); }, resolvedId);
137
+ q.getAndSend(after, a => a.depth);
138
+ q.getAndSend(after, a => a.type);
139
+ sendStringList(q, after, a => a.compileAssemblies);
140
+ sendStringList(q, after, a => a.runtimeAssemblies);
141
+ sendStringList(q, after, a => a.frameworkAssemblies);
142
+ sendStringList(q, after, a => a.buildFiles);
143
+ sendStringList(q, after, a => a.buildMultiTargetingFiles);
144
+ sendStringList(q, after, a => a.contentFiles);
145
+ sendStringList(q, after, a => a.runtimeTargets);
146
+ sendStringList(q, after, a => a.resourceAssemblies);
147
+ sendStringList(q, after, a => a.analyzerAssemblies);
148
+ q.getAndSend(after, a => a.hasInstallScripts);
149
+ q.getAndSend(after, a => a.hasXdtTransforms);
150
+ q.getAndSend(after, a => a.hasLegacyContentFolder);
151
+ // Java sends an absent map as two empty lists
152
+ sendStringList(q, after, a => { var _a; return Object.keys((_a = a.dependencyRanges) !== null && _a !== void 0 ? _a : {}); });
153
+ sendStringList(q, after, a => { var _a; return Object.values((_a = a.dependencyRanges) !== null && _a !== void 0 ? _a : {}); });
154
+ }
155
+ });
156
+ queue_1.RpcCodecs.registerCodec(exports.MSBuildProjectReferenceKind, {
157
+ rpcReceive(before, q) {
158
+ // Populated in place for the same reason as a resolved package: Java sends project
159
+ // references only by ref, and the project graph may close back onto this instance.
160
+ const pr = (0, mutative_1.castDraft)(before);
161
+ pr.include = q.receive(before.include);
162
+ pr.name = q.receive(before.name);
163
+ pr.depth = q.receive(before.depth);
164
+ pr.projectReferences = (0, mutative_1.castDraft)(q.receiveListDefined(before.projectReferences));
165
+ pr.resolvedPackages = (0, mutative_1.castDraft)(q.receiveListDefined(before.resolvedPackages));
166
+ return before;
167
+ },
168
+ rpcSend(after, q) {
169
+ q.getAndSend(after, a => a.include);
170
+ q.getAndSend(after, a => a.name);
171
+ q.getAndSend(after, a => a.depth);
172
+ q.getAndSendList(after, a => { var _a; return (_a = a.projectReferences) === null || _a === void 0 ? void 0 : _a.map(p => (0, reference_1.asRef)(p)); }, p => p.include);
173
+ q.getAndSendList(after, a => { var _a; return (_a = a.resolvedPackages) === null || _a === void 0 ? void 0 : _a.map(r => (0, reference_1.asRef)(r)); }, resolvedId);
174
+ }
175
+ });
176
+ queue_1.RpcCodecs.registerCodec(exports.MSBuildTargetFrameworkKind, {
177
+ rpcReceive(before, q) {
178
+ return (0, util_1.updateIfChanged)(before, {
179
+ targetFramework: q.receive(before.targetFramework),
180
+ packageReferences: q.receiveListDefined(before.packageReferences),
181
+ resolvedPackages: q.receiveListDefined(before.resolvedPackages),
182
+ projectReferences: q.receiveListDefined(before.projectReferences),
183
+ });
184
+ },
185
+ rpcSend(after, q) {
186
+ q.getAndSend(after, a => a.targetFramework);
187
+ q.getAndSendList(after, a => a.packageReferences, pr => pr.include);
188
+ // Java's getAndSendListAsRef: each element travels once, later occurrences as its ref
189
+ q.getAndSendList(after, a => { var _a; return (_a = a.resolvedPackages) === null || _a === void 0 ? void 0 : _a.map(r => (0, reference_1.asRef)(r)); }, resolvedId);
190
+ q.getAndSendList(after, a => { var _a; return (_a = a.projectReferences) === null || _a === void 0 ? void 0 : _a.map(p => (0, reference_1.asRef)(p)); }, p => p.include);
191
+ }
192
+ });
193
+ queue_1.RpcCodecs.registerCodec(exports.MSBuildProjectKind, {
194
+ rpcReceive(before, q) {
195
+ var _a;
196
+ const id = q.receive(before.id);
197
+ const sdk = q.receive(before.sdk);
198
+ const beforeProperties = (_a = before.properties) !== null && _a !== void 0 ? _a : {};
199
+ const keys = q.receiveList(Object.keys(beforeProperties), k => q.receive(k));
200
+ const values = q.receiveList(Object.values(beforeProperties));
201
+ return (0, util_1.updateIfChanged)(before, {
202
+ id,
203
+ sdk,
204
+ properties: zip(keys, values, before.properties),
205
+ packageSources: q.receiveListDefined(before.packageSources),
206
+ targetFrameworks: q.receiveListDefined(before.targetFrameworks),
207
+ });
208
+ },
209
+ rpcSend(after, q) {
210
+ q.getAndSend(after, a => a.id);
211
+ q.getAndSend(after, a => a.sdk);
212
+ // The properties map travels as parallel lists: keys, then values through their codec
213
+ q.getAndSendList(after, a => { var _a; return Object.keys((_a = a.properties) !== null && _a !== void 0 ? _a : {}); }, k => k, k => q.getAndSend(k, x => x));
214
+ q.getAndSendList(after, a => { var _a; return Object.values((_a = a.properties) !== null && _a !== void 0 ? _a : {}); }, v => v.value);
215
+ q.getAndSendList(after, a => a.packageSources, ps => ps.key);
216
+ q.getAndSendList(after, a => a.targetFrameworks, tf => tf.targetFramework);
217
+ }
218
+ });
219
+ //# sourceMappingURL=msbuild-project.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"msbuild-project.js","sourceRoot":"","sources":["../../src/rpc/msbuild-project.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;GAcG;AACH,uCAAmC;AAEnC,4CAAmC;AACnC,kCAAwC;AAExC,mCAAiE;AAEjE;;;;;;GAMG;AAEU,QAAA,kBAAkB,GAAG,8CAAuD,CAAC;AAC7E,QAAA,0BAA0B,GAAG,8DAAuE,CAAC;AACrG,QAAA,2BAA2B,GAAG,+DAAwE,CAAC;AACvG,QAAA,0BAA0B,GAAG,8DAAuE,CAAC;AACrG,QAAA,2BAA2B,GAAG,+DAAwE,CAAC;AACvG,QAAA,wBAAwB,GAAG,4DAAqE,CAAC;AACjG,QAAA,wBAAwB,GA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CAAC,EAAE,CAAC,EAAE,CAAC,eAAe,CAAC,CAAC;IAC/E,CAAC;CACJ,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name" : "@openrewrite/rewrite",
3
- "version" : "8.111.0",
3
+ "version" : "8.112.0",
4
4
  "license" : "Moderne Source Available License",
5
5
  "description" : "OpenRewrite JavaScript.",
6
6
  "repository" : {
@@ -95,7 +95,7 @@
95
95
  "vitest" : "^4.1.8"
96
96
  },
97
97
  "peerDependencies" : {
98
- "vitest" : "^4.0.0"
98
+ "vitest" : "^4.0.0 || ^5.0.0"
99
99
  },
100
100
  "peerDependenciesMeta" : {
101
101
  "vitest" : {
@@ -2,6 +2,7 @@ import {JavaScriptVisitor} from "./visitor";
2
2
  import {Comment, ElementRemovalFormatter, emptySpace, isIdentifier, J, rightPadded, singleSpace, space, spaceContainsNewline, splitAtLineEnd, Statement, TextComment, TrailingComma, Type} from "../java";
3
3
  import {isObjectBindingPattern, JS, JSX, moduleExportName} from "./tree";
4
4
  import {randomId, UUID} from "../uuid";
5
+ import {Cursor} from "../tree";
5
6
  import {TypeVisitor} from "../java/type-visitor";
6
7
  import {mapAsync, updateIfChanged} from "../util";
7
8
  import {moduleName, packageNameOf, sameModule, spelledLike} from "./package-name";
@@ -2623,6 +2624,44 @@ function withRunSorted(cu: JS.CompilationUnit, ids: ReadonlySet<UUID>): JS.Compi
2623
2624
  return {...cu, statements};
2624
2625
  }
2625
2626
 
2627
+ /** For `(NS)` the cursor steps inside, since `resolve` wants the cursor of the node holding `NS`. */
2628
+ export function readsModuleObject(qualifier: J | undefined, holder: Cursor, moduleObjects: ReadonlySet<string>): boolean {
2629
+ let object = qualifier, cursor = holder;
2630
+ while (object?.kind === J.Kind.Parentheses) {
2631
+ cursor = new Cursor(object, cursor);
2632
+ object = (object as J.Parentheses<J>).tree.element;
2633
+ }
2634
+ return object !== undefined && isIdentifier(object) && moduleObjects.has(object.simpleName) &&
2635
+ resolve(cursor, object)?.kind === JS.Kind.CompilationUnit;
2636
+ }
2637
+
2638
+ export function isModuleObjectRead(cursor: Cursor, moduleObjects: ReadonlySet<string>): boolean {
2639
+ if (moduleObjects.size === 0) {
2640
+ return false;
2641
+ }
2642
+ const node = cursor.value as J;
2643
+ switch (node.kind) {
2644
+ case J.Kind.MethodInvocation:
2645
+ return readsModuleObject((node as J.MethodInvocation).select?.element, cursor, moduleObjects);
2646
+ case J.Kind.FieldAccess:
2647
+ return readsModuleObject((node as J.FieldAccess).target, cursor, moduleObjects);
2648
+ case J.Kind.NewClass: {
2649
+ const clazz = (node as J.NewClass).class;
2650
+ return clazz?.kind === J.Kind.FieldAccess && isModuleObjectRead(new Cursor(clazz, cursor), moduleObjects);
2651
+ }
2652
+ case J.Kind.Identifier: {
2653
+ const holder = cursor.parentTree();
2654
+ const read = holder?.value as J | undefined;
2655
+ return holder !== undefined &&
2656
+ (read?.kind === J.Kind.MethodInvocation && (read as J.MethodInvocation).name === node ||
2657
+ read?.kind === J.Kind.FieldAccess && (read as J.FieldAccess).name.element === node) &&
2658
+ isModuleObjectRead(holder, moduleObjects);
2659
+ }
2660
+ default:
2661
+ return false;
2662
+ }
2663
+ }
2664
+
2626
2665
  /**
2627
2666
  * Moves the binding `from` names to `to`, binding it under `boundName`, and the file's references
2628
2667
  * follow a `boundName` of its own. Whether the statement is rewritten in place or replaced is decided
@@ -2637,7 +2676,8 @@ export class RebindImport<P> extends JavaScriptVisitor<P> {
2637
2676
  readonly to: {module: string; member?: string},
2638
2677
  readonly localName: string,
2639
2678
  readonly boundName: string,
2640
- readonly group?: RebindGroup
2679
+ readonly group?: RebindGroup,
2680
+ readonly keptModuleObjects: ReadonlySet<string> = new Set()
2641
2681
  ) {
2642
2682
  super();
2643
2683
  }
@@ -2793,7 +2833,8 @@ export class RebindImport<P> extends JavaScriptVisitor<P> {
2793
2833
 
2794
2834
  /** Any type in the file can name what moved, whether or not it references the binding. */
2795
2835
  protected override async visitType(javaType: Type | undefined, p: P): Promise<Type | undefined> {
2796
- return this.movedTypes.visit(javaType, undefined);
2836
+ return isModuleObjectRead(this.cursor, this.keptModuleObjects) ? javaType :
2837
+ this.movedTypes.visit(javaType, undefined);
2797
2838
  }
2798
2839
 
2799
2840
  /**
@@ -3129,12 +3170,13 @@ function movedTypesIn(
3129
3170
  export async function withMovedTypes(
3130
3171
  cu: JS.CompilationUnit,
3131
3172
  from: {module: string; member?: string},
3132
- to: {module: string; member?: string}
3173
+ to: {module: string; member?: string},
3174
+ keptModuleObjects: ReadonlySet<string> = new Set()
3133
3175
  ): Promise<JS.CompilationUnit> {
3134
3176
  const movedTypes = movedTypesIn(cu, from, to, false);
3135
3177
  return await new class extends JavaScriptVisitor<undefined> {
3136
3178
  protected override async visitType(javaType: Type | undefined): Promise<Type | undefined> {
3137
- return movedTypes.visit(javaType, undefined);
3179
+ return isModuleObjectRead(this.cursor, keptModuleObjects) ? javaType : movedTypes.visit(javaType, undefined);
3138
3180
  }
3139
3181
  }().visit(cu, undefined) as JS.CompilationUnit;
3140
3182
  }
@@ -427,17 +427,31 @@ export function maybeRebind(visitor: JavaScriptVisitor<any>, options: MaybeRebin
427
427
  new RebindGroup(statement.id, statement as JS.Import);
428
428
  group.add(existing.localName, boundName, options.to, existing.typeOnly);
429
429
  }
430
- visitor.afterVisit.push(new RebindImport(options.from, options.to, existing.localName, boundName, group));
430
+ visitor.afterVisit.push(new RebindImport(options.from, options.to, existing.localName, boundName, group,
431
+ keptModuleObjects(cu, options)));
431
432
  }
432
433
  queueRenameMemberSpellings(visitor, cu, options);
433
434
  return boundName;
434
435
  }
435
436
 
437
+ function keptModuleObjects(cu: JS.CompilationUnit, options: MaybeRebindOptions): ReadonlySet<string> {
438
+ return bindingShape(options.from.member) !== "named" || sameModule(options.from.module, options.to.module) ?
439
+ new Set() : moduleObjectsOf(cu, options.from.module, false);
440
+ }
441
+
442
+ function moduleObjectsOf(cu: JS.CompilationUnit, module: string, neverAssigned: boolean): Set<string> {
443
+ const root = new Cursor(cu);
444
+ return new Set(moduleObjectBindings(cu)
445
+ .filter(b => b.shape !== "default" && sameModule(b.module, module) &&
446
+ !(neverAssigned && b.reassignable && referencesOf(root, b.name).some(r => r.writes)))
447
+ .map(b => b.name));
448
+ }
449
+
436
450
  /**
437
451
  * Queues one {@link RenameMemberSpellings} per move of a named member, after any `RebindImport` of the move.
438
452
  * Skips `import D from "m"`: `D.Old` reads the default export, which is the module only with `export =`.
439
- * Skips a `let` or `var` `NS` of the module that the file assigns to: after `NS = other`, `NS.Old` may not
440
- * read the module. Only those are looked for, since finding an assignment reads the whole file.
453
+ * Within one module, skips a `let` or `var` `NS` of the module that the file assigns to: after `NS = other`,
454
+ * `NS.Old` may not read the module. Only those are looked for, since finding an assignment reads the whole file.
441
455
  */
442
456
  function queueRenameMemberSpellings(
443
457
  visitor: JavaScriptVisitor<any>,
@@ -451,13 +465,9 @@ function queueRenameMemberSpellings(
451
465
  sameModule(v.from.module, options.from.module) && v.to.member === to && sameModule(v.to.module, options.to.module))) {
452
466
  return;
453
467
  }
454
- const root = new Cursor(cu);
455
- const moduleObjects = new Set(moduleObjectBindings(cu)
456
- .filter(b => b.shape !== "default" && sameModule(b.module, options.from.module) &&
457
- !(b.reassignable && referencesOf(root, b.name).some(r => r.writes)))
458
- .map(b => b.name));
459
- visitor.afterVisit.push(new RenameMemberSpellings(
460
- {module: options.from.module, member: from}, {module: options.to.module, member: to}, moduleObjects));
468
+ visitor.afterVisit.push(new RenameMemberSpellings({module: options.from.module, member: from},
469
+ {module: options.to.module, member: to},
470
+ moduleObjectsOf(cu, options.from.module, sameModule(options.from.module, options.to.module))));
461
471
  }
462
472
 
463
473
  /**
@@ -16,12 +16,12 @@
16
16
  import {create as produce, Draft} from "mutative";
17
17
  import {ElementRemovalFormatter, emptySpace, Expression, isIdentifier, isStringLiteral, J, singleSpace, splitAtLineEnd, Statement} from "../java";
18
18
  import {randomId, UUID} from "../uuid";
19
- import {Cursor} from "../tree";
20
19
  import {isObjectBindingPattern, JS, moduleExportName} from "./tree";
21
20
  import {JavaScriptVisitor} from "./visitor";
22
- import {resolve, rootName} from "./scope";
21
+ import {rootName} from "./scope";
23
22
  import {
24
- aliasing, moduleNameOf, movedBinding, newIdentifier, respecify, sameAttributes, withMovedTypes, withoutStatement
23
+ aliasing, moduleNameOf, movedBinding, newIdentifier, readsModuleObject, respecify, sameAttributes, withMovedTypes,
24
+ withoutStatement
25
25
  } from "./add-import";
26
26
  import {sameModule, spelledLike} from "./package-name";
27
27
 
@@ -59,26 +59,18 @@ export class RenameMemberSpellings<P> extends JavaScriptVisitor<P> {
59
59
  override async visitJsCompilationUnit(cu: JS.CompilationUnit, p: P): Promise<J | undefined> {
60
60
  const visited = await super.visitJsCompilationUnit(cu, p) as JS.CompilationUnit;
61
61
  const moved = this.withinModule ? visited : this.withReExportsMoved(visited);
62
- return moved === cu ? moved : withMovedTypes(moved, this.from, this.to);
62
+ return moved === cu ? moved :
63
+ withMovedTypes(moved, this.from, this.to, this.withinModule ? new Set() : this.moduleObjects);
63
64
  }
64
65
 
65
66
  private get withinModule(): boolean {
66
67
  return sameModule(this.from.module, this.to.module);
67
68
  }
68
69
 
69
- /**
70
- * Whether `qualifier.member` reads the moved member through a module object: `NS.Old`, `(NS).Old`.
71
- * For `(NS)` the cursor steps inside, since `resolve` wants the cursor of the node holding `NS`.
72
- */
70
+ /** Whether `qualifier.member` reads the moved member through a module object: `NS.Old`, `(NS).Old`. */
73
71
  private readsMovedMember(qualifier: J | undefined, member: string): boolean {
74
- let object = qualifier, cursor = this.cursor;
75
- while (object?.kind === J.Kind.Parentheses) {
76
- cursor = new Cursor(object, cursor);
77
- object = (object as J.Parentheses<J>).tree.element;
78
- }
79
72
  return this.withinModule && member === this.from.member &&
80
- object !== undefined && isIdentifier(object) && this.moduleObjects.has(object.simpleName) &&
81
- resolve(cursor, object)?.kind === JS.Kind.CompilationUnit;
73
+ readsModuleObject(qualifier, this.cursor, this.moduleObjects);
82
74
  }
83
75
 
84
76
  /** `NS.Old` → `NS.New` */
@@ -873,7 +873,7 @@ export class JavaScriptTypeMapping {
873
873
  * as TypeScript names the module of an export. A function declared in the file is declared on it.
874
874
  */
875
875
  private sourceModuleType(sourceFile: ts.SourceFile): Type.FullyQualified {
876
- return this.moduleType(this.sourceModuleName(sourceFile));
876
+ return moduleClass(this.sourceModuleName(sourceFile));
877
877
  }
878
878
 
879
879
  private sourceModuleName(sourceFile: ts.SourceFile): string {
@@ -913,7 +913,7 @@ export class JavaScriptTypeMapping {
913
913
  private functionOwner(declaration: ts.FunctionDeclaration): Type.FullyQualified {
914
914
  const namespace = this.enclosingNamespace(declaration);
915
915
  return namespace ?
916
- this.moduleType(this.namespaceName(namespace)) :
916
+ moduleClass(this.namespaceName(namespace)) :
917
917
  this.sourceModuleType(declaration.getSourceFile());
918
918
  }
919
919
 
@@ -930,14 +930,6 @@ export class JavaScriptTypeMapping {
930
930
  !!(ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Static);
931
931
  }
932
932
 
933
- private moduleType(module: string): Type.FullyQualified {
934
- return {
935
- kind: Type.Kind.Class,
936
- flags: 0, // TODO - determine flags
937
- fullyQualifiedName: module
938
- } as Type.FullyQualified;
939
- }
940
-
941
933
  /**
942
934
  * Extract the npm module name from a file path.
943
935
  * Handles various package manager layouts:
@@ -1116,7 +1108,7 @@ export class JavaScriptTypeMapping {
1116
1108
  // Calling a module itself, as in `require('m')()`, calls its default export.
1117
1109
  const calledModule = ts.isCallExpression(node) ? this.loadedModule(node.expression) : undefined;
1118
1110
  if (calledModule) {
1119
- return this.createMethodType(signature, node, this.moduleType(calledModule), '<default>');
1111
+ return this.createMethodType(signature, node, moduleClass(calledModule), '<default>');
1120
1112
  }
1121
1113
 
1122
1114
  let symbol = this.checker.getSymbolAtLocation(node.expression);
@@ -1145,11 +1137,7 @@ export class JavaScriptTypeMapping {
1145
1137
  const moduleName = this.moduleOfExpression(node.expression.expression);
1146
1138
 
1147
1139
  // Use the inferred type or fall back to unknown
1148
- declaringType = moduleName ? {
1149
- kind: Type.Kind.Class,
1150
- flags: 0, // TODO - determine flags
1151
- fullyQualifiedName: moduleName
1152
- } as Type.FullyQualified : Type.unknownType as Type.FullyQualified;
1140
+ declaringType = moduleName ? moduleClass(moduleName) : Type.unknownType as Type.FullyQualified;
1153
1141
 
1154
1142
  // Create the method type using the helper
1155
1143
  return this.createMethodType(signature, node, declaringType, methodName);
@@ -1181,18 +1169,21 @@ export class JavaScriptTypeMapping {
1181
1169
  // A namespace import of an ES module is typed as its anonymous namespace object, so the
1182
1170
  // module is what declares its members, and its `default` is the default export. One
1183
1171
  // declared as a TypeScript namespace (`export = React`) keeps that namespace's name.
1172
+ // An installed module's object is named after its package, so where the receiver is an
1173
+ // import's alias, which resolves to the module its specifier names, the specifier wins.
1184
1174
  const receiverBinding = this.bindingOf(node.expression.expression);
1185
1175
  const anonymous = !mappedType || mappedType.kind !== Type.Kind.Class ||
1186
- (mappedType as Type.Class).fullyQualifiedName.startsWith('{');
1176
+ (mappedType as Type.Class).fullyQualifiedName.startsWith('{') ||
1177
+ (isImport && this.isInstalledModuleObject(exprType));
1187
1178
 
1188
1179
  const declaringClass = this.declaringClassOf(symbol);
1189
1180
  if (declaringClass) {
1190
1181
  declaringType = declaringClass;
1191
1182
  } else if (declaringNamespace) {
1192
1183
  // A callable module's receiver is typed as its function, which names no declaration
1193
- declaringType = this.moduleType(this.namespaceName(declaringNamespace));
1184
+ declaringType = moduleClass(this.namespaceName(declaringNamespace));
1194
1185
  } else if (receiverBinding?.kind === 'namespace' && anonymous) {
1195
- declaringType = this.moduleType(receiverBinding.module);
1186
+ declaringType = moduleClass(receiverBinding.module);
1196
1187
  if (methodName === 'default') {
1197
1188
  methodName = '<default>';
1198
1189
  }
@@ -1213,11 +1204,7 @@ export class JavaScriptTypeMapping {
1213
1204
  // A `require()` result, and an import of a package without type declarations, is typed
1214
1205
  // `any`, so nothing above identifies the module; the specifier in the call or import does.
1215
1206
  if (declaringType === Type.unknownType && receiverBinding && receiverBinding.kind !== 'named') {
1216
- declaringType = {
1217
- kind: Type.Kind.Class,
1218
- flags: 0, // TODO - determine flags
1219
- fullyQualifiedName: receiverBinding.module
1220
- } as Type.FullyQualified;
1207
+ declaringType = moduleClass(receiverBinding.module);
1221
1208
  }
1222
1209
 
1223
1210
  // For string methods like 'hello'.split(), ensure we have a proper declaring type for primitives
@@ -1272,11 +1259,7 @@ export class JavaScriptTypeMapping {
1272
1259
  methodName = declared.name ? declared.name.text : "<anonymous>";
1273
1260
  } else if (moduleSpecifier) {
1274
1261
  // This is an imported function - use the module specifier as declaring type
1275
- declaringType = {
1276
- kind: Type.Kind.Class,
1277
- flags: 0, // TODO - determine flags
1278
- fullyQualifiedName: moduleSpecifier
1279
- } as Type.FullyQualified;
1262
+ declaringType = moduleClass(moduleSpecifier);
1280
1263
  // Calling a default export or a whole module calls the default export, whose aliased symbol
1281
1264
  // carries its internal name (e.g. `e` for express), so represent it as `<default>`. Named
1282
1265
  // bindings keep the exported name.
@@ -1292,7 +1275,7 @@ export class JavaScriptTypeMapping {
1292
1275
  }
1293
1276
  } else if (declaringNamespace) {
1294
1277
  // A local holding a namespace's function, as `const utc = moment.utc`, calls that function
1295
- declaringType = this.moduleType(this.namespaceName(declaringNamespace));
1278
+ declaringType = moduleClass(this.namespaceName(declaringNamespace));
1296
1279
  methodName = ts.getNameOfDeclaration(signature.getDeclaration())?.getText() ?? methodName;
1297
1280
  } else {
1298
1281
  // Fall back to the original logic for non-imported functions
@@ -1305,11 +1288,7 @@ export class JavaScriptTypeMapping {
1305
1288
 
1306
1289
  if (lastDot > 0) {
1307
1290
  // For functions from modules, use the module part as declaring type
1308
- declaringType = {
1309
- kind: Type.Kind.Class,
1310
- flags: 0, // TODO - determine flags
1311
- fullyQualifiedName: fqn.substring(0, lastDot)
1312
- } as Type.FullyQualified;
1291
+ declaringType = moduleClass(fqn.substring(0, lastDot));
1313
1292
  } else {
1314
1293
  // No dots in the name - the type IS the module itself
1315
1294
  declaringType = funcType as Type.FullyQualified;
@@ -1560,6 +1539,10 @@ export class JavaScriptTypeMapping {
1560
1539
  this.sourceModuleName(sourceFile) : undefined;
1561
1540
  }
1562
1541
 
1542
+ private isInstalledModuleObject(type: ts.Type): boolean {
1543
+ return !!type.getSymbol()?.declarations?.some(d => ts.isSourceFile(d) && this.isInstalled(d.fileName));
1544
+ }
1545
+
1563
1546
  /** Whether a path is in a package installed under the source, where a package's own files are not. */
1564
1547
  private isInstalled(fileName: string): boolean {
1565
1548
  return (this.sourceIsPackage ? fileName.substring(this.sourceRoot!.length) : fileName).includes('node_modules/');
package/src/rpc/index.ts CHANGED
@@ -38,6 +38,8 @@ export {DelegatingRecipe, prepareJavaRecipe} from "./java-recipe";
38
38
  export {findTestClasspath, JavaRpcTestServer} from "./java-rpc-client";
39
39
  export type {JavaRpcOptions} from "./java-rpc-client";
40
40
  export {registerVisitor} from "./request/visitor-registry";
41
+ // Registers the codecs for the Java C# marker, which a Java host sends on .csproj files
42
+ export * from "./msbuild-project";
41
43
 
42
44
  RpcCodecs.registerCodec(TreeKind.Checksum, {
43
45
  rpcReceive(before: Checksum, q: RpcReceiveQueue): Checksum {