Skip to content

Commit

Permalink
Fix cursor tracking inside JSXText (prettier#14163)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored and medikoo committed Feb 13, 2024
1 parent ceba531 commit 4116ea4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -749,7 +749,7 @@ Error: Comment "comment" was not printed. Please report this error!
<Same as input>
```

#### Fix formatting for comments inside JSX attribute ([#14082](https://github.com/prettier/prettier/pull/14082) with by [@fisker](https://github.com/fisker))
#### Fix formatting for comments inside JSX attribute ([#14082](https://github.com/prettier/prettier/pull/14082) by [@fisker](https://github.com/fisker))

<!-- prettier-ignore -->
```jsx
Expand Down
18 changes: 18 additions & 0 deletions changelog_unreleased/javascript/14163.md
@@ -0,0 +1,18 @@
#### Fix cursor tracking inside JSX Text (#14163 by @fisker)

<!-- prettier-ignore -->
```js
// Prettier stable
formatWithCursor(
["<>a", " <div>hi</div>", "</>"].join("\n"),
{ cursorOffset: 3, parser: "babel" }
).cursorOffset;
// -> 2

// Prettier main
(await formatWithCursor(
["<>a", " <div>hi</div>", "</>"].join("\n"),
{ cursorOffset: 3, parser: "babel" }
)).cursorOffset;
// -> 6
```
16 changes: 15 additions & 1 deletion src/language-js/print/jsx.js
Expand Up @@ -13,6 +13,7 @@ import {
ifBreak,
lineSuffixBoundary,
join,
cursor,
} from "../../document/builders.js";
import { willBreak, replaceEndOfLine } from "../../document/utils.js";
import UnexpectedNodeError from "../../utils/unexpected-node-error.js";
Expand Down Expand Up @@ -50,6 +51,7 @@ const isEmptyStringOrAnyLine = (doc) =>
* @typedef {import("../../common/ast-path.js").default} AstPath
* @typedef {import("../types/estree.js").Node} Node
* @typedef {import("../types/estree.js").JSXElement} JSXElement
* @typedef {import("../../document/builders.js").Doc} Doc
*/

// JSX expands children from the inside-out, instead of the outside-in.
Expand Down Expand Up @@ -236,10 +238,22 @@ function printJsxElementInternal(path, options, print) {
// If there is text we use `fill` to fit as much onto each line as possible.
// When there is no text (just tags and expressions) we use `group`
// to output each on a separate line.
const content = containsText
/** @type {Doc} */
let content = containsText
? fill(multilineChildren)
: group(multilineChildren, { shouldBreak: true });

/*
`printJsxChildren` won't call `print` on `JSXText`
When the cursorNode is inside `cursor` won't get print.
*/
if (
options.cursorNode?.type === "JSXText" &&
node.children.includes(options.cursorNode)
) {
content = [cursor, content, cursor];
}

if (isMdxBlock) {
return content;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/format/jsx/cursor/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`in-jsx-text.js format 1`] = `
====================================options=====================================
cursorOffset: 3
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
=====================================input======================================
<>a<|>
<div>hi</div>
</>
=====================================output=====================================
<>
a<|><div>hi</div>
</>;
================================================================================
`;
3 changes: 3 additions & 0 deletions tests/format/jsx/cursor/in-jsx-text.js
@@ -0,0 +1,3 @@
<>a<|>
<div>hi</div>
</>
1 change: 1 addition & 0 deletions tests/format/jsx/cursor/jsfmt.spec.js
@@ -0,0 +1 @@
run_spec(import.meta, ["babel", "typescript", "flow"]);

0 comments on commit 4116ea4

Please sign in to comment.