我怎么能';返回此';在当前JavaScript代码片段中,在不违反我的ESLint规则的情况下满足调用方中的链接?

我怎么能';返回此';在当前JavaScript代码片段中,在不违反我的ESLint规则的情况下满足调用方中的链接?,javascript,ecmascript-6,eslint,Javascript,Ecmascript 6,Eslint,我有以下代码。它工作正常,调用方需要每个部分返回此,因为链接: module.exports = function(res){ return { success: function(content, contentType, resultCode) { sendResponse( res, content, validateContentType(contentType), v

我有以下代码。它工作正常,调用方需要每个部分
返回此
,因为链接:

module.exports = function(res){
return {
    success: function(content, contentType, resultCode) {
        sendResponse(
            res,
            content,
            validateContentType(contentType),
            validateResultCode(resultCode||'ok')
        )
        return this
    },
    error: function(resultCode, content, contentType){
        sendResponse(
            res,
            content,
            validateContentType(contentType),
            validateResultCode(resultCode||'bad_request')
        )
        return this
    },
    end: function(callback){
        res.end()
        callback&&callback()
        return this
    }
}
}
module.exports = res => ({
success: (content, contentType, resultCode) => {
    sendResponse(
        res,
        content,
        validateContentType(contentType),
        validateResultCode(resultCode || 'ok'),
    );
},
error: (resultCode, content, contentType) => {
    sendResponse(
        res,
        content,
        validateContentType(contentType),
        validateResultCode(resultCode || 'bad_request'),
    );
},
end: (callback) => {
    res.end();
    if (callback) {
        callback();
        return undefined;
    }
    return undefined;
},
});
问题是我需要我的代码来传递一组
ESLint
规则。上述操作失败,出现以下情况,其中第36行是代码的第一行:

36:18  warning  Unexpected unnamed function                                            func-names
36:26  error    Missing space before function parentheses                              space-before-function-paren
36:31  error    Missing space before opening brace                                     space-before-blocks
38:12  warning  Unexpected unnamed method 'success'                                    func-names
38:20  error    Missing space before function parentheses                              space-before-function-paren
43:34  error    Infix operators must be spaced                                         space-infix-ops
43:41  error    Missing trailing comma                                                 comma-dangle
44:5   error    Missing semicolon                                                      semi
45:15  error    Missing semicolon                                                      semi
47:10  warning  Unexpected unnamed method 'error'                                      func-names
47:18  error    Missing space before function parentheses                              space-before-function-paren
47:52  error    Missing space before opening brace                                     space-before-blocks
52:34  error    Infix operators must be spaced                                         space-infix-ops
52:50  error    Missing trailing comma                                                 comma-dangle
53:5   error    Missing semicolon                                                      semi
54:15  error    Missing semicolon                                                      semi
56:8   warning  Unexpected unnamed method 'end'                                        func-names
56:16  error    Missing space before function parentheses                              space-before-function-paren
56:26  error    Missing space before opening brace                                     space-before-blocks
57:13  error    Missing semicolon                                                      semi
58:4   error    Expected an assignment or function call and instead saw an expression  no-unused-expressions
58:12  error    Infix operators must be spaced                                         space-infix-ops
58:24  error    Missing semicolon                                                      semi
59:15  error    Missing semicolon                                                      semi
60:4   error    Missing trailing comma                                                 comma-dangle
61:3   error    Missing semicolon                                                      semi
62:2   error    Missing semicolon                                                      semi
以下代码段通过了规则,但没有返回此,因为这将由于
无无效的
规则而导致另一个
ESLint
错误,并且严重地不再工作,从而破坏链接:

module.exports = function(res){
return {
    success: function(content, contentType, resultCode) {
        sendResponse(
            res,
            content,
            validateContentType(contentType),
            validateResultCode(resultCode||'ok')
        )
        return this
    },
    error: function(resultCode, content, contentType){
        sendResponse(
            res,
            content,
            validateContentType(contentType),
            validateResultCode(resultCode||'bad_request')
        )
        return this
    },
    end: function(callback){
        res.end()
        callback&&callback()
        return this
    }
}
}
module.exports = res => ({
success: (content, contentType, resultCode) => {
    sendResponse(
        res,
        content,
        validateContentType(contentType),
        validateResultCode(resultCode || 'ok'),
    );
},
error: (resultCode, content, contentType) => {
    sendResponse(
        res,
        content,
        validateContentType(contentType),
        validateResultCode(resultCode || 'bad_request'),
    );
},
end: (callback) => {
    res.end();
    if (callback) {
        callback();
        return undefined;
    }
    return undefined;
},
});
我的问题是,如何使第二个代码段(或者实际上是第一个)在功能上与第一个代码段相同,但仍然传递
ESLint
规则?如何正确返回以允许与调用方链接

My
.eslintrc.json

{
  "extends": "airbnb-base",
 "rules": {
// use tabs, not spaces, and in switch statements the case statement should indent again (the default is to be level with the switch)
    "indent": [ "error", "tab", { "SwitchCase": 1 } ],
// if you want to put a blank line at the beginning or end of a block, knock yourself out
"padded-blocks": [ "off" ],
// i like tabs. besides, we set indent to require them
"no-tabs": [ "off" ],
// seriously, who cares if there's a blank line at the end of the file or not?
"eol-last": [ "off" ],
// sometimes having a long single line makes sense, this also seems buggy and inconsistent, so we ignore it
"max-len": [ "off" ],
// webstorm repeatedly tries to add it for us. it's easier not to fight it, even if it's not required.
"strict": [ "off" ],
// when setting the property of an object, you can specify the name even if it's unnecessary (ie: { foo: foo })
"object-shorthand": [ "off" ],
// unused vars are an error, except for function arguments.
// particularly with callbacks we may not use all the args, but we still like knowing they're available
"no-unused-vars": [ "error", { "vars": "all", "args": "none", "ignoreRestSiblings": true } ],
// you don't have to use operator assignment if you don't want to
"operator-assignment": [ "warn" ],
// we don't want else to be on the same line as the closing } of an if statement
"brace-style": [ "error", "stroustrup" ],
// warn about overly complex code that you may want to refactor
"complexity": [ "warn", 15 ],
// it's possible that implicit coercion is not what you intended. webstorm warns about it, so should we
"no-implicit-coercion": [ "warn" ],
// if you're using 'this' somewhere that isn't a class you're probably doing something wrong
"no-invalid-this": [ "error" ],
// if you're not modifying the variable used in a loop condition, you've probably done something wrong...
"no-unmodified-loop-condition": [ "warn" ],
// don't use .call or .apply when you don't need to
"no-useless-call": [ "warn" ],
// we want to slap you if you don't update your jsdoc, but not necessarily break one of your fingers
"valid-jsdoc": [ "warn" ],
// forgetting to return after calling a callback is an easy mistake to make, so we'll warn you if you are
"callback-return": [ "warn" ]
  }
}

我决定使用第三个代码段,它在功能上与第一个代码段等效,没有ESLint错误,只有ESLint警告:

module.exports = function (res) {
return {
    success: function (content, contentType, resultCode) {
        sendResponse(
            res,
            content,
            validateContentType(contentType),
            validateResultCode(resultCode || 'ok'),
        );
        return this;
    },
    error: function (resultCode, content, contentType) {
        sendResponse(
            res,
            content,
            validateContentType(contentType),
            validateResultCode(resultCode || 'bad_request'),
        );
        return this;
    },
    end: function (callback) {
        res.end();
        if (callback) {
            callback();
            return undefined;
        }
        return this;
    },
};
})

但是,如果有人愿意将我问题中的代码段2改编为“返回此”,传递我的ESLint错误、警告并在功能上等同于代码段1或我的答案,那么我会将该答案标记为正确

以及的文档,您需要 匿名函数表达式包含一个名称(因此它们不再是匿名的)。
顺便说一下,这是一个很好的实践,因为您可以更好地调试它,并在需要时将其作为递归函数调用

那为什么不给他们起个名字呢

module.exports = function exports(res) {
  return {
    success: function success(content, contentType, resultCode) {
      sendResponse(
        res,
        content,
        validateContentType(contentType),
        validateResultCode(resultCode || 'ok'),
      );
      return this;
    },
    error: function error(resultCode, content, contentType) {
      sendResponse(
        res,
        content,
        validateContentType(contentType),
        validateResultCode(resultCode || 'bad_request'),
      );
      return this;
    },
    end: function end(callback) {
      res.end();
      if (callback) {
        callback();
        return undefined;
      }
      return this;
    },
  }
}

我猜您应该对方法使用方法语法,而不是函数表达式:

module.exports = (res) => ({
    success(content, contentType, resultCode) {
        sendResponse(
            res,
            content,
            validateContentType(contentType),
            validateResultCode(resultCode || 'ok'),
        );
        return this;
    },
    error(resultCode, content, contentType) {
        sendResponse(
            res,
            content,
            validateContentType(contentType),
            validateResultCode(resultCode || 'bad_request'),
        );
        return this;
    },
    end(callback) {
        res.end();
        if (callback) {
            callback();
        }
        return this;
    },
});

真奇怪。它们都是对象的函数,应该通过规则…@Li357。它通过了规则,但因为不再返回这个,所以不起作用。我是说第一个返回这个的代码段。根据它通过的文件。可能是因为它在导出语句中,或者您返回的是对象文字?@Li357谢谢您的帮助。现在在有轨电车上,但30分钟后在办公室,我将粘贴输出的短绒。代码段1违反了许多规则:-(等等,你是说第二个代码段失败了?它应该失败,因为你使用的是不绑定它的arrow函数。谢谢。我已经接受了这个正确答案,因为它没有破坏代码,并且处理了4个lint警告中的3个。干杯
36:18警告意外的未命名函数func name
。匿名函数
module.exports=function(res){……
好吧,还是给它起个名字吧
function someName(res){……}
好的。再次谢谢你,没有问题。我已经更新了答案,以反映你的问题。我相信你是对的。我以后会看的。谢谢。