@tsrx/prettier-plugin 0.3.94 → 0.3.95
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 +1 -1
- package/src/index.js +10 -0
- package/src/index.test.js +36 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -403,6 +403,16 @@ function binaryExpressionNeedsParens(node, parent) {
|
|
|
403
403
|
if (nodePrecedence === parentPrecedence && node.operator !== parent.operator) {
|
|
404
404
|
return true;
|
|
405
405
|
}
|
|
406
|
+
if (nodePrecedence === parentPrecedence) {
|
|
407
|
+
// Same precedence, same operator: dropping the parens regroups a
|
|
408
|
+
// left-associative chain (`a - (b - c)` !== `a - b - c`; even `+` is
|
|
409
|
+
// non-associative once strings are involved), so the RIGHT operand
|
|
410
|
+
// keeps its parens. `**` is right-associative — there it's the LEFT
|
|
411
|
+
// operand that must keep them.
|
|
412
|
+
if (parent.operator === '**' ? parent.left === node : parent.right === node) {
|
|
413
|
+
return true;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
406
416
|
}
|
|
407
417
|
|
|
408
418
|
return false;
|
package/src/index.test.js
CHANGED
|
@@ -2285,6 +2285,42 @@ const program =
|
|
|
2285
2285
|
expect(result).toBeWithNewline(expected);
|
|
2286
2286
|
});
|
|
2287
2287
|
|
|
2288
|
+
it('should keep parens around the right operand of a same-operator subtraction', async () => {
|
|
2289
|
+
const expected = `const d = a - (b - c);`;
|
|
2290
|
+
|
|
2291
|
+
const result = await format(expected, { singleQuote: true, printWidth: 100 });
|
|
2292
|
+
expect(result).toBeWithNewline(expected);
|
|
2293
|
+
});
|
|
2294
|
+
|
|
2295
|
+
it('should keep parens around the right operand of a same-operator division', async () => {
|
|
2296
|
+
const expected = `const d = a / (b / c);`;
|
|
2297
|
+
|
|
2298
|
+
const result = await format(expected, { singleQuote: true, printWidth: 100 });
|
|
2299
|
+
expect(result).toBeWithNewline(expected);
|
|
2300
|
+
});
|
|
2301
|
+
|
|
2302
|
+
it('should keep parens around a right-side addition under string concatenation', async () => {
|
|
2303
|
+
const expected = `const s = 'x' + (n + 1);`;
|
|
2304
|
+
|
|
2305
|
+
const result = await format(expected, { singleQuote: true, printWidth: 100 });
|
|
2306
|
+
expect(result).toBeWithNewline(expected);
|
|
2307
|
+
});
|
|
2308
|
+
|
|
2309
|
+
it('should drop redundant parens around the left operand of a same-operator addition', async () => {
|
|
2310
|
+
const input = `const s = (a + b) + c;`;
|
|
2311
|
+
const expected = `const s = a + b + c;`;
|
|
2312
|
+
|
|
2313
|
+
const result = await format(input, { singleQuote: true, printWidth: 100 });
|
|
2314
|
+
expect(result).toBeWithNewline(expected);
|
|
2315
|
+
});
|
|
2316
|
+
|
|
2317
|
+
it('should keep parens around the left operand of exponentiation', async () => {
|
|
2318
|
+
const expected = `const p = (a ** b) ** c;`;
|
|
2319
|
+
|
|
2320
|
+
const result = await format(expected, { singleQuote: true, printWidth: 100 });
|
|
2321
|
+
expect(result).toBeWithNewline(expected);
|
|
2322
|
+
});
|
|
2323
|
+
|
|
2288
2324
|
it('should have parents around low-precedence logical expression', async () => {
|
|
2289
2325
|
const input = `files = [...files ?? [], ...dt.files];
|
|
2290
2326
|
files = [...(files ?? []), ...dt.files];`;
|