Comments (4)
代码中多余的分号分被解析成EmptyStatement
,但并不是所有EmptyStatement
都是多余的,比如if(cond);
这个空语句就是有用的,再比如for(;;);
,我想来想去这种有作用的空语句其实也就这几种情况,排除这些情况,删除其余的空语句可解分号多余的问题。
从ast转文本代码理论上generator可决定要不要添加分号,或者在什么情况下必须添加分号。不过recast
没有提供这个选项,即使有这个功能,估计也不能应用,我猜是为了尽可能的保持原有格式,使用了recast.print
来生成文本代码。
为了保持原格式,会引入新的问题,比如对start.js
中的测试代码稍做调整
const from = `
import a from 'a';
console.log('get A');
var b = console.log();
console.log.bind();
var c = console.log;console.log = func;
`;
我把倒数第1行和倒数第2行人工合并到一行上,有分号,代码正常。经过gogocode
处理后的结果是
import a from 'a';
;
var b = void 0
console.log.bind();
var c = function(){}console.log = func;
按前面人的测试,这就不对了。
如果改成recast.prettyPrint
,可得结果如下
import a from "a";
;
var b = void 0;
console.log.bind();
var c = function() {};
console.log = func;
recast
就会格式化并插入分号。但用recast.prettyPrint
又好像不符合作者的意图。
如果按最大格式的保持原有格式输出,那就需要对用户传入的replacement
语句做识别,因为限制了用户传入的是合法的、能被解析的代码,因此根据情况来插入一个分号语句,或对替换前的语句识别最后有没有分号,如果有则保留也可以。
先插入替换,最后删除多余分号。
from gogocode.
感谢 @kiinlam @zzh3319 提出问题,也感谢 @xinglie 行老师的点拨~
- 多余分号问题已经修复了,我在remove节点的时候增加了一个判断:如果待删除的语句父节点是ExpressionStatement并且只有一个子语句,就删除父节点,同时generate增加了isPretty入参,为true时可以格式化输出
- 压缩代码无分号问题只要将replace语句改为:.replace(
var $_$ = console.log()
,var $_$ = void 0;
)即可
from gogocode.
疑惑+1
const from = console.log('get A');
;
const output = $(from)
.replace(`console.log($_$);`, ``)
.generate();
console.log(output);
log输出了;分号 和预期不一样。
from gogocode.
分号问题 正在寻求解决方案,由于ast中没有分号相关的属性设置,所以不太好解决,请耐心等待~
from gogocode.
Related Issues (20)
- transform failed! HOT 1
- 有没有大神有Ant Design Vue1升级到3
- import.meta.env 无法解析 HOT 1
- replaceSelBySel is not a function HOT 1
- 我在B站观看你哦!
- 文件转换异常,规则:emitsOptions,文件:/Users/zhangsan/workspace/MCP_Vue3/src/commonModal/BillModal.vue Error: replace failed: export default {name: "BillModal"
- 恶性bug-html标签有对象属性值带两个左花括号的情况下,写一句find,就会出问题 HOT 1
- 查找import 重写from 路径报错 HOT 1
- Bug Report From Playground: Your Problem
- ELloading 不兼容导致的报错
- find template 转换报错
- `gogo-code-plugin-prettier` misplacement for innerHTML text with no surrounding spaces
- 报错 SyntaxError: Unexpected closing tag "b",检查代码没有问题 HOT 1
- Bug Report From Playground: ast.find('<template></template>')报错
- 插件编写
- @element-plus/icons is deprecated, so you need to convert it with @element-plus/icons-vue HOT 1
- 可以支持 options API 写法 转 composition API 写法吗 HOT 3
- 即将4202了还有人从B站过来吗?
- 模版字符串如何用函数包裹? HOT 1
- 升级vue是排查多个规则不生效问题 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gogocode.