Skip to content

Commit

Permalink
Merge pull request #223 from dylansturg/opt_in_rules
Browse files Browse the repository at this point in the history
Convert some rules to be opt-in because they are noisy for non-strict…
  • Loading branch information
allevato committed Sep 17, 2020
1 parent 97628a8 commit 642c2d1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Sources/SwiftFormat/Pipelines+Generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class LintPipeline: SyntaxVisitor {

override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
visitIfEnabled(AllPublicDeclarationsHaveDocumentation.visit, in: context, for: node)
visitIfEnabled(AlwaysUseLowerCamelCase.visit, in: context, for: node)
visitIfEnabled(BeginDocumentationCommentWithOneLineSummary.visit, in: context, for: node)
visitIfEnabled(DontRepeatTypeInStaticProperties.visit, in: context, for: node)
visitIfEnabled(NoLeadingUnderscores.visit, in: context, for: node)
Expand Down Expand Up @@ -205,6 +206,7 @@ class LintPipeline: SyntaxVisitor {
}

override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {
visitIfEnabled(AlwaysUseLowerCamelCase.visit, in: context, for: node)
visitIfEnabled(AmbiguousTrailingClosureOverload.visit, in: context, for: node)
visitIfEnabled(FileScopedDeclarationPrivacy.visit, in: context, for: node)
visitIfEnabled(NeverForceUnwrap.visit, in: context, for: node)
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftFormatConfiguration/RuleRegistry+Generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

enum RuleRegistry {
static let rules: [String: Bool] = [
"AllPublicDeclarationsHaveDocumentation": true,
"AllPublicDeclarationsHaveDocumentation": false,
"AlwaysUseLowerCamelCase": true,
"AmbiguousTrailingClosureOverload": true,
"BeginDocumentationCommentWithOneLineSummary": true,
Expand All @@ -24,9 +24,9 @@ enum RuleRegistry {
"FullyIndirectEnum": true,
"GroupNumericLiterals": true,
"IdentifiersMustBeASCII": true,
"NeverForceUnwrap": true,
"NeverUseForceTry": true,
"NeverUseImplicitlyUnwrappedOptionals": true,
"NeverForceUnwrap": false,
"NeverUseForceTry": false,
"NeverUseImplicitlyUnwrappedOptionals": false,
"NoAccessLevelOnExtensionDeclaration": true,
"NoBlockComments": true,
"NoCasesWithOnlyFallthrough": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import SwiftSyntax
/// Lint: If a public declaration is missing a documentation comment, a lint error is raised.
public final class AllPublicDeclarationsHaveDocumentation: SyntaxLintRule {

/// Identifies this rule was being opt-in. While docs on most public declarations are beneficial,
/// there are a number of public decls where docs are either redundant or superfluous. This rule
/// can't differentiate those situations and will make a lot of noise for projects that are
/// intentionally avoiding docs on some decls.
public override class var isOptIn: Bool { return true }

public override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseMissingDocComment(DeclSyntax(node), name: node.fullDeclName, modifiers: node.modifiers)
return .skipChildren
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftFormatRules/NeverForceUnwrap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import SwiftSyntax
/// Lint: If a force unwrap is used, a lint warning is raised.
public final class NeverForceUnwrap: SyntaxLintRule {

/// Identifies this rule was being opt-in. While force unwrap is an unsafe pattern (i.e. it can
/// crash), there are valid contexts for force unwrap where it won't crash. This rule can't
/// evaluate the context around the force unwrap to make that determination.
public override class var isOptIn: Bool { return true }

public override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {
// Tracks whether "XCTest" is imported in the source file before processing individual nodes.
setImportsXCTest(context: context, sourceFile: node)
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftFormatRules/NeverUseForceTry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import SwiftSyntax
/// TODO: Create exception for NSRegularExpression
public final class NeverUseForceTry: SyntaxLintRule {

/// Identifies this rule was being opt-in. While force try is an unsafe pattern (i.e. it can
/// crash), there are valid contexts for force try where it won't crash. This rule can't
/// evaluate the context around the force try to make that determination.
public override class var isOptIn: Bool { return true }

public override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {
setImportsXCTest(context: context, sourceFile: node)
return .visitChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import SwiftSyntax
/// Lint: Declaring a property with an implicitly unwrapped type yields a lint error.
public final class NeverUseImplicitlyUnwrappedOptionals: SyntaxLintRule {

/// Identifies this rule was being opt-in. While accessing implicitly unwrapped optionals is an
/// unsafe pattern (i.e. it can crash), there are valid contexts for using implicitly unwrapped
/// optionals where it won't crash. This rule can't evaluate the context around the usage to make
/// that determination.
public override class var isOptIn: Bool { return true }

// Checks if "XCTest" is an import statement
public override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {
setImportsXCTest(context: context, sourceFile: node)
Expand Down

0 comments on commit 642c2d1

Please sign in to comment.