ESLint在使用之前不使用

Uni*_*dan 9 javascript reactjs eslint

如何让这个ESLint规则(no-use-before-define)在这些情况下不发出警告;

class App extends React.Component {
    render() { return <div>{messages.helloWorld}</div> }
}

const messages = { helloWorld: 'Hello world!' }
Run Code Online (Sandbox Code Playgroud)

这是一个简化的例子,但我真的想messages在每个组件的文件底部定义(按照惯例).

Rya*_*yan 21

variables对于此规则,您可能对该选项感兴趣.您可以在此处阅读该选项.

你可以在你的配置中.eslintrc,像这样 ......

{
  "no-use-before-define": ["error", { "variables": false }]
}
Run Code Online (Sandbox Code Playgroud)

这将使该规则针对其他事物(例如类和函数)启用,但会为变量放宽它.

  • 有没有办法将“变量”标记为“警告”而不是放宽规则? (3认同)
  • 实际上,为函数启用此规则没有任何意义 - 它们被提升,而类和变量则不然。就像我在下面的评论中所说的那样,全局改变这个规则只会让 linter 跳过在定义变量之前访问变量的非常糟糕的情况。 (2认同)

Aga*_*Aga 11

对函数和类声明禁用规则

"rules": {    
  "no-use-before-define": ["error", {"functions": false, "classes": false}]
}
Run Code Online (Sandbox Code Playgroud)