@openrewrite/rewrite 8.103.1 → 8.104.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.
Files changed (49) hide show
  1. package/dist/java/tree.d.ts +3 -0
  2. package/dist/java/tree.d.ts.map +1 -1
  3. package/dist/java/tree.js +3 -1
  4. package/dist/java/tree.js.map +1 -1
  5. package/dist/javascript/add-import.d.ts +28 -0
  6. package/dist/javascript/add-import.d.ts.map +1 -1
  7. package/dist/javascript/add-import.js +48 -25
  8. package/dist/javascript/add-import.js.map +1 -1
  9. package/dist/javascript/assertions.d.ts.map +1 -1
  10. package/dist/javascript/assertions.js +60 -67
  11. package/dist/javascript/assertions.js.map +1 -1
  12. package/dist/javascript/binding.d.ts +5 -2
  13. package/dist/javascript/binding.d.ts.map +1 -1
  14. package/dist/javascript/binding.js +58 -14
  15. package/dist/javascript/binding.js.map +1 -1
  16. package/dist/javascript/format/prettier-config-loader.d.ts +1 -0
  17. package/dist/javascript/format/prettier-config-loader.d.ts.map +1 -1
  18. package/dist/javascript/format/prettier-config-loader.js +23 -4
  19. package/dist/javascript/format/prettier-config-loader.js.map +1 -1
  20. package/dist/javascript/member-spellings.d.ts +88 -0
  21. package/dist/javascript/member-spellings.d.ts.map +1 -0
  22. package/dist/javascript/member-spellings.js +357 -0
  23. package/dist/javascript/member-spellings.js.map +1 -0
  24. package/dist/javascript/package-manager.d.ts +3 -0
  25. package/dist/javascript/package-manager.d.ts.map +1 -1
  26. package/dist/javascript/package-manager.js +49 -0
  27. package/dist/javascript/package-manager.js.map +1 -1
  28. package/dist/javascript/project-parser.d.ts.map +1 -1
  29. package/dist/javascript/project-parser.js +70 -109
  30. package/dist/javascript/project-parser.js.map +1 -1
  31. package/dist/javascript/scope.d.ts +2 -0
  32. package/dist/javascript/scope.d.ts.map +1 -1
  33. package/dist/javascript/scope.js +2 -1
  34. package/dist/javascript/scope.js.map +1 -1
  35. package/dist/rewrite-javascript-version.txt +1 -1
  36. package/dist/rpc/request/parse-project.d.ts.map +1 -1
  37. package/dist/rpc/request/parse-project.js +20 -23
  38. package/dist/rpc/request/parse-project.js.map +1 -1
  39. package/package.json +1 -1
  40. package/src/java/tree.ts +2 -0
  41. package/src/javascript/add-import.ts +58 -30
  42. package/src/javascript/assertions.ts +43 -57
  43. package/src/javascript/binding.ts +72 -17
  44. package/src/javascript/format/prettier-config-loader.ts +22 -3
  45. package/src/javascript/member-spellings.ts +374 -0
  46. package/src/javascript/package-manager.ts +51 -0
  47. package/src/javascript/project-parser.ts +17 -50
  48. package/src/javascript/scope.ts +2 -2
  49. package/src/rpc/request/parse-project.ts +22 -32
@@ -339,57 +339,24 @@ export class ProjectParser {
339
339
  relativeTo: this.relativeTo
340
340
  });
341
341
 
342
- // Check if Prettier is available
343
- const detection = await prettierLoader.detectPrettier();
344
-
345
- if (detection.available) {
346
- // Prettier is available: add per-file PrettierStyle markers
347
- for await (const sf of parser.parse(...discovered.jsFiles)) {
348
- current++;
349
- this.onProgress?.("parsing", current, totalFiles, sf.sourcePath);
350
-
351
- const prettierMarker = await prettierLoader.getConfigMarker(
352
- path.join(this.relativeTo, sf.sourcePath)
353
- );
354
- if (prettierMarker) {
355
- yield produce(sf, draft => {
356
- draft.markers = replaceMarkerByKind(draft.markers, prettierMarker);
357
- });
358
- } else {
359
- yield sf;
360
- }
361
- }
362
- } else {
363
- // Prettier is NOT available: auto-detect styles from parsed files
364
- this.log("Prettier not found, auto-detecting styles...");
365
-
366
- // Dynamic import to break circular dependency at module load time
367
- // (see buildAutodetectMarker for explanation)
368
- const {Autodetect} = await import("./autodetect.js");
369
- const {JS} = await import("./tree.js");
370
-
371
- const parsedFiles: SourceFile[] = [];
372
-
373
- // Parse all JS files and collect them for sampling
374
- for await (const sf of parser.parse(...discovered.jsFiles)) {
375
- current++;
376
- this.onProgress?.("parsing", current, totalFiles, sf.sourcePath);
377
- if (sf.kind === JS.Kind.CompilationUnit) {
378
- parsedFiles.push(sf);
379
- }
380
- }
381
-
382
- // Sample all parsed files and build Autodetect marker
383
- const detector = Autodetect.detector();
384
- for (const sf of parsedFiles) {
385
- await detector.sample(sf);
342
+ const fallbackFiles: SourceFile[] = [];
343
+ for await (const sf of parser.parse(...discovered.jsFiles)) {
344
+ current++;
345
+ this.onProgress?.("parsing", current, totalFiles, sf.sourcePath);
346
+ const prettierMarker = await prettierLoader.getConfigMarker(
347
+ path.join(this.relativeTo, sf.sourcePath)
348
+ );
349
+ if (prettierMarker) {
350
+ yield produce(sf, draft => {
351
+ draft.markers = replaceMarkerByKind(draft.markers, prettierMarker);
352
+ });
353
+ } else {
354
+ fallbackFiles.push(sf);
386
355
  }
387
- const autodetectMarker = detector.build();
388
- this.log(`Auto-detected styles: indent=${detector.getTabsAndIndentsStyle().indentSize}, ` +
389
- `useTabs=${detector.getTabsAndIndentsStyle().useTabCharacter}`);
390
-
391
- // Yield all files with the Autodetect marker
392
- for (const sf of parsedFiles) {
356
+ }
357
+ if (fallbackFiles.length > 0) {
358
+ const autodetectMarker = await this.buildAutodetectMarker(fallbackFiles);
359
+ for (const sf of fallbackFiles) {
393
360
  yield produce(sf, draft => {
394
361
  draft.markers = replaceMarkerByKind(draft.markers, autodetectMarker);
395
362
  });
@@ -786,8 +786,8 @@ function enumMemberNames(body: J.Block): Bound[] {
786
786
  });
787
787
  }
788
788
 
789
- /** The identifier a dotted name `A.B.C` declares, which is `A`: the rest are members of it. */
790
- function rootName(name: J | undefined): J | undefined {
789
+ /** The root of a dotted name: `A` in `A.B.C`. */
790
+ export function rootName(name: J | undefined): J | undefined {
791
791
  return name?.kind === J.Kind.FieldAccess ? rootName((name as J.FieldAccess).target) : name;
792
792
  }
793
793
 
@@ -20,7 +20,7 @@ import {randomId, UUID} from "../../uuid";
20
20
  import {SourceFile} from "../../tree";
21
21
  import {Parsers} from "../../parser";
22
22
  import {withMetrics} from "./metrics";
23
- import {replaceMarkerByKind} from "../../markers";
23
+ import {Marker, replaceMarkerByKind} from "../../markers";
24
24
  import {setLastParsedProject} from "./last-parsed-project";
25
25
 
26
26
  /**
@@ -177,60 +177,50 @@ export class ParseProject {
177
177
  relativeTo
178
178
  });
179
179
 
180
- // Check if Prettier is available
181
- const detection = await prettierLoader.detectPrettier();
182
-
183
- if (detection.available) {
184
- // Prettier is available: add per-file PrettierStyle markers
185
- const generator = parser.parse(...discovered.jsFiles);
186
-
180
+ const prettierMarkers = new Map<string, Marker>();
181
+ for (const filePath of discovered.jsFiles) {
182
+ const marker = await prettierLoader.getConfigMarker(filePath);
183
+ if (marker) {
184
+ prettierMarkers.set(filePath, marker);
185
+ }
186
+ }
187
+ const generator = parser.parse(...discovered.jsFiles);
188
+ if (prettierMarkers.size === discovered.jsFiles.length) {
187
189
  for (const filePath of discovered.jsFiles) {
190
+ const marker = prettierMarkers.get(filePath)!;
188
191
  const id = randomId();
189
192
  localObjects.set(id, async (id: string) => {
190
193
  const sourceFile: SourceFile = (await generator.next()).value;
191
- // Add PrettierStyle marker if Prettier is available
192
- const prettierMarker = await prettierLoader.getConfigMarker(filePath);
193
194
  return {
194
195
  ...sourceFile,
195
196
  id,
196
- markers: prettierMarker
197
- ? replaceMarkerByKind(sourceFile.markers, prettierMarker)
198
- : sourceFile.markers
197
+ markers: replaceMarkerByKind(sourceFile.markers, marker)
199
198
  };
200
199
  });
201
200
  resultItems.push({
202
201
  id,
203
- sourceFileType: "org.openrewrite.javascript.tree.JS$CompilationUnit", // break cycle
202
+ sourceFileType: "org.openrewrite.javascript.tree.JS$CompilationUnit",
204
203
  sourcePath: path.relative(relativeTo, filePath)
205
204
  });
206
205
  }
207
206
  } else {
208
- // Prettier is NOT available: auto-detect styles from parsed files
209
- // Parse all files first to sample them
210
207
  const parsedFiles: {id: string, sourceFile: SourceFile, filePath: string}[] = [];
211
208
  let fileIndex = 0;
212
- for await (const sourceFile of parser.parse(...discovered.jsFiles)) {
213
- const id = randomId();
214
- parsedFiles.push({id, sourceFile, filePath: discovered.jsFiles[fileIndex++]});
209
+ for await (const sourceFile of generator) {
210
+ parsedFiles.push({id: randomId(), sourceFile, filePath: discovered.jsFiles[fileIndex++]});
215
211
  }
216
-
217
- // Sample all parsed files and build Autodetect marker using ProjectParser helper
218
212
  const autodetectMarker = await projectParser.buildAutodetectMarker(
219
- parsedFiles.map(p => p.sourceFile)
213
+ parsedFiles.filter(p => !prettierMarkers.has(p.filePath)).map(p => p.sourceFile)
220
214
  );
221
-
222
- // Store thunks that add the Autodetect marker
223
215
  for (const {id, sourceFile, filePath} of parsedFiles) {
224
- localObjects.set(id, async (newId: string) => {
225
- return {
226
- ...sourceFile,
227
- id: newId,
228
- markers: replaceMarkerByKind(sourceFile.markers, autodetectMarker)
229
- };
230
- });
216
+ localObjects.set(id, async (newId: string) => ({
217
+ ...sourceFile,
218
+ id: newId,
219
+ markers: replaceMarkerByKind(sourceFile.markers, prettierMarkers.get(filePath) ?? autodetectMarker)
220
+ }));
231
221
  resultItems.push({
232
222
  id,
233
- sourceFileType: "org.openrewrite.javascript.tree.JS$CompilationUnit", // break cycle
223
+ sourceFileType: "org.openrewrite.javascript.tree.JS$CompilationUnit",
234
224
  sourcePath: path.relative(relativeTo, filePath)
235
225
  });
236
226
  }