Skip to content

Commit 18cee31

Browse files
authored
NonInclusiveLanguageChecker to flag words with multiple spaces (#302)
The non-inclusive language checker doesn’t match against words with multiple spaces. It matches words like "whitelist’" and "white list" but not "white list". rdar://75552499
1 parent d4cb079 commit 18cee31

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

Sources/SwiftDocC/Checker/Checkers/NonInclusiveLanguageChecker.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -163,7 +163,7 @@ public struct NonInclusiveLanguageChecker: Checker {
163163
/// The default list of terms to look for in documentation.
164164
fileprivate let builtinExcludedTerms: [NonInclusiveLanguageChecker.Term] = [
165165
NonInclusiveLanguageChecker.Term(
166-
expression: #"black\W?list\w{0,2}"#,
166+
expression: #"black\W*list\w{0,2}"#,
167167
message: "Choose a more inclusive alternative that’s appropriate to the context, such as deny list/allow list or unapproved list/approved list.",
168168
replacement: "deny list"
169169
),
@@ -178,7 +178,7 @@ fileprivate let builtinExcludedTerms: [NonInclusiveLanguageChecker.Term] = [
178178
replacement: "secondary"
179179
),
180180
NonInclusiveLanguageChecker.Term(
181-
expression: #"white\W?list\w{0,2}"#,
181+
expression: #"white\W*list\w{0,2}"#,
182182
message: "Choose a more inclusive alternative that’s appropriate to the context, such as deny list/allow list or unapproved list/approved list.",
183183
replacement: "allow list"
184184
)

Tests/SwiftDocCTests/Checker/Checkers/NonInclusiveLanguageCheckerTests.swift

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -31,6 +31,39 @@ class NonInclusiveLanguageCheckerTests: XCTestCase {
3131
XCTAssertEqual(range.upperBound.column, 16)
3232
}
3333

34+
func testMatchTermWithSpaces() throws {
35+
let source = """
36+
# A White listed title
37+
# A Black listed title
38+
# A White listed title
39+
"""
40+
let document = Document(parsing: source)
41+
var checker = NonInclusiveLanguageChecker(sourceFile: nil)
42+
checker.visit(document)
43+
XCTAssertEqual(checker.problems.count, 3)
44+
45+
let problem = try XCTUnwrap(checker.problems.first)
46+
let range = try XCTUnwrap(problem.diagnostic.range)
47+
XCTAssertEqual(range.lowerBound.line, 1)
48+
XCTAssertEqual(range.lowerBound.column, 5)
49+
XCTAssertEqual(range.upperBound.line, 1)
50+
XCTAssertEqual(range.upperBound.column, 18)
51+
52+
let problemTwo = try XCTUnwrap(checker.problems[1])
53+
let rangeTwo = try XCTUnwrap(problemTwo.diagnostic.range)
54+
XCTAssertEqual(rangeTwo.lowerBound.line, 2)
55+
XCTAssertEqual(rangeTwo.lowerBound.column, 5)
56+
XCTAssertEqual(rangeTwo.upperBound.line, 2)
57+
XCTAssertEqual(rangeTwo.upperBound.column, 20)
58+
59+
let problemThree = try XCTUnwrap(checker.problems[2])
60+
let rangeThree = try XCTUnwrap(problemThree.diagnostic.range)
61+
XCTAssertEqual(rangeThree.lowerBound.line, 3)
62+
XCTAssertEqual(rangeThree.lowerBound.column, 5)
63+
XCTAssertEqual(rangeThree.upperBound.line, 3)
64+
XCTAssertEqual(rangeThree.upperBound.column, 17)
65+
}
66+
3467
func testMatchTermInAbstract() throws {
3568
let source = """
3669
# Title

0 commit comments

Comments
 (0)