diff --git a/changelog_unreleased/javascript/pr-7911.md b/changelog_unreleased/javascript/pr-7911.md new file mode 100644 index 000000000000..a6996241e7a7 --- /dev/null +++ b/changelog_unreleased/javascript/pr-7911.md @@ -0,0 +1,16 @@ +#### Fix bug with holes in array literals ([#7911](https://github.com/prettier/prettier/pull/7911) by [@bakkot](https://github.com/bakkot)) + + +```jsx +// Input +new Test() + .test() + .test([, 0]) + .test(); + +// Prettier stable +[error] in.js: TypeError: Cannot read property 'type' of null + +// Prettier master +new Test().test().test([, 0]).test(); +``` diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 905fe59e871a..8c485a366010 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -958,7 +958,7 @@ function isSimpleCallArgument(node, depth) { ); } if (node.type === "ArrayExpression") { - return node.elements.every(isChildSimple); + return node.elements.every((x) => x == null || isChildSimple(x)); } if ( node.type === "CallExpression" || diff --git a/tests/arrays/__snapshots__/jsfmt.spec.js.snap b/tests/arrays/__snapshots__/jsfmt.spec.js.snap index dbef45d26434..8a431f442806 100644 --- a/tests/arrays/__snapshots__/jsfmt.spec.js.snap +++ b/tests/arrays/__snapshots__/jsfmt.spec.js.snap @@ -18,6 +18,23 @@ const b = ================================================================================ `; +exports[`holes-in-args.js 1`] = ` +====================================options===================================== +parsers: ["flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +new Test() + .test() + .test([, 0]) + .test(); + +=====================================output===================================== +new Test().test().test([, 0]).test(); + +================================================================================ +`; + exports[`last.js 1`] = ` ====================================options===================================== parsers: ["flow", "typescript"] diff --git a/tests/arrays/holes-in-args.js b/tests/arrays/holes-in-args.js new file mode 100644 index 000000000000..953ab176e6aa --- /dev/null +++ b/tests/arrays/holes-in-args.js @@ -0,0 +1,4 @@ +new Test() + .test() + .test([, 0]) + .test();