|
| 1 | +import SwiftSyntax |
| 2 | + |
| 3 | +@SwiftSyntaxRule(correctable: true) |
| 4 | +struct InvisibleCharacterRule: Rule { |
| 5 | + var configuration = InvisibleCharacterConfiguration() |
| 6 | + |
| 7 | + // swiftlint:disable invisible_character |
| 8 | + static let description = RuleDescription( |
| 9 | + identifier: "invisible_character", |
| 10 | + name: "Invisible Character", |
| 11 | + description: """ |
| 12 | + Disallows invisible characters like zero-width space (U+200B), \ |
| 13 | + zero-width non-joiner (U+200C), and FEFF formatting character (U+FEFF) \ |
| 14 | + in string literals as they can cause hard-to-debug issues. |
| 15 | + """, |
| 16 | + kind: .lint, |
| 17 | + nonTriggeringExamples: [ |
| 18 | + Example(#"let s = "HelloWorld""#), |
| 19 | + Example(#"let s = "Hello World""#), |
| 20 | + Example(#"let url = "https://example.com/api""#), |
| 21 | + Example(##"let s = #"Hello World"#"##), |
| 22 | + Example(""" |
| 23 | + let multiline = \"\"\" |
| 24 | + Hello |
| 25 | + World |
| 26 | + \"\"\" |
| 27 | + """), |
| 28 | + Example(#"let empty = """#), |
| 29 | + Example(#"let tab = "Hello\tWorld""#), |
| 30 | + Example(#"let newline = "Hello\nWorld""#), |
| 31 | + Example(#"let unicode = "Hello 👋 World""#), |
| 32 | + ], |
| 33 | + triggeringExamples: [ |
| 34 | + Example(#"let s = "Hello↓World" // U+200B zero-width space"#), |
| 35 | + Example(#"let s = "Hello↓World" // U+200C zero-width non-joiner"#), |
| 36 | + Example(#"let s = "Hello↓World" // U+FEFF formatting character"#), |
| 37 | + Example(#"let url = "https://example↓.com" // U+200B in URL"#), |
| 38 | + Example(""" |
| 39 | + // U+200B in multiline string |
| 40 | + let multiline = \"\"\" |
| 41 | + Hello↓World |
| 42 | + \"\"\" |
| 43 | + """), |
| 44 | + Example(#"let s = "Test↓String↓Here" // Multiple invisible characters"#), |
| 45 | + Example(#"let s = "Hel↓lo" + "World" // string concatenation with U+200C"#), |
| 46 | + Example(#"let s = "Hel↓lo \(name)" // U+200C in interpolated string"#), |
| 47 | + Example(""" |
| 48 | + // |
| 49 | + // additional_code_points: ["00AD"] |
| 50 | + // |
| 51 | + let s = "Hello↓World" |
| 52 | + """, |
| 53 | + configuration: [ |
| 54 | + "additional_code_points": ["00AD"], |
| 55 | + ] |
| 56 | + ), |
| 57 | + Example(""" |
| 58 | + // |
| 59 | + // additional_code_points: ["200D"] |
| 60 | + // |
| 61 | + let s = "Hello↓World" |
| 62 | + """, |
| 63 | + configuration: [ |
| 64 | + "additional_code_points": ["200D"], |
| 65 | + ] |
| 66 | + ), |
| 67 | + ], |
| 68 | + corrections: [ |
| 69 | + Example(#"let s = "HelloWorld""#): Example(#"let s = "HelloWorld""#), |
| 70 | + Example(#"let s = "HelloWorld""#): Example(#"let s = "HelloWorld""#), |
| 71 | + Example(#"let s = "HelloWorld""#): Example(#"let s = "HelloWorld""#), |
| 72 | + Example(#"let url = "https://example.com""#): Example(#"let url = "https://example.com""#), |
| 73 | + Example(""" |
| 74 | + let multiline = \"\"\" |
| 75 | + HelloWorld |
| 76 | + \"\"\" |
| 77 | + """): Example(""" |
| 78 | + let multiline = \"\"\" |
| 79 | + HelloWorld |
| 80 | + \"\"\" |
| 81 | + """), |
| 82 | + Example(#"let s = "TestStringHere""#): Example(#"let s = "TestStringHere""#), |
| 83 | + Example(#"let s = "Hello" + "World""#): Example(#"let s = "Hello" + "World""#), |
| 84 | + Example(#"let s = "Hello \(name)""#): Example(#"let s = "Hello \(name)""#), |
| 85 | + Example( |
| 86 | + #"let s = "HelloWorld""#, |
| 87 | + configuration: [ |
| 88 | + "additional_code_points": ["00AD"], |
| 89 | + ] |
| 90 | + ): Example( |
| 91 | + #"let s = "HelloWorld""#, |
| 92 | + configuration: [ |
| 93 | + "additional_code_points": ["00AD"], |
| 94 | + ] |
| 95 | + ), |
| 96 | + Example( |
| 97 | + #"let s = "HelloWorld""#, |
| 98 | + configuration: [ |
| 99 | + "additional_code_points": ["200D"], |
| 100 | + ] |
| 101 | + ): Example( |
| 102 | + #"let s = "HelloWorld""#, |
| 103 | + configuration: [ |
| 104 | + "additional_code_points": ["200D"], |
| 105 | + ] |
| 106 | + ), |
| 107 | + ] |
| 108 | + ) |
| 109 | + // swiftlint:enable invisible_character |
| 110 | +} |
| 111 | + |
| 112 | +private extension InvisibleCharacterRule { |
| 113 | + final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { |
| 114 | + override func visitPost(_ node: StringLiteralExprSyntax) { |
| 115 | + let violatingCharacters = configuration.violatingCharacters |
| 116 | + for segment in node.segments { |
| 117 | + guard let stringSegment = segment.as(StringSegmentSyntax.self) else { |
| 118 | + continue |
| 119 | + } |
| 120 | + let text = stringSegment.content.text |
| 121 | + let scalars = text.unicodeScalars |
| 122 | + guard scalars.contains(where: { violatingCharacters.contains($0) }) else { |
| 123 | + continue |
| 124 | + } |
| 125 | + var utf8Offset = 0 |
| 126 | + |
| 127 | + for scalar in scalars { |
| 128 | + defer { |
| 129 | + utf8Offset += scalar.utf8.count |
| 130 | + } |
| 131 | + guard violatingCharacters.contains(scalar) else { |
| 132 | + continue |
| 133 | + } |
| 134 | + |
| 135 | + let characterName = InvisibleCharacterConfiguration.defaultCharacterDescriptions[scalar] |
| 136 | + ?? scalar.escaped(asASCII: true) |
| 137 | + |
| 138 | + let position = stringSegment.content.positionAfterSkippingLeadingTrivia.advanced(by: utf8Offset) |
| 139 | + violations.append( |
| 140 | + ReasonedRuleViolation( |
| 141 | + position: position, |
| 142 | + reason: "String literal should not contain invisible character \(characterName)", |
| 143 | + correction: .init( |
| 144 | + start: position, |
| 145 | + end: position.advanced(by: scalar.utf8.count), |
| 146 | + replacement: "" |
| 147 | + ) |
| 148 | + ) |
| 149 | + ) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments