【问题标题】:Require multiline parameters and arguments to start on a new line after the opening parenthesis要求多行参数和参数在左括号后的新行开始
【发布时间】:2022-06-22 23:50:38
【问题描述】:

我正在为一个项目配置 SwiftLint,我想强制执行的标准之一是:当一个函数被声明或调用,并且它的参数或参数被分成多行时,那么第一个参数应该总是在函数名之后。

换句话说,它应该总是如下所示:

func foo(
  bar: Int,
  baz: Int
  ...

foo(
  bar: 0,
  baz: 1

并且从不这样:

func foo(bar: Int
         baz: Int
         ...

foo(bar: 0,
    baz: 1
    ...

我在预定义的规则中查找了这样的规则,但找不到。我真的希望我只是错过了它,因为这似乎是一个可以很容易地通过 --fix 自动应用的规则。

如果不存在这样的规则,我想创建自定义规则不会太难,但是(据我了解)将其设置为自动应用是不可能的。还是我错了?

【问题讨论】:

    标签: swiftlint


    【解决方案1】:

    回答我自己的问题:

    1. 不,该规则似乎不支持开箱即用。似乎存在的最接近的是multiline_parameters,它对我想要考虑违规的 sn-p 非常满意
    2. 不,SwiftLint doesn't support autocorrecting custom rules
    3. 我的自定义规则仍然需要很多的爱,但这是我到目前为止所得到的,至少似乎不会引发误报:
    # Triggered when a multi-line parameter or argument list starts on the same line as the opening bracket
    # func foo(x: Int,
    #      y: Int...
    # ---OR---
    # foo(x: 1,
    #     y: 2...
        multi_line_args_start_on_same_line:
            name: "Multi-line args format"
            message: "Multi-line arguments or parameters should start on a new line"
            included: ".*\\.swift"
            # Line-by-line:
            # - start of function with opening bracket; e.g. `foo(`
            # - A parameter name, then a colon, and then whitespace; e.g. `x: `
            # - A parameter value or type, followed by a comma and newline,
            #   e.g. `Int,\n` or `10,\n`
            # - Anything, to account for subsequent parameters or args
            # - A closing bracket at the end
            regex: "\
            [\\w\\d]+\\(\
            [\\w\\d]+:\\s*\
            [\\w\\d]+,\\n\
            .*\
            \\)$"
            severity: error 
    

    【讨论】:

      猜你喜欢
      • 2017-03-28
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 2019-12-17
      • 2013-08-21
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      相关资源
      最近更新 更多