@kezh/my-custom-js-converter 1.11.0 → 1.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/util/ast.js +32 -2
- package/package.json +1 -1
package/core/util/ast.js
CHANGED
|
@@ -557,13 +557,43 @@ function stripCommentsDeep(node) {
|
|
|
557
557
|
}
|
|
558
558
|
}
|
|
559
559
|
/**
|
|
560
|
-
*
|
|
561
|
-
*
|
|
560
|
+
* 递归统一字符串字面量的引号风格(统一为双引号)。
|
|
561
|
+
* 通过重写 extra.raw 让 generate 输出统一引号,使仅引号风格差异的代码视为相同。
|
|
562
|
+
* 仅处理 StringLiteral:方法体节点内不存在 DirectiveLiteral('use strict' 只在 Program 级)
|
|
563
|
+
* 与 JSX 属性(parse 未启用 jsx 插件),无需处理。
|
|
564
|
+
*/
|
|
565
|
+
function normalizeStringQuotesDeep(node) {
|
|
566
|
+
if (!node || typeof node !== "object") {
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
if (Array.isArray(node)) {
|
|
570
|
+
node.forEach((child) => normalizeStringQuotesDeep(child));
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
const n = node;
|
|
574
|
+
if (core_1.types.isStringLiteral(n)) {
|
|
575
|
+
// 统一为 JSON.stringify 生成的双引号 raw(含转义),忽略原引号风格
|
|
576
|
+
n.extra = { raw: JSON.stringify(n.value) };
|
|
577
|
+
}
|
|
578
|
+
for (const key of Object.keys(n)) {
|
|
579
|
+
if (key === "loc" || key === "start" || key === "end" || key === "range") {
|
|
580
|
+
continue;
|
|
581
|
+
}
|
|
582
|
+
const val = n[key];
|
|
583
|
+
if (val && typeof val === "object") {
|
|
584
|
+
normalizeStringQuotesDeep(val);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* 生成节点的归一化代码:去注释 + 统一字符串引号 + generate + 压缩空白/换行,
|
|
590
|
+
* 使仅注释、排版或字符串引号风格差异的代码视为相同。
|
|
562
591
|
*/
|
|
563
592
|
function normalizeNodeCode(node) {
|
|
564
593
|
// 克隆避免污染原 AST
|
|
565
594
|
const cloned = JSON.parse(JSON.stringify(node));
|
|
566
595
|
stripCommentsDeep(cloned);
|
|
596
|
+
normalizeStringQuotesDeep(cloned);
|
|
567
597
|
const { code } = (0, generator_1.default)(cloned, { compact: true });
|
|
568
598
|
// 压缩连续空白与换行,统一比较基准
|
|
569
599
|
return code.replace(/\s+/g, " ").trim();
|