ESLint:即使变量未被重新分配,prefer-const也不会抛出错误

问题描述:

在我的.eslintrc文件中,prefer-const的值已设置为2,并且我没有覆盖文件中的规则。ESLint:即使变量未被重新分配,prefer-const也不会抛出错误

所有的规则似乎工作正常。即使prefer-const规则似乎工作正常。但是,就这个特定的代码而言,它并不会给我带来任何错误。我不重新分配值,我期望eslint抛出一个错误。我正在使用Atom作为编辑器。

/** 
    * Main render function 
    */ 
    render: function() { 
    // TODO: Show title based on the modal being opened i.e if New FAQ Modal 
    // is opened show NEW FAQ else show the title of the FAQ. 
    let title = ""; 

    return (
     <Modal title={title} 
      className="faq-modal" 
      loading={this.state.isModalLoading} 
      hideHeader={this.state.isModalLoading} 
      width="900px" /> 
    ); 
    } 

原子:1.12.9 棉短绒(原子):1.11.8 棉短绒-eslint(原子):8.1.2 eslint:2.7.0,

+0

有趣。删除'Modal'上的'title'属性,那么它会抛出一个错误。不知道为什么 – Venugopal

+0

当我将你的代码片段粘贴到http://eslint.org/demo/并选择prefer-const时,它会警告标题。 –

+0

@Venugopal你说得对。我得到2个错误。 1.'title'已定义,但从未使用 2.'title'永远不会被重新分配,而是使用'const'。 奇怪。不知道为什么会这样。 –

当变量title用作属性Modal,eslint-plugin-react添加名为eslintUsed的属性(正在使用该属性,由eslint决定是否重新分配该变量)。

我不完全知道这是如何评估和所有,但这是我发现迄今为止。

橡子JSX

acorn-jsx模块决定,在解析表达式JSX,变量是否被重新分配。

文件acorn-jsx\inject.js enter image description here

然后eslint-plugin-react将设置可变的eslintUsed道具。

eslint-插件-反应:

文件eslint-plugin-react\lib\rules\jsx-uses-vars.js enter image description here

文件eslint-plugin-react\lib\util\variable.js enter image description here

最后,该支柱(eslintUsed)正被在prefer-const使用规则在eslint(或不)抛出错误/警告。

希望这会有所帮助。