Fix cursor positioning for files containing emoji (#13340)

Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>
This commit is contained in:
fisker CheungandGeorgii Dolzhykov authored and GitHub committed 2022-08-22 19:24:14 +08:00
1 parent e28ae2670c
commit 3cae07c9df
4 files changed
+36 -2

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
#### Fix cursor positioning for files containing emoji (#13340 by @fisker)
```console
$ cat test.js
const { formatWithCursor } = await import("prettier");
const code = "'😀😀😀😀'";
await formatWithCursor(code, {parser: "babel", cursorOffset: 9})
# Prettier stable
$ node test.js
{ formatted: '"😀😀😀😀";\n', cursorOffset: 5, comments: [] }
# Prettier main
$ node test.js
{ formatted: '"😀😀😀😀";\n', cursorOffset: 9, comments: [] }
```
+4 -2
View File
@@ -105,14 +105,16 @@ async function coreFormat(originalText, opts, addAlignmentSize = 0) {
// diff old and new cursor node texts, with a special cursor
// symbol inserted to find out where it moves to
const oldCursorNodeCharArray = [...oldCursorNodeText];
// eslint-disable-next-line unicorn/prefer-spread
const oldCursorNodeCharArray = oldCursorNodeText.split("");
oldCursorNodeCharArray.splice(
cursorOffsetRelativeToOldCursorNode,
0,
CURSOR
);
const newCursorNodeCharArray = [...newCursorNodeText];
// eslint-disable-next-line unicorn/prefer-spread
const newCursorNodeCharArray = newCursorNodeText.split("");
const cursorNodeDiff = diffArrays(
oldCursorNodeCharArray,
@@ -271,6 +271,21 @@ const y = 5;
================================================================================
`;
exports[`cursor-emoji.js format 1`] = `
====================================options=====================================
cursorOffset: 9
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
=====================================input======================================
"😀😀😀😀<|>"
=====================================output=====================================
"😀😀😀😀<|>";
================================================================================
`;
exports[`file-start-with-comment-1.js format 1`] = `
====================================options=====================================
cursorOffset: 5
+1
View File
@@ -0,0 +1 @@
"😀😀😀😀<|>"