【问题标题】:Adding a custom rule to a certain type of function for Swiftlint为 Swiftlint 的某种类型的函数添加自定义规则
【发布时间】:2020-02-11 19:42:03
【问题描述】:

我正在为 SwiftLint 创建一些自定义规则,我只希望我的规则适用于某些函数中的字符串,例如仅适用于 logEvent 中的字符串

analytics.logEvent('goal_completion', { name: 'lever_puzzle'});

SwiftLint Github 似乎没有提到如何仅在某些函数中将 lint 应用于字符串。

swiftLint 自定义规则:

    namingConvention:
        name: "event naming"
        regex: "[^a-z]"
        message: "any event should only be in letters"
        severity: warning

【问题讨论】:

    标签: ios swift swiftlint


    【解决方案1】:

    您需要为您的规则确定match_kinds

    match_kinds:
      - identifier
      - string
    

    【讨论】:

      【解决方案2】:

      是的,你是对的,目前按功能规则不可用。

      但是,您可以制作多个 SwiftLint yml 文件!文档是这样说的:

      Nested Configurations
      
      SwiftLint supports nesting configuration files for more granular control over the linting process.
      
      Include additional .swiftlint.yml files where necessary in your directory structure.
      Each file will be linted using the configuration file that is in its directory or at the deepest level of its parent directories. Otherwise the root configuration will be used.
      included is ignored for nested configurations.
      

      因此,基本上,如果您可以将这些函数组合在一起,它们都可以拥有各自独立的 SwiftLint yml 文件。不是最好的解决方案,但可能是当前产品的最佳解决方案。

      【讨论】:

        猜你喜欢
        • 2016-07-22
        • 2018-07-13
        • 2020-09-30
        • 2021-12-08
        • 2020-09-12
        • 2017-08-02
        • 2014-10-30
        • 2016-09-09
        • 2015-08-28
        相关资源
        最近更新 更多