@openrewrite/rewrite 8.99.0 → 8.100.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 (41) hide show
  1. package/dist/java/formatting-utils.d.ts +3 -1
  2. package/dist/java/formatting-utils.d.ts.map +1 -1
  3. package/dist/java/formatting-utils.js +5 -1
  4. package/dist/java/formatting-utils.js.map +1 -1
  5. package/dist/javascript/add-import.d.ts +31 -2
  6. package/dist/javascript/add-import.d.ts.map +1 -1
  7. package/dist/javascript/add-import.js +54 -8
  8. package/dist/javascript/add-import.js.map +1 -1
  9. package/dist/javascript/amd.d.ts +6 -0
  10. package/dist/javascript/amd.d.ts.map +1 -1
  11. package/dist/javascript/amd.js +18 -0
  12. package/dist/javascript/amd.js.map +1 -1
  13. package/dist/javascript/index.d.ts +4 -3
  14. package/dist/javascript/index.d.ts.map +1 -1
  15. package/dist/javascript/index.js +16 -1
  16. package/dist/javascript/index.js.map +1 -1
  17. package/dist/javascript/package-exported-types.d.ts.map +1 -1
  18. package/dist/javascript/package-exported-types.js +3 -3
  19. package/dist/javascript/package-exported-types.js.map +1 -1
  20. package/dist/javascript/scope.d.ts +8 -0
  21. package/dist/javascript/scope.d.ts.map +1 -1
  22. package/dist/javascript/scope.js +13 -0
  23. package/dist/javascript/scope.js.map +1 -1
  24. package/dist/javascript/templating/precedence.d.ts +27 -0
  25. package/dist/javascript/templating/precedence.d.ts.map +1 -1
  26. package/dist/javascript/templating/precedence.js +37 -3
  27. package/dist/javascript/templating/precedence.js.map +1 -1
  28. package/dist/javascript/tree.d.ts +2 -0
  29. package/dist/javascript/tree.d.ts.map +1 -1
  30. package/dist/javascript/tree.js +6 -0
  31. package/dist/javascript/tree.js.map +1 -1
  32. package/dist/rewrite-javascript-version.txt +1 -1
  33. package/package.json +1 -1
  34. package/src/java/formatting-utils.ts +4 -1
  35. package/src/javascript/add-import.ts +55 -8
  36. package/src/javascript/amd.ts +24 -0
  37. package/src/javascript/index.ts +10 -3
  38. package/src/javascript/package-exported-types.ts +3 -4
  39. package/src/javascript/scope.ts +13 -0
  40. package/src/javascript/templating/precedence.ts +36 -7
  41. package/src/javascript/tree.ts +7 -0
@@ -1117,6 +1117,19 @@ export function isWrite(cursor: Cursor, identifier: J.Identifier): boolean {
1117
1117
  return position(pathTo(cursor, identifier), identifier)[1] === 'write';
1118
1118
  }
1119
1119
 
1120
+ /**
1121
+ * Whether the node at the cursor is assigned to, in the positions {@link isWrite} names, for any node
1122
+ * rather than only an identifier: `x.y` in `x.y = 1`, or a whole destructuring target and each part
1123
+ * of it, as `[a = 1]` and `a = 1` are in `[a = 1] = list` and `for ([a = 1] of lists)`. A `J.Assignment`
1124
+ * that is one is a destructuring default, ESTree's `AssignmentPattern`. A declaration's pattern, as in
1125
+ * `const [a = 1] = list`, names rather than assigns, and so does not count.
1126
+ */
1127
+ export function isAssignmentTarget(cursor: Cursor): boolean {
1128
+ const path = treePath(cursor);
1129
+ const node = path.pop();
1130
+ return node !== undefined && node === cursor.value && path.length > 0 && position(path, node)[1] === 'write';
1131
+ }
1132
+
1120
1133
  /** One identifier referring to a binding, from {@link referencesOf}. */
