【问题标题】:React: expected an assignment or function call and instead saw an expression no-unused-expressionsReact:期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions
【发布时间】:2019-08-15 08:41:05
【问题描述】:

我在我的 react 应用程序中使用了第 3 方库“FoamTree”来进行树可视化。当我在我的组件中导入它的文件“carrotsearch.foamtree.js”时,它在多行中给了我这个错误:

Expected an assignment or function call and instead saw an expression  no-unused-expressions

它在普通的 Javascript 中运行良好。只有在 react 中导入时才会出错 该文件中有很多行给了我这个错误。我分享的很少:

this.j = function (m, k) {
      var f = a[m];
      f || (f = [], a[m] = f);
      f.push(k);
    };
 function m(a) {
      var d = a.O,
          c = a.Sb[0].length - 1;
      a.Ib[c] && (d.setLineDash(a.Ib[c]), d.Uj = a.Ld[c]);
      d.miterLimit = a.Qd[c];
      d.lineWidth = a.Md[c];
      d.shadowBlur = a.ie[c];
      d.shadowOffsetX = a.je[c];
      d.shadowOffsetY = a.ke[c];
      d.font = a.Nc[c].replace("#SIZE#", a.hc[c].toString());

    }
for (e = 0; e < g; e++) {
       h = c[e].kd, m[h.index] = !0, 0 > r * (h.x - k.x) + s * (h.y - k.y) + l * (h.z - k.z) && a.d(b, h);
}
 this.kc = function (a, b) {
      D.V(b) || (n[a] = b, m(a));
      return n[a];
 };

编辑: 当我更改此块时:

this.kc = function (a, b) {
    D.V(b) || (n[a] = b, m(a));
    return n[a];
};

到这里:

  this.kc = function (a, b) {
      if( D.V(b) || (n[a] = b, m(a)) ){
          return n[a];
      }
  };

那么错误就消失了

【问题讨论】:

  • 这与 React 无关。作为一般规则,我通常不会打扰 linting 第三方代码。您可以创建一个 .eslintignore 文件来禁用特定文件、目录和 glob 模式中的 linting。
  • @JoelCornett 如何在我的文件结构中创建和添加 .eslintignore。请帮我解决这个问题

标签: javascript reactjs


【解决方案1】:

通过将其添加到文件顶部来解决问题:

/* eslint-disable */

【讨论】:

  • 您至少应该将其更改为/* eslint-disable no-unused-expressions */ 或在发生此错误的行上方添加// eslint-disable-next-line no-unused-expressions
【解决方案2】:

显然是导致问题的表达式。但是看你的表情,我想你想做的只是:

a[m] = f || []

代替:

 var f = a[m];
      f || (f = [], a[m] = f);

无论如何,allowShortCircuit 应该可以解决您的问题:

/*eslint no-unused-expressions: [
  "error", { 
    "allowShortCircuit": true
  }]*/

更多配置见:

no unused expression

【讨论】:

  • 我认为文件中的代码存在一些问题。它在普通 javascript 中运行良好,它只会在我将它导入 react 时出错,现在当我在功能块中添加 return 时,它不会给我错误。
  • 你是否按照我的回答更改了 eslint 配置?
  • 是的,您的代码删除了该行的错误,但“eslint”的东西不起作用
  • but the 'eslint' thing did not work - 你是什么意思?
【解决方案3】:

这只是一个 lint 错误。你可以忽略它。错误在这一行:

D.V(b) || (n[a] = b, m(a));

如您所见,它是一个表达式(|| 在两个表达式的中间),而不是赋值或函数调用。您可以简单地忽略它或重写它以将表达式作为语句删除,并将其替换为 if 条件或类似的东西(任何用于相同目的的东西)。

【讨论】:

    猜你喜欢
    • 2021-11-17
    • 2021-08-23
    • 2021-05-20
    • 2020-12-10
    • 2020-04-21
    • 2019-11-23
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    相关资源
    最近更新 更多