Fix formatting error on member chain (#15915)

This commit is contained in:
SUZUKI Sosuke authored and GitHub committed 2024-01-13 04:12:55 +09:00
1 parent 12f91dd1a9
commit 7e862bbc41
4 files changed
+43

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
#### Fix formatting error on member chain (#15915 by @sosukesuzuki)
<!-- prettier-ignore -->
```jsx
// Input
test().test2().test2(thing?.something);
// Prettier stable
TypeError: Cannot read properties of undefined (reading 'type')
// Prettier main
test().test2().test2(thing?.something);
```
+10
View File
@@ -405,6 +405,16 @@ function isTestCall(node, parent) {
/** @return {(node: Node) => boolean} */
const skipChainExpression = (fn) => (node) => {
if (node?.type === "ChainExpression") {
// @ts-expect-error
if (!node.object) {
// @ts-expect-error
node.object = node.expression.object;
}
// @ts-expect-error
if (!node.property) {
// @ts-expect-error
node.property = node.expression.property;
}
node = node.expression;
}
@@ -81,6 +81,22 @@ logger.log(
================================================================================
`;
exports[`issue-15912.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
=====================================input======================================
test()
.test2()
.test2(thing?.something);
=====================================output=====================================
test().test2().test2(thing?.something);
================================================================================
`;
exports[`test.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript", "flow"]
@@ -0,0 +1,3 @@
test()
.test2()
.test2(thing?.something);