Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule Request: explicit_view_spacing #4962

Open
knellr opened this issue May 5, 2023 · 2 comments
Open

Rule Request: explicit_view_spacing #4962

knellr opened this issue May 5, 2023 · 2 comments
Labels
rule-request Requests for a new rules.

Comments

@knellr
Copy link

knellr commented May 5, 2023

New rule request

Having migrated to SwiftUI, a major issue we encountered was not understanding (and subsequently remembering to account for) the fact that Spacer, and V/HStack variants had a non-zero default spacing. We've added a rule locally to enforce that spacing should be explicit to avoid unexpected behaviour.

Triggering examples:

Spacer()
VStack {
    Text("Hello World")
}
LazyHStack(
    alignment: .leading
) {
    Text("Hello World")
}

Non-triggering examples:

Spacer(minLength: 8)
Spacer(minLength: .zero)
VStack(spacing: 8) {
    Text("Hello World")
}
LazyHStack(
    alignment: .leading,
    spacing: .zero
) {
    Text("Hello World")
}

This should not be enabled by default as the default behaviour is desired in some applications.

@SimplyDanny SimplyDanny added the rule-request Requests for a new rules. label May 6, 2023
@SimplyDanny
Copy link
Collaborator

Would you like to contribute the rule you already use internally?

@knellr
Copy link
Author

knellr commented May 6, 2023

We have two very hacked together regular expression based rules currently:

  swiftui_stack_spacing:
    included: ".*\\.swift"
    name: "SwiftUI Stack spacing"
    regex: "[^A-Z^a-z][V|H]Stack(?![^\\)]+spacing[^\\)]+)"
    capture_group: 0
    message: "VStacks and HStacks must be given explicit spacing"
    severity: error
  swiftui_spacer_length:
    included: ".*\\.swift"
    name: "SwiftUI Spacer minimum length"
    regex: "[^A-Z^a-z]Spacer(?![^\\)]+minLength[^\\)]+)"
    capture_group: 0
    message: "Spacers must be given explicit minimum length"

These don't account for lazy stacks or much else but I had forgotten they do try to ignore unrelated definitions that start SomeOtherStack()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule-request Requests for a new rules.
Projects
None yet
Development

No branches or pull requests

2 participants