@kezh/my-custom-js-converter 1.6.2 → 1.6.4
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 +27 -1
- package/package.json +2 -1
package/core/util/ast.js
CHANGED
|
@@ -37,12 +37,37 @@ function parseSrcAst(srcPath) {
|
|
|
37
37
|
});
|
|
38
38
|
return (0, parser_1.parse)(src);
|
|
39
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* 修复 Babel generator 对对象字面量/方法属性间行注释的错位问题。
|
|
42
|
+
*
|
|
43
|
+
* Babel 解析 `prop, // comment\n nextProp` 时,会把 `// comment` 挂到下一个
|
|
44
|
+
* 属性的 leadingComments,生成时输出在下一属性上方,导致注释视觉上"下移一行"。
|
|
45
|
+
* 此函数将 `,\n // comment\n 标识符` 还原为 `, // comment\n 标识符`。
|
|
46
|
+
*/
|
|
47
|
+
function fixMisplacedLineComments(code) {
|
|
48
|
+
// 1. 先处理连续两行注释、其中第一个本应属于上一属性行尾的情况:
|
|
49
|
+
// `prop,\n //c1\n //c2\n nextKey` → `prop, //c1\n //c2\n nextKey`
|
|
50
|
+
// (c1 上移到上一属性行尾,c2 保持独占行)
|
|
51
|
+
code = code.replace(/,\r?\n(\s*)(\/\/[^\r\n]*)\r?\n(\s*)(\/\/[^\r\n]*)\r?\n(\s*)(\w)/g, (_m, sp1, c1, sp2, c2, sp3, key) => `, ${c1}\r\n${sp2}${c2}\r\n${sp3}${key}`);
|
|
52
|
+
// 2. 再处理单个行注释错位:
|
|
53
|
+
// `prop,\n //comment\n nextKey` → `prop, //comment\n nextKey`
|
|
54
|
+
code = code.replace(/,\r?\n(\s*)(\/\/[^\r\n]*)\r?\n(\s*)(\w)/g, (_m, _sp1, comment, sp2, key) => `, ${comment}\r\n${sp2}${key}`);
|
|
55
|
+
return code;
|
|
56
|
+
}
|
|
40
57
|
function outputObjectMethods(outPath, methods) {
|
|
41
58
|
let dist = methods
|
|
42
59
|
.map((m) => {
|
|
43
60
|
var _a;
|
|
61
|
+
// 区分处理 leadingComments:
|
|
62
|
+
// - CommentBlock(/** ... */ JSDoc):属于方法自身的文档注释,encode 扁平化时应删除。
|
|
63
|
+
// - CommentLine(// ...):Babel 会把上一属性/方法的行尾注释错挂到当前方法的
|
|
64
|
+
// leadingComments,删除会导致行尾注释丢失,因此保留,交由 fixMisplacedLineComments
|
|
65
|
+
// 在生成后还原到上一属性行尾。
|
|
44
66
|
if ((_a = m.leadingComments) === null || _a === void 0 ? void 0 : _a.length) {
|
|
45
|
-
m.leadingComments =
|
|
67
|
+
m.leadingComments = m.leadingComments.filter((c) => c.type === "CommentLine");
|
|
68
|
+
if (!m.leadingComments.length) {
|
|
69
|
+
m.leadingComments = undefined;
|
|
70
|
+
}
|
|
46
71
|
}
|
|
47
72
|
return m;
|
|
48
73
|
})
|
|
@@ -50,6 +75,7 @@ function outputObjectMethods(outPath, methods) {
|
|
|
50
75
|
.map((m) => m.code)
|
|
51
76
|
.join(`,\n`);
|
|
52
77
|
dist = dist.replace(/\n/gm, os_1.EOL);
|
|
78
|
+
dist = fixMisplacedLineComments(dist);
|
|
53
79
|
fs_1.default.writeFileSync(outPath, dist, "utf-8");
|
|
54
80
|
}
|
|
55
81
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kezh/my-custom-js-converter",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.4",
|
|
4
4
|
"description": "自定义代码转换器",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"scripts": {
|
|
15
15
|
"build": "npx tsc --build",
|
|
16
|
+
"prepublishOnly": "npm run build",
|
|
16
17
|
"test": "npm run build && node test/run_tests.js",
|
|
17
18
|
"test:manual": "npm run build && node test/manual_review.js"
|
|
18
19
|
},
|