基于eslint改进js代码

improve the js code based on es lint

我正在使用以下代码 有效 但我们使用的是 ESlint,它会发出警告:

no-unused-expression expected an assignment or function call and instead saw expression

我怎样才能避免这种情况?

_createNew: function(Filecontent, config) {
    var self = this;
    config.position ? self._insertAfter(Filecontent, config) :
        self._addAsLast(Filecontent, config);
    return Filecontent;
},

当我尝试将 return 放在开头时,它不起作用...有什么想法吗?

而不是你的三元:

config.position
  ? self._insertAfter(Filecontent, config)
  : self._addAsLast(Filecontent, config);

您应该使用 if/else 语句。我知道你的方式是单行的,但它的可读性并不高,eslint 规则的存在是有原因的。

if (config.position) {
  self._insertAfter(Filecontent, config)
} else {
  self._addAsLast(Filecontent, config);
}

多了几行,但对于将使用此代码库的每个人来说都更具可读性。

_createNew: function(Filecontent, config) {
    if(config.position) {
        this._insertAfter(Filecontent, config)
    }
    else {
        this._addAsLast(Filecontent, config);
    }

    return Filecontent;
},