-
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.
Add a new
non_optional_string_data_conversion
rule (#5264)
- Loading branch information
1 parent
0d54c2a
commit 544e1c6
Showing
11 changed files
with
75 additions
and
16 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
50 changes: 50 additions & 0 deletions
50
Source/SwiftLintBuiltInRules/Rules/Lint/NonOptionalStringDataConversionRule.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,50 @@ | ||
import SwiftSyntax | ||
|
||
@SwiftSyntaxRule | ||
struct NonOptionalStringDataConversionRule: Rule { | ||
var configuration = SeverityConfiguration<Self>(.warning) | ||
static let description = RuleDescription( | ||
identifier: "non_optional_string_data_conversion", | ||
name: "Non-Optional String <-> Data Conversion", | ||
description: "Prefer using UTF-8 encoded strings when converting between `String` and `Data`", | ||
kind: .lint, | ||
nonTriggeringExamples: [ | ||
Example("Data(\"foo\".utf8)"), | ||
Example("String(decoding: data, as: UTF8.self)") | ||
], | ||
triggeringExamples: [ | ||
Example("\"foo\".data(using: .utf8)"), | ||
Example("String(data: data, encoding: .utf8)") | ||
] | ||
) | ||
} | ||
|
||
private extension NonOptionalStringDataConversionRule { | ||
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { | ||
override func visitPost(_ node: MemberAccessExprSyntax) { | ||
if node.base?.is(StringLiteralExprSyntax.self) == true, | ||
node.declName.baseName.text == "data", | ||
let parent = node.parent?.as(FunctionCallExprSyntax.self), | ||
let argument = parent.arguments.onlyElement, | ||
argument.label?.text == "using", | ||
argument.expression.as(MemberAccessExprSyntax.self)?.isUTF8 == true { | ||
violations.append(node.positionAfterSkippingLeadingTrivia) | ||
} | ||
} | ||
|
||
override func visitPost(_ node: DeclReferenceExprSyntax) { | ||
if node.baseName.text == "String", | ||
let parent = node.parent?.as(FunctionCallExprSyntax.self), | ||
parent.arguments.map({ $0.label?.text }) == ["data", "encoding"], | ||
parent.arguments.last?.expression.as(MemberAccessExprSyntax.self)?.isUTF8 == true { | ||
violations.append(node.positionAfterSkippingLeadingTrivia) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private extension MemberAccessExprSyntax { | ||
var isUTF8: Bool { | ||
declName.baseName.text == "utf8" | ||
} | ||
} |
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
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