From d3fbb24fb56ed1be01d05b77f3c79338aa73d412 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 30 Oct 2023 02:57:04 +0800 Subject: [PATCH] Switch `eastasianwidth` to `get-east-asian-width` (#15566) --- package.json | 2 +- scripts/build/build-javascript-module.js | 6 -- .../build/transform/eastasianwidth-module.js | 57 ------------------- src/utils/get-string-width.js | 6 +- yarn.lock | 11 +++- 5 files changed, 13 insertions(+), 69 deletions(-) delete mode 100644 scripts/build/transform/eastasianwidth-module.js diff --git a/package.json b/package.json index 69e065f3b..4c2733207 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,6 @@ "css-units-list": "2.0.1", "dashify": "2.0.0", "diff": "5.1.0", - "eastasianwidth": "0.2.0", "editorconfig": "0.15.3", "emoji-regex": "10.3.0", "escape-string-regexp": "5.0.0", @@ -63,6 +62,7 @@ "find-cache-dir": "5.0.0", "find-parent-dir": "0.3.1", "flow-parser": "0.217.0", + "get-east-asian-width": "1.2.0", "get-stdin": "9.0.0", "graphql": "16.8.1", "hermes-parser": "0.16.0", diff --git a/scripts/build/build-javascript-module.js b/scripts/build/build-javascript-module.js index 0aaa9171d..adbab2e67 100644 --- a/scripts/build/build-javascript-module.js +++ b/scripts/build/build-javascript-module.js @@ -16,7 +16,6 @@ import esbuildPluginShimCommonjsObjects from "./esbuild-plugins/shim-commonjs-ob import esbuildPluginPrimitiveDefine from "./esbuild-plugins/primitive-define.js"; import esbuildPluginAddDefaultExport from "./esbuild-plugins/add-default-export.js"; import transform from "./transform/index.js"; -import transformEastAsianWidthModule from "./transform/eastasianwidth-module.js"; import { getPackageFile } from "./utils.js"; const { dirname, readJsonSync, require } = createEsmUtils(import.meta); @@ -92,11 +91,6 @@ function getEsbuildOptions({ file, files, shouldCollectLicenses, cliOptions }) { .replace(/\nfunction _os\(\).*?\n}/s, "") .replace(/\nfunction _detectNewline\(\).*?\n}/s, ""), }, - // Reduce size - { - module: require.resolve("eastasianwidth"), - process: transformEastAsianWidthModule, - }, ]; const define = { diff --git a/scripts/build/transform/eastasianwidth-module.js b/scripts/build/transform/eastasianwidth-module.js deleted file mode 100644 index 95f894968..000000000 --- a/scripts/build/transform/eastasianwidth-module.js +++ /dev/null @@ -1,57 +0,0 @@ -import { parse } from "@babel/parser"; -import { traverseFast as traverse } from "@babel/types"; -import babelGenerator from "@babel/generator"; - -const generate = babelGenerator.default; - -function transformEastAsianWidthModule(original) { - let eastAsianWidthFunction; - traverse(parse(original), (node) => { - // We only need `eaw.eastAsianWidth` - if ( - node.type === "AssignmentExpression" && - node.left.type === "MemberExpression" && - !node.left.computed && - !node.left.optional && - node.left.object.type === "Identifier" && - node.left.object.name === "eaw" && - node.left.property.type === "Identifier" && - node.left.property.name === "eastAsianWidth" - ) { - eastAsianWidthFunction = node.right; - } - }); - - const functionBody = eastAsianWidthFunction.body.body; - - // We only need `IfStatement`s returns `"F"` or `"W"` - for (let index = functionBody.length - 1; index >= 0; index--) { - const node = functionBody[index]; - - if ( - node.type === "IfStatement" && - node.consequent.type === "BlockStatement" && - node.consequent.body.length === 1 && - node.consequent.body[0].type === "ReturnStatement" && - node.consequent.body[0].argument.type === "StringLiteral" - ) { - const { value } = node.consequent.body[0].argument; - if (value !== "F" && value !== "W") { - functionBody.splice(index, 1); - } - } - } - - const ast = parse("export default { eastAsianWidth() {} };", { - sourceType: "module", - }); - Object.assign(ast.program.body[0].declaration.properties[0], { - params: eastAsianWidthFunction.params, - body: eastAsianWidthFunction.body, - }); - - const { code } = generate(ast); - return code; -} - -export default transformEastAsianWidthModule; diff --git a/src/utils/get-string-width.js b/src/utils/get-string-width.js index cc8740804..2138468f9 100644 --- a/src/utils/get-string-width.js +++ b/src/utils/get-string-width.js @@ -1,5 +1,6 @@ import emojiRegex from "emoji-regex"; -import eastAsianWidth from "eastasianwidth"; +// @ts-expect-error -- Special export for us, https://github.com/sindresorhus/get-east-asian-width/pull/6 +import { _isNarrowWidth as isNarrowWidth } from "get-east-asian-width"; const notAsciiRegex = /[^\x20-\x7F]/; @@ -38,8 +39,7 @@ function getStringWidth(text) { continue; } - const code = eastAsianWidth.eastAsianWidth(character); - width += code === "F" || code === "W" ? 2 : 1; + width += isNarrowWidth(codePoint) ? 1 : 2; } return width; diff --git a/yarn.lock b/yarn.lock index 237e7cdea..be58d8705 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3248,7 +3248,7 @@ __metadata: languageName: node linkType: hard -"eastasianwidth@npm:0.2.0, eastasianwidth@npm:^0.2.0": +"eastasianwidth@npm:^0.2.0": version: 0.2.0 resolution: "eastasianwidth@npm:0.2.0" checksum: 7d00d7cd8e49b9afa762a813faac332dee781932d6f2c848dc348939c4253f1d4564341b7af1d041853bc3f32c2ef141b58e0a4d9862c17a7f08f68df1e0f1ed @@ -4326,6 +4326,13 @@ __metadata: languageName: node linkType: hard +"get-east-asian-width@npm:1.2.0": + version: 1.2.0 + resolution: "get-east-asian-width@npm:1.2.0" + checksum: ea55f4d4a42c4b00d3d9be3111bc17eb0161f60ed23fc257c1390323bb780a592d7a8bdd550260fd4627dabee9a118cdfa3475ae54edca35ebcd3bdae04179e3 + languageName: node + linkType: hard + "get-intrinsic@npm:^1.0.2, get-intrinsic@npm:^1.1.1, get-intrinsic@npm:^1.1.3, get-intrinsic@npm:^1.2.0, get-intrinsic@npm:^1.2.1": version: 1.2.1 resolution: "get-intrinsic@npm:1.2.1" @@ -7157,7 +7164,6 @@ __metadata: css-units-list: 2.0.1 dashify: 2.0.0 diff: 5.1.0 - eastasianwidth: 0.2.0 editorconfig: 0.15.3 emoji-regex: 10.3.0 enquirer: 2.4.1 @@ -7185,6 +7191,7 @@ __metadata: find-cache-dir: 5.0.0 find-parent-dir: 0.3.1 flow-parser: 0.217.0 + get-east-asian-width: 1.2.0 get-stdin: 9.0.0 graphql: 16.8.1 hermes-parser: 0.16.0