SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

How to make custom rules work?

Open steve21124 opened this issue 10 years ago • 11 comments

swiftlint version : 0.9.2 swiftlint rules => this only give standard rules not custom rules define in swiftlint.yml

=> even with example to catch all s character, it still did not use custom rule custom_rules: no_scott: # rule identifier name: "No Scott Rule" # rule name regex: "([s,S])" # matching pattern match_kinds: # SyntaxKinds to match - comment - string message: "There should be no Scotts here" severity: error

steve21124 avatar Apr 19 '16 04:04 steve21124

All match kinds must match, so your custom rule is trying to match a single character to two different sequential tokens (comment followed by a string literal).

To match any token, just omit match_kinds

jpsim avatar Apr 19 '16 04:04 jpsim

even with these two below it is not working. What is the best way to debug it?

custom_rules: no_scott: # rule identifier name: "No Scott Rule" # rule name regex: "([s,S])" # matching pattern message: "There should be no Scotts here" severity: error
no_hiding_in_strings: regex: "([n,N]inja)" match_kinds: string

`

steve21124 avatar Apr 19 '16 05:04 steve21124

What is the best way to debug it?

I recommend to using Atom. The linter-swiftlint plugin for Atom supports live linting with SwiftLint. Live linting will be updated by changing contents without saving. screenshot 2016-04-19 14 56 32

Atom recognize (maybe)same regular expression syntax on find. screenshot 2016-04-19 15 06 42

If you want to specify match_kinds, I recommend to using linter-sourcekitten-syntax that displays syntax kinds recognized by SourceKitService. screenshot 2016-04-19 15 03 03

norio-nomura avatar Apr 19 '16 06:04 norio-nomura

great info to debug problem in swiftlint. thanks

steve21124 avatar Apr 19 '16 06:04 steve21124

in the same topic. if your configuration already has custom rules. what it will display if print "swiftlint rules"

on my case

+-----------------------------+--------+-------------+------------------------+--------------------------------------------------+
| identifier                  | opt-in | correctable | enabled in your config | configuration                                    |
+-----------------------------+--------+-------------+------------------------+--------------------------------------------------+
| closing_brace               | no     | yes         | yes                    | warning                                          |
| colon                       | no     | yes         | yes                    | warning                                          |
| comma                       | no     | yes         | no                     | warning                                          |
| conditional_binding_cascade | no     | no          | yes                    | warning                                          |
| control_statement           | no     | no          | no                     | warning                                          |
| custom_rules                | no     | no          | no                     | user-defined                                     |
| cyclomatic_complexity       | no     | no          | yes                    | warning: 10, error: 20                           |
| empty_count                 | yes    | no          | no                     | error                                            |
| file_length                 | no     | no          | no                     | warning: 400, error: 1000                        |
| force_cast                  | no     | no          | yes                    | error                                            |
| force_try                   | no     | no          | yes                    | warning                                          |
| force_unwrapping            | yes    | no          | no                     | warning                                          |
| function_body_length        | no     | no          | no                     | warning: 40, error: 100                          |
| function_parameter_count    | no     | no          | yes                    | warning: 5, error: 8                             |
| leading_whitespace          | no     | no          | yes                    | warning                                          |
| legacy_constant             | no     | yes         | yes                    | warning                                          |
| legacy_constructor          | no     | yes         | yes                    | warning                                          |
| line_length                 | no     | no          | no                     | warning: 100, error: 200                         |
| missing_docs                | yes    | no          | no                     | warning: public                                  |
| nesting                     | no     | no          | yes                    | warning                                          |
| opening_brace               | no     | yes         | no                     | warning                                          |
| operator_whitespace         | no     | no          | yes                    | warning                                          |
| return_arrow_whitespace     | no     | no          | no                     | warning                                          |
| statement_position          | no     | yes         | no                     | warning                                          |
| todo                        | no     | no          | no                     | warning                                          |
| trailing_newline            | no     | yes         | no                     | warning                                          |
| trailing_semicolon          | no     | yes         | no                     | warning                                          |
| trailing_whitespace         | no     | yes         | no                     | warning                                          |
| type_body_length            | no     | no          | no                     | warning: 200, error: 350                         |
| type_name                   | no     | no          | yes                    | (min_length) w/e: 3/0, (max_length) w/e: 40/1000 |
| valid_docs                  | no     | no          | no                     | warning                                          |
| variable_name               | no     | no          | yes                    | (min_length) w/e: 3/2, (max_length) w/e: 40/60   |
+-----------------------------+--------+-------------+------------------------+--------------------------------------------------+

steve21124 avatar Apr 19 '16 06:04 steve21124

CustomRules does not yet support displaying configuration on swiftlint rules. https://github.com/realm/SwiftLint/blob/master/Source%2FSwiftLintFramework%2FRules%2FCustomRules.swift#L15

public struct CustomRulesConfiguration: RuleConfiguration, Equatable {
    public var consoleDescription: String { return "user-defined" }

Contribution is welcome! 😎

norio-nomura avatar Apr 19 '16 06:04 norio-nomura

i'm using version 0.12.0 and when i'm adding a custom rule it does not work. I've checked the regex with the linter-swiftlint plugin and it checks out, but on Xcode it seems that it is not working.

Any advices? thanks

yuvigr avatar Aug 29 '16 12:08 yuvigr

Custom rule uses NSRegularExpression that recognizes ICU regular expression. The ICU regular expressions are described at http://userguide.icu-project.org/strings/regexp .

norio-nomura avatar Aug 31 '16 08:08 norio-nomura

Hi! Seems that custom rules don't work when using whitelist_rules. Any ideas?

DeeSee avatar Sep 19 '16 20:09 DeeSee

Oh, it turned out that I should add custom_rules to whitelist instead of custom rule name.

DeeSee avatar Sep 19 '16 20:09 DeeSee

@DeeSee That's exactly the information I needed to address my problem. I have submitted a PR for the README.md to clarify this: https://github.com/realm/SwiftLint/pull/1982

micnguyen avatar Dec 27 '17 01:12 micnguyen