Switch string-width to eastasianwidth (#14793)
This commit is contained in:
1 parent
8108864d7f
commit
05a32d377f
7 files changed
+129
-36
No files matched your search
@@ -0,0 +1,57 @@
|
||||
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;
|
||||
Reference in new issue
Block a user