Javascript 传入变量的eslint对象速记错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47635509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
eslint object-shorthand error with variable passed in
提问by Pete
I have the following function that is setting up a select2 plugin, which needs selects to stay open if they are multiple but closed if they are not:
我有以下功能正在设置一个 select2 插件,如果它们是多个,则需要选择保持打开状态,如果不是,则关闭:
function setUpSelects($selects, closeOnSelect) {
$selects.each((i, item) => {
const $item = $(item);
$item.select2({
closeOnSelect: closeOnSelect, // <-- error on this line
minimumResultsForSearch: Infinity,
placeholder: $item.data('placeholder') || $item.attr('placeholder'),
});
});
}
setUpSelects($('select:not([multiple])'), false);
setUpSelects($('select[multiple]'), true);
However, when I try to run this code, the eslint checker is giving me an error (on the line shown above) of:
但是,当我尝试运行此代码时,eslint 检查器给了我一个错误(在上面显示的行中):
error Expected property shorthand object-shorthand
错误预期属性简写对象简写
I have done a search and read the docs but it doesn't show how you are meant to use a variable and the unaccepted answer on this questionseems to think it may be a bug in eslint (although I have found no evidence to support that)
我已经进行了搜索并阅读了文档,但它没有显示您打算如何使用变量,并且这个问题上未接受的答案似乎认为它可能是 eslint 中的一个错误(尽管我没有找到任何证据来支持这一点) )
Is there a way to make this work or should I just disable the rule for that line?
有没有办法使这项工作正常进行,或者我应该禁用该行的规则?
回答by Carl Edwards
An excerptfrom eslint regarding the issue:
eslint 关于这个问题的摘录:
Require Object Literal Shorthand Syntax (object-shorthand) - Rule Details
This rule enforces the use of the shorthand syntax. This applies to all methods (including generators) defined in object literals and any properties defined where the key name matches name of the assigned variable.
需要对象文字速记语法 (object-shorthand) - 规则详细信息
此规则强制使用速记语法。这适用于在对象字面量中定义的所有方法(包括生成器)以及在键名称与分配变量的名称匹配的情况下定义的任何属性。
Change
改变
closeOnSelect: closeOnSelect
to just
只是
closeOnSelect
回答by Valeriy Katkov
This rulechecks that object literal shorthand syntaxis used, e.g {a, b}instead of {a: a, b: b}. The rule is configurable, see optionsfor more details.
此规则检查是否使用了对象字面量简写语法,例如{a, b}代替{a: a, b: b}. 该规则是可配置的,有关更多详细信息,请参阅选项。
Despite this shorthand syntax is convenient, in some cases you may not want to force it usage. You can disable the check in your config:
尽管这种速记语法很方便,但在某些情况下您可能不想强制使用它。您可以在配置中禁用检查:
// .eslintrc.json
{
"rules": {
// Disables the rule. You can just remove it,
// if it is not enabled by a parent config.
"object-shorthand": 0
}
}
In case of TSLintthere is a different option:
在TSLint 的情况下,有一个不同的选择:
// tslint.json
{
"rules": {
// Disables the rule. You can just remove it,
// if it is not enabled by a parent config.
"object-literal-shorthand": false
}
}

