@openrewrite/rewrite 8.109.0 → 8.110.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.
@@ -24,7 +24,7 @@ import {Cursor, isTree} from "../tree";
24
24
  import {
25
25
  AddImportOptions, bindImport, bindingShape, existingImportBinding, ExistingImportBinding, hasEsmSyntax, ImportStyle,
26
26
  isCommonJs, memberName, moduleNameOf, moduleScopeBindings, nameTaken, RebindGroup, RebindImport, requireBinds,
27
- requireDeclarationOf, requiredModuleOf, requiredModuleOfDeclaration, requireJoinTarget
27
+ requireDeclarationOf, requiredModuleOf, requiredModuleOfDeclaration, requireJoinTarget, sameAttributes
28
28
  } from "./add-import";
29
29
  import {RemoveImport} from "./remove-import";
30
30
  import {RenameMemberSpellings} from "./member-spellings";
@@ -498,7 +498,10 @@ function sharesNameWithValue(cu: JS.CompilationUnit, existing: ExistingImportBin
498
498
 
499
499
  const noNames: ReadonlySet<string> = new Set();
500
500
 
501
- /** Whether the moved binding can merge into a value import of `to`'s named member that `name` binds. */
501
+ /**
502
+ * Whether the moved binding can merge into a value import of `to`'s named member that `name` binds,
503
+ * loaded with the same attributes.
504
+ */
502
505
  function reusesTargetImport(
503
506
  cu: JS.CompilationUnit,
504
507
  name: string,
@@ -512,7 +515,13 @@ function reusesTargetImport(
512
515
  const joined = !moved.viaRequire || (!moved.reassignable && joinTarget !== undefined &&
513
516
  requireBinds(joinTarget, to.module, to.member) === name);
514
517
  return target?.localName === name && target.viaRequire === moved.viaRequire && joined &&
515
- bindingShape(to.member) === "named" && !target.typeOnly && !moved.typeOnly && onlyReferences(cu, name);
518
+ bindingShape(to.member) === "named" && !target.typeOnly && !moved.typeOnly &&
519
+ sameAttributes(attributesOf(target), attributesOf(moved)) && onlyReferences(cu, name);
520
+ }
521
+
522
+ /** The `with {…}` clause of the import that binds it. A `require` has none. */
523
+ function attributesOf(binding: ExistingImportBinding): JS.ImportAttributes | undefined {
524
+ return binding.statement.kind === JS.Kind.Import ? binding.statement.attributes : undefined;
516
525
  }
517
526
 
518
527
  /**
@@ -14,13 +14,15 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import {create as produce, Draft} from "mutative";
17
- import {ElementRemovalFormatter, emptySpace, Expression, isIdentifier, isStringLiteral, J, singleSpace, Statement} from "../java";
17
+ import {ElementRemovalFormatter, emptySpace, Expression, isIdentifier, isStringLiteral, J, singleSpace, splitAtLineEnd, Statement} from "../java";
18
18
  import {randomId, UUID} from "../uuid";
19
19
  import {Cursor} from "../tree";
20
20
  import {isObjectBindingPattern, JS, moduleExportName} from "./tree";
21
21
  import {JavaScriptVisitor} from "./visitor";
22
22
  import {resolve, rootName} from "./scope";
23
- import {aliasing, moduleNameOf, movedBinding, newIdentifier, respecify, withMovedTypes, withoutStatement} from "./add-import";
23
+ import {
24
+ aliasing, moduleNameOf, movedBinding, newIdentifier, respecify, sameAttributes, withMovedTypes, withoutStatement
25
+ } from "./add-import";
24
26
  import {sameModule, spelledLike} from "./package-name";
25
27
 
26
28
  /**
@@ -182,7 +184,8 @@ export class RenameMemberSpellings<P> extends JavaScriptVisitor<P> {
182
184
  * export {Old} from "m" → export {New as Old} from "m2"
183
185
  * export {Old, X} from "m" → export {X} from "m"; export {New as Old} from "m2"
184
186
  * export {Old} from "m"; → export {Y, New as Old} from "m2"
185
- * export {Y} from "m2" (joins a re-export of the same kind, `export type` or not)
187
+ * export {Y} from "m2" (joins a re-export of the same kind, `export type` or not,
188
+ * with the same import attributes)
186
189
  *
187
190
  * A comment ending the line (`export {X} from "m"; // note`) stays on it, before the new statement.
188
191
  */
@@ -200,8 +203,11 @@ export class RenameMemberSpellings<P> extends JavaScriptVisitor<P> {
200
203
  }
201
204
  changed = true;
202
205
  const staying = new Set(elements.filter(e => !moving.has(e)));
203
- const target = statements.findIndex((s, j) =>
204
- j !== i && reExportFrom(s.element, this.to.module)?.typeOnly.element === declaration.typeOnly.element);
206
+ const target = statements.findIndex((s, j) => {
207
+ const into = j === i ? undefined : reExportFrom(s.element, this.to.module);
208
+ return into !== undefined && into.typeOnly.element === declaration.typeOnly.element &&
209
+ sameAttributes(into.attributes, declaration.attributes);
210
+ });
205
211
  if (target < 0 && staying.size === 0) {
206
212
  statements[i] = {...statements[i], element: this.respecified(withElements(declaration, elements.map(e => this.renamed(e))))};
207
213
  continue;
@@ -302,25 +308,6 @@ function reExportFrom(statement: J | undefined, module: string): JS.ExportDeclar
302
308
  ? declaration : undefined;
303
309
  }
304
310
 
305
- /**
306
- * The space after a statement, split where its line ends:
307
- * ` // note\n// next` → `line` ` // note`, `rest` `\n// next`. At the end of the file all of it is `line`.
308
- * `undefined` where no comment follows on the same line.
309
- */
310
- function splitAtLineEnd(space: J.Space, endsFile: boolean): {line: J.Space, rest: J.Space} | undefined {
311
- if (space.whitespace.includes("\n") || space.comments.length === 0) {
312
- return undefined;
313
- }
314
- const end = space.comments.findIndex(c => c.suffix.includes("\n"));
315
- if (end < 0) {
316
- return endsFile ? {line: space, rest: {...space, whitespace: "", comments: []}} : undefined;
317
- }
318
- return {
319
- line: {...space, comments: space.comments.slice(0, end + 1)},
320
- rest: {...space, whitespace: space.comments[end].suffix, comments: space.comments.slice(end + 1)}
321
- };
322
- }
323
-
324
311
  function clauseOf(declaration: JS.ExportDeclaration): JS.NamedExports {
325
312
  return declaration.exportClause as JS.NamedExports;
326
313
  }
@@ -53,6 +53,13 @@ export function addDependency(options: AddDependencyOptions): Promise<Recipe> {
53
53
  export interface RemoveDependencyOptions {
54
54
  packageName: string;
55
55
  scope?: NpmDependencyScope;
56
+ /**
57
+ * Keep the dependency in a `package.json` while a source still imports a package matching one of these globs, by
58
+ * `import`, `export ... from`, `require()` or `import()`: the nearest `package.json` above that source declaring
59
+ * the dependency. Imports are read before the cycle's edits, so the recipe runs another cycle, in which it removes
60
+ * the dependency where an earlier recipe removed the last import.
61
+ */
62
+ unlessImporting?: string[];
56
63
  }
57
64
 
58
65
  /**