Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
17 changes: 3 additions & 14 deletions 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;
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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 };
11 changes: 11 additions & 0 deletions 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'})
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
3 changes: 3 additions & 0 deletions 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
15 changes: 11 additions & 4 deletions yarn.lock
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down