久美子
久美子

Reputation: 121

Adding a custom rule to a certain type of function for Swiftlint

I am creating some custom rules for SwiftLint, I only want my rule to be applied to strings in certain functions, for example only the strings in a logEvent

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

The SwiftLint Github doesn't seem to mention how to apply the lint to strings in certain functions only.

swiftLint custom rules:

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

Upvotes: 0

Views: 1510

Answers (2)

Akash Kundu
Akash Kundu

Reputation: 1358

Yup, you're right, currently function by function rules are not available.

However, you can make multiple SwiftLint yml files! Here's what the docs say:

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.

So basically if you can group those functions together, they can all have their separate SwiftLint yml file. Not the best solution, but maybe the best solution for the current offering.

Upvotes: 0

jacob
jacob

Reputation: 1052

You need to identify the match_kinds for your rule.

match_kinds:
  - identifier
  - string

Upvotes: 0

Related Questions