@openrewrite/rewrite 8.107.0 → 8.107.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.
@@ -2496,6 +2496,7 @@ export class JavaScriptSemanticComparatorVisitor extends JavaScriptComparatorVis
2496
2496
  * 1. Type checking for field access
2497
2497
  * 2. Semantic equivalence between `undefined` identifier and void expressions
2498
2498
  * 3. Names that differ but denote one declaration, as an import alias and its export do
2499
+ * 4. An import read through its module, as `os.EOL` is for `EOL`
2499
2500
  */
2500
2501
  override async visitIdentifier(identifier: J.Identifier, other: J): Promise<J | undefined> {
2501
2502
  // Check if this identifier is "undefined" and the other is a void expression
@@ -2504,6 +2505,10 @@ export class JavaScriptSemanticComparatorVisitor extends JavaScriptComparatorVis
2504
2505
  return identifier;
2505
2506
  }
2506
2507
 
2508
+ if (other.kind === J.Kind.FieldAccess && this.readsImport(other as J.FieldAccess, identifier)) {
2509
+ return identifier;
2510
+ }
2511
+
2507
2512
  if (other.kind !== J.Kind.Identifier) {
2508
2513
  return this.kindMismatch<J>();
2509
2514
  }
@@ -2516,7 +2521,8 @@ export class JavaScriptSemanticComparatorVisitor extends JavaScriptComparatorVis
2516
2521
 
2517
2522
  // For identifiers with field types, check type attribution
