fix(doc-printer): use `string-width` (#3015)

* refactor(doc-printer): use `string-width`

* test: add test case
master
Ika 2017-10-12 02:27:02 -05:00 committed by GitHub
parent 9f6f3e7355
commit f009a96ae4
5 changed files with 29 additions and 19 deletions

View File

@ -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",

View File

@ -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 };

View File

@ -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'})
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -0,0 +1,3 @@
const x = ["ÁÀĀÉÈĒẸE̩Ẹ́É̩Ẹ̀È̩Ẹ̄Ē̩ÍÌĪÓÒŌỌO̩Ọ́Ó̩Ọ̀Ò̩Ọ̄Ō̩ÚÙŪṢS̩áàāéèēẹe̩ẹ́é̩ẹ̀è̩ẹ̄ē̩íìīóòōọo̩ọ́ó̩ọ̀ò̩ọ̄ō̩úùū"];
//345678901234567890123456789012345678901234567890123456789012345678901234567890
// 1 2 3 4 5 6 7 8

View File

@ -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"