From f009a96ae41ac96db98293ebb3fc15bf2fb8cda6 Mon Sep 17 00:00:00 2001 From: Ika Date: Thu, 12 Oct 2017 02:27:02 -0500 Subject: [PATCH] fix(doc-printer): use `string-width` (#3015) * refactor(doc-printer): use `string-width` * test: add test case --- package.json | 2 +- src/doc-printer.js | 17 +++-------------- tests/unicode/__snapshots__/jsfmt.spec.js.snap | 11 +++++++++++ tests/unicode/combining-characters.js | 3 +++ yarn.lock | 15 +++++++++++---- 5 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 tests/unicode/combining-characters.js diff --git a/package.json b/package.json index e3f5c513..d3f963da 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ "globby": "6.1.0", "graphql": "0.10.1", "ignore": "3.3.5", - "is-fullwidth-code-point": "2.0.0", "jest-docblock": "21.3.0-beta.1", "jest-validate": "21.1.0", "leven": "2.1.0", @@ -43,6 +42,7 @@ "postcss-values-parser": "1.3.1", "remark-frontmatter": "1.1.0", "remark-parse": "4.0.0", + "string-width": "2.1.1", "strip-bom": "3.0.0", "typescript": "2.5.3", "typescript-eslint-parser": "git://github.com/eslint/typescript-eslint-parser.git#9c71a627da36e97da52ed2731d58509c952b67ae", diff --git a/src/doc-printer.js b/src/doc-printer.js index 4ec0c8ca..32f91c7c 100644 --- a/src/doc-printer.js +++ b/src/doc-printer.js @@ -1,6 +1,6 @@ "use strict"; -const isFullwidthCodePoint = require("is-fullwidth-code-point"); +const stringWidth = require("string-width"); const docBuilders = require("./doc-builders"); const concat = docBuilders.concat; @@ -68,7 +68,7 @@ function fits(next, restCommands, width, mustBeFlat) { const doc = x[2]; if (typeof doc === "string") { - width -= getStringWidth(doc); + width -= stringWidth(doc); } else { switch (doc.type) { case "concat": @@ -155,7 +155,7 @@ function printDocToString(doc, options) { if (typeof doc === "string") { out.push(doc); - pos += getStringWidth(doc); + pos += stringWidth(doc); } else { switch (doc.type) { case "cursor": @@ -430,15 +430,4 @@ function printDocToString(doc, options) { return { formatted: out.join("") }; } -function getStringWidth(str) { - let width = 0; - - for (let i = 0; i < str.length; i++) { - const codePoint = str.codePointAt(i); - width += isFullwidthCodePoint(codePoint) ? 2 : 1; - } - - return width; -} - module.exports = { printDocToString }; diff --git a/tests/unicode/__snapshots__/jsfmt.spec.js.snap b/tests/unicode/__snapshots__/jsfmt.spec.js.snap index a8ab3278..93be04fa 100644 --- a/tests/unicode/__snapshots__/jsfmt.spec.js.snap +++ b/tests/unicode/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,16 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`combining-characters.js 1`] = ` +const x = ["ÁÀĀÉÈĒẸE̩Ẹ́É̩Ẹ̀È̩Ẹ̄Ē̩ÍÌĪÓÒŌỌO̩Ọ́Ó̩Ọ̀Ò̩Ọ̄Ō̩ÚÙŪṢS̩áàāéèēẹe̩ẹ́é̩ẹ̀è̩ẹ̄ē̩íìīóòōọo̩ọ́ó̩ọ̀ò̩ọ̄ō̩úùū"]; +//345678901234567890123456789012345678901234567890123456789012345678901234567890 +// 1 2 3 4 5 6 7 8 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +const x = ["ÁÀĀÉÈĒẸE̩Ẹ́É̩Ẹ̀È̩Ẹ̄Ē̩ÍÌĪÓÒŌỌO̩Ọ́Ó̩Ọ̀Ò̩Ọ̄Ō̩ÚÙŪṢS̩áàāéèēẹe̩ẹ́é̩ẹ̀è̩ẹ̄ē̩íìīóòōọo̩ọ́ó̩ọ̀ò̩ọ̄ō̩úùū"]; +//345678901234567890123456789012345678901234567890123456789012345678901234567890 +// 1 2 3 4 5 6 7 8 + +`; + exports[`keys.js 1`] = ` ({'この事はつもり素晴らしいことさ': '35jL9V'}) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/unicode/combining-characters.js b/tests/unicode/combining-characters.js new file mode 100644 index 00000000..c8d0ef85 --- /dev/null +++ b/tests/unicode/combining-characters.js @@ -0,0 +1,3 @@ +const x = ["ÁÀĀÉÈĒẸE̩Ẹ́É̩Ẹ̀È̩Ẹ̄Ē̩ÍÌĪÓÒŌỌO̩Ọ́Ó̩Ọ̀Ò̩Ọ̄Ō̩ÚÙŪṢS̩áàāéèēẹe̩ẹ́é̩ẹ̀è̩ẹ̄ē̩íìīóòōọo̩ọ́ó̩ọ̀ò̩ọ̄ō̩úùū"]; +//345678901234567890123456789012345678901234567890123456789012345678901234567890 +// 1 2 3 4 5 6 7 8 diff --git a/yarn.lock b/yarn.lock index c29dc711..3ae1dd59 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2194,16 +2194,16 @@ is-finite@^1.0.0: dependencies: number-is-nan "^1.0.0" -is-fullwidth-code-point@2.0.0, is-fullwidth-code-point@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz#a3b30a5c4f199183167aaab93beefae3ddfb654f" - is-fullwidth-code-point@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz#ef9e31386f031a7f0d643af82fde50c457ef00cb" dependencies: number-is-nan "^1.0.0" +is-fullwidth-code-point@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz#a3b30a5c4f199183167aaab93beefae3ddfb654f" + is-glob@^2.0.0, is-glob@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-2.0.1.tgz#d096f926a3ded5600f3fdfd91198cb0888c2d863" @@ -4072,6 +4072,13 @@ string-length@^2.0.0: astral-regex "^1.0.0" strip-ansi "^4.0.0" +string-width@2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/string-width/-/string-width-2.1.1.tgz#ab93f27a8dc13d28cac815c462143a6d9012ae9e" + dependencies: + is-fullwidth-code-point "^2.0.0" + strip-ansi "^4.0.0" + string-width@^1.0.1, string-width@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"