Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with holes in array literals #7911

Merged
merged 3 commits into from Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions 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))

<!-- prettier-ignore -->
```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();
```
2 changes: 1 addition & 1 deletion src/language-js/utils.js
Expand Up @@ -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));
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be === null ? holes always null

}
if (
node.type === "CallExpression" ||
Expand Down
17 changes: 17 additions & 0 deletions tests/arrays/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -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"]
Expand Down
4 changes: 4 additions & 0 deletions tests/arrays/holes-in-args.js
@@ -0,0 +1,4 @@
new Test()
.test()
.test([, 0])
.test();