简体   繁体   English

eslint 中是否有拒绝“分配,然后返回”模式的规则?

[英]Is there a rule in eslint to deny "assign, then return" pattern?

I want to deny using the following pattern in code:我想拒绝在代码中使用以下模式:

function do_something_bad() {
    // bad
    const x = do_something_else();
    return x;
}

function do_something_good() {
    // good
    return do_something_else();
}

It looks very weird for me, and is purely style issue, so eslint should be a good way to handle it (I'm open to suggestions, any other linter is also OK, I just have eslint configured, so ask about it).它对我来说看起来很奇怪,纯粹是风格问题,所以 eslint 应该是处理它的好方法(我愿意接受建议,任何其他 linter 也可以,我只是配置了 eslint,所以问问它)。 Is there any rule (or maybe plugin) I can install/enable to require only the "Good" option?有没有我可以安装/启用的规则(或者插件)只需要“好”选项?

For background, I'm mostly python man, and it is inspired by flake8 R504 (from flake8-return plugin) error.对于背景,我主要是 python 人,它的灵感来自flake8 R504(来自flake8-return插件)错误。 It is also present in tslint Microsoft extensions , but they are "archived and read-only" (rule no-unnecessary-local-variable ).它也存在于tslint Microsoft extensions中,但它们是“存档的和只读的”(规则no-unnecessary-local-variable )。

After some research I found out that "sonarjs" ESLint plugin has prefer-immediate-return rule (enabled by default) for exactly this case.经过一些研究后,我发现“sonarjs” ESLint 插件具有针对这种情况prefer-immediate-return规则(默认情况下启用)。 Running eslint with this plugin reveals all such usages (as well as some other style defects).使用此插件运行 eslint 会揭示所有此类用法(以及一些其他样式缺陷)。

(I'm not affiliated in any way with this plugin) (我与这个插件没有任何关系)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM