简体   繁体   English

javascript eslint 局部变量是多余的

[英]javascript eslint local variable is redundant

I'm trying to find out how to suppress the eslint error in my javascript code.我试图找出如何在我的 javascript 代码中抑制 eslint 错误。

Local variable 'rtn' is redundant局部变量 'rtn' 是多余的

I found the documentation for this here: https://eslint.org/docs/rules/no-useless-return我在这里找到了相关文档: https://eslint.org/docs/rules/no-useless-return

The docs say:文档说:

If you don't care about disallowing redundant return statements, you can turn off this rule.如果您不关心不允许多余的 return 语句,您可以关闭此规则。

But don't say how to disable this error.但是不要说如何禁用这个错误。 The only example they give is having it enabled: /* eslint no-useless-return: "error" */ .他们给出的唯一示例是启用它: /* eslint no-useless-return: "error" */

I'm new to eslint so how do I do this ?我是 eslint 的新手,所以我该怎么做 I've tried我试过了

/* eslint no-useless-return: "suppress" */
export function dateToYYYYMMDD(theDate : Date, separator  = "-") : string {
  const rtn = theDate.getFullYear() + separator
    + ("0" + (theDate.getMonth() + 1)).substr(-2) + separator
    + ("0" + theDate.getDate()).substr(-2);
  return rtn;
}

but this didn't work.但这没有用。 I tried other values like "ok" but that didn't work.我尝试了其他值,例如“ok”,但没有奏效。 What is the right value to use?什么是正确的使用价值?

The same question but for Java is here: "Local variable is redundant" using Java .同样的问题,但对于 Java 在这里: “局部变量是冗余的”使用 Java The Java annotation @SuppressWarnings("UnnecessaryLocalVariable") does not work in Javascript. Java 注释@SuppressWarnings("UnnecessaryLocalVariable")在 Javascript 中不起作用。 :-) :-)

I think you can solve it inline like this:我认为您可以像这样内联解决它:

// eslint-disable-next-line no-useless-return
export function dateToYYYYMMDD(theDate : Date, separator  = "-") : string {
.....

or you can create an eslint configuration file like .eslintrc.json and disable it in the rules, for example:或者您可以创建一个 eslint 配置文件,如.eslintrc.json并在规则中禁用它,例如:

{
   "globals": {
     "__DEV__": "readonly"
   },
   "env": {
     "es2021": true
   },
   "extends": [
     ....
   ],
   "plugins": [
     ....
   ],
   "rules": {
     "no-useless-return": "off"
   }
}

There is no need to declare a variable and then immediately return it - just return the expression itself without assigning it to a variable first.无需先声明变量然后立即返回它 - 只需返回表达式本身,而无需先将其分配给变量。

export function dateToYYYYMMDD(theDate : Date, separator  = "-") : string {
  return theDate.getFullYear() + separator
    + ("0" + (theDate.getMonth() + 1)).substr(-2) + separator
    + ("0" + theDate.getDate()).substr(-2);
}

So, first thing is, if you want to disable a rule in ESLint, please refer to: https://eslint.org/docs/user-guide/configuring/rules#disabling-rules所以,第一件事是,如果你想禁用 ESLint 中的规则,请参考: https://eslint.org/docs/user-guide/configuring/rules#disabling-rules

Second, it doesn't seem like that specific rule warning makes sense in your code - I have tried to reproduce it in here ( ESLint Demo ) and I get no messages whatsoever after turning no-useless-return on.其次,您的代码中的特定规则警告似乎没有意义 - 我试图在这里重现它( ESLint Demo )并且在打开no-useless-return后我没有收到任何消息。

You might want to double-check your IDE and see if this is really coming from ESLint.您可能需要仔细检查您的 IDE 并查看这是否真的来自 ESLint。

I know that an extension such as SonarLint would catch such a thing so you have to check your editor to see what's triggering the message you got.我知道像 SonarLint 这样的扩展程序会捕捉到这样的事情,所以你必须检查你的编辑器,看看是什么触发了你收到的消息。

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

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