file_types_order triggers when multiple classes are declared in the same file:
class Foo {
}
class Bar: Foo {
}
0.35.0HomebrewFixing this rule will become even more of an issue with SwiftUI with structs derived from View and PreviewProvider in the same file
struct ContentView: View {
var body: some View {
Text("Hello World")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
@timbms I agree that it doesn't make sense to put the PreviewProvider above the actual view. Since this is a very SwiftUI-specific thing, I think we should handle PreviewProvider subclasses as a special file type and place it even below extensions by default. Will make a PR for that soonish.
@weakfl That's actually the expected behavior. When there are multiple classes within a file, the class whose names matches the file name is considered to be the main type and thus other types (including classes) are declared supportingTypes. In case that there is no type matching the file name, the class with the most lines of code is considered to be the main type.
The best way to make sure the rule works correctly is to use it together with the File Name rule and to separate main types always into their own files, only keeping supporting types in a single file with the main type.
@Jeehut Thanks for the explanation. It feels a bit counter intuitive to declare derived classes before the main class, but maybe that's just me?
Would be great if this could be configured. Same goes for order of properties (see #2410).
@weakfl And I find it counter-intuitive to find two main types in a single Swift file. A subclass should always be in its own file in my opinion. At least in projects where it would make sense to enable this rule.
Most helpful comment
@timbms I agree that it doesn't make sense to put the
PreviewProviderabove the actual view. Since this is a verySwiftUI-specific thing, I think we should handlePreviewProvidersubclasses as a special file type and place it even below extensions by default. Will make a PR for that soonish.