-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reverse Data -> String conversion rule
- Loading branch information
Showing
11 changed files
with
62 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
Source/SwiftLintBuiltInRules/Rules/Lint/OptionalDataStringConversionRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import SwiftSyntax | ||
|
||
@SwiftSyntaxRule | ||
struct OptionalDataStringConversionRule: Rule { | ||
var configuration = SeverityConfiguration<Self>(.warning) | ||
static let description = RuleDescription( | ||
identifier: "optional_data_string_conversion", | ||
name: "Optional Data -> String Conversion", | ||
description: "Prefer using failable String(data:encoding:) when converting from `Data` to a UTF-8 `String`", | ||
kind: .lint, | ||
nonTriggeringExamples: [ | ||
Example("String(data: data, encoding: .utf8)") | ||
], | ||
triggeringExamples: [ | ||
Example("String(decoding: data, as: UTF8.self)") | ||
] | ||
) | ||
} | ||
|
||
private extension OptionalDataStringConversionRule { | ||
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { | ||
override func visitPost(_ node: DeclReferenceExprSyntax) { | ||
if node.baseName.text == "String", | ||
let parent = node.parent?.as(FunctionCallExprSyntax.self), | ||
parent.arguments.map({ $0.label?.text }) == ["decoding", "as"], | ||
parent.arguments.last?.expression.as(IdentifierTypeSyntax.self)?.name == "UTF8" { | ||
violations.append(node.positionAfterSkippingLeadingTrivia) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters