2017-02-24 331 views
1

在我的.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'。 奇怪。不知道爲什麼會這樣。 –

回答

0

當變量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(或不)拋出錯誤/警告。

希望這會有所幫助。

相關問題