fix(doc-printer): respect CJK width (#3003)

* test: add failing test

* fix(doc-printer): respect CJK width
master
Ika 2017-10-11 00:32:45 -05:00 committed by GitHub
parent abc3b12c5e
commit f7907b9282
5 changed files with 65 additions and 6 deletions

View File

@ -27,6 +27,7 @@
"globby": "6.1.0", "globby": "6.1.0",
"graphql": "0.10.1", "graphql": "0.10.1",
"ignore": "3.3.5", "ignore": "3.3.5",
"is-fullwidth-code-point": "2.0.0",
"jest-docblock": "21.3.0-beta.1", "jest-docblock": "21.3.0-beta.1",
"jest-validate": "21.1.0", "jest-validate": "21.1.0",
"leven": "2.1.0", "leven": "2.1.0",

View File

@ -1,5 +1,7 @@
"use strict"; "use strict";
const isFullwidthCodePoint = require("is-fullwidth-code-point");
const docBuilders = require("./doc-builders"); const docBuilders = require("./doc-builders");
const concat = docBuilders.concat; const concat = docBuilders.concat;
const fill = docBuilders.fill; const fill = docBuilders.fill;
@ -66,7 +68,7 @@ function fits(next, restCommands, width, mustBeFlat) {
const doc = x[2]; const doc = x[2];
if (typeof doc === "string") { if (typeof doc === "string") {
width -= doc.length; width -= getStringWidth(doc);
} else { } else {
switch (doc.type) { switch (doc.type) {
case "concat": case "concat":
@ -153,7 +155,7 @@ function printDocToString(doc, options) {
if (typeof doc === "string") { if (typeof doc === "string") {
out.push(doc); out.push(doc);
pos += doc.length; pos += getStringWidth(doc);
} else { } else {
switch (doc.type) { switch (doc.type) {
case "cursor": case "cursor":
@ -428,4 +430,15 @@ function printDocToString(doc, options) {
return { formatted: out.join("") }; 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 }; module.exports = { printDocToString };

View File

@ -1,5 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`cursorOffset should not be affected by full-width character (stdout) 1`] = `
"const x = [
\\"中文\\",
\\"中文\\",
\\"中文\\",
\\"中文\\",
\\"中文\\",
\\"中文\\",
\\"中文\\",
\\"中文\\",
\\"中文\\",
\\"中文\\",
\\"中文\\"
];
"
`;
exports[`cursorOffset should not be affected by full-width character (write) 1`] = `Array []`;
exports[`write cursorOffset to stderr with --cursor-offset <int> (stderr) 1`] = ` exports[`write cursorOffset to stderr with --cursor-offset <int> (stderr) 1`] = `
"1 "1
" "

View File

@ -7,3 +7,29 @@ describe("write cursorOffset to stderr with --cursor-offset <int>", () => {
status: 0 status: 0
}); });
}); });
describe("cursorOffset should not be affected by full-width character", () => {
runPrettier("cli", ["--cursor-offset", "21"], {
input: `const x = ["中文", "中文", "中文", "中文", "中文", "中文", "中文", "中文", "中文", "中文", "中文"];`
// ^ offset = 21 ^ width = 80
}).test({
/**
* const x = [
* "中文",
* "中文",
* ^ offset = 26
* "中文",
* "中文",
* "中文",
* "中文",
* "中文",
* "中文",
* "中文",
* "中文",
* "中文"
* ];
*/
stderr: "26\n",
status: 0
});
});

View File

@ -2149,16 +2149,16 @@ is-finite@^1.0.0:
dependencies: dependencies:
number-is-nan "^1.0.0" 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: is-fullwidth-code-point@^1.0.0:
version "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" resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz#ef9e31386f031a7f0d643af82fde50c457ef00cb"
dependencies: dependencies:
number-is-nan "^1.0.0" 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: is-glob@^2.0.0, is-glob@^2.0.1:
version "2.0.1" version "2.0.1"
resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-2.0.1.tgz#d096f926a3ded5600f3fdfd91198cb0888c2d863" resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-2.0.1.tgz#d096f926a3ded5600f3fdfd91198cb0888c2d863"