SwiftFormat icon indicating copy to clipboard operation
SwiftFormat copied to clipboard

Is it possible to apply filters to specific rules?

Open gsabran opened this issue 7 months ago • 3 comments

For instance I would like my --header rule to have a different exclude list than for the rest of my rules. Is is possible? I could not find anything about it so I would assume it's not.

My current workaround is to have separate rule files, each with their own exclude list, and that cach to different locations.

gsabran avatar May 21 '25 19:05 gsabran

@gsabran yes, this is the recommended solution.

I'm curious about the "cach to different locations" comment though - this shouldn't be necessary as the cache keys include the rule settings. Were you finding that if you didn't do this the cache wasn't working properly?

nicklockwood avatar Jun 05 '25 05:06 nicklockwood

Interesting. Yes I noticed that linting was much slower if I didn't use different caches.

# 0.7s
mkdir -p .build/caches/swiftformat &&
swiftformat --config rules-header.swiftformat . &&
swiftformat --config rules.swiftformat . --cache .build/caches/swiftformat
# 3.7s
swiftformat --config rules-header.swiftformat . &&
swiftformat --config rules.swiftformat .

gsabran avatar Jun 05 '25 07:06 gsabran

My header rule is

# Exclude checkout directories for common package managers
--exclude Carthage,Pods,.build,**/Module.swift,**/Package.swift,build

# options
--header Copyright ...

# rules
--rules fileHeader

The other one is similar to https://github.com/airbnb/swift/blob/master/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat

gsabran avatar Jun 05 '25 07:06 gsabran