@tsrx/prettier-plugin 0.3.57 → 0.3.59

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsrx/prettier-plugin",
3
- "version": "0.3.57",
3
+ "version": "0.3.59",
4
4
  "description": "Ripple plugin for Prettier",
5
5
  "type": "module",
6
6
  "module": "src/index.js",
@@ -27,7 +27,7 @@
27
27
  "prettier": "^3.8.3"
28
28
  },
29
29
  "dependencies": {
30
- "@tsrx/core": "0.1.7"
30
+ "@tsrx/core": "0.1.9"
31
31
  },
32
32
  "files": [
33
33
  "src/"
package/src/index.js CHANGED
@@ -4286,23 +4286,17 @@ function printTSInterfaceBody(node, path, options, print) {
4286
4286
  * @param {AstPath<AST.TSTypeAliasDeclaration>} path - The AST path
4287
4287
  * @param {RippleFormatOptions} options - Prettier options
4288
4288
  * @param {PrintFn} print - Print callback
4289
- * @returns {Doc[]}
4289
+ * @returns {Doc}
4290
4290
  */
4291
4291
  function printTSTypeAliasDeclaration(node, path, options, print) {
4292
4292
  /** @type {Doc[]} */
4293
- const parts = [];
4294
- parts.push('type ');
4295
- parts.push(node.id.name);
4293
+ const head = ['type ', node.id.name];
4296
4294
 
4297
4295
  if (node.typeParameters) {
4298
- parts.push(path.call(print, 'typeParameters'));
4296
+ head.push(path.call(print, 'typeParameters'));
4299
4297
  }
4300
4298
 
4301
- parts.push(' = ');
4302
- parts.push(path.call(print, 'typeAnnotation'));
4303
- parts.push(semi(options));
4304
-
4305
- return parts;
4299
+ return group([head, ' =', indent([line, path.call(print, 'typeAnnotation')]), semi(options)]);
4306
4300
  }
4307
4301
 
4308
4302
  /**
@@ -5394,19 +5388,22 @@ function printTSConstructorType(node, path, options, print) {
5394
5388
  * @param {AstPath<AST.TSConditionalType>} path - The AST path
5395
5389
  * @param {RippleFormatOptions} options - Prettier options
5396
5390
  * @param {PrintFn} print - Print callback
5397
- * @returns {Doc[]}
5391
+ * @returns {Doc}
5398
5392
  */
5399
5393
  function printTSConditionalType(node, path, options, print) {
5400
- /** @type {Doc[]} */
5401
- const parts = [];
5402
- parts.push(path.call(print, 'checkType'));
5403
- parts.push(' extends ');
5404
- parts.push(path.call(print, 'extendsType'));
5405
- parts.push(' ? ');
5406
- parts.push(path.call(print, 'trueType'));
5407
- parts.push(' : ');
5408
- parts.push(path.call(print, 'falseType'));
5409
- return parts;
5394
+ const trueType = path.call(print, 'trueType');
5395
+ const falseType = path.call(print, 'falseType');
5396
+
5397
+ const shouldIndentTrueType = node.trueType.type !== 'TSConditionalType';
5398
+ const shouldIndentFalseType = node.falseType.type !== 'TSConditionalType';
5399
+
5400
+ return group([
5401
+ path.call(print, 'checkType'),
5402
+ ' extends ',
5403
+ path.call(print, 'extendsType'),
5404
+ indent([line, '? ', shouldIndentTrueType ? indent(trueType) : trueType]),
5405
+ indent([line, ': ', shouldIndentFalseType ? indent(falseType) : falseType]),
5406
+ ]);
5410
5407
  }
5411
5408
 
5412
5409
  /**
@@ -6384,12 +6381,23 @@ function printElement(element, path, options, print) {
6384
6381
  isTextLikeChild && Array.isArray(currentChild.expression?.leadingComments)
6385
6382
  ? currentChild.expression.leadingComments
6386
6383
  : null;
6384
+ const elementBodyLeadingComments =
6385
+ hasTextLeadingComments && node.openingElement
6386
+ ? /** @type {AST.Comment[]} */ (currentChild.leadingComments).filter(
6387
+ (comment) =>
6388
+ comment.context?.containerId === node.metadata?.commentContainerId &&
6389
+ comment.context?.beforeMeaningfulChild &&
6390
+ typeof comment.start === 'number' &&
6391
+ comment.start >= /** @type {AST.NodeWithLocation} */ (node.openingElement).end &&
6392
+ comment.start < /** @type {AST.NodeWithLocation} */ (currentChild).start,
6393
+ )
6394
+ : [];
6387
6395
 
6388
6396
  if (hasTextLeadingComments) {
6389
6397
  for (let j = 0; j < /** @type {AST.Comment[]} */ (currentChild.leadingComments).length; j++) {
6390
6398
  const comment = /** @type {AST.Comment[]} */ (currentChild.leadingComments)[j];
6391
6399
  // Don't lift comments that belong inside the opening tag (handled in attribute section)
6392
- if (!openingTagCommentsSet.has(comment)) {
6400
+ if (!openingTagCommentsSet.has(comment) && !elementBodyLeadingComments.includes(comment)) {
6393
6401
  fallbackElementComments.push(comment);
6394
6402
  }
6395
6403
  }
@@ -6408,10 +6416,15 @@ function printElement(element, path, options, print) {
6408
6416
  ? path.call((childPath) => print(childPath, childPrintArgs), 'children', i)
6409
6417
  : path.call(print, 'children', i);
6410
6418
 
6411
- const childDoc =
6412
- rawExpressionLeadingComments && rawExpressionLeadingComments.length > 0
6413
- ? [...createElementLevelCommentParts(rawExpressionLeadingComments), printedChild]
6414
- : printedChild;
6419
+ const childLeadingCommentParts =
6420
+ elementBodyLeadingComments.length > 0
6421
+ ? createElementLevelCommentParts(elementBodyLeadingComments)
6422
+ : rawExpressionLeadingComments && rawExpressionLeadingComments.length > 0
6423
+ ? createElementLevelCommentParts(rawExpressionLeadingComments)
6424
+ : null;
6425
+ const childDoc = childLeadingCommentParts
6426
+ ? [...childLeadingCommentParts, printedChild]
6427
+ : printedChild;
6415
6428
  finalChildren.push(childDoc);
6416
6429
 
6417
6430
  // Insert element-body comments that fall between this child and the next child (or the closing tag).
package/src/index.test.js CHANGED
@@ -3474,6 +3474,20 @@ const items = [] as unknown[];`;
3474
3474
  expect(result).toBeWithNewline(expected);
3475
3475
  });
3476
3476
 
3477
+ it('should break long nested TypeScript conditional type aliases', async () => {
3478
+ const input = `type PageModelValue<Value> = Value extends ReadonlySignal<unknown> ? Value : Value extends (...args: any[]) => any ? Value : Value extends object ? { [Key in keyof Value]: PageModelValue<Value[Key]> } : never;`;
3479
+ const expected = `type PageModelValue<Value> =
3480
+ Value extends ReadonlySignal<unknown>
3481
+ ? Value
3482
+ : Value extends (...args: any[]) => any
3483
+ ? Value
3484
+ : Value extends object
3485
+ ? { [Key in keyof Value]: PageModelValue<Value[Key]> }
3486
+ : never;`;
3487
+ const result = await format(input);
3488
+ expect(result).toBeWithNewline(expected);
3489
+ });
3490
+
3477
3491
  it('should format TypeScript mapped types (TSMappedType)', async () => {
3478
3492
  const input = `type ReadonlyPartial<T> = { readonly [K in keyof T]?: T[K] }`;
3479
3493
  const expected = `type ReadonlyPartial<T> = { readonly [K in keyof T]?: T[K] };`;