|
| 1 | +import SwiftSyntax |
| 2 | + |
| 3 | +@SwiftSyntaxRule |
| 4 | +struct InvisibleCharactersRule: Rule { |
| 5 | + var configuration = SeverityConfiguration<Self>(.error) |
| 6 | + |
| 7 | + static let description = RuleDescription( |
| 8 | + identifier: "invisible_characters", |
| 9 | + name: "Invisible Characters", |
| 10 | + description: "Disallows invisible characters like zero-width space (U+200B) " + |
| 11 | + "and FEFF formatting character (U+FEFF) " + |
| 12 | + "in string literals as they can cause hard-to-debug issues", |
| 13 | + kind: .lint, |
| 14 | + nonTriggeringExamples: [ |
| 15 | + Example(#"let s = "HelloWorld""#), |
| 16 | + Example(#"let s = "Hello World""#), |
| 17 | + Example(#"let url = "https://example.com/api""#), |
| 18 | + Example(##"let s = #"Hello World"#"##), |
| 19 | + Example(""" |
| 20 | + let multiline = \"\"\" |
| 21 | + Hello |
| 22 | + World |
| 23 | + \"\"\" |
| 24 | + """), |
| 25 | + Example(#"let empty = """#), |
| 26 | + Example(#"let tab = "Hello\tWorld""#), |
| 27 | + Example(#"let newline = "Hello\nWorld""#), |
| 28 | + Example(#"let unicode = "Hello 👋 World""#), |
| 29 | + ], |
| 30 | + triggeringExamples: [ |
| 31 | + // swiftlint:disable invisible_characters |
| 32 | + Example(#"let s = "Hello↓World""#), // U+200B zero-width space |
| 33 | + Example(#"let s = "Hello↓World""#), // U+FEFF formatting character |
| 34 | + Example(#"let url = "https://example↓.com""#), // U+200B in URL |
| 35 | + Example(""" |
| 36 | + let multiline = \"\"\" |
| 37 | + Hello↓World |
| 38 | + \"\"\" |
| 39 | + """), // U+200B in multiline string |
| 40 | + Example(#"let s = "Test↓String↓Here""#), // Multiple invisible characters |
| 41 | + // swiftlint:enable invisible_characters |
| 42 | + ] |
| 43 | + ) |
| 44 | + |
| 45 | + private static let invisibleCharacters: Set<Unicode.Scalar> = [ |
| 46 | + "\u{200B}", // Zero-width space |
| 47 | + "\u{FEFF}", // Zero-width no-break space (BOM/FEFF) |
| 48 | + ] |
| 49 | +} |
| 50 | + |
| 51 | +private extension InvisibleCharactersRule { |
| 52 | + final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { |
| 53 | + override func visitPost(_ node: StringLiteralExprSyntax) { |
| 54 | + for segment in node.segments { |
| 55 | + guard let stringSegment = segment.as(StringSegmentSyntax.self) else { |
| 56 | + continue |
| 57 | + } |
| 58 | + |
| 59 | + let text = stringSegment.content.text |
| 60 | + |
| 61 | + // Early exit if no invisible characters present |
| 62 | + guard text.unicodeScalars.contains(where: { |
| 63 | + InvisibleCharactersRule.invisibleCharacters.contains($0) |
| 64 | + }) else { |
| 65 | + continue |
| 66 | + } |
| 67 | + |
| 68 | + // Find all invisible characters and their positions |
| 69 | + var utf8Offset = 0 |
| 70 | + for scalar in text.unicodeScalars { |
| 71 | + if InvisibleCharactersRule.invisibleCharacters.contains(scalar) { |
| 72 | + // Calculate absolute position of the invisible character |
| 73 | + let invisibleCharPosition = stringSegment.content.positionAfterSkippingLeadingTrivia |
| 74 | + .advanced(by: utf8Offset) |
| 75 | + |
| 76 | + let charName = characterName(for: scalar) |
| 77 | + let reason = "String literal should not contain invisible character: \(charName)" |
| 78 | + violations.append( |
| 79 | + ReasonedRuleViolation( |
| 80 | + position: invisibleCharPosition, |
| 81 | + reason: reason |
| 82 | + ) |
| 83 | + ) |
| 84 | + } |
| 85 | + utf8Offset += String(scalar).utf8.count |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private func characterName(for scalar: Unicode.Scalar) -> String { |
| 91 | + switch scalar { |
| 92 | + case "\u{200B}": |
| 93 | + return "U+200B (zero-width space)" |
| 94 | + case "\u{FEFF}": |
| 95 | + return "U+FEFF (zero-width no-break space)" |
| 96 | + default: |
| 97 | + return "U+\(String(scalar.value, radix: 16, uppercase: true))" |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments