@openrewrite/rewrite 8.107.1 → 8.107.2

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.
@@ -1,5 +1,5 @@
1
1
  import { JavaScriptVisitor } from "./visitor";
2
- import { J, Type } from "../java";
2
+ import { J, TrailingComma, Type } from "../java";
3
3
  import { JS } from "./tree";
4
4
  import { UUID } from "../uuid";
5
5
  import { Draft } from "mutative";
@@ -118,6 +118,7 @@ export declare function moduleNameOf(module: string | J.Literal): string;
118
118
  * learns which name came back; a recipe driving this visitor is stuck with the one it derived.
119
119
  */
120
120
  export declare class AddImport<P> extends JavaScriptVisitor<P> {
121
+ private readonly group?;
121
122
  readonly module: string;
122
123
  /** Set when the caller supplied the module specifier as a literal; printed verbatim. */
123
124
  readonly moduleValueSource?: string;
@@ -136,7 +137,11 @@ export declare class AddImport<P> extends JavaScriptVisitor<P> {
136
137
  * that module answers it.
137
138
  */
138
139
  readonly anyNameAnswers: boolean;
139
- constructor(options: AddImportOptions, bindingName?: string);
140
+ /**
141
+ * @param group The rebind moves this import takes in. It goes where their statement was,
142
+ * laid out and ordered as it was.
143
+ */
144
+ constructor(options: AddImportOptions, bindingName?: string, group?: RebindGroup | undefined);
140
145
  /**
141
146
  * Extract module name from a module specifier literal
142
147
  */
@@ -199,6 +204,8 @@ export declare class AddImport<P> extends JavaScriptVisitor<P> {
199
204
  * Determine the appropriate spacing before the import statement
200
205
  */
201
206
  private determineImportPrefix;
207
+ /** Where an import a rebind group creates goes, which is where the statement its members left stands or stood. */
208
+ private placementInGroup;
202
209
  /**
203
210
  * Find the index where the new import should be inserted
204
211
  */
@@ -243,6 +250,8 @@ export interface ExistingImportBinding {
243
250
  viaRequire: boolean;
244
251
  /** Whether a `let` or `var` declares it, which the file may reassign. */
245
252
  reassignable: boolean;
253
+ /** The import or `require` declaration that binds it. */
254
+ statement: JS.Import | J.VariableDeclarations;
246
255
  }
247
256
  /**
248
257
  * The existing binding for `member` of `module`, read from `cu`'s own import statements and
@@ -258,6 +267,56 @@ export declare function existingImportBinding(cu: JS.CompilationUnit, module: st
258
267
  export declare function aliasing(local: J.Identifier | J.Literal, member: string): JS.Alias;
259
268
  /** `cu` without the statement `id`, its prefix carried onto the next one so a file header survives. */
260
269
  export declare function withoutStatement(cu: JS.CompilationUnit, id: UUID): JS.CompilationUnit;
270
+ /**
271
+ * The moves out of one import statement queued in one visit, shared by the `RebindImport`s that carry them out
272
+ * so that the imports they leave behind keep the statement's place, member order and layout.
273
+ */
274
+ export declare class RebindGroup {
275
+ readonly sourceId: UUID;
276
+ /** Each specifier's local name in the order written. */
277
+ private readonly written;
278
+ /** The same, a moved one under the name it moves to. */
279
+ private readonly bound;
280
+ private readonly targets;
281
+ /** The imports the moves created, which sit together where the statement was, sorted by module. */
282
+ readonly created: UUID[];
283
+ /**
284
+ * The dropped statement's prefix, for an import created in its place, and the statement that followed it
285
+ * with the prefix it had then.
286
+ */
287
+ dropped?: {
288
+ prefix: J.Space;
289
+ next?: {
290
+ id: UUID;
291
+ prefix: J.Space;
292
+ };
293
+ };
294
+ /** How the statement's named list spans lines, where it does. */
295
+ readonly multiLine?: {
296
+ first: J.Space;
297
+ closing: J.Space;
298
+ trailingComma?: TrailingComma;
299
+ };
300
+ constructor(sourceId: UUID, jsImport: JS.Import);
301
+ add(localName: string, boundName: string, to: {
302
+ module: string;
303
+ member?: string;
304
+ }, typeOnly: boolean): void;
305
+ /** Whether the statement listed several named members, so that one left alone in it stands on a line of its own. */
306
+ get listedSeveral(): boolean;
307
+ /**
308
+ * The multi-line layout an import of `module` takes, which only several members moved together keep.
309
+ * A type-only import and a value import of one module are two lists.
310
+ */
311
+ layoutFor(module: string, typeOnly: boolean): RebindGroup["multiLine"];
312
+ /** A name's place in the statement, whether its move has run yet or not. */
313
+ private positionOf;
314
+ /**
315
+ * Where `name` goes among a list's `names` to keep the order the statement wrote them in,
316
+ * or undefined where the list holds a name the statement did not.
317
+ */
318
+ insertionIndex(names: readonly string[], name: string): number | undefined;
319
+ }
261
320
  /**
262
321
  * Moves the binding `from` names to `to`, binding it under `boundName`, and the file's references
263
322
  * follow a `boundName` of its own. Whether the statement is rewritten in place or replaced is decided
@@ -277,13 +336,14 @@ export declare class RebindImport<P> extends JavaScriptVisitor<P> {
277
336
  };
278
337
  readonly localName: string;
279
338
  readonly boundName: string;
339
+ readonly group?: RebindGroup | undefined;
280
340
  constructor(from: {
281
341
  module: string;
282
342
  member?: string;
283
343
  }, to: {
284
344
  module: string;
285
345
  member?: string;
286
- }, localName: string, boundName: string);
346
+ }, localName: string, boundName: string, group?: RebindGroup | undefined);
287
347
  private transformedInPlace;
288
348
  private typeOnly;
289
349
  private cu?;
@@ -1 +1 @@
1
- {"version":3,"file":"add-import.d.ts","sourceRoot":"","sources":["../../src/javascript/add-import.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,WAAW,CAAC;AAC5C,OAAO,EAA6D,CAAC,EAA0E,IAAI,EAAC,MAAM,SAAS,CAAC;AACpK,OAAO,EAAyB,EAAE,EAAwB,MAAM,QAAQ,CAAC;AACzE,OAAO,EAAW,IAAI,EAAC,MAAM,SAAS,CAAC;AAQvC,OAAO,EAAoB,KAAK,EAAC,MAAM,UAAU,CAAC;AAKlD,MAAM,MAAM,SAAS,GAAG,GAAG,GAAG,GAAG,CAAC;AAElC,oBAAY,WAAW;IACnB,QAAQ,IAAA,CAAO,6BAA6B;IAC5C,YAAY,IAAA,CAAG,8BAA8B;IAC7C,UAAU,IAAA,CAAK,yBAAyB;IACxC,QAAQ,IAAA;CACX;AAED,MAAM,WAAW,gBAAgB;IAC7B;;;oCAGgC;IAChC,MAAM,EAAE,MAAM,GAAG,CAAC,CAAC,OAAO,CAAC;IAE3B;;;;;;;mDAO+C;IAC/C,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;;;mDAI+C;IAC/C,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;mDAE+C;IAC/C,aAAa,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return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.RebindImport = exports.AddImport = exports.ImportStyle = void 0;
6
+ exports.RebindImport = exports.RebindGroup = exports.AddImport = exports.ImportStyle = void 0;
7
7
  exports.bindImport = bindImport;
8
8
  exports.memberName = memberName;
9
9
  exports.moduleScopeBindings = moduleScopeBindings;
@@ -678,24 +678,26 @@ function detectSemi(cu) {
678
678
  }
679
679
  /**
680
680
  * Whether a brace list added to `cu` is written `{ x }` rather than `{x}`. A Prettier configuration
681
- * settles it. Short of one, the file's own single-line import and `require` lists decide, then the style in force.
681
+ * settles it. Short of one, the file's own single-line import, export and `require` lists decide,
682
+ * then the style in force.
682
683
  */
683
684
  function detectBraceSpacing(cu) {
684
- var _a, _b, _c, _d;
685
+ var _a, _b, _c;
685
686
  const bracketSpacing = prettierOption(cu, 'bracketSpacing', true);
686
687
  if (bracketSpacing !== undefined) {
687
688
  return bracketSpacing;
688
689
  }
689
690
  let score = 0;
690
691
  for (const statement of cu.statements) {
691
- const namedBindings = ((_a = statement.element) === null || _a === void 0 ? void 0 : _a.kind) === tree_1.JS.Kind.Import
692
- ? (_b = statement.element.importClause) === null || _b === void 0 ? void 0 : _b.namedBindings
693
- : undefined;
694
- const declaration = requireDeclarationOf(statement.element);
692
+ const element = statement.element;
693
+ const namedBindings = (element === null || element === void 0 ? void 0 : element.kind) === tree_1.JS.Kind.Import
694
+ ? (_a = element.importClause) === null || _a === void 0 ? void 0 : _a.namedBindings
695
+ : (element === null || element === void 0 ? void 0 : element.kind) === tree_1.JS.Kind.ExportDeclaration ? element.exportClause : undefined;
696
+ const declaration = requireDeclarationOf(element);
695
697
  const pattern = declaration && requirePattern(declaration);
696
- const first = (namedBindings === null || namedBindings === void 0 ? void 0 : namedBindings.kind) === tree_1.JS.Kind.NamedImports
697
- ? (_c = namedBindings.elements.elements[0]) === null || _c === void 0 ? void 0 : _c.element
698
- : (0, tree_1.isObjectBindingPattern)(pattern) ? (_d = pattern.bindings.elements[0]) === null || _d === void 0 ? void 0 : _d.element : undefined;
698
+ const first = (namedBindings === null || namedBindings === void 0 ? void 0 : namedBindings.kind) === tree_1.JS.Kind.NamedImports || (namedBindings === null || namedBindings === void 0 ? void 0 : namedBindings.kind) === tree_1.JS.Kind.NamedExports
699
+ ? (_b = namedBindings.elements.elements[0]) === null || _b === void 0 ? void 0 : _b.element
700
+ : (0, tree_1.isObjectBindingPattern)(pattern) ? (_c = pattern.bindings.elements[0]) === null || _c === void 0 ? void 0 : _c.element : undefined;
699
701
  // A list laid out one per line says nothing about how a one-line list is spaced.
700
702
  if (first && !isEmptyPlaceholder(first) && !first.prefix.whitespace.includes('\n')) {
701
703
  score += first.prefix.whitespace.length > 0 ? 1 : -1;
@@ -743,9 +745,14 @@ async function formatMergedImport(cu, id, p) {
743
745
  * learns which name came back; a recipe driving this visitor is stuck with the one it derived.
744
746
  */
745
747
  class AddImport extends visitor_1.JavaScriptVisitor {
746
- constructor(options, bindingName) {
748
+ /**
749
+ * @param group The rebind moves this import takes in. It goes where their statement was,
750
+ * laid out and ordered as it was.
751
+ */
752
+ constructor(options, bindingName, group) {
747
753
  var _a, _b, _c;
748
754
  super();
755
+ this.group = group;
749
756
  validate(options);
750
757
  this.module = moduleNameOf(options.module);
751
758
  this.moduleValueSource = typeof options.module === 'string' ? undefined : options.module.valueSource;
@@ -770,6 +777,7 @@ class AddImport extends visitor_1.JavaScriptVisitor {
770
777
  return moduleNameOf(moduleSpecifier);
771
778
  }
772
779
  async visitJsCompilationUnit(compilationUnit, p) {
780
+ var _a, _b;
773
781
  // First, check if the import already exists
774
782
  const hasImport = await this.checkImportExists(compilationUnit);
775
783
  if (hasImport) {
@@ -816,14 +824,28 @@ class AddImport extends visitor_1.JavaScriptVisitor {
816
824
  }
817
825
  // Add ES6 import (handles ES6Named, ES6Namespace, ES6Default)
818
826
  // Find the position to insert the import
819
- const insertionIndex = this.findImportInsertionIndex(compilationUnit);
820
- const newImport = await this.createImportStatement(compilationUnit, insertionIndex, p);
827
+ const placement = this.placementInGroup(compilationUnit);
828
+ const insertionIndex = (_a = placement === null || placement === void 0 ? void 0 : placement.index) !== null && _a !== void 0 ? _a : this.findImportInsertionIndex(compilationUnit);
829
+ const created = await this.createImportStatement(compilationUnit, insertionIndex, p);
830
+ const newImport = (placement === null || placement === void 0 ? void 0 : placement.replacing) ? Object.assign(Object.assign({}, created), { prefix: placement.replacing.prefix }) : created;
831
+ (_b = this.group) === null || _b === void 0 ? void 0 : _b.created.push(newImport.id);
821
832
  const withImport = await this.produceJavaScript(compilationUnit, p, async (draft) => {
822
833
  // Insert the import at the appropriate position
823
834
  const semicolonMarkers = detectSemi(compilationUnit)
824
835
  ? (0, markers_1.markers)({ kind: java_1.J.Markers.Semicolon, id: (0, uuid_1.randomId)() })
825
836
  : markers_1.emptyMarkers;
826
- if (insertionIndex === prologueLength(compilationUnit)) {
837
+ if (placement === null || placement === void 0 ? void 0 : placement.replacing) {
838
+ // The import goes before the statement `replacing.next` names, or at the end of the file.
839
+ const following = placement.replacing.next;
840
+ const next = compilationUnit.statements[insertionIndex];
841
+ draft.statements = [
842
+ ...compilationUnit.statements.slice(0, insertionIndex),
843
+ (0, java_1.rightPadded)(newImport, java_1.emptySpace, semicolonMarkers),
844
+ ...(following ? [Object.assign(Object.assign({}, next), { element: Object.assign(Object.assign({}, next.element), { prefix: following.prefix }) })] : []),
845
+ ...compilationUnit.statements.slice(insertionIndex + 1)
846
+ ];
847
+ }
848
+ else if (insertionIndex === prologueLength(compilationUnit)) {
827
849
  // The `after` space should be empty since any semicolon is printed after it
828
850
  const next = compilationUnit.statements[insertionIndex];
829
851
  draft.statements = [
@@ -860,7 +882,8 @@ class AddImport extends visitor_1.JavaScriptVisitor {
860
882
  }
861
883
  }
862
884
  });
863
- return formatImport(withoutModuleMarker(withImport), newImport.id, p, detectBraceSpacing(compilationUnit));
885
+ const formatted = await formatImport(withoutModuleMarker(withImport), newImport.id, p, detectBraceSpacing(compilationUnit));
886
+ return this.group ? withRunSorted(formatted, new Set(this.group.created)) : formatted;
864
887
  }
865
888
  /**
866
889
  * Try to merge the new member into an existing import from the same module
@@ -879,29 +902,33 @@ class AddImport extends visitor_1.JavaScriptVisitor {
879
902
  if (importClause.namedBindings) {
880
903
  // We found a matching import with named bindings - merge into it
881
904
  return formatMergedImport(await this.produceJavaScript(compilationUnit, p, async (draft) => {
882
- var _a, _b, _c, _d, _e, _f;
905
+ var _a, _b, _c, _d, _e, _f, _g, _h;
883
906
  const namedImports = importClause.namedBindings;
884
907
  const existingElements = namedImports.elements.elements;
885
- // Find the correct insertion position (alphabetical, case-insensitive)
886
- const newName = this.bindingName.toLowerCase();
887
- let insertIndex = existingElements.findIndex(elem => {
908
+ // A list the rebind group's statement wrote keeps that order. Any other list is
909
+ // taken as alphabetical, case-insensitive.
910
+ const names = existingElements.map(elem => {
888
911
  var _a, _b, _c;
889
- if (((_a = elem.element) === null || _a === void 0 ? void 0 : _a.kind) === tree_1.JS.Kind.ImportSpecifier) {
890
- const name = (_c = (_b = specifierBinding(elem.element)) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : '';
891
- return newName.localeCompare(name.toLowerCase()) < 0;
892
- }
893
- return false;
912
+ return ((_a = elem.element) === null || _a === void 0 ? void 0 : _a.kind) === tree_1.JS.Kind.ImportSpecifier
913
+ ? (_c = (_b = specifierBinding(elem.element)) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : ''
914
+ : '';
915
+ });
916
+ const newName = this.bindingName.toLowerCase();
917
+ let insertIndex = (_b = (_a = this.group) === null || _a === void 0 ? void 0 : _a.insertionIndex(names, this.bindingName)) !== null && _b !== void 0 ? _b : names.findIndex((name, j) => {
918
+ var _a;
919
+ return ((_a = existingElements[j].element) === null || _a === void 0 ? void 0 : _a.kind) === tree_1.JS.Kind.ImportSpecifier &&
920
+ newName.localeCompare(name.toLowerCase()) < 0;
894
921
  });
895
922
  if (insertIndex === -1)
896
923
  insertIndex = existingElements.length;
897
924
  // Detect spacing style from existing elements:
898
925
  // - firstElementPrefix: space after { (from first element's prefix)
899
926
  // - trailingSpace: space before } (from last element's after)
900
- const firstElementPrefix = (_c = (_b = (_a = existingElements[0]) === null || _a === void 0 ? void 0 : _a.element) === null || _b === void 0 ? void 0 : _b.prefix) !== null && _c !== void 0 ? _c : java_1.emptySpace;
927
+ const firstElementPrefix = (_e = (_d = (_c = existingElements[0]) === null || _c === void 0 ? void 0 : _c.element) === null || _d === void 0 ? void 0 : _d.prefix) !== null && _e !== void 0 ? _e : java_1.emptySpace;
901
928
  const lastIndex = existingElements.length - 1;
902
929
  // What separates one element from the next after its comma: the space in front of
903
930
  // the second element, or for a lone element on its own line, that line break.
904
- const separator = (_f = (_e = (_d = existingElements[1]) === null || _d === void 0 ? void 0 : _d.element) === null || _e === void 0 ? void 0 : _e.prefix) !== null && _f !== void 0 ? _f : (firstElementPrefix.whitespace.includes('\n') ? firstElementPrefix : java_1.singleSpace);
931
+ const separator = (_h = (_g = (_f = existingElements[1]) === null || _f === void 0 ? void 0 : _f.element) === null || _g === void 0 ? void 0 : _g.prefix) !== null && _h !== void 0 ? _h : (firstElementPrefix.whitespace.includes('\n') ? firstElementPrefix : java_1.singleSpace);
905
932
  // A trailing comma is a marker on the last element rather than padding, so with
906
933
  // one present the space before `}` lives in the marker's suffix, not in `after`.
907
934
  const trailingComma = (0, markers_1.findMarker)(existingElements[lastIndex], java_1.J.Markers.TrailingComma);
@@ -1274,6 +1301,7 @@ class AddImport extends visitor_1.JavaScriptVisitor {
1274
1301
  * Create a new import statement
1275
1302
  */
1276
1303
  async createImportStatement(compilationUnit, insertionIndex, p) {
1304
+ var _a;
1277
1305
  // Determine the appropriate prefix (spacing before the import)
1278
1306
  const prefix = this.determineImportPrefix(compilationUnit, insertionIndex);
1279
1307
  // Whitespace throughout follows the parser's placement, so that formatting the result is a
@@ -1352,11 +1380,16 @@ class AddImport extends visitor_1.JavaScriptVisitor {
1352
1380
  }
1353
1381
  else {
1354
1382
  // Named import: import { member } from 'module'
1355
- const braceSpace = detectBraceSpacing(compilationUnit) ? java_1.singleSpace : java_1.emptySpace;
1356
1383
  const importSpec = this.createImportSpecifier();
1357
- // Apply brace spacing: the space after { is in the specifier's prefix,
1358
- // and the space before } is in the rightPadded's after
1359
- const importSpecWithSpacing = Object.assign(Object.assign({}, importSpec), { prefix: braceSpace });
1384
+ const layout = (_a = this.group) === null || _a === void 0 ? void 0 : _a.layoutFor(this.module, this.typeOnly);
1385
+ const braceSpace = layout ? undefined : detectBraceSpacing(compilationUnit) ? java_1.singleSpace : java_1.emptySpace;
1386
+ // The space after { is in the specifier's prefix, and the space before } is in the rightPadded's
1387
+ // after, or in the suffix of a trailing comma.
1388
+ const element = layout
1389
+ ? layout.trailingComma
1390
+ ? (0, java_1.rightPadded)(Object.assign(Object.assign({}, importSpec), { prefix: layout.first }), java_1.emptySpace, (0, markers_1.markers)(Object.assign(Object.assign({}, layout.trailingComma), { id: (0, uuid_1.randomId)() })))
1391
+ : (0, java_1.rightPadded)(Object.assign(Object.assign({}, importSpec), { prefix: layout.first }), layout.closing)
1392
+ : (0, java_1.rightPadded)(Object.assign(Object.assign({}, importSpec), { prefix: braceSpace }), braceSpace);
1360
1393
  const namedImports = {
1361
1394
  id: (0, uuid_1.randomId)(),
1362
1395
  kind: tree_1.JS.Kind.NamedImports,
@@ -1365,7 +1398,7 @@ class AddImport extends visitor_1.JavaScriptVisitor {
1365
1398
  elements: {
1366
1399
  kind: java_1.J.Kind.Container,
1367
1400
  before: this.typeOnly ? java_1.singleSpace : java_1.emptySpace,
1368
- elements: [(0, java_1.rightPadded)(importSpecWithSpacing, braceSpace)],
1401
+ elements: [element],
1369
1402
  markers: markers_1.emptyMarkers
1370
1403
  }
1371
1404
  };
@@ -1552,6 +1585,30 @@ class AddImport extends visitor_1.JavaScriptVisitor {
1552
1585
  // to separate from the previous statement
1553
1586
  return (0, java_1.space)("\n");
1554
1587
  }
1588
+ /** Where an import a rebind group creates goes, which is where the statement its members left stands or stood. */
1589
+ placementInGroup(cu) {
1590
+ const group = this.group;
1591
+ if (group === undefined) {
1592
+ return undefined;
1593
+ }
1594
+ const indexOf = (id) => cu.statements.findIndex(s => { var _a; return ((_a = s.element) === null || _a === void 0 ? void 0 : _a.id) === id; });
1595
+ // The import joins the end of the run, which is then sorted with each slot keeping its prefix,
1596
+ // so a comment or blank line before the run stays before it.
1597
+ const created = Math.max(-1, ...group.created.map(indexOf));
1598
+ if (created >= 0) {
1599
+ return { index: created + 1 };
1600
+ }
1601
+ const source = indexOf(group.sourceId);
1602
+ if (source >= 0) {
1603
+ return { index: source + 1 };
1604
+ }
1605
+ const dropped = group.dropped;
1606
+ if (dropped === undefined) {
1607
+ return undefined;
1608
+ }
1609
+ const next = dropped.next === undefined ? cu.statements.length : indexOf(dropped.next.id);
1610
+ return next >= 0 ? { index: next, replacing: dropped } : undefined;
1611
+ }
1555
1612
  /**
1556
1613
  * Find the index where the new import should be inserted
1557
1614
  */
@@ -1894,7 +1951,8 @@ function existingImportBinding(cu, module, member) {
1894
1951
  aliased: required !== memberName(member),
1895
1952
  typeOnly: false,
1896
1953
  viaRequire: true,
1897
- reassignable: keywordsOf(declaration) !== 'const'
1954
+ reassignable: keywordsOf(declaration) !== 'const',
1955
+ statement: declaration
1898
1956
  };
1899
1957
  }
1900
1958
  if ((element === null || element === void 0 ? void 0 : element.kind) !== tree_1.JS.Kind.Import) {
@@ -1908,7 +1966,8 @@ function existingImportBinding(cu, module, member) {
1908
1966
  aliased: localName !== memberName(member),
1909
1967
  typeOnly: bindsTypeOnly(element, member),
1910
1968
  viaRequire: false,
1911
- reassignable: false
1969
+ reassignable: false,
1970
+ statement: element
1912
1971
  };
1913
1972
  }
1914
1973
  }
@@ -2008,6 +2067,22 @@ function removeBinding(jsImport, member, localName) {
2008
2067
  const updatedNamedImports = Object.assign(Object.assign({}, namedImports), { elements: Object.assign(Object.assign({}, namedImports.elements), { elements: kept }) });
2009
2068
  return Object.assign(Object.assign({}, jsImport), { importClause: Object.assign(Object.assign({}, importClause), { namedBindings: updatedNamedImports }) });
2010
2069
  }
2070
+ /** `jsImport` with a named list of one member that spans lines put on one line, without a trailing comma. */
2071
+ function onOneLine(jsImport, braceSpacing) {
2072
+ var _a;
2073
+ const namedBindings = (_a = jsImport.importClause) === null || _a === void 0 ? void 0 : _a.namedBindings;
2074
+ if ((namedBindings === null || namedBindings === void 0 ? void 0 : namedBindings.kind) !== tree_1.JS.Kind.NamedImports) {
2075
+ return jsImport;
2076
+ }
2077
+ const namedImports = namedBindings;
2078
+ const [only, ...rest] = namedImports.elements.elements;
2079
+ if (only === undefined || rest.length > 0 || !only.element.prefix.whitespace.includes('\n')) {
2080
+ return jsImport;
2081
+ }
2082
+ const braceSpace = braceSpacing ? java_1.singleSpace : java_1.emptySpace;
2083
+ const element = Object.assign(Object.assign({}, only), { element: Object.assign(Object.assign({}, only.element), { prefix: braceSpace }), after: braceSpace, markers: Object.assign(Object.assign({}, only.markers), { markers: only.markers.markers.filter(m => m.kind !== java_1.J.Markers.TrailingComma) }) });
2084
+ return Object.assign(Object.assign({}, jsImport), { importClause: Object.assign(Object.assign({}, jsImport.importClause), { namedBindings: Object.assign(Object.assign({}, namedImports), { elements: Object.assign(Object.assign({}, namedImports.elements), { elements: [element] }) }) }) });
2085
+ }
2011
2086
  /** `cu` without the statement `id`, its prefix carried onto the next one so a file header survives. */
2012
2087
  function withoutStatement(cu, id) {
2013
2088
  var _a;
@@ -2037,6 +2112,103 @@ function replacesStatement(cu, jsImport, from, to, typeOnly) {
2037
2112
  }));
2038
2113
  }
2039
2114
  const esmStyle = { default: ImportStyle.ES6Default, namespace: ImportStyle.ES6Namespace, named: ImportStyle.ES6Named };
2115
+ /**
2116
+ * The moves out of one import statement queued in one visit, shared by the `RebindImport`s that carry them out
2117
+ * so that the imports they leave behind keep the statement's place, member order and layout.
2118
+ */
2119
+ class RebindGroup {
2120
+ constructor(sourceId, jsImport) {
2121
+ var _a, _b;
2122
+ this.sourceId = sourceId;
2123
+ this.targets = [];
2124
+ /** The imports the moves created, which sit together where the statement was, sorted by module. */
2125
+ this.created = [];
2126
+ const namedBindings = (_a = jsImport.importClause) === null || _a === void 0 ? void 0 : _a.namedBindings;
2127
+ const elements = (namedBindings === null || namedBindings === void 0 ? void 0 : namedBindings.kind) === tree_1.JS.Kind.NamedImports
2128
+ ? namedBindings.elements.elements.filter(e => !isEmptyPlaceholder(e.element))
2129
+ : [];
2130
+ this.written = elements.map(e => { var _a, _b; return (_b = (_a = specifierBinding(e.element)) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : ''; });
2131
+ this.bound = [...this.written];
2132
+ const first = (_b = elements[0]) === null || _b === void 0 ? void 0 : _b.element.prefix;
2133
+ if (first === null || first === void 0 ? void 0 : first.whitespace.includes('\n')) {
2134
+ const last = elements[elements.length - 1];
2135
+ const trailingComma = (0, markers_1.findMarker)(last, java_1.J.Markers.TrailingComma);
2136
+ this.multiLine = { first, closing: trailingComma ? trailingComma.suffix : last.after, trailingComma };
2137
+ }
2138
+ }
2139
+ add(localName, boundName, to, typeOnly) {
2140
+ const index = this.written.indexOf(localName);
2141
+ if (index >= 0) {
2142
+ this.bound[index] = boundName;
2143
+ }
2144
+ // A default or namespace binding gets an import of its own, so it never shares a named list.
2145
+ if (bindingShape(to.member) === "named") {
2146
+ this.targets.push({ module: to.module, typeOnly });
2147
+ }
2148
+ }
2149
+ /** Whether the statement listed several named members, so that one left alone in it stands on a line of its own. */
2150
+ get listedSeveral() {
2151
+ return this.written.length > 1;
2152
+ }
2153
+ /**
2154
+ * The multi-line layout an import of `module` takes, which only several members moved together keep.
2155
+ * A type-only import and a value import of one module are two lists.
2156
+ */
2157
+ layoutFor(module, typeOnly) {
2158
+ return this.targets.filter(t => (0, package_name_1.sameModule)(t.module, module) && t.typeOnly === typeOnly).length > 1
2159
+ ? this.multiLine
2160
+ : undefined;
2161
+ }
2162
+ /** A name's place in the statement, whether its move has run yet or not. */
2163
+ positionOf(name) {
2164
+ const bound = this.bound.indexOf(name);
2165
+ return bound >= 0 ? bound : this.written.indexOf(name);
2166
+ }
2167
+ /**
2168
+ * Where `name` goes among a list's `names` to keep the order the statement wrote them in,
2169
+ * or undefined where the list holds a name the statement did not.
2170
+ */
2171
+ insertionIndex(names, name) {
2172
+ const position = this.positionOf(name);
2173
+ const positions = names.map(n => this.positionOf(n));
2174
+ if (position < 0 || positions.some(p => p < 0)) {
2175
+ return undefined;
2176
+ }
2177
+ const after = positions.findIndex(p => p > position);
2178
+ return after < 0 ? names.length : after;
2179
+ }
2180
+ }
2181
+ exports.RebindGroup = RebindGroup;
2182
+ /** Orders the imports a rebind group leaves by module and ignoring case, as `OrderImports` does. */
2183
+ function compareModules(a, b) {
2184
+ return (a !== null && a !== void 0 ? a : '').toLowerCase().localeCompare((b !== null && b !== void 0 ? b : '').toLowerCase());
2185
+ }
2186
+ /**
2187
+ * `cu` with the run of statements `ids` names sorted by module, each slot keeping its prefix so that a
2188
+ * header or blank line before the run stays where it is. A run that is not contiguous is left alone.
2189
+ */
2190
+ function withRunSorted(cu, ids) {
2191
+ const indices = cu.statements.flatMap((s, i) => s.element && ids.has(s.element.id) ? [i] : []);
2192
+ if (indices.length < 2 || indices[indices.length - 1] - indices[0] !== indices.length - 1) {
2193
+ return cu;
2194
+ }
2195
+ const slots = indices.map(i => cu.statements[i]);
2196
+ const moduleOf = (s) => {
2197
+ var _a;
2198
+ return ((_a = s.element) === null || _a === void 0 ? void 0 : _a.kind) === tree_1.JS.Kind.Import
2199
+ ? importedModule(s.element)
2200
+ : undefined;
2201
+ };
2202
+ const sorted = [...slots].sort((a, b) => compareModules(moduleOf(a), moduleOf(b)));
2203
+ if (sorted.every((s, k) => s === slots[k])) {
2204
+ return cu;
2205
+ }
2206
+ const statements = [...cu.statements];
2207
+ indices.forEach((i, k) => {
2208
+ statements[i] = Object.assign(Object.assign({}, sorted[k]), { element: Object.assign(Object.assign({}, sorted[k].element), { prefix: slots[k].element.prefix }) });
2209
+ });
2210
+ return Object.assign(Object.assign({}, cu), { statements });
2211
+ }
2040
2212
  /**
2041
2213
  * Moves the binding `from` names to `to`, binding it under `boundName`, and the file's references
2042
2214
  * follow a `boundName` of its own. Whether the statement is rewritten in place or replaced is decided
@@ -2046,12 +2218,13 @@ const esmStyle = { default: ImportStyle.ES6Default, namespace: ImportStyle.ES6Na
2046
2218
  * it, but a rebind moves one that is still in use — removal here has to be unconditional.
2047
2219
  */
2048
2220
  class RebindImport extends visitor_1.JavaScriptVisitor {
2049
- constructor(from, to, localName, boundName) {
2221
+ constructor(from, to, localName, boundName, group) {
2050
2222
  super();
2051
2223
  this.from = from;
2052
2224
  this.to = to;
2053
2225
  this.localName = localName;
2054
2226
  this.boundName = boundName;
2227
+ this.group = group;
2055
2228
  this.transformedInPlace = false;
2056
2229
  this.typeOnly = false;
2057
2230
  this.viaRequire = false;
@@ -2060,14 +2233,36 @@ class RebindImport extends visitor_1.JavaScriptVisitor {
2060
2233
  return this.boundName !== this.localName;
2061
2234
  }
2062
2235
  async visitJsCompilationUnit(cu, p) {
2236
+ var _a, _b;
2063
2237
  this.cu = cu;
2064
2238
  this.movedTypes = movedTypesIn(cu, this.from, this.to, true);
2065
2239
  let visited = await super.visitJsCompilationUnit(cu, p);
2066
- if (this.dropped !== undefined) {
2067
- visited = withoutStatement(visited, this.dropped);
2240
+ const dropped = this.dropped;
2241
+ const droppedAt = dropped === undefined ? -1 : visited.statements.findIndex(s => { var _a; return ((_a = s.element) === null || _a === void 0 ? void 0 : _a.id) === dropped; });
2242
+ if (droppedAt >= 0) {
2243
+ const next = (_a = visited.statements[droppedAt + 1]) === null || _a === void 0 ? void 0 : _a.element;
2244
+ const prefix = visited.statements[droppedAt].element.prefix;
2245
+ if (this.group !== undefined) {
2246
+ this.group.dropped = { prefix, next: next && { id: next.id, prefix: next.prefix } };
2247
+ }
2248
+ visited = withoutStatement(visited, dropped);
2249
+ // An import split from the statement takes its place whole, comments included, which
2250
+ // `withoutStatement` keeps only for a file header.
2251
+ if (next !== undefined && ((_b = this.group) === null || _b === void 0 ? void 0 : _b.created.includes(next.id))) {
2252
+ visited = Object.assign(Object.assign({}, visited), { statements: visited.statements.map(s => {
2253
+ var _a;
2254
+ return ((_a = s.element) === null || _a === void 0 ? void 0 : _a.id) === next.id
2255
+ ? Object.assign(Object.assign({}, s), { element: Object.assign(Object.assign({}, s.element), { prefix }) }) : s;
2256
+ }) });
2257
+ }
2258
+ }
2259
+ if (this.transformedInPlace && this.group !== undefined) {
2260
+ visited = withRunSorted(visited, new Set([this.group.sourceId, ...this.group.created]));
2068
2261
  }
2069
2262
  if (!this.transformedInPlace) {
2070
- bindImport(this, {
2263
+ // A pinned alias is the whole answer `bindImport` would give, so the import is queued directly,
2264
+ // where it can carry the group.
2265
+ this.afterVisit.push(new AddImport({
2071
2266
  module: this.fromModule === undefined ? this.to.module : (0, package_name_1.spelledLike)(this.to.module, this.fromModule),
2072
2267
  member: this.to.member,
2073
2268
  alias: this.boundName,
@@ -2076,12 +2271,12 @@ class RebindImport extends visitor_1.JavaScriptVisitor {
2076
2271
  quoteStyle: this.droppedQuote,
2077
2272
  // The binding keeps its lane, though the file may read otherwise without the statement it left.
2078
2273
  style: this.viaRequire ? ImportStyle.CommonJS : esmStyle[bindingShape(this.to.member)]
2079
- });
2274
+ }, this.boundName, this.group));
2080
2275
  }
2081
2276
  return visited;
2082
2277
  }
2083
2278
  async visitImportDeclaration(jsImport, p) {
2084
- var _a;
2279
+ var _a, _b;
2085
2280
  const imp = await super.visitImportDeclaration(jsImport, p);
2086
2281
  const key = memberName(this.from.member);
2087
2282
  // One call moves one module-scope binding, and no two of those share a local name, so the
@@ -2103,7 +2298,7 @@ class RebindImport extends visitor_1.JavaScriptVisitor {
2103
2298
  return imp;
2104
2299
  }
2105
2300
  this.transformedInPlace = true;
2106
- return (0, mutative_1.create)(imp, draft => {
2301
+ const rewritten = (0, mutative_1.create)(imp, draft => {
2107
2302
  var _a;
2108
2303
  const literal = draft.moduleSpecifier.element;
2109
2304
  respecify(literal, (0, package_name_1.spelledLike)(this.to.module, String(literal.value)));
@@ -2117,6 +2312,9 @@ class RebindImport extends visitor_1.JavaScriptVisitor {
2117
2312
  }
2118
2313
  rewriteNamedSpecifier(draft.importClause, key, (_a = memberName(this.to.member)) !== null && _a !== void 0 ? _a : key, this.boundName);
2119
2314
  });
2315
+ return ((_b = this.group) === null || _b === void 0 ? void 0 : _b.listedSeveral) && this.group.multiLine
2316
+ ? onOneLine(rewritten, detectBraceSpacing(cu))
2317
+ : rewritten;
2120
2318
  }
2121
2319
  async visitVariableDeclarations(declaration, p) {
2122
2320
  var _a;