You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置ESLint让对象简写等换行不计入函数行数限制

The Problem

Your arrow function is triggering the max-lines-per-function error because ESLint counts every physical line break—including those introduced by rules like object-shorthand and function-paren-newline (configured with minItems: 3). These formatting lines don’t add actual code complexity, so we can adjust ESLint to ignore them when counting lines.

Solution: Use ignorePattern in max-lines-per-function

The max-lines-per-function rule has an ignorePattern option that lets you define regex patterns for lines to exclude from the line count. We can use this to skip lines that are purely formatting-related (like braces, commas, or single property names in object literals).

Here’s how to configure it in your .eslintrc (or equivalent):

{
  "rules": {
    "max-lines-per-function": [
      "error",
      {
        "max": 15,
        "ignorePattern": "^\\s*(\\{|\\}|,|\\w+,?)$",
        "skipBlankLines": true,
        "skipComments": true
      }
    ],
    "object-shorthand": ["error", "always"],
    "function-paren-newline": ["error", { "minItems": 3 }]
  }
}

Breakdown of the ignorePattern

The regex ^\\s*(\\{|\\}|,|\\w+,?)$ matches:

  • Lines with only whitespace followed by an opening brace {
  • Lines with only whitespace followed by a closing brace }
  • Lines with only whitespace followed by a comma ,
  • Lines with only whitespace followed by a property name (word characters) optionally ending with a comma (for object shorthand properties)

This will ignore all the formatting lines in your arrow function, reducing the counted lines to just the logical code lines (around 5, as you requested).

Alternative: Refactor into Helper Functions

If the regex approach doesn’t cover all edge cases, splitting the function into smaller, focused helpers is another great option. This not only fixes the line count issue but also improves readability:

const createDestination = ({ ToAddresses, CcAddresses }) => ({
  Destination: { ToAddresses, CcAddresses }
});

const createMessage = ({ subject, mailTemplate }) => ({
  Message: {
    Body: { Html: { Charset: 'UTF-8', Data: mailTemplate } },
    Subject: { Charset: 'UTF-8', Data: `${subject}` }
  }
});

const mailParams = ({ ToAddresses, CcAddresses, subject, sourceEmail, mailTemplate }) => ({
  ...createDestination({ ToAddresses, CcAddresses }),
  ...createMessage({ subject, mailTemplate }),
  Source: `${sourceEmail}`
});

内容的提问来源于stack exchange,提问作者zabusa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 19:49:04