Skip to content

Commit

Permalink
Fix formatting error on member chain (#15915)
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki authored Jan 12, 2024
1 parent 12f91dd commit 7e862bb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
14 changes: 14 additions & 0 deletions changelog_unreleased/javascript/15915.md
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions src/language-js/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/format/js/chain-expression/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 3 additions & 0 deletions tests/format/js/chain-expression/issue-15912.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test()
.test2()
.test2(thing?.something);

0 comments on commit 7e862bb

Please sign in to comment.