Allow including and excluding files name patterns on a per rule basis.
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
We'd like to have a way to enable and disable rules based on the file name of the swift file.
Solutions we considered
Child/parent configs seem like it's for refining the rules for the whole project/folder, not on a per-file basis. It also only seems to allow a one-to-one relationship.
Nested configurations only apply to a whole folder but sometimes there's rules that should only be applied to some files in the folder. For example, a view and view controller in the same folder but there's a rule that should only be enabled for the view controller.
Updating every rule to allow passing a file pattern seems like it'd be a huge and and it'd conflict with some existing rules that already have parameters for include and exclude.
Suggested solution
I'd like the propose the idea to have a config file that can reference multiple sub-config files. The root config file would contain all the default settings that are used for the matched files. The sub-configs would then further refine the settings allowing including or excluding files that it's rules/settings applied to.
Example
.swiftlint.yml
opt_in_rules:
- force_unwrapping
sub_configs:
- .swiftlint-tests.yml
.swiftlint-tests.yml
disabled_rules:
- force_unwrapping
sub_configs:
- .swiftlint-spec.yml
- .swiftlint-mock.yml
included:
- **/Tests
.swiftlint-spec.yml
disabled_rules:
- function_body_length
included:
- *.spec.swift
.swiftlint-mock.yml
custom_rules:
my_custom_rule:
...
included:
- *+Mock.swift
If it was linting the following files, the following would occur.
Foo/Sources/Foo.swift- .swiftlint.yml would enable
force_unwrapping
- .swiftlint.yml would enable
Foo/Tests/Foo.spec.swift- .swiftlint.yml would enable
force_unwrapping - .swiftlint-tests.yml would match the file name and disable
force_unwrapping - .swiftlint-spec.yml would match the file name and disable
function_body_length
- .swiftlint.yml would enable
Foo/Tests/FooService+Mock.swift- .swiftlint.yml would enable
force_unwrapping - .swiftlint-tests.yml would match the file name and disable
force_unwrapping - .swiftlint-mock.yml would match the file name and enable
my_custom_rule
- .swiftlint.yml would enable
Bar/Source/BarService+Mock.swift- .swiftlint.yml would enable
force_unwrapping - .swiftlint-tests.yml would not match the file name
- .swiftlint-mock.yml would not be used
- .swiftlint.yml would enable
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Begin with the proposed .swiftlint.yml and sub-config examples, then compare them with current configuration behavior. Verify how included and excluded patterns should compose for the listed Foo and Bar file paths; done means the per-rule configuration works consistently for those cases and existing configuration behavior remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100