@openrewrite/rewrite 8.97.0 → 8.97.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.
Files changed (152) hide show
  1. package/README.md +42 -0
  2. package/dist/java/visitor.d.ts +2 -0
  3. package/dist/java/visitor.d.ts.map +1 -1
  4. package/dist/java/visitor.js +7 -3
  5. package/dist/java/visitor.js.map +1 -1
  6. package/dist/javascript/add-import.d.ts +1 -6
  7. package/dist/javascript/add-import.d.ts.map +1 -1
  8. package/dist/javascript/add-import.js +90 -58
  9. package/dist/javascript/add-import.js.map +1 -1
  10. package/dist/javascript/comparator.d.ts +16 -10
  11. package/dist/javascript/comparator.d.ts.map +1 -1
  12. package/dist/javascript/comparator.js +10 -8
  13. package/dist/javascript/comparator.js.map +1 -1
  14. package/dist/javascript/package-json-parser.d.ts +2 -0
  15. package/dist/javascript/package-json-parser.d.ts.map +1 -1
  16. package/dist/javascript/package-json-parser.js +27 -15
  17. package/dist/javascript/package-json-parser.js.map +1 -1
  18. package/dist/javascript/package-manager.d.ts +13 -4
  19. package/dist/javascript/package-manager.d.ts.map +1 -1
  20. package/dist/javascript/package-manager.js +134 -17
  21. package/dist/javascript/package-manager.js.map +1 -1
  22. package/dist/javascript/parser.d.ts +2 -0
  23. package/dist/javascript/parser.d.ts.map +1 -1
  24. package/dist/javascript/parser.js +23 -58
  25. package/dist/javascript/parser.js.map +1 -1
  26. package/dist/javascript/print.d.ts.map +1 -1
  27. package/dist/javascript/print.js +1 -1
  28. package/dist/javascript/print.js.map +1 -1
  29. package/dist/javascript/recipes/change-method-target-to-import.d.ts +4 -0
  30. package/dist/javascript/recipes/change-method-target-to-import.d.ts.map +1 -1
  31. package/dist/javascript/recipes/change-method-target-to-import.js +20 -2
  32. package/dist/javascript/recipes/change-method-target-to-import.js.map +1 -1
  33. package/dist/javascript/recipes/dependencies.d.ts +19 -0
  34. package/dist/javascript/recipes/dependencies.d.ts.map +1 -1
  35. package/dist/javascript/recipes/dependencies.js +8 -0
  36. package/dist/javascript/recipes/dependencies.js.map +1 -1
  37. package/dist/javascript/recipes/order-imports.js +3 -3
  38. package/dist/javascript/recipes/order-imports.js.map +1 -1
  39. package/dist/javascript/remove-import.d.ts.map +1 -1
  40. package/dist/javascript/remove-import.js +115 -33
  41. package/dist/javascript/remove-import.js.map +1 -1
  42. package/dist/javascript/scope.d.ts +7 -0
  43. package/dist/javascript/scope.d.ts.map +1 -1
  44. package/dist/javascript/scope.js +23 -0
  45. package/dist/javascript/scope.js.map +1 -1
  46. package/dist/javascript/templating/bindings.d.ts.map +1 -1
  47. package/dist/javascript/templating/bindings.js +0 -4
  48. package/dist/javascript/templating/bindings.js.map +1 -1
  49. package/dist/javascript/templating/comparator.d.ts +32 -79
  50. package/dist/javascript/templating/comparator.d.ts.map +1 -1
  51. package/dist/javascript/templating/comparator.js +35 -628
  52. package/dist/javascript/templating/comparator.js.map +1 -1
  53. package/dist/javascript/templating/debug.d.ts +143 -0
  54. package/dist/javascript/templating/debug.d.ts.map +1 -0
  55. package/dist/javascript/templating/debug.js +769 -0
  56. package/dist/javascript/templating/debug.js.map +1 -0
  57. package/dist/javascript/templating/engine.d.ts +12 -3
  58. package/dist/javascript/templating/engine.d.ts.map +1 -1
  59. package/dist/javascript/templating/engine.js +135 -76
  60. package/dist/javascript/templating/engine.js.map +1 -1
  61. package/dist/javascript/templating/index.d.ts +1 -1
  62. package/dist/javascript/templating/index.d.ts.map +1 -1
  63. package/dist/javascript/templating/index.js.map +1 -1
  64. package/dist/javascript/templating/pattern.d.ts +44 -35
  65. package/dist/javascript/templating/pattern.d.ts.map +1 -1
  66. package/dist/javascript/templating/pattern.js +224 -238
  67. package/dist/javascript/templating/pattern.js.map +1 -1
  68. package/dist/javascript/templating/rewrite.d.ts +4 -0
  69. package/dist/javascript/templating/rewrite.d.ts.map +1 -1
  70. package/dist/javascript/templating/rewrite.js +5 -1
  71. package/dist/javascript/templating/rewrite.js.map +1 -1
  72. package/dist/javascript/templating/template.js +1 -1
  73. package/dist/javascript/templating/template.js.map +1 -1
  74. package/dist/javascript/templating/types.d.ts +38 -35
  75. package/dist/javascript/templating/types.d.ts.map +1 -1
  76. package/dist/javascript/templating/utils.d.ts.map +1 -1
  77. package/dist/javascript/templating/utils.js +18 -9
  78. package/dist/javascript/templating/utils.js.map +1 -1
  79. package/dist/javascript/tree.d.ts +3 -1
  80. package/dist/javascript/tree.d.ts.map +1 -1
  81. package/dist/javascript/tree.js +5 -0
  82. package/dist/javascript/tree.js.map +1 -1
  83. package/dist/json/tree.d.ts +1 -2
  84. package/dist/json/tree.d.ts.map +1 -1
  85. package/dist/json/tree.js +10 -7
  86. package/dist/json/tree.js.map +1 -1
  87. package/dist/rewrite-javascript-version.txt +1 -1
  88. package/dist/rpc/request/get-object.d.ts.map +1 -1
  89. package/dist/rpc/request/get-object.js +5 -3
  90. package/dist/rpc/request/get-object.js.map +1 -1
  91. package/dist/rpc/request/install-recipes.d.ts +10 -0
  92. package/dist/rpc/request/install-recipes.d.ts.map +1 -1
  93. package/dist/rpc/request/install-recipes.js +125 -51
  94. package/dist/rpc/request/install-recipes.js.map +1 -1
  95. package/dist/rpc/request/prepare-recipe.d.ts +1 -0
  96. package/dist/rpc/request/prepare-recipe.d.ts.map +1 -1
  97. package/dist/rpc/request/prepare-recipe.js +13 -6
  98. package/dist/rpc/request/prepare-recipe.js.map +1 -1
  99. package/dist/rpc/rewrite-rpc.d.ts +1 -1
  100. package/dist/rpc/rewrite-rpc.d.ts.map +1 -1
  101. package/dist/rpc/rewrite-rpc.js +2 -3
  102. package/dist/rpc/rewrite-rpc.js.map +1 -1
  103. package/dist/rpc/worker-transport.d.ts +7 -3
  104. package/dist/rpc/worker-transport.d.ts.map +1 -1
  105. package/dist/rpc/worker-transport.js +54 -41
  106. package/dist/rpc/worker-transport.js.map +1 -1
  107. package/dist/test/rewrite-test.d.ts +1 -16
  108. package/dist/test/rewrite-test.d.ts.map +1 -1
  109. package/dist/test/rewrite-test.js +7 -58
  110. package/dist/test/rewrite-test.js.map +1 -1
  111. package/dist/util.d.ts +16 -0
  112. package/dist/util.d.ts.map +1 -1
  113. package/dist/util.js +53 -0
  114. package/dist/util.js.map +1 -1
  115. package/dist/yaml/parser.js +8 -5
  116. package/dist/yaml/parser.js.map +1 -1
  117. package/dist/yaml/print.js +9 -3
  118. package/dist/yaml/print.js.map +1 -1
  119. package/package.json +1 -1
  120. package/src/java/visitor.ts +8 -3
  121. package/src/javascript/add-import.ts +98 -64
  122. package/src/javascript/comparator.ts +66 -53
  123. package/src/javascript/package-json-parser.ts +25 -16
  124. package/src/javascript/package-manager.ts +121 -15
  125. package/src/javascript/parser.ts +29 -81
  126. package/src/javascript/print.ts +1 -2
  127. package/src/javascript/recipes/change-method-target-to-import.ts +23 -3
  128. package/src/javascript/recipes/dependencies.ts +23 -0
  129. package/src/javascript/recipes/order-imports.ts +5 -5
  130. package/src/javascript/remove-import.ts +113 -41
  131. package/src/javascript/scope.ts +23 -0
  132. package/src/javascript/templating/bindings.ts +1 -6
  133. package/src/javascript/templating/comparator.ts +76 -766
  134. package/src/javascript/templating/debug.ts +806 -0
  135. package/src/javascript/templating/engine.ts +138 -77
  136. package/src/javascript/templating/index.ts +1 -0
  137. package/src/javascript/templating/pattern.ts +267 -270
  138. package/src/javascript/templating/rewrite.ts +5 -1
  139. package/src/javascript/templating/template.ts +1 -1
  140. package/src/javascript/templating/types.ts +40 -37
  141. package/src/javascript/templating/utils.ts +4 -12
  142. package/src/javascript/tree.ts +6 -1
  143. package/src/json/tree.ts +9 -7
  144. package/src/rpc/request/get-object.ts +34 -28
  145. package/src/rpc/request/install-recipes.ts +128 -52
  146. package/src/rpc/request/prepare-recipe.ts +18 -15
  147. package/src/rpc/rewrite-rpc.ts +2 -4
  148. package/src/rpc/worker-transport.ts +63 -43
  149. package/src/test/rewrite-test.ts +2 -59
  150. package/src/util.ts +58 -0
  151. package/src/yaml/parser.ts +9 -5
  152. package/src/yaml/print.ts +9 -3
