@ripple-ts/prettier-plugin 0.2.160 → 0.2.162
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 +2 -2
- package/src/index.js +117 -11
- package/src/index.test.js +47 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ripple-ts/prettier-plugin",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.162",
|
|
4
4
|
"description": "Ripple plugin for Prettier",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "src/index.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"prettier": "^3.6.2",
|
|
28
|
-
"ripple": "0.2.
|
|
28
|
+
"ripple": "0.2.162"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {},
|
|
31
31
|
"files": [
|
package/src/index.js
CHANGED
|
@@ -627,7 +627,7 @@ function printRippleNode(node, path, options, print, args) {
|
|
|
627
627
|
break;
|
|
628
628
|
|
|
629
629
|
case 'Component':
|
|
630
|
-
nodeContent = printComponent(node, path, options, print);
|
|
630
|
+
nodeContent = printComponent(node, path, options, print, innerCommentParts);
|
|
631
631
|
break;
|
|
632
632
|
|
|
633
633
|
case 'ExportNamedDeclaration':
|
|
@@ -1456,13 +1456,59 @@ function printRippleNode(node, path, options, print, args) {
|
|
|
1456
1456
|
if (!node.body || node.body.length === 0) {
|
|
1457
1457
|
// Handle innerComments for empty blocks
|
|
1458
1458
|
if (innerCommentParts.length > 0) {
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1459
|
+
// Check if we need to preserve blank lines between comments
|
|
1460
|
+
if (node.innerComments && node.innerComments.length > 0) {
|
|
1461
|
+
const commentDocs = [];
|
|
1462
|
+
const comments = node.innerComments;
|
|
1463
|
+
|
|
1464
|
+
for (let i = 0; i < comments.length; i++) {
|
|
1465
|
+
const comment = comments[i];
|
|
1466
|
+
const prevComment = i > 0 ? comments[i - 1] : null;
|
|
1467
|
+
|
|
1468
|
+
// Check if there's a blank line before this comment
|
|
1469
|
+
const hasBlankLineBefore =
|
|
1470
|
+
prevComment && getBlankLinesBetweenNodes(prevComment, comment) > 0;
|
|
1471
|
+
|
|
1472
|
+
let commentDoc;
|
|
1473
|
+
if (comment.type === 'Line') {
|
|
1474
|
+
commentDoc = '//' + comment.value;
|
|
1475
|
+
} else if (comment.type === 'Block') {
|
|
1476
|
+
commentDoc = '/*' + comment.value + '*/';
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
commentDocs.push({ doc: commentDoc, hasBlankLineBefore });
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
// Build the content with proper spacing
|
|
1483
|
+
const contentParts = [];
|
|
1484
|
+
for (let i = 0; i < commentDocs.length; i++) {
|
|
1485
|
+
const { doc, hasBlankLineBefore } = commentDocs[i];
|
|
1486
|
+
|
|
1487
|
+
if (i > 0) {
|
|
1488
|
+
// Add blank line if needed (two hardlines = one blank line)
|
|
1489
|
+
if (hasBlankLineBefore) {
|
|
1490
|
+
contentParts.push(hardline);
|
|
1491
|
+
contentParts.push(hardline);
|
|
1492
|
+
} else {
|
|
1493
|
+
contentParts.push(hardline);
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
contentParts.push(doc);
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
nodeContent = group(['{', indent([hardline, concat(contentParts)]), hardline, '}']);
|
|
1501
|
+
break;
|
|
1502
|
+
} else {
|
|
1503
|
+
// Fallback to simple join
|
|
1504
|
+
nodeContent = group([
|
|
1505
|
+
'{',
|
|
1506
|
+
indent([hardline, join(hardline, innerCommentParts)]),
|
|
1507
|
+
hardline,
|
|
1508
|
+
'}',
|
|
1509
|
+
]);
|
|
1510
|
+
break;
|
|
1511
|
+
}
|
|
1466
1512
|
}
|
|
1467
1513
|
nodeContent = '{}';
|
|
1468
1514
|
break;
|
|
@@ -2096,7 +2142,7 @@ function printExportNamedDeclaration(node, path, options, print) {
|
|
|
2096
2142
|
return 'export';
|
|
2097
2143
|
}
|
|
2098
2144
|
|
|
2099
|
-
function printComponent(node, path, options, print) {
|
|
2145
|
+
function printComponent(node, path, options, print, innerCommentParts = []) {
|
|
2100
2146
|
// Use arrays instead of string concatenation
|
|
2101
2147
|
const signatureParts = ['component ', node.id.name];
|
|
2102
2148
|
|
|
@@ -2196,10 +2242,70 @@ function printComponent(node, path, options, print) {
|
|
|
2196
2242
|
// Add the body and closing brace
|
|
2197
2243
|
parts.push(indentedContent, hardline, '}');
|
|
2198
2244
|
} else {
|
|
2199
|
-
// Empty component body
|
|
2245
|
+
// Empty component body - check for inner comments or trailing comments on id
|
|
2246
|
+
// When a component body is empty with only comments, the parser attaches them
|
|
2247
|
+
// as trailingComments on the id node (component name)
|
|
2248
|
+
const commentDocs = [];
|
|
2249
|
+
|
|
2250
|
+
// Check innerComments first (standard case for empty blocks)
|
|
2251
|
+
if (innerCommentParts.length > 0) {
|
|
2252
|
+
for (const part of innerCommentParts) {
|
|
2253
|
+
commentDocs.push({ doc: part, hasBlankLineBefore: false });
|
|
2254
|
+
}
|
|
2255
|
+
}
|
|
2256
|
+
|
|
2257
|
+
// Check for trailing comments on the id (component name)
|
|
2258
|
+
// These are comments that appear inside an empty component body
|
|
2259
|
+
if (node.id && node.id.trailingComments && node.id.trailingComments.length > 0) {
|
|
2260
|
+
const comments = node.id.trailingComments;
|
|
2261
|
+
|
|
2262
|
+
for (let i = 0; i < comments.length; i++) {
|
|
2263
|
+
const comment = comments[i];
|
|
2264
|
+
const prevComment = i > 0 ? comments[i - 1] : null;
|
|
2265
|
+
|
|
2266
|
+
// Check if there's a blank line before this comment
|
|
2267
|
+
const hasBlankLineBefore =
|
|
2268
|
+
prevComment && getBlankLinesBetweenNodes(prevComment, comment) > 0;
|
|
2269
|
+
|
|
2270
|
+
let commentDoc;
|
|
2271
|
+
if (comment.type === 'Line') {
|
|
2272
|
+
commentDoc = '//' + comment.value;
|
|
2273
|
+
} else if (comment.type === 'Block') {
|
|
2274
|
+
commentDoc = '/*' + comment.value + '*/';
|
|
2275
|
+
}
|
|
2276
|
+
|
|
2277
|
+
commentDocs.push({ doc: commentDoc, hasBlankLineBefore });
|
|
2278
|
+
}
|
|
2279
|
+
}
|
|
2280
|
+
|
|
2281
|
+
if (commentDocs.length > 0) {
|
|
2282
|
+
// Build the content with proper spacing
|
|
2283
|
+
const contentParts = [];
|
|
2284
|
+
for (let i = 0; i < commentDocs.length; i++) {
|
|
2285
|
+
const { doc, hasBlankLineBefore } = commentDocs[i];
|
|
2286
|
+
|
|
2287
|
+
if (i > 0) {
|
|
2288
|
+
// Add blank line if needed (two hardlines = one blank line)
|
|
2289
|
+
if (hasBlankLineBefore) {
|
|
2290
|
+
contentParts.push(hardline);
|
|
2291
|
+
contentParts.push(hardline);
|
|
2292
|
+
} else {
|
|
2293
|
+
contentParts.push(hardline);
|
|
2294
|
+
}
|
|
2295
|
+
}
|
|
2296
|
+
|
|
2297
|
+
contentParts.push(doc);
|
|
2298
|
+
}
|
|
2299
|
+
|
|
2300
|
+
return concat([
|
|
2301
|
+
concat(signatureParts),
|
|
2302
|
+
' ',
|
|
2303
|
+
group(['{', indent([hardline, concat(contentParts)]), hardline, '}']),
|
|
2304
|
+
]);
|
|
2305
|
+
}
|
|
2306
|
+
|
|
2200
2307
|
parts[1] = ' {}';
|
|
2201
2308
|
}
|
|
2202
|
-
|
|
2203
2309
|
return concat(parts);
|
|
2204
2310
|
}
|
|
2205
2311
|
|
package/src/index.test.js
CHANGED
|
@@ -1881,6 +1881,53 @@ const obj2 = #{
|
|
|
1881
1881
|
expect(result).toBeWithNewline(expected);
|
|
1882
1882
|
});
|
|
1883
1883
|
|
|
1884
|
+
it('should preserve comment if the whole component code is commented out', async () => {
|
|
1885
|
+
const expected = `export component Test() {
|
|
1886
|
+
// thing
|
|
1887
|
+
// thing
|
|
1888
|
+
// thing
|
|
1889
|
+
}`;
|
|
1890
|
+
|
|
1891
|
+
const result = await format(expected, { singleQuote: true });
|
|
1892
|
+
expect(result).toBeWithNewline(expected);
|
|
1893
|
+
});
|
|
1894
|
+
|
|
1895
|
+
it('should preserve comment if the whole component code is commented out, including blank lines', async () => {
|
|
1896
|
+
const expected = `export component Test() {
|
|
1897
|
+
// thing
|
|
1898
|
+
// thing
|
|
1899
|
+
/* thing */
|
|
1900
|
+
// thing
|
|
1901
|
+
|
|
1902
|
+
/* thing */
|
|
1903
|
+
// thing
|
|
1904
|
+
|
|
1905
|
+
/* thing */
|
|
1906
|
+
// thing
|
|
1907
|
+
}`;
|
|
1908
|
+
|
|
1909
|
+
const result = await format(expected, { singleQuote: true });
|
|
1910
|
+
expect(result).toBeWithNewline(expected);
|
|
1911
|
+
});
|
|
1912
|
+
|
|
1913
|
+
it('should preserve comment if the whole function code is commented out, including blank lines', async () => {
|
|
1914
|
+
const expected = `export function Test() {
|
|
1915
|
+
// thing
|
|
1916
|
+
// thing
|
|
1917
|
+
/* thing */
|
|
1918
|
+
// thing
|
|
1919
|
+
|
|
1920
|
+
/* thing */
|
|
1921
|
+
// thing
|
|
1922
|
+
|
|
1923
|
+
/* thing */
|
|
1924
|
+
// thing
|
|
1925
|
+
}`;
|
|
1926
|
+
|
|
1927
|
+
const result = await format(expected, { singleQuote: true });
|
|
1928
|
+
expect(result).toBeWithNewline(expected);
|
|
1929
|
+
});
|
|
1930
|
+
|
|
1884
1931
|
it('should preserve comments in arrays with width 80', async () => {
|
|
1885
1932
|
const input = `const arr = [
|
|
1886
1933
|
1,
|