2518
2523
  if (identifier.fieldType && otherIdentifier.fieldType) {
2519
- if (!this.isOfType(identifier.fieldType, otherIdentifier.fieldType)) {
2524
+ if (!this.isOfType(identifier.fieldType, otherIdentifier.fieldType) ||
2525
+ !this.lenientTypeMatching && !this.sameOwner(identifier, otherIdentifier)) {
2520
2526
  return this.typeMismatch('fieldType');
2521
2527
  }
2522
2528
  } else if (identifier.fieldType || otherIdentifier.fieldType) {
@@ -2530,13 +2536,40 @@ export class JavaScriptSemanticComparatorVisitor extends JavaScriptComparatorVis
2530
2536
  return super.visitIdentifier(identifier, other);
2531
2537
  }
2532
2538
 
2539
+ /** The counterpart of `visitIdentifier`'s case for an import read through its module. */
2540
+ override async visitFieldAccess(fieldAccess: J.FieldAccess, other: J): Promise<J | undefined> {
2541
+ if (other.kind === J.Kind.Identifier && this.readsImport(fieldAccess, other as J.Identifier)) {
2542
+ return fieldAccess;
2543
+ }
2544
+ return super.visitFieldAccess(fieldAccess, other);
2545
+ }
2546
+
2547
+ /** Whether `fieldAccess` reads through its module what `identifier` imports by name, as `os.EOL` does for `EOL`. */
2548
+ private readsImport(fieldAccess: J.FieldAccess, identifier: J.Identifier): boolean {
2549
+ return this.sameVariable(identifier.fieldType, fieldAccess.name.element.fieldType);
2550
+ }
2551
+
2552
+ /**
2553
+ * Whether two variables of one spelling have one owner, which is a module for an import and nothing for a
2554
+ * variable the file declares. Only a reference is asked. A member's owner is whichever class in a
2555
+ * hierarchy declares it, and it is named where it is read from a receiver or declared.
2556
+ */
2557
+ private sameOwner(identifier: J.Identifier, other: J.Identifier): boolean {
2558
+ const parent = this.cursor.parentTree()?.value;
2559
+ if (parent?.kind === J.Kind.FieldAccess && (parent as J.FieldAccess).name.element === identifier ||
2560
+ parent?.kind === J.Kind.NamedVariable && (parent as J.VariableDeclarations.NamedVariable).name === identifier) {
2561
+ return true;
2562
+ }
2563
+ const owner = identifier.fieldType?.owner;
2564
+ const otherOwner = other.fieldType?.owner;
2565
+ return Type.isFullyQualified(owner) || Type.isFullyQualified(otherOwner) ?
2566
+ this.sameFullyQualifiedName(owner, otherOwner) : true;
2567
+ }
2568
+
2533
2569
  /** An import alias and its export denote one declaration, where a variable holding the same value does not. */
2534
2570
  private denoteOneDeclaration(identifier: J.Identifier, other: J.Identifier): boolean {
2535
2571
  if (identifier.fieldType || other.fieldType) {
2536
- // An unresolved import has the placeholder name `unknown`, which identifies nothing
2537
- return !!identifier.fieldType && !!other.fieldType &&
2538
- identifier.fieldType.name === other.fieldType.name && identifier.fieldType.name !== 'unknown' &&
2539
- this.sameFullyQualifiedName(identifier.fieldType.owner, other.fieldType.owner);
2572
+ return this.sameVariable(identifier.fieldType, other.fieldType);
2540
2573
  }
2541
2574
  // Only a nominal type names a declaration. The function and object type shells are shared,
2542
2575
  // and a parameterized type is named after its base alone.
@@ -2545,6 +2578,12 @@ export class JavaScriptSemanticComparatorVisitor extends JavaScriptComparatorVis
2545
2578
  this.sameFullyQualifiedName(identifier.type, other.type);
2546
2579
  }
2547
2580
 
2581
+ private sameVariable(variable?: Type.Variable, other?: Type.Variable): boolean {
2582
+ // An unresolved import has the placeholder name `unknown`, which identifies nothing
2583
+ return !!variable && !!other && variable.name === other.name && variable.name !== 'unknown' &&
2584
+ this.sameFullyQualifiedName(variable.owner, other.owner);
2585
+ }
2586
+
2548
2587
  private sameFullyQualifiedName(type?: Type, other?: Type): boolean {
2549
2588
  return Type.isFullyQualified(type) && Type.isFullyQualified(other) &&
2550
2589
  Type.FullyQualified.getFullyQualifiedName(type) === Type.FullyQualified.getFullyQualifiedName(other);
@@ -651,18 +651,7 @@ export class JavaScriptTypeMapping {
651
651
  const binding = this.moduleBinding(symbol.declarations?.[0], node);
652
652
  if (symbol.flags & ts.SymbolFlags.Alias || (binding && binding.kind !== 'namespace')) {
653
653
  if (binding) {
654
- ownerType = {
655
- kind: Type.Kind.Class,
656
- flags: 0,
657
- classKind: Type.Class.Kind.Interface,
658
- fullyQualifiedName: binding.module,
659
- typeParameters: [],
660
- annotations: [],
661
- interfaces: [],
662
- members: [],
663
- methods: [],
664
- toJSON: typeSignatureToJSON
665
- } as Type.Class;
654
+ ownerType = this.importOwner(binding.module);
666
655
  }
667
656
  } else {
668
657
  // For non-imported variables, check if they belong to a class/interface/namespace
@@ -711,6 +700,12 @@ export class JavaScriptTypeMapping {
711
700
  }
712
701
  }
713
702
 
703
+ // An export read as `os.EOL` has the owner `import {EOL} from 'os'` gives it, unless a class or namespace declares it
704
+ if (!Type.isFullyQualified(ownerType)) {
705
+ const module = this.moduleReadThrough(node);
706
+ ownerType = module ? this.importOwner(module) : ownerType;
707
+ }
708
+
714
709
  // Create the Type.Variable
715
710
  const variable = {
716
711
  kind: Type.Kind.Variable,
@@ -841,6 +836,30 @@ export class JavaScriptTypeMapping {
841
836
  return undefined;
842
837
  }
843
838
 
839
+ /** The module `os` binds in `os.EOL`, given the node of `EOL`, where `os` binds a whole module. */
840
+ private moduleReadThrough(node: ts.Node): string | undefined {
841
+ if (!ts.isIdentifier(node) || !ts.isPropertyAccessExpression(node.parent) || node.parent.name !== node) {
842
+ return undefined;
843
+ }
844
+ const receiver = this.bindingOf(node.parent.expression);
845
+ return receiver?.kind === 'namespace' ? receiver.module : undefined;
846
+ }
847
+
848
+ private importOwner(module: string): Type.Class {
849
+ return {
850
+ kind: Type.Kind.Class,
851
+ flags: 0,
852
+ classKind: Type.Class.Kind.Interface,
853
+ fullyQualifiedName: module,
854
+ typeParameters: [],
855
+ annotations: [],
856
+ interfaces: [],
857
+ members: [],
858
+ methods: [],
859
+ toJSON: typeSignatureToJSON
860
+ } as Type.Class;
861
+ }
862
+
844
863
  /** A binding of the export `name` names, where that is a static name. `default` names the default export. */
845
864
  private memberBinding(module: string, name: ts.Node): ModuleBinding | undefined {
846
865
  if (!ts.isIdentifier(name) && !ts.isStringLiteralLike(name)) {
package/src/rpc/index.ts CHANGED
@@ -35,6 +35,8 @@ export * from "../reference";
35
35
  export {RewriteRpc} from "./rewrite-rpc";
36
36
  export {RpcRecipe, RpcVisitor} from "./recipe";
37
37
  export {DelegatingRecipe, prepareJavaRecipe} from "./java-recipe";
38
+ export {findTestClasspath, JavaRpcTestServer} from "./java-rpc-client";
39
+ export type {JavaRpcOptions} from "./java-rpc-client";
38
40
  export {registerVisitor} from "./request/visitor-registry";
39
41
 
40
42
  RpcCodecs.registerCodec(TreeKind.Checksum, {
@@ -174,13 +174,15 @@ export class PrepareRecipe {
174
174
  }
175
175
 
176
176
  if (visitor! instanceof Check) {
177
- const wireEntry = this.conditionWireEntry(visitor.check, phase);
177
+ const wireEntry = this.conditionWireEntry(visitor.check);
178
178
  if (wireEntry) {
179
179
  preconditions.push(wireEntry);
180
180
  if (phase === "edit") {
181
+ const editor = recipe.editor;
181
182
  recipe = PrepareRecipe.withOverrides(recipe, {
182
- async editor(): Promise<TreeVisitor<any, ExecutionContext>> {
183
- return visitor.v;
183
+ async editor(this: Recipe): Promise<TreeVisitor<any, ExecutionContext>> {
184
+ const editVisitor = await editor.call(this);
185
+ return editVisitor instanceof Check ? editVisitor.v : editVisitor;
184
186
  }
185
187
  });
186
188
  } else {
@@ -214,11 +216,11 @@ export class PrepareRecipe {
214
216
  * condition can't be serialized — the caller leaves the wrapper
215
217
  * intact so the gate runs in-process as a fallback.
216
218
  */
217
- private static conditionWireEntry(condition: CheckArg, phase: "edit" | "scan"): Precondition | undefined {
219
+ private static conditionWireEntry(condition: CheckArg): Precondition | undefined {
218
220
  if (condition instanceof CompositePrecondition) {
219
221
  const operands: Precondition[] = [];
220
222
  for (const operand of condition.operands) {
221
- const entry = this.conditionWireEntry(operand, phase);
223
+ const entry = this.conditionWireEntry(operand);
222
224
  if (entry === undefined) {
223
225
  return undefined;
224
226
  }
@@ -235,7 +237,8 @@ export class PrepareRecipe {
235
237
  return {visitorName: ref.recipeName, visitorOptions: {...ref.options}};
236
238
  }
237
239
  if (condition instanceof RpcRecipe) {
238
- return {visitorName: phase === "edit" ? condition.editVisitor : condition.scanVisitor!};
240
+ // A recipe gates with its editor in either phase, as Check evaluates it in-process.
241
+ return {visitorName: condition.editVisitor};
239
242
  }
240
243
  return undefined;
241
244
  }
@@ -36,13 +36,16 @@
36
36
  *
37
37
  * The classpath is resolved via {@link findTestClasspath}; the suite is skipped
38
38
  * (via {@link describeJavaRpc}) when no classpath has been configured.
39
+ *
40
+ * This is an ES module, since vitest cannot be loaded with `require`. A CommonJS TypeScript
41
+ * file needs `module: nodenext` or `moduleResolution: bundler` to type-check its import.
39
42
  */
40
43
  import {describe, type SuiteAPI, test as base} from "vitest";
41
- import {findTestClasspath, JavaRpcTestServer} from "../rpc/java-rpc-client";
42
- import {RewriteRpc} from "../rpc/rewrite-rpc";
44
+ import {findTestClasspath, JavaRpcTestServer} from "../rpc/java-rpc-client.js";
45
+ import {RewriteRpc} from "../rpc/rewrite-rpc.js";
43
46
 
44
- export {findTestClasspath, JavaRpcTestServer} from "../rpc/java-rpc-client";
45
- export type {JavaRpcOptions} from "../rpc/java-rpc-client";
47
+ export {findTestClasspath, JavaRpcTestServer} from "../rpc/java-rpc-client.js";
48
+ export type {JavaRpcOptions} from "../rpc/java-rpc-client.js";
46
49
 
47
50
  interface JavaRpcFixtures {
48
51
  /**
@@ -1 +0,0 @@
1
- {"version":3,"file":"java-rpc.d.ts","sourceRoot":"","sources":["../../src/test/java-rpc.ts"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,EAAW,KAAK,QAAQ,EAAe,MAAM,QAAQ,CAAC;AAC7D,OAAO,EAAoB,iBAAiB,EAAC,MAAM,wBAAwB,CAAC;AAG5E,OAAO,EAAC,iBAAiB,EAAE,iBAAiB,EAAC,MAAM,wBAAwB,CAAC;AAC5E,YAAY,EAAC,cAAc,EAAC,MAAM,wBAAwB,CAAC;AAkB3D;;;GAGG;AACH,eAAO,MAAM,WAAW;;;EAkBtB,CAAC;AAEH;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,EAAE,QAC6C,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"java-rpc.js","sourceRoot":"","sources":["../../src/test/java-rpc.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;GAcG;AACH;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,mCAA6D;AAC7D,4DAA4E;AAC5E,oDAA8C;AAE9C,0DAA4E;AAApE,oHAAA,iBAAiB,OAAA;AAAE,oHAAA,iBAAiB,OAAA;AAmB5C;;;GAGG;AACU,QAAA,WAAW,GAAG,aAAI,CAAC,MAAM,CAAkB;IACpD,cAAc,EAAE;QACZ,KAAK,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE;YACd,MAAM,MAAM,GAAG,MAAM,mCAAiB,CAAC,KAAK,EAAE,CAAC;YAC/C,IAAI,CAAC;gBACD,MAAM,GAAG,CAAC,MAAM,CAAC,CAAC;YACtB,CAAC;oBAAS,CAAC;gBACP,MAAM,MAAM,CAAC,OAAO,EAAE,CAAC;YAC3B,CAAC;QACL,CAAC;QACD,EAAC,KAAK,EAAE,QAAQ,EAAC;KACpB;IACD,OAAO,EAAE,KAAK,EAAE,EAAC,cAAc,EAAC,EAAE,GAAG,EAAE,EAAE;QACrC,2FAA2F;QAC3F,wBAAU,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;QACnC,MAAM,cAAc,CAAC,KAAK,EAAE,CAAC;QAC7B,MAAM,GAAG,CAAC,cAAc,CAAC,CAAC;IAC9B,CAAC;CACJ,CAAC,CAAC;AAEH;;;;;;;GAOG;AACU,QAAA,eAAe,GACxB,IAAA,mCAAiB,GAAE,CAAC,CAAC,CAAC,iBAAQ,CAAC,CAAC,CAAE,iBAAQ,CAAC,IAA4B,CAAC"}