Skip to content

Commit 0e8d46c

Browse files
committed
Support adding and testing new rules without using Xcode (#2065)
1 parent 5179e40 commit 0e8d46c

File tree

4 files changed

+85
-1695
lines changed

4 files changed

+85
-1695
lines changed

.github/workflows/build.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ jobs:
99
fail-fast: false
1010
matrix:
1111
macos:
12-
- 13
13-
- latest
12+
- 15
1413
xcode:
15-
- latest-stable
14+
- 16.3
1615
runs-on: macos-${{ matrix.macos }}
1716
steps:
1817
- name: Select Xcode version
@@ -91,9 +90,9 @@ jobs:
9190
fail-fast: false
9291
matrix:
9392
macos:
94-
- 13
93+
- 15
9594
xcode:
96-
- latest-stable
95+
- 16.3
9796
runs-on: macos-${{ matrix.macos }}
9897
steps:
9998
- name: Select Xcode version
@@ -110,9 +109,9 @@ jobs:
110109
fail-fast: false
111110
matrix:
112111
macos:
113-
- 13
112+
- 15
114113
xcode:
115-
- latest-stable
114+
- 16.3
116115
runs-on: macos-${{ matrix.macos }}
117116
steps:
118117
- name: Select Xcode version
@@ -129,9 +128,9 @@ jobs:
129128
fail-fast: false
130129
matrix:
131130
macos:
132-
- 13
131+
- 15
133132
xcode:
134-
- latest-stable
133+
- 16.3
135134
runs-on: macos-${{ matrix.macos }}
136135
steps:
137136
- name: Select Xcode version

CONTRIBUTING.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ Tests are run automatically on all pull requests, branches and tags. These are t
6565

6666
There is a separate Performance Tests scheme that you should run manually if your code changes are likely to affect performance.
6767

68+
Test cases for individual rules can be ran via the command line using the `./Scripts/test_rule.sh` script. For example:
69+
70+
```sh
71+
$ ./Scripts/test_rule.sh blankLinesAtStartOfScope
72+
$ ./Scripts/test_rule.sh indent
73+
$ ./Scripts/test_rule.sh wrap
74+
```
75+
76+
## Tips
77+
78+
* Use existing helpers in `ParsingHelpers.swift` or `FormattingHelpers.swift` where possible. There are existing parsing helpers for many parts of the Swift grammar, like:
79+
* Types (`parseType(at:)`)
80+
* Declarations (`parseDeclarations()`)
81+
* Expressions (`parseExpressionRange(startingAt:)`)
82+
* Properties (`parsePropertyDeclaration(atIntroducerIndex:)`)
83+
* Functions (`parseFunctionDeclaration(keywordIndex:)`)
84+
85+
* Define helpers in extensions on `Formatter` at the bottom of the Rule file. These should be `internal` to improve discoverability. Helpers used by multiple rules should be moved to `ParsingHelpers`.
86+
* Before writing new parsing helpers, consider checking if any existing rule has parsing helpers that could be reused.
87+
6888
## Prerelease builds
6989

7090
If you contribute a new rule or option, it would be published in the following major version release. To start using the new rule or option right away in your own project, you could use a prerelease build of the `develop` branch. More information is available [here](https://github.com/nicklockwood/SwiftFormat#prerelease-builds).

Scripts/test_rule.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
# Script for testing individual SwiftFormat rules.
4+
# $ ./Script/test_rule.sh blankLinesAtStartOfScope
5+
6+
# Argument validation
7+
if [ "$#" -ne 1 ]; then
8+
echo "Usage: $0 <RuleName>" >&2
9+
exit 1
10+
fi
11+
12+
rule_name="$1"
13+
14+
# Capitalize the first letter
15+
first_char_upper=$(echo ${rule_name:0:1} | tr '[:lower:]' '[:upper:]')
16+
rest_of_name="${rule_name:1}"
17+
class_name_stem="${first_char_upper}${rest_of_name}"
18+
19+
# Append "Tests"
20+
TEST_CLASS_NAME="${class_name_stem}Tests"
21+
22+
# Validate test file existence
23+
TEST_FILE_PATH="Tests/Rules/${TEST_CLASS_NAME}.swift"
24+
if [ ! -f "$TEST_FILE_PATH" ]; then
25+
echo "Error: Test file ${TEST_FILE_PATH} not found." >&2
26+
exit 2
27+
fi
28+
29+
echo "Testing ${rule_name} rule..."
30+
31+
# Run tests for the specific rule using swift test
32+
SWIFT_TEST_TARGET="SwiftFormatTests"
33+
swift test --filter "${SWIFT_TEST_TARGET}.${TEST_CLASS_NAME}" 2>&1

0 commit comments

Comments
 (0)