@@ -1,10 +1,12 @@
1
1
  import {JavaScriptVisitor} from "./visitor";
2
- import {isIdentifier, J} from "../java";
3
- import {bindingNames, namesReferencedWithin} from "./scope";
2
+ import {isIdentifier, J, TextComment} from "../java";
3
+ import {bindingNames, namesReferencedAsTypesWithin, namesReferencedWithin, valuesDeclaredAtTopLevel, walk} from "./scope";
4
4
  import {sameModule} from "./package-name";
5
- import {isObjectBindingPattern, JS, JSX} from "./tree";
5
+ import {isObjectBindingPattern, JS, JSX, moduleExportName} from "./tree";
6
6
  import {mapAsync, updateIfChanged} from "../util";
7
7
  import {ElementRemovalFormatter} from "../java";
8
+ import {randomId} from "../uuid";
9
+ import {JavaScriptParser} from "./parser";
8
10
 
9
11
  // Type alias for RightPadded elements to simplify type signatures
10
12
  type RightPaddedElement<T extends J> = {
@@ -71,10 +73,11 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
71
73
  }
72
74
 
73
75
  // If the original last element was removed and we have remaining elements,
74
- // transfer its trailing space to the new last element
76
+ // transfer its trailing layout (space and markers, which carry TrailingComma)
77
+ // to the new last element, so the closing brace keeps its position.
75
78
  if (filtered.length > 0 && originalLastElement?.element && !shouldKeep(originalLastElement.element)) {
76
79
  const lastIdx = filtered.length - 1;
77
- filtered[lastIdx] = {...filtered[lastIdx], after: originalTrailingSpace};
80
+ filtered[lastIdx] = {...filtered[lastIdx], after: originalTrailingSpace, markers: originalLastElement.markers};
78
81
  }
