From 3cae07c9df165fe520423dbf42d6d32145003525 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 22 Aug 2022 19:24:14 +0800 Subject: [PATCH] Fix cursor positioning for files containing emoji (#13340) Co-authored-by: Georgii Dolzhykov --- changelog_unreleased/javascript/13340.md | 16 ++++++++++++++++ src/main/core.js | 6 ++++-- .../js/cursor/__snapshots__/jsfmt.spec.js.snap | 15 +++++++++++++++ tests/format/js/cursor/cursor-emoji.js | 1 + 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 changelog_unreleased/javascript/13340.md create mode 100644 tests/format/js/cursor/cursor-emoji.js diff --git a/changelog_unreleased/javascript/13340.md b/changelog_unreleased/javascript/13340.md new file mode 100644 index 000000000..74fb5036a --- /dev/null +++ b/changelog_unreleased/javascript/13340.md @@ -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: [] } +``` diff --git a/src/main/core.js b/src/main/core.js index e71a23d52..38d4e9123 100644 --- a/src/main/core.js +++ b/src/main/core.js @@ -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, diff --git a/tests/format/js/cursor/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/cursor/__snapshots__/jsfmt.spec.js.snap index 0690c25f0..7db63fe75 100644 --- a/tests/format/js/cursor/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/cursor/__snapshots__/jsfmt.spec.js.snap @@ -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 diff --git a/tests/format/js/cursor/cursor-emoji.js b/tests/format/js/cursor/cursor-emoji.js new file mode 100644 index 000000000..c1a8863d5 --- /dev/null +++ b/tests/format/js/cursor/cursor-emoji.js @@ -0,0 +1 @@ +"😀😀😀😀<|>"