将三元条件运算符转换为 if 语句?
对于如下所示的精简代码,
f&&!f.error?k.button.b==k.button.c.G?k.button.Q(b,e,f,c,d):k.button.b==k.button.c.o&&k.button.P(b,e,f,c,d):(console.error(f),f=f.error.message||chrome.i18n.getMessage("error_tooltip"),k.button.v(b.id,f),d({action:"error"}))
是否有一种自动化工具可以将一行条件运算符转换为一系列 if 语句?
示例 1:
从
(i < 0 ? function1() : function2())
到
if (i < 0) {
function1();
} else {
function2();
}
示例 2:
从
(i < 0 ? function1() : (i === 0 ? function2() : function3()))
到
if (i < 0) {
function1();
} else {
if (i === 0) {
function2();
} else {
function3();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从
使用
这个(然后是 JSBeautifier):
From
to
using this (and then JSBeautifier):
babel-plugin-transform-ternary-to-if-else
我不知道是不是太晚了,这个问题毕竟已经有五年了。
我昨天遇到了完全相同的问题,并设法安装了一个 babel 插件来将条件表达式转换为 if-else 语句。它有一个非常简单的名称: babel-plugin- transform-ternary-to-if-else
重要提示:我确实说过表达式和语句,我们稍后会再讨论它们。
示例
在这里,我将两个示例代码作为输入,并使用插件运行它们。
伟大的?
您可能会想:嗯……没那么多。这些IIFE(立即调用函数表达式)是怎么回事?
IIFE
实际上需要 IIFE。因为,正如我在开头所说,条件表达式是表达式,if 语句是语句。
表达式可以是语句的一部分,这是肯定的。但是一个语句可以成为另一个语句的一部分吗? 不,它不能,除非包裹在 IIFE 内。
当然,作为特殊情况,一个简单的表达式语句可以替换为另一个 if 语句,例如
a1() ? a2() : a3();
可以替换为if (a1()) {a2();} else {a3();}
。但这并不适用于所有情况。所以,我们来了,IIFE。
未来的
好消息是,当 do 表达式提案 纳入 ECMAScript 规范时,我们将没有 IIFE 的冗长。
事实上, babel-plugin-syntax-do-expressions 转换 将表达式转换为条件表达式,这意味着它们彼此完全替换。
链接
babel-plugin-transform-ternary-to-if-否则
do 表达式提案
babel-plugin-syntax -do-表达式
babel-plugin-transform-ternary-to-if-else
I don't know if it is too late, this question is five years old after all.
I ran into the exact same problem yesterday, and managed to put up a babel plugin to transform conditional expressions into if-else statements. It has a pretty straight-forward name: babel-plugin-transform-ternary-to-if-else
Important: I did say expressions and statements, we will get back to them later.
Examples
Here I take the two example code as input, and run them with the plugin.
Great?
You may think: Mmmm... Not so much. What's with all these IIFEs (Immediately Invoked Function Expressions)?
IIFEs
IIFEs are actually needed. Because, as I stated at the beginning, a conditional expression is an expression, an if statement is a statement.
An expression can be part of a statement, that's for sure. But can a statement become part of another statement? No it can not, unless wrapped inside an IIFE.
Of course, as a special case, a simple expression statement can be replaced with another if statement, e.g.
a1() ? a2() : a3();
can be replace withif (a1()) {a2();} else {a3();}
. But this can't apply to all the cases.So, here we are, IIFEs.
The future
Good news is, when the do expressions proposal make it into the ECMAScript spec, we will go without the verbosity of IIFEs.
In fact, babel-plugin-syntax-do-expressions transforms do expressions into conditional expressions, meaning they are the exact replacement of each other.
Links
babel-plugin-transform-ternary-to-if-else
do expressions proposal
babel-plugin-syntax-do-expressions