79
82
 
80
83
  return {
@@ -102,7 +105,8 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
102
105
  }
103
106
 
104
107
  override async visitJsCompilationUnit(compilationUnit: JS.CompilationUnit, p: P): Promise<J | undefined> {
105
- const usedNames = collectUsedNames(compilationUnit);
108
+ const memberScoped = this.member !== undefined && this.member !== '*' && this.member !== 'default';
109
+ const usedNames = collectUsedNames(compilationUnit, memberScoped ? jsxFactory(compilationUnit) : undefined);
106
110
  return this.produceJavaScript(compilationUnit, p, async draft => {
107
111
  const formatter = new ElementRemovalFormatter<J>(true); // Preserve file headers from first import
108
112
 
@@ -127,7 +131,7 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
127
131
  // Multi-variable declarations might come as JS.ScopedVariableDeclarations
128
132
  if (statement?.kind === J.Kind.VariableDeclarations) {
129
133
  const varDecl = statement as J.VariableDeclarations;
130
- const result = await this.processRequireFromVarDecls(varDecl, usedNames, p);
134
+ const result = await this.processRequireFromVarDecls(varDecl, usedNames.all, p);
131
135
  if (result === undefined) {
132
136
  formatter.markRemoved(statement);
133
137
  return undefined;
@@ -148,7 +152,7 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
148
152
  for (const v of scopedVarDecl.variables) {
149
153
  const varDecl = v.element;
150
154
  if (varDecl?.kind === J.Kind.VariableDeclarations) {
151
- const result = await this.processRequireFromVarDecls(varDecl as J.VariableDeclarations, usedNames, p);
155
+ const result = await this.processRequireFromVarDecls(varDecl as J.VariableDeclarations, usedNames.all, p);
152
156
  if (result === undefined) {
153
157
  hasChanges = true;
154
158
  varFormatter.markRemoved(varDecl);
@@ -182,21 +186,36 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
182
186
  return stmt;
183
187
  });
184
188
 
185
- draft.statements = newStatements.some(s => s === undefined)
189
+ const filtered = newStatements.some(s => s === undefined)
186
190
  ? newStatements.filter(s => s !== undefined)
187
191
  : newStatements;
188
- draft.eof = await this.visitSpace(compilationUnit.eof, p);
192
+
193
+ const eof = await this.visitSpace(compilationUnit.eof, p);
194
+ if (isTypeScriptSource(compilationUnit.sourcePath) && hasModuleSyntax(compilationUnit.statements) &&
195
+ !hasModuleSyntax(filtered) && !hasJsx(compilationUnit)) {
196
+ // The marker goes after the file's trailing comments, which belong to the code above them.
197
+ const marker = await parseModuleMarker();
198
+ const last = eof.comments[eof.comments.length - 1];
199
+ const prefix = last
200
+ ? {...eof, comments: [...eof.comments.slice(0, -1), {...last, suffix: '\n\n'}]}
201
+ : {...marker.element.prefix, whitespace: filtered.length ? '\n\n' : ''};
202
+ draft.statements = [...filtered, {...marker, element: {...marker.element, id: randomId(), prefix}}];
203
+ draft.eof = last ? {...eof, whitespace: last.suffix, comments: []} : eof;
204
+ } else {
205
+ draft.statements = filtered;
206
+ draft.eof = eof;
207
+ }
189
208
  });
190
209
  }
191
210
 
192
211
  private async processImport(
193
212
  jsImport: JS.Import,
194
- usedNames: Set<string>,
213
+ used: UsedNames,
195
214
  p: P
196
215
  ): Promise<JS.Import | undefined> {
197
216
  // Handle import-equals-require syntax: import util = require("util");
198
217
  if (jsImport.initializer) {
199
- return this.processImportEqualsRequire(jsImport, usedNames, p);
218
+ return this.processImportEqualsRequire(jsImport, used.all, p);
200
219
  }
201
220
 
202
221
  // Check if this import is from the target module
@@ -208,6 +227,7 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
208
227
  if (!importClause) {
209
228
  return jsImport;
210
229
  }
230
+ const usedNames = importClause.typeOnly ? used.types : used.all;
211
231
 
212
232
  // Process default import
213
233
  if (importClause.name) {
@@ -266,13 +286,9 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
266
286
  const identifier = namedBindings as J.Identifier;
267
287
  const name = identifier.simpleName;
268
288
 
269
- // When removing a specific member from a namespace import,
270
- // we can only remove the entire namespace if it's not used
289
+ // A member-scoped removal removes the whole namespace when it's unused
271
290
  if (this.member !== undefined) {
272
- // We're trying to remove a specific member from this namespace
273
- // Check if the namespace itself is used
274
291
  if (!usedNames.has(name)) {
275
- // Namespace is not used, remove the entire import
276
292
  if (!importClause.name) {
277
293
  return undefined;
278
294
  }
@@ -280,7 +296,6 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
280
296
  draft.namedBindings = undefined;
281
297
  }, p);
282
298
  }
283
- // Namespace is used, we can't remove individual members from it
284
299
  } else if (this.shouldRemoveImport(name, usedNames, name)) {
285
300
  // If there's no default import, remove the entire import
286
301
  if (!importClause.name) {
@@ -296,13 +311,9 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
296
311
  const alias = namedBindings as JS.Alias;
297
312
  const aliasName = (alias.alias as J.Identifier).simpleName;
298
313
 
299
- // When removing a specific member from a namespace import,
300
- // we can only remove the entire namespace if it's not used
314
+ // Same as above: member-scoped removal removes unused namespace
301
315
  if (this.member !== undefined) {
302
- // We're trying to remove a specific member from this namespace
303
- // Check if the namespace itself is used
304
316
  if (!usedNames.has(aliasName)) {
305
- // Namespace is not used, remove the entire import
306
317
  if (!importClause.name) {
307
318
  return undefined;
308
319
  }
@@ -310,7 +321,6 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
310
321
  draft.namedBindings = undefined;
311
322
  }, p);
312
323
  }
313
- // Namespace is used, we can't remove individual members from it
314
324
  } else if (this.shouldRemoveImport(aliasName, usedNames, aliasName)) {
315
325
  // If there's no default import, remove the entire import
316
326
  if (!importClause.name) {
@@ -326,7 +336,7 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
326
336
  // Handle named imports: import { a, b } from 'module'
327
337
  if (namedBindings.kind === JS.Kind.NamedImports) {
328
338
  const namedImports = namedBindings as JS.NamedImports;
329
- const updatedImports = await this.processNamedImports(namedImports, usedNames, p);
339
+ const updatedImports = await this.processNamedImports(namedImports, importClause.typeOnly, used, p);
330
340
 
331
341
  if (updatedImports === undefined) {
332
342
  // All named imports were removed
@@ -356,7 +366,7 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
356
366
  */
357
367
  private async processImportEqualsRequire(
358
368
  jsImport: JS.Import,
359
- usedNames: Set<string>,
369
+ usedNames: ReadonlySet<string>,
360
370
  p: P
361
371
  ): Promise<JS.Import | undefined> {
362
372
  const initializer = jsImport.initializer?.element;
@@ -398,7 +408,7 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
398
408
  /**
399
409
  * Check if an identifier should be removed based on usage
400
410
  */
401
- private shouldRemoveIdentifier(name: string, usedNames: Set<string>): boolean {
411
+ private shouldRemoveIdentifier(name: string, usedNames: ReadonlySet<string>): boolean {
402
412
  // For CommonJS and import-equals-require, we're removing the entire import
403
413
  // if the identifier is not used (member is typically undefined for these cases,
404
414
  // or we're checking if a specific binding is used)
@@ -407,7 +417,8 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
407
417
 
408
418
  private async processNamedImports(
409
419
  namedImports: JS.NamedImports,
410
- usedNames: Set<string>,
420
+ typeOnly: boolean,
421
+ used: UsedNames,
411
422
  p: P
412
423
  ): Promise<JS.NamedImports | undefined> {
413
424
  const {filtered, allRemoved} = await this.filterElementsWithPrefixPreservation(
@@ -415,6 +426,7 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
415
426
  (elem: J) => {
416
427
  if (elem.kind === JS.Kind.ImportSpecifier) {
417
428
  const specifier = elem as JS.ImportSpecifier;
429
+ const usedNames = typeOnly || specifier.importType.element ? used.types : used.all;
418
430
  const importName = this.getImportName(specifier);
419
431
  const aliasName = this.getImportAlias(specifier);
420
432
 
@@ -469,7 +481,7 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
469
481
 
470
482
  private async processRequireFromVarDecls(
471
483
  varDecls: J.VariableDeclarations,
472
- usedNames: Set<string>,
484
+ usedNames: ReadonlySet<string>,
473
485
  p: P
474
486
  ): Promise<J.VariableDeclarations | undefined> {
475
487
  // Check if this is a require() call
@@ -539,7 +551,7 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
539
551
 
540
552
  private async processObjectBindingPattern(
541
553
  pattern: JS.ObjectBindingPattern,
542
- usedNames: Set<string>,
554
+ usedNames: ReadonlySet<string>,
543
555
  p: P
544
556
  ): Promise<JS.ObjectBindingPattern | undefined> {
545
557
  const {filtered, allRemoved} = await this.filterElementsWithPrefixPreservation(
@@ -590,11 +602,7 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
590
602
  if (spec?.kind === JS.Kind.Alias) {
591
603
  // Handle aliased import: import { foo as bar }
592
604
  // Return the original name (foo)
593
- const alias = spec as JS.Alias;
594
- const propertyName = alias.propertyName.element;
595
- if (propertyName?.kind === J.Kind.Identifier) {
596
- return (propertyName as J.Identifier).simpleName;
597
- }
605
+ return moduleExportName((spec as JS.Alias).propertyName.element);
598
606
  } else if (spec?.kind === J.Kind.Identifier) {
599
607
  // Handle regular import: import { foo }
600
608
  return (spec as J.Identifier).simpleName;
@@ -626,7 +634,7 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
626
634
 
627
635
  private shouldRemoveImport(
628
636
  name: string,
629
- usedNames: Set<string>,
637
+ usedNames: ReadonlySet<string>,
630
638
  member: string | undefined
631
639
  ): boolean {
632
640
  // If member is specified, we're removing a specific member from the module
@@ -660,20 +668,61 @@ export class RemoveImport<P> extends JavaScriptVisitor<P> {
660
668
 
661
669
  }
662
670
 
671
+ /** The names the file references, and of those, the ones a type-only import answers for. */
672
+ interface UsedNames {
673
+ all: ReadonlySet<string>;
674
+ types: ReadonlySet<string>;
675
+ }
676
+
663
677
  /**
664
678
  * The names the file references, less its own imports: a specifier names what it binds, so counting
665
- * one would read an import as a use of itself.
679
+ * one would read an import as a use of itself. A value read reaches a type-only import, as `typeof X`
680
+ * does, only where the file declares no value of that name itself. The JSX factory is read as a value.
666
681
  */
667
- function collectUsedNames(cu: JS.CompilationUnit): Set<string> {
668
- const used = new Set<string>();
682
+ function collectUsedNames(cu: JS.CompilationUnit, jsxFactory: string | undefined): UsedNames {
683
+ const all = new Set<string>();
684
+ const types = new Set<string>();
669
685
  for (const statement of cu.statements) {
670
686
  const element = statement.element;
671
687
  if (!element || element.kind === JS.Kind.Import) {
672
688
  continue;
673
689
  }
674
- namesReferencedWithin(element).forEach(name => used.add(name));
690
+ namesReferencedWithin(element).forEach(name => all.add(name));
691
+ namesReferencedAsTypesWithin(element).forEach(name => types.add(name));
692
+ }
693
+ const values = valuesDeclaredAtTopLevel(cu);
694
+ all.forEach(name => !values.has(name) && types.add(name));
695
+ if (jsxFactory) {
696
+ all.add(jsxFactory);
697
+ }
698
+ return {all, types};
699
+ }
700
+
701
+ /**
702
+ * The name classic JSX reads its factory through, `React` for `React.createElement`, unless a leading
703
+ * `@jsx` pragma names another. No reference in the file spells it.
704
+ */
705
+ function jsxFactory(cu: JS.CompilationUnit): string | undefined {
706
+ if (!hasJsx(cu)) {
707
+ return undefined;
708
+ }
709
+ const first = cu.statements.find(s => s.element.kind !== JS.Kind.Shebang)?.element;
710
+ for (const comment of [...cu.prefix.comments, ...(first?.prefix.comments ?? [])]) {
711
+ const pragma = comment.kind === J.Kind.TextComment && /@jsx\s+([\w$]+)/.exec((comment as TextComment).text);
712
+ if (pragma) {
713
+ return pragma[1];
714
+ }
675
715
  }
676
- return used;
716
+ return 'React';
717
+ }
718
+
719
+ function hasJsx(cu: JS.CompilationUnit): boolean {
720
+ let found = false;
721
+ walk(cu.statements, node => {
722
+ found ||= node.kind === JS.Kind.JsxTag;
723
+ return !found;
724
+ });
725
+ return found;
677
726
  }
678
727
 
679
728
  function isRequireCall(node: J): boolean {
@@ -683,3 +732,26 @@ function isRequireCall(node: J): boolean {
683
732
  const name = (node as J.MethodInvocation).name;
684
733
  return name?.kind === J.Kind.Identifier && (name as J.Identifier).simpleName === 'require';
685
734
  }
735
+
736
+ function hasModuleSyntax(statements: J.RightPadded<J>[]): boolean {
737
+ return statements.some(({element: s}) =>
738
+ s.kind === JS.Kind.Import || s.kind === JS.Kind.ExportDeclaration || s.kind === JS.Kind.ExportAssignment ||
739
+ ((s as {modifiers?: J.Modifier[]}).modifiers ?? []).some(m => m.keyword === 'export'));
740
+ }
741
+
742
+ // `.mts` and `.cts` files are modules whatever they hold.
743
+ const TS_EXT = /\.tsx?$/;
744
+ function isTypeScriptSource(sourcePath: string): boolean {
745
+ return TS_EXT.test(sourcePath);
746
+ }
747
+
748
+ let moduleMarkerCache: J.RightPadded<J> | undefined;
749
+ async function parseModuleMarker(): Promise<J.RightPadded<J>> {
750
+ if (moduleMarkerCache) return moduleMarkerCache;
751
+ const parser = new JavaScriptParser();
752
+ for await (const sf of parser.parse({text: 'export {};', sourcePath: 'module-marker.ts'})) {
753
+ moduleMarkerCache = (sf as JS.CompilationUnit).statements[0];
754
+ }
755
+ return moduleMarkerCache!;
756
+ }
757
+
@@ -233,6 +233,29 @@ export function namesReferencedWithin(node: unknown, cacheKey: object = node as
233
233
  return names;
234
234
  }
235
235
 
236
+ /**
237
+ * As {@link namesReferencedWithin}, less the names read only as values. An export clause reads
238
+ * with any meaning, so its names are kept.
239
+ */
240
+ export function namesReferencedAsTypesWithin(node: unknown): ReadonlySet<string> {
241
+ const names = new Set<string>();
242
+ treesIn(node).forEach(tree => readReferences(tree, undefined, undefined, 'value', (identifier, reading, scopes) => {
243
+ if (reading !== undefined && reading !== 'value' && reading !== 'write' &&
244
+ !bindingFrame(scopes, identifier.simpleName, reading)) {
245
+ names.add(identifier.simpleName);
246
+ }
247
+ }));
248
+ return names;
249
+ }
250
+
251
+ /** The values a file's own top-level declarations bind, leaving out what its imports bind. */
252
+ export function valuesDeclaredAtTopLevel(cu: JS.CompilationUnit): ReadonlySet<string> {
253
+ const statements = cu.statements.filter(s => s.element?.kind !== JS.Kind.Import);
254
+ return new Set([...declaredNames(statements), ...hoistedNames(statements)]
255
+ .filter(declaration => declaration.meanings.includes('value'))
256
+ .map(declaration => declaration.identifier.simpleName));
257
+ }
258
+
236
259
  /** The scopes enclosing a node, innermost first, each paired with what {@link frameBindings} reads. */
237
260
  interface Frames {
238
261
  node: Tree;
@@ -13,7 +13,7 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import {J, NameTree} from '../../java';
16
+ import {J} from '../../java';
17
17
  import {JavaScriptVisitor} from '../visitor';
18
18
  import {isReference, scopeOf} from '../scope';
19
19
 
@@ -31,11 +31,6 @@ class RenameBindingsVisitor extends JavaScriptVisitor<undefined> {
31
31
  super();
32
32
  }
33
33
 
34
- // The class of `Mocked<T>` and a decorator's name sit behind this hook, which the base visitor holds closed
35
- protected override async visitTypeName<N extends NameTree>(nameTree: N, p: undefined): Promise<N> {
36
- return await this.visit(nameTree, p) as N;
37
- }
38
-
39
34
  override async visitIdentifier(identifier: J.Identifier, p: undefined): Promise<J | undefined> {
40
35
  const renamed = this.renames[identifier.simpleName];
41
36
  if (renamed === undefined || renamed === identifier.simpleName) {