JS Build错误,Lint错误非恒定条件

问题描述

我遇到错误尝试构建此代码时出现意外的恒定条件no-constant-condition。 (错误显示在>>>上)

代码用户输入中运行,commandVariable0和commandVariable1从输入中获取数据,在这种情况下, b2b开启 b2b关闭(如果用户应将 > b2b测试,那么它应该发出错误警告:commandVariable1不是有效的设置选项。

任何人都可以帮助我理解为什么它不起作用的情况。如果用户进行了 b2b测试,则接受该测试为有效。

将寻求任何帮助。

const b2b = function(data)
   {
      const commandVariable0 = data.cmd.params.split(" ")[0].toLowerCase();
      const commandVariable1 = data.cmd.params.split(" ")[1].toLowerCase();

  >>> if (commandVariable1 === "on"||"off")
      {
         b2bvar = commandVariable1;
         var output =
         "**```Bot 2 Bot Command Here```**\n" +
         `Embeded Messages : ${b2bvar}\n\n` +
         `Command Variable 0 : ${commandVariable0}\n` +
         `Command Variable 1 : ${commandVariable1}\n`;

         data.color = "info";
         data.text = output;
         return botSend(data);
      }

      data.color = "error";
      data.text =
         ":warning:  **`" + commandVariable1 +
         "`** is not a valid settings option.";
      return botSend(data);
   };

请忽略草率的代码

解决方法

更改您的行
if (commandVariable1 === "on"||"off")

if (commandVariable1 === "on" || commandVariable1 === "off")

怎么了?
在上面的代码行中,JavaScript首先将检查commandVariable1是否等于"on",如果不相等,它将把"off"转换为布尔值,并检查其是否正确。
由于字符串"off"是一个常量,并且在JavaScript中始终会生成true,因此您的linter会抱怨不必要的OR条件。