Is there a way to stop scan the SwiftUI "ContentView_Previews"

We’ve been encounter an issue where SonarQube rules keep scanning the SwiftUI and report this as a code smell but it is not. We currently just flag this as info level warning but it doesn’t help with the metrics.

Can anyone help or can we implement something to not scan this previews?? Thank you

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

FYI, this shows the preview layout during the Xcode development

1 Like

Hi,

Welcome to the community!

I’m not familiar with Swift. Are the previews all collected in separate files, or scattered throughout regular code? If the former, you could set up an exclusion so those files aren’t analyzed - either at all or by specific rules.

 
HTH,
Ann

It’s actually scattered throughout the code and it’s auto generated by Xcode. We also don’t want to exclude any files but we do want to stop scanning this. I tried //NOSONAR but that doesn’t seems working. From what I read on other topics that the Sonar team didn’t implement this for Swift language.

Hi,

Thanks for the explanaton. I’m going to flag this for attention from the language experts. This may be something we want to handle more systematically.

 
Ann

1 Like

Thank you for the help :slight_smile:

Any update on this one?

Not from Sonar team but I do have something I’d like to try to see if works and if does I will share here

I found an alternate solution to avoid sonar from scan this for now.
https://docs.sonarqube.org/8.9/project-administration/narrowing-the-focus/#header-3

You can add a regex in Sonar to " Ignore Issues in Blocks "

EX:

// START SWIFTUI PREVIEW
struct MyView_Previews: PreviewProvider {
    static var previews: some View {
        MyView()
    }
}
// END SWIFTUI PREVIEW
1 Like

Hi there,

Sorry for the long delay in answering this thread. As @RedDragonJ rightfully pointed out, ignoring issues in blocks is typically the feature one would need to skip specific code patterns, especially generated code.

Hope this helps,
Yassin

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.