【问题标题】:SwiftLint Custom Spacing Rule After slass/struct/enum/extensionslass/struct/enum/extension 之后的 SwiftLint 自定义间距规则
【发布时间】:2021-12-08 10:26:37
【问题描述】:

我有以下通过https://regex101.com/r/7D6fAL/1 工作的正则表达式,但在添加为 swiftlint 自定义规则时不起作用。我希望规则在以下条件下触发(在 regex101 中按预期工作):

// should trigger
public final class Aaaaaaa {
var cat: Bool
}

// should not trigger
class Aaa {

var cat: Bool
}

// should not trigger
func bbb {
}

// should trigger
struct Ccc {
}

// should not trigger
struct Ccc {

}

// should trigger
enum Ddd {
}

// should not trigger
enum Ddd {

}

// should trigger
extension Eee {
}

// should not trigger
extension Eee {

}

但当添加为 swiftlint 自定义规则时,不会触发任何警告:

custom_rules:
  space_after_main_type:
    name: "No space after main type"
    regex: '(class|struct|enum|extension)[\h\S]*{$\n(?!\s)' 
    message: "Empty line required after main declarations"
    severity: warning

【问题讨论】:

    标签: swift swiftlint


    【解决方案1】:

    感谢this comment to an issue on the swiftlint repo。在这里发布我的答案,以防其他人去寻找类似的解决方案并且不想像我一样花费数小时:

    (class|struct|enum|extension)((?-s)\s.*\{$\n)(?!^\s*$)
    

    几乎可以肯定,还有其他方法可以解决这个问题,但我会让比我更聪明的人发布关于为什么我因为没有弄清楚这些而成为傻瓜的原因。 ;)

    【讨论】:

      猜你喜欢
      • 2020-09-12
      • 2016-07-22
      • 2020-09-30
      • 2018-07-13
      • 2016-09-09
      • 2020-02-11
      • 2018-01-10
      • 2017-02-01
      • 2017-07-30
      相关资源
      最近更新 更多