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

Enum namespace rule #737

Merged
merged 5 commits into from
Sep 20, 2020
Merged

Conversation

facumenzella
Copy link
Contributor

Why?

This PR tackles #733

What?

This PR tries to tackle an existing rule from Swiftlint. Types used to host static members should be enums to avoid instantiation.

I excluded the rule from many tests. I wasn't sure about updating them.

@facumenzella facumenzella force-pushed the convenience-type branch 4 times, most recently from 5cbe1bf to a6765d3 Compare September 12, 2020 15:36
- Added test suite
var j = startIndex
while j < endIndex, let token = formatter.token(at: j) {
// exit if there's a explicit init
if token == .keyword("init") {
Copy link
Collaborator

@calda calda Sep 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this implementation may run into the same issues as #728. This should probably only check declarations at the top level of the type definition.

Some examples that I think may get handled incorrectly right now:

struct Namespace {
    static func staticFunction() {
        func nestedFunction() { /* ... */ }
    }
}
struct Namespace {
    static func staticFunction() {
        struct NestedType {
            init() {}
        }
    }
}
struct Namespace {
    struct TypeNestedInNamespace {
        let foo: Int
        let bar: Int
    }
}
struct Namespace {
    struct NestedNamespace {
       static let foo: Int
       static let bar: Int
    }
}

You could potentially use parseDeclarations() to get the list of top-level declarations for each type (this could make it easier to handle recursively nested types as well).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!!
I'll add some tests for that and fix them.

Rules.md Outdated
@@ -9,6 +9,7 @@
* [braces](#braces)
* [consecutiveBlankLines](#consecutiveBlankLines)
* [consecutiveSpaces](#consecutiveSpaces)
* [convenienceType](#convenienceType)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the SwiftLint rule is also called convenienceType, but I'm not really sure what "convenience" means in this context. I've always heard caseless enums referred to as namespaces. I think a name like enumNamespaces could make sense here.

@@ -57,7 +57,7 @@ extension RulesTests {
// foo
}
"""
testFormatting(for: input, rule: FormatRules.braces)
testFormatting(for: input, rule: FormatRules.braces, exclude: ["convenienceType"])
Copy link
Collaborator

@calda calda Sep 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that the SwiftLint rule doesn't trigger for empty types like class Foo {}. That may be a nice addition to this implementation (and would help avoid the need to exclude this rule from so many existing tests)

@facumenzella facumenzella force-pushed the convenience-type branch 2 times, most recently from 1a4f1c3 to faa84fe Compare September 13, 2020 13:44
@coveralls
Copy link

Coverage Status

Coverage increased (+0.06%) to 93.551% when pulling f87cb32 on facumenzella:convenience-type into 0d61559 on nicklockwood:master.

@nicklockwood nicklockwood merged commit fcbfd62 into nicklockwood:master Sep 20, 2020
nicklockwood pushed a commit that referenced this pull request Sep 20, 2020
nicklockwood pushed a commit that referenced this pull request Sep 20, 2020
@@ -3,7 +3,7 @@
import Foundation

/// A protocol for view-type classes that can be configured using Layout
@objc protocol LayoutConfigurable: class {
@objc protocol LayoutConfigurable: enum {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@facumenzella I didn't spot this before, but this looks like a bug. I'd guess the rule wrongly identified this as a class whereas in fact it's a protocol.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I completely missed this one. I thought it was under control. I'll take a look

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@facumenzella don't worry, I checked and the rule itself is fine (you added a unit test for this case) - I think you must've just forgotten to roll back this change to the snapshot tests.

calda added a commit to calda/SwiftFormat that referenced this pull request Sep 20, 2020
Adopt new helpers in organizeDeclarations

Make tests pass

Add another test case

Add extensionacl option to place the ACL keyword on the extension or on the body declarations

Fix unit test line assignment

Fix spurious removal of type in ternary assignment

Updated for 0.46.3 release

Simplify example

Add more test cases

Updated for 0.46.3 release

Enum namespace rule (nicklockwood#737)
@nicklockwood nicklockwood changed the title Convenience type rule Enum namespace rule Sep 22, 2020
nicklockwood pushed a commit that referenced this pull request Sep 22, 2020
@facumenzella facumenzella deleted the convenience-type branch September 22, 2020 09:25
nicklockwood pushed a commit that referenced this pull request Oct 13, 2020
nicklockwood pushed a commit that referenced this pull request Oct 13, 2020
Lukasz2891 pushed a commit to Lukasz2891/SwiftFormat that referenced this pull request Oct 23, 2020
leogdion pushed a commit to brightdigit/SwiftFormatSlim that referenced this pull request Nov 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants