哪种eslint规则可以通过爆炸对象来防止变量定义?

时间:2020-05-19 12:45:50

标签: eslint

对此有什么拖延规则?

错误代码:

const {bar} = foo;

function Foo(props) {
  const {bar} = props;
}

好代码:

const bar = foo.bar;

function Foo({bar}){

}

1 个答案:

答案 0 :(得分:0)

到目前为止,我得到了这两个规则,以防止从对象声明变量,并防止没有对象的函数的有效载荷:

'no-restricted-syntax': [
  'warn',
  {
    'selector': 'VariableDeclarator > ObjectPattern',
    'message': 'Can not declare variable by exploding the object.',
  },
  {
    'selector': 'FunctionDeclaration[params] > Identifier.params',
    'message': 'Function\'s payload must be sent with object.',
  },
],

虽然我仍在测试这些行为,但我将其设置为“警告”级别

no-restricted-syntax规则要求CSS之类的选择器在AST树中进行选择,

https://astexplorer.net/#/gist/219f178851d23e46276c7d06460b3b31/d2fd2f8c435e1f3d997beefb4eee268ee2c9abd2