1121
1134
  export interface Reference {
1122
1135
  /** The identifier's position, holding the tree nodes above it with no padding between them. */
@@ -490,8 +490,8 @@ function hasOmitParentheses(newClass: J.NewClass): boolean {
490
490
  return !!newClass.arguments?.markers?.markers?.some(marker => marker.kind === J.Markers.OmitParentheses);
491
491
  }
492
492
 
493
- /** The JS parser reuses `J.NewClass` for object literals, which have no `class`. */
494
- function isObjectLiteral(expression: J): boolean {
493
+ /** Whether `expression` is an object literal, which the JS parser models as a `J.NewClass` with no `class`. */
494
+ export function isObjectLiteral(expression: J): expression is J.NewClass {
495
495
  return expression.kind === J.Kind.NewClass && !(expression as J.NewClass).class;
496
496
  }
497
497
 
@@ -544,8 +544,11 @@ export function startsWithDeclarationToken(expression: J): boolean {
544
544
  return isObjectLiteral(leftmost) || isFunctionOrClassExpression(leftmost);
545
545
  }
546
546
 
547
- /** A function or class *expression*, which the JS parser wraps in a `JS.StatementExpression`. */
548
- function isFunctionOrClassExpression(expression: J): boolean {
547
+ /**
548
+ * Whether `expression` is a function or class *expression*, which the JS parser wraps in a
549
+ * `JS.StatementExpression`. An arrow function is a `JS.ArrowFunction` and does not count.
550
+ */
551
+ export function isFunctionOrClassExpression(expression: J): expression is JS.StatementExpression {
549
552
  if (expression.kind !== JS.Kind.StatementExpression) {
550
553
  return false;
551
554
  }
@@ -553,8 +556,24 @@ function isFunctionOrClassExpression(expression: J): boolean {
553
556
  return statement?.kind === J.Kind.MethodDeclaration || statement?.kind === J.Kind.ClassDeclaration;
554
557
  }
555
558
 
556
- /** Walks down the left spine, to the token the expression starts with. */
557
- function leftmostExpression(expression: J): J {
559
+ /**
560
+ * The function a function *expression* `function () {}` declares, unwrapped from the
561
+ * `JS.StatementExpression` the JS parser puts around it. Anything else, a function declaration
562
+ * or a method included, answers undefined, and so does a class expression.
563
+ */
564
+ export function functionExpressionOf(expression: J): J.MethodDeclaration | undefined {
565
+ if (expression.kind !== JS.Kind.StatementExpression) {
566
+ return undefined;
567
+ }
568
+ const statement = (expression as JS.StatementExpression).statement;
569
+ return statement?.kind === J.Kind.MethodDeclaration ? statement as J.MethodDeclaration : undefined;
570
+ }
571
+
572
+ /**
573
+ * Walks down the left spine, to the node holding the token the expression is printed starting
574
+ * with: `a` in `a.b().c + 1`. A node with no left operand answers itself.
575
+ */
576
+ export function leftmostExpression(expression: J): J {
558
577
  let current = expression;
559
578
  for (let next = leftmostChild(current); next; next = leftmostChild(current)) {
560
579
  current = next;
@@ -562,7 +581,17 @@ function leftmostExpression(expression: J): J {
562
581
  return current;
563
582
  }
564
583
 
565
- function leftmostChild(expression: J): J | undefined {
584
+ /** Whether `child` is the {@link leftmostChild} of `parent`, so that `parent` is printed starting with it. */
585
+ export function isLeftmostChild(parent: J, child: J): boolean {
586
+ return leftmostChild(parent)?.id === child.id;
587
+ }
588
+
589
+ /**
590
+ * The operand `expression` is printed starting with, one step down its left spine: the left of a
591
+ * binary, the target of a member access, the callee of a call, the operand of a postfix `++`/`--`.
592
+ * Undefined where the expression starts with a token of its own, as a prefix unary or a literal does.
593
+ */
594
+ export function leftmostChild(expression: J): J | undefined {
566
595
  switch (expression.kind) {
567
596
  case J.Kind.Binary:
568
597
  return (expression as J.Binary).left;
@@ -973,6 +973,13 @@ function javascriptVisitorAdapter<P>(visitor: JavaVisitor<P>): JavaVisitor<P> {
973
973
 
974
974
  registerJavaExtensionKinds(Object.values(JS.Kind), javascriptVisitorAdapter);
975
975
 
976
+ /** Whether a file, or a source path, is TypeScript by its extension: `.ts`, `.tsx`, `.mts` or `.cts`, declaration files included. */
977
+ export function isTypeScript(source: JS.CompilationUnit | string): boolean {
978
+ return TYPESCRIPT_EXTENSION.test(typeof source === "string" ? source : source.sourcePath);
979
+ }
980
+
981
+ const TYPESCRIPT_EXTENSION = /\.[cm]?tsx?$/;
982
+
976
983
  export function isJavaScript(tree: any): tree is JS {
977
984
  return KindValues.has(tree["kind"]);
978
985
  }