Skip to content

Commit c3dd523

Browse files
atamez31allevato
authored andcommitted
Implementation of IdentifiersMustBeASCII (swiftlang#62)
1 parent 2423d7b commit c3dd523

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

tools/swift-format/Sources/Rules/IdentifiersMustBeASCII.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,18 @@ import SwiftSyntax
88
///
99
/// - SeeAlso: https://google.github.io/swift#identifiers
1010
public final class IdentifiersMustBeASCII: SyntaxLintRule {
11+
public override func visit(_ node: IdentifierPatternSyntax) {
12+
let identifier = node.identifier.text
13+
let invalidCharacters = identifier.unicodeScalars.filter { !$0.isASCII }.map { $0.description }
14+
15+
if !invalidCharacters.isEmpty {
16+
diagnose(.nonASCIICharsNotAllowed(invalidCharacters, identifier), on: node)
17+
}
18+
}
19+
}
1120

21+
extension Diagnostic.Message {
22+
static func nonASCIICharsNotAllowed(_ invalidCharacters: [String], _ identifierName: String) -> Diagnostic.Message {
23+
return .init(.warning, "The identifier '\(identifierName)' contains the following non-ASCII characters: \(invalidCharacters.joined(separator: ", "))")
24+
}
1225
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Foundation
2+
import SwiftSyntax
3+
import XCTest
4+
5+
@testable import Rules
6+
7+
public class IdentifiersMustBeASCIITests: DiagnosingTestCase {
8+
public func testInvalidIdentifiers() {
9+
let input =
10+
"""
11+
let Te$t = 1
12+
var fo😎o = 2
13+
let Δx = newX - previousX
14+
var 🤩😆 = 20
15+
"""
16+
performLint(IdentifiersMustBeASCII.self, input: input)
17+
XCTAssertDiagnosed(.nonASCIICharsNotAllowed(["😎"],"fo😎o"))
18+
XCTAssertDiagnosed(.nonASCIICharsNotAllowed(["🤩", "😆"], "🤩😆"))
19+
}
20+
21+
#if !os(macOS)
22+
static let allTests = [
23+
IdentifiersMustBeASCIITests.testInvalidIdentifiers,
24+
]
25+
#endif
26+
}

0 commit comments

Comments
 (0)