首页 > 解决方案 > Eslint 在 for..of 循环中解构时抛出“no-undef”

问题描述

我正在使用 eslint 和一些定制的 airbnb 标准。

我的 eslintrc 文件,

module.exports = {
  extends: 'airbnb-base',
  rules: {
    'no-restricted-syntax': 'off', // allow for..of loop
  },
};

这是一个也有解构的示例循环。

for ({ bar: foo, baz: fiz, biz } of data) {}

Eslint 抛出错误,

'foo' is not defined. (no-undef)
'fiz' is not defined. (no-undef)
'biz' is not defined. (no-undef)

for...of反模式中这样做吗?

问题是无论我如何定义它都会引发错误。避免这种情况的唯一方法是,

for (const sample of data) {
  let { bar: foo, baz: fiz, biz } = sample;
}

我不想禁用no-undef,因为它对程序的其他领域非常重要。但是,我找不到有关此行为的资源或解释。

可能是因为我禁用了 no-restricted-syntax 并且 airbnb 没有针对这种行为的正确规则吗?

出于好奇,我浏览了repo,发现了一些有趣的问题,

标签: javascripteslinteslint-config-airbnb

解决方案


推荐阅读