在正则表达式测试器中工作,在SwiftLint构建中失败



这是一个指向正则表达式测试器的链接。

成功匹配class Second {class MyClass {。在测试中,但是当我将相同的规则带入我的swiftlint.yml时,它与我的项目中的相同类不匹配。

unnecessary_class:
name: "Class doesn't need to exist"
regex: 'class(?!(.*XC)|(.*UI)|(.*Model)|(.*model))'
message: "A new class should have a reason to exist, if it isn't a model, codable, UI subclass, nor a View conformer, then there are alternative approaches instead of creating a new class."
severity: error

观察到的差异与SwiftLint中的默认正则表达式选项有关,其中包括anchorsMatchLines和dotMatchesLineSeparators。后一个选项是导致你的regex测试器和SwiftLint给出的结果之间存在差异的原因。

要解决这个问题,你可以通过在SwiftLint配置中添加(?-s)语法来禁用dotMatchesLineSeparators。这将覆盖默认选项,并禁用特定规则的dotMatchesLineSeparators选项。

下面是一个如何修改SwiftLint配置中的unnecessary_class规则的示例,以包含(?-s)并禁用dotMatchesLineSeparators:

unnecessary_class:
name: "Class doesn't need to exist"
regex: '(?-s)class(?!(.*XC)|(.*UI)|(.*Model)|(.*model))'
message: "A new class should have a reason to exist, if it isn't a model, codable, UI subclass, nor a View conformer, then there are alternative approaches instead of creating a new class."
severity: error

最新更新