nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.63k stars 623 forks source link

contribution questions #1603

Open mesqueeb opened 6 months ago

mesqueeb commented 6 months ago

So when I want to contribute new rules, I believe I need to add the rules as functions to this file: https://github.com/nicklockwood/SwiftFormat/blob/main/Sources/Rules.swift

I wonder:

  1. what's a the way to add unit tests for my rule when I write the swift code for my new rule there
  2. how can unit tests be executed
  3. do I need to ask for permission before writing a new rule, eg. what if I write a rule that I like but you don't? (my goal is to write new rules that format more strictly like Prettier for TS)
  4. can I come up with my own names for the rules? (i'd reference Prettier rules myself or just prepend the names with prettier or something)
  5. how can I group my new rules into a set of rules
    • the reason I ask is because when using SwiftFormat for Xcode.app I'd love to have my ruleset available as a single "prettier ruleset" checkmark. Instead of having to hunt down all the individual rules

And finally, can the above be added as a "contribution guide" to the readme? 🙏🏻

nicklockwood commented 6 months ago

@mesqueeb

  1. what's a the way to add unit tests for my rule when I write the swift code for my new rule there

The tests all live inside the Tests directory. They are grouped by rule, with multiple rules per file, loosely grouped by the rule type to prevent the files getting unmanageably large.

  1. how can unit tests be executed

If you are using a Mac, just select Test from the Product menu. For Linux or other platforms you can run swift test --enable-test-discovery on the command line. Tests also run automatically on any github PR.

  1. do I need to ask for permission before writing a new rule, eg. what if I write a rule that I like but you don't? (my goal is to write new rules that format more strictly like Prettier for TS)

It's advisable to discuss any rules you want to add because I am fairly choosy about which rules are included and how they behave, so it will save you time and increase the chance of your rule being accepted. Rules can be disabled by default, so they don't necessarily have to be universally desirable, but if they are too obscure or controversial they'll probably be rejected.

Rules also cannot be contradictory or mutually exclusive - you can't add a rule that has the opposite effect of an existing rule, or have two different rules that both indent lines but with different results. If you want to change or extend the existing indenting behavior it should probably be added as an extension or configuration option for an existing rule.

  1. can I come up with my own names for the rules? (i'd reference Prettier rules myself or just prepend the names with prettier or something)

I don't think it makes sense to name SwiftFormat rules with a "prettier" prefix. Matching the behavior of Prettier exactly is not a goal of SwiftFormat, and even if it was the rules will inevitably diverge over time. Better just to name them after what they do. Most likely, there are existing rules that already overlap with Prettier's rules, but if you'd prefer to start from scratch with a clean set of rules I can figure out how to unify them later.

  1. how can I group my new rules into a set of rules
    • the reason I ask is because when using SwiftFormat for Xcode.app I'd love to have my ruleset available as a single "prettier ruleset" checkmark. Instead of having to hunt down all the individual rules

Potentially we could add a shared configuration option that makes new and existing rules behave like prettier. For example, there's a current option --xcodeindentation that tries to match Xcode's indenting behavior. This could be deprecated and replaced with an option like --indentationstyle xcode|prettier|default or we could have an option like --preset prettier that both enables the relevant rules and also preconfigures the options.

And finally, can the above be added as a "contribution guide" to the readme? 🙏🏻

Good idea!

mesqueeb commented 6 months ago

@nicklockwood thanks a lot for your reply.

The first rule I would love to start and contribute is this one: https://github.com/nicklockwood/SwiftFormat/issues/1564

I have added some extra questions for you in there. Thank you for your time. Looking forward to 2024.