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

SwiftFormat 0.54.0: redundantSelf rule adds invalid "self." prefix #1729

Closed
yanniks opened this issue Jun 12, 2024 · 3 comments
Closed

SwiftFormat 0.54.0: redundantSelf rule adds invalid "self." prefix #1729

yanniks opened this issue Jun 12, 2024 · 3 comments
Labels
bug fixed in develop bug/feature resolved in the develop branch

Comments

@yanniks
Copy link

yanniks commented Jun 12, 2024

After updating to SwiftFormat 0.54.0, we're facing an issue where an invalid self. prefix is added a variable reference.
In our example, we're retrieving a view model from the environment and are using @Bindable inside the view to get a bindable reference, like in the following example:

struct MyView: View {
    @Environment(ViewModel.self) var viewModel

    var body: some View {
        @Bindable var viewModel = self.viewModel
        MySubview(
            navigationPath: $viewModel.navigationPath
        )
    }
}

What happens with SwiftFormat 0.54.0, the redundantSelf rule now inserts a self. before the $viewModel.navigationPath which results in the following code:

struct MyView: View {
    @Environment(ViewModel.self) var viewModel

    var body: some View {
        @Bindable var viewModel = self.viewModel
        MySubview(
            navigationPath: self.$viewModel.navigationPath
        )
    }
}

Unfortunately, as self.viewModel is not a Binding, the compilation fails. The issue does not occur with SwiftFormat 0.53.10.

@nicklockwood
Copy link
Owner

@yanniks I'm not able to reproduce the bug with the code sample you've provided. Can you double check?

@yanniks
Copy link
Author

yanniks commented Jun 15, 2024

Hi @nicklockwood , thanks for looking into this!

I have investigated this once more and found out that the issue only occurs if the @Bindable variable definition is not at the same nesting level as the variable use. So while the code example I provided in the original post does not seem to trigger the issue, the following does:

struct MyView: View {
    @Environment(ViewModel.self) var viewModel

    var body: some View {
        @Bindable var viewModel = self.viewModel
        ZStack {
            MySubview(
                navigationPath: $viewModel.navigationPath
            )
        }
    }
}

I have created a GitHub repo that includes an example project which reproduces the issue, including the .swiftformat file which we use.

@nicklockwood nicklockwood added fixed in develop bug/feature resolved in the develop branch and removed can't reproduce labels Jun 18, 2024
@nicklockwood
Copy link
Owner

@yanniks fixed in 0.54.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug fixed in develop bug/feature resolved in the develop branch
Projects
None yet
Development

No branches or pull requests

2 participants