Repository: prettier/prettier (JavaScript)

Issue:
TypeError: Cannot read properties of undefined (reading 'type')
**Prettier 3.2.0**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuE8DOMAUBKAOlXGAOnRgCYd9CS4MKYALASygHMB+ItCAWzkZatsAbnwgANCAgAHGE2hpkoAIYAnVRADuABTUJFKZQBtNygJ6LJAI1XKwAa34BlZXwAyLOMgBmxtHGtbB2dpO0FkGFUAVwCQfx4mCOjYuAAPaThVJj5YYwAVTKg1JlofP1i0QSM4AEUoiHgyo39JACs0VKcq2vrGpF9m2IBHXrhtDWkDEGU0AFooODgAE2WJVFsmI0EAYV4eZWRpoyM1yrZqgEEYSKYrKPhtTI8FppaQBhgeIwB1ZnRQsBwJz6JhyABuoLMhzAaEsIDBMQAklAVrAnGAsrILiinDAzNVXrFpBp-N9bNJDsTaJkwV5JCx-KoYONlKx9oTJKFVIzDniMmgMUxZGtiSwYN8mEtGMgABwABkkqjgIyYSpZbIO-XKkhgyisEqlDGQZEkUX8eT1BgGbzgPCsyxWSzcyjYUVZcAAYhBVPtruFDPcICAAL7BoA)
<!-- prettier-ignore -->
```sh
--parser typescript
```

**Input:**
<!-- prettier-ignore -->
```tsx
test()
	.test2()
	.test2(thing?.something);

```

**Output:**
<!-- prettier-ignore -->
```tsx
TypeError: Cannot read properties of undefined (reading 'type')
    at Jn (https://prettier.io/lib/plugins/estree.js:7:16595)
    at be (https://prettier.io/lib/plugins/estree.js:9:605)
    at be (https://prettier.io/lib/plugins/estree.js:10:289)
    at https://prettier.io/lib/plugins/estree.js:36:14282
    at Array.every (<anonymous>)
    at https://prettier.io/lib/plugins/estree.js:36:14273
    at Array.some (<anonymous>)
    at Oi (https://prettier.io/lib/plugins/estree.js:36:14252)
    at Kr (https://prettier.io/lib/plugins/estree.js:36:14852)
    at Ta (https://prettier.io/lib/plugins/estree.js:36:58423)
```

**Expected output:**

Not exactly sure, it's an error.

**Why?**

This was not an error in 3.1.x.


Hints:
Thank you! I'll look into this

Patch:
diff --git a/src/language-js/utils/index.js b/src/language-js/utils/index.js
--- a/src/language-js/utils/index.js
+++ b/src/language-js/utils/index.js
@@ -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;
   }
 


Test patch:
diff --git a/tests/format/js/chain-expression/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/chain-expression/__snapshots__/jsfmt.spec.js.snap
--- a/tests/format/js/chain-expression/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/format/js/chain-expression/__snapshots__/jsfmt.spec.js.snap
@@ -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"]
diff --git a/tests/format/js/chain-expression/issue-15912.js b/tests/format/js/chain-expression/issue-15912.js
new file mode 100644
--- /dev/null
+++ b/tests/format/js/chain-expression/issue-15912.js
@@ -0,0 +1,3 @@
+test()
+  .test2()
+  .test2(thing?.something);