@kamaal111/kamaal-quality-config 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -1
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +72 -0
- package/dist/cli.js.map +1 -0
- package/package.json +11 -3
- package/swift/SwiftLintChecks/Package.resolved +15 -0
- package/swift/SwiftLintChecks/Package.swift +22 -0
- package/swift/SwiftLintChecks/Sources/SwiftLintChecks/LintViolation.swift +23 -0
- package/swift/SwiftLintChecks/Sources/SwiftLintChecks/MultilineChainSpacingVisitor.swift +35 -0
- package/swift/SwiftLintChecks/Sources/SwiftLintChecks/SwiftLint.swift +64 -0
- package/swift/SwiftLintChecks/Sources/SwiftLintChecks/SwiftLintChecks.swift +12 -0
- package/swift/SwiftLintChecks/Sources/SwiftLintChecks/SwiftLintCommand.swift +47 -0
- package/swift/SwiftLintChecks/Sources/SwiftLintChecks/ViewBodyDeclarationVisitor.swift +36 -0
- package/swift/SwiftLintChecks/Tests/SwiftLintChecksTests/MemberSpacingTests.swift +171 -0
- package/swift/SwiftLintChecks/Tests/SwiftLintChecksTests/MultilineChainSpacingTests.swift +68 -0
- package/swift/SwiftLintChecks/Tests/SwiftLintChecksTests/SwiftLintCommandTests.swift +143 -0
- package/swift/SwiftLintChecks/Tests/SwiftLintChecksTests/ViewBodyDeclarationTests.swift +129 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @kamaal111/kamaal-quality-config
|
|
2
2
|
|
|
3
|
-
Kamaal Farah's shared quality configs for [oxlint and oxfmt](https://oxc.rs) and Swift
|
|
3
|
+
Kamaal Farah's shared quality configs for [oxlint and oxfmt](https://oxc.rs), Swift format, and custom Swift lint checks.
|
|
4
4
|
|
|
5
5
|
Currently enables every rule from [`@kamaal111/oxlint-plugin-anti-slop`](https://github.com/kamaal111/anti-slop),
|
|
6
6
|
Kamaal's fork of [`dmmulroy/anti-slop`](https://github.com/dmmulroy/anti-slop), plus a curated set of
|
|
@@ -29,6 +29,7 @@ More rule sources will be folded in over time.
|
|
|
29
29
|
- [oxlint](#oxlint)
|
|
30
30
|
- [oxfmt](#oxfmt)
|
|
31
31
|
- [Swift format](#swift-format)
|
|
32
|
+
- [Swift lint](#swift-lint)
|
|
32
33
|
- [Development](#development)
|
|
33
34
|
|
|
34
35
|
## Install
|
|
@@ -139,6 +140,40 @@ For programmatic consumption, import the same plain object from
|
|
|
139
140
|
`@kamaal111/kamaal-quality-config/swift-format` or use `kamaalQuality.swiftFormat` from the root
|
|
140
141
|
namespace. The generated JSON is also exported at the `./swift-format.json` package subpath.
|
|
141
142
|
|
|
143
|
+
### Swift lint
|
|
144
|
+
|
|
145
|
+
The package provides `kqc swift-lint` for syntax-based Swift checks. Install a Swift 6.4 or newer
|
|
146
|
+
toolchain separately, then run from your project root:
|
|
147
|
+
|
|
148
|
+
```sh
|
|
149
|
+
pnpm exec kqc swift-lint
|
|
150
|
+
pnpm exec kqc swift-lint Sources Tests
|
|
151
|
+
pnpm exec kqc swift-lint "Source Files/Example.swift"
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
In package scripts, use `"lint:swift": "kqc swift-lint Sources Tests"`. With the package installed
|
|
155
|
+
globally, invoke `kqc swift-lint` directly.
|
|
156
|
+
|
|
157
|
+
Without paths, the command checks the current directory recursively. Explicit file and directory
|
|
158
|
+
paths are relative to your working directory. Recursive scanning skips hidden paths (including
|
|
159
|
+
`.build`), `node_modules`, symlinks, and non-Swift files. Use `--` before paths starting with a dash.
|
|
160
|
+
|
|
161
|
+
All three checks are enabled:
|
|
162
|
+
|
|
163
|
+
- `member_spacing`: separate methods and initializers from adjacent members with a blank line.
|
|
164
|
+
- `multiline_chain_spacing`: put each chained call on its own line in multiline expressions.
|
|
165
|
+
- `view_body_declarations`: move local `let` and `var` declarations out of SwiftUI
|
|
166
|
+
`var body: some View`, including nested builder closures. Conditional bindings remain allowed.
|
|
167
|
+
|
|
168
|
+
The CLI runs `swift run --package-path` against the bundled `SwiftLintChecks` package and does not
|
|
169
|
+
modify source files. Diagnostics include file, line, column, and rule identifier. Exit codes are
|
|
170
|
+
`0` for clean sources, `1` for violations or unreadable/invalid sources, `2` for usage errors,
|
|
171
|
+
and `127` when Swift is missing. Swift build failures retain their exit status.
|
|
172
|
+
|
|
173
|
+
The first invocation may download SwiftSyntax and compile the checker. SwiftPM caches build
|
|
174
|
+
artifacts in the installed Swift package's `.build` directory, so that directory must be writable.
|
|
175
|
+
Subsequent invocations reuse the build. Run `kqc swift-lint --help` for usage.
|
|
176
|
+
|
|
142
177
|
## Development
|
|
143
178
|
|
|
144
179
|
Use the devcontainer with Node.js 26 (as specified in `.nvmrc`) and pnpm 12.14.0.
|
|
@@ -162,3 +197,8 @@ lints it using the packed npm artifact, rejects two-space indentation, and verif
|
|
|
162
197
|
and idempotence. To run that command locally, Swift must be available inside the devcontainer.
|
|
163
198
|
The dedicated command fails if Swift is missing; ordinary `pnpm test` skips only the Swift toolchain
|
|
164
199
|
cases while still checking the packed configuration and exports, including on Node.js 22.
|
|
200
|
+
|
|
201
|
+
Run `pnpm test:swift-lint` with Swift 6.4 or newer available inside the devcontainer to run the
|
|
202
|
+
bundled checker regression suites and packed CLI integration tests. CI supplies Swift 6.4.1.
|
|
203
|
+
The dedicated command fails if Swift is missing; ordinary tests still verify CLI dispatch,
|
|
204
|
+
packaging, and consumer compatibility without requiring Swift.
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import childProcess from 'node:child_process';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import url from 'node:url';
|
|
6
|
+
import util from 'node:util';
|
|
7
|
+
const usage = `Usage: kqc swift-lint [paths...]
|
|
8
|
+
|
|
9
|
+
Check Swift sources recursively (default: current directory).
|
|
10
|
+
Requires Swift 6.4 or newer. Use -- before paths beginning with a dash.
|
|
11
|
+
`;
|
|
12
|
+
function runSwift(paths) {
|
|
13
|
+
const packageDirectory = url.fileURLToPath(new URL('../swift/SwiftLintChecks', import.meta.url));
|
|
14
|
+
const child = childProcess.spawn('swift', ['run', '--package-path', packageDirectory, 'kqc-swift-lint', ...paths.map(target => path.resolve(target))], { stdio: 'inherit' });
|
|
15
|
+
const interrupt = () => {
|
|
16
|
+
child.kill('SIGINT');
|
|
17
|
+
};
|
|
18
|
+
const terminate = () => {
|
|
19
|
+
child.kill('SIGTERM');
|
|
20
|
+
};
|
|
21
|
+
process.on('SIGINT', interrupt);
|
|
22
|
+
process.on('SIGTERM', terminate);
|
|
23
|
+
child.once('error', error => {
|
|
24
|
+
if ('code' in error && error.code === 'ENOENT') {
|
|
25
|
+
console.error('kqc: Swift 6.4 or newer is required; install it and ensure swift is on PATH.');
|
|
26
|
+
process.exitCode = 127;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
console.error(`kqc: Cannot launch Swift: ${error.message}`);
|
|
30
|
+
process.exitCode = 1;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
child.once('close', (code, signal) => {
|
|
34
|
+
process.off('SIGINT', interrupt);
|
|
35
|
+
process.off('SIGTERM', terminate);
|
|
36
|
+
if (signal) {
|
|
37
|
+
process.kill(process.pid, signal);
|
|
38
|
+
process.exitCode = 128 + os.constants.signals[signal];
|
|
39
|
+
}
|
|
40
|
+
else if (code !== null && code >= 0) {
|
|
41
|
+
process.exitCode = code;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function parseArguments() {
|
|
46
|
+
try {
|
|
47
|
+
return util.parseArgs({
|
|
48
|
+
options: { help: { type: 'boolean' } },
|
|
49
|
+
allowPositionals: true,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
console.error(usage);
|
|
54
|
+
process.exitCode = 2;
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const parsed = parseArguments();
|
|
59
|
+
if (parsed !== undefined) {
|
|
60
|
+
const [command, ...paths] = parsed.positionals;
|
|
61
|
+
if (parsed.values.help && paths.length === 0 && (command === undefined || command === 'swift-lint')) {
|
|
62
|
+
console.log(usage);
|
|
63
|
+
}
|
|
64
|
+
else if (command !== 'swift-lint' || parsed.values.help) {
|
|
65
|
+
console.error(usage);
|
|
66
|
+
process.exitCode = 2;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
runSwift(paths.length === 0 ? ['.'] : paths);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kamaal111/kamaal-quality-config",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Kamaal Farah's shared quality configurations.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lint",
|
|
@@ -14,9 +14,16 @@
|
|
|
14
14
|
"type": "git",
|
|
15
15
|
"url": "git+https://github.com/kamaal111/kamaal-quality-config.git"
|
|
16
16
|
},
|
|
17
|
+
"bin": {
|
|
18
|
+
"kqc": "./dist/cli.js"
|
|
19
|
+
},
|
|
17
20
|
"files": [
|
|
18
21
|
"dist",
|
|
19
|
-
"swift-format.json"
|
|
22
|
+
"swift-format.json",
|
|
23
|
+
"swift/SwiftLintChecks/Package.swift",
|
|
24
|
+
"swift/SwiftLintChecks/Package.resolved",
|
|
25
|
+
"swift/SwiftLintChecks/Sources",
|
|
26
|
+
"swift/SwiftLintChecks/Tests"
|
|
20
27
|
],
|
|
21
28
|
"type": "module",
|
|
22
29
|
"main": "./dist/index.js",
|
|
@@ -86,6 +93,7 @@
|
|
|
86
93
|
"quality": "pnpm run lint && pnpm run format:check && pnpm run type-check",
|
|
87
94
|
"test": "pnpm run build && tsc -p tsconfig.tests.json && vitest run",
|
|
88
95
|
"release": "node ./scripts/publish.ts",
|
|
89
|
-
"test:swift-format": "SWIFT_FORMAT_TEST=1 pnpm test src/packed-consumer.test.ts"
|
|
96
|
+
"test:swift-format": "SWIFT_FORMAT_TEST=1 pnpm test src/packed-consumer.test.ts",
|
|
97
|
+
"test:swift-lint": "swift test --package-path swift/SwiftLintChecks && SWIFT_LINT_TEST=1 pnpm test src/packed-consumer.test.ts"
|
|
90
98
|
}
|
|
91
99
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"originHash" : "a875d193e1e9527c2ef2d74f177469872114cdfa86907e7b82b1d2290d4fb61d",
|
|
3
|
+
"pins" : [
|
|
4
|
+
{
|
|
5
|
+
"identity" : "swift-syntax",
|
|
6
|
+
"kind" : "remoteSourceControl",
|
|
7
|
+
"location" : "https://github.com/swiftlang/swift-syntax",
|
|
8
|
+
"state" : {
|
|
9
|
+
"revision" : "050f1a346fbbac0ca2cfb15a95274f7bd1cf0ccf",
|
|
10
|
+
"version" : "604.0.0"
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
],
|
|
14
|
+
"version" : 3
|
|
15
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// swift-tools-version: 6.4
|
|
2
|
+
|
|
3
|
+
import PackageDescription
|
|
4
|
+
|
|
5
|
+
let package = Package(
|
|
6
|
+
name: "SwiftLintChecks",
|
|
7
|
+
platforms: [.macOS(.v15)],
|
|
8
|
+
products: [.executable(name: "kqc-swift-lint", targets: ["SwiftLintChecks"])],
|
|
9
|
+
dependencies: [
|
|
10
|
+
.package(url: "https://github.com/swiftlang/swift-syntax", exact: "604.0.0")
|
|
11
|
+
],
|
|
12
|
+
targets: [
|
|
13
|
+
.executableTarget(
|
|
14
|
+
name: "SwiftLintChecks",
|
|
15
|
+
dependencies: [
|
|
16
|
+
.product(name: "SwiftSyntax", package: "swift-syntax"),
|
|
17
|
+
.product(name: "SwiftParser", package: "swift-syntax"),
|
|
18
|
+
]
|
|
19
|
+
),
|
|
20
|
+
.testTarget(name: "SwiftLintChecksTests", dependencies: ["SwiftLintChecks"]),
|
|
21
|
+
]
|
|
22
|
+
)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import SwiftSyntax
|
|
2
|
+
|
|
3
|
+
struct LintViolation {
|
|
4
|
+
let position: AbsolutePosition
|
|
5
|
+
let rule: LintRule
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
enum LintRule: String {
|
|
9
|
+
case memberSpacing = "member_spacing"
|
|
10
|
+
case multilineChainSpacing = "multiline_chain_spacing"
|
|
11
|
+
case viewBodyDeclarations = "view_body_declarations"
|
|
12
|
+
|
|
13
|
+
var message: String {
|
|
14
|
+
switch self {
|
|
15
|
+
case .memberSpacing:
|
|
16
|
+
"Separate initializers and methods from adjacent members with a blank line."
|
|
17
|
+
case .multilineChainSpacing:
|
|
18
|
+
"Place each chained call on its own line in a multiline expression."
|
|
19
|
+
case .viewBodyDeclarations:
|
|
20
|
+
"Move local declarations out of SwiftUI body into computed properties or helper functions."
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import SwiftSyntax
|
|
2
|
+
|
|
3
|
+
final class MultilineChainSpacingVisitor: SyntaxVisitor {
|
|
4
|
+
private(set) var violations: [LintViolation] = []
|
|
5
|
+
private let converter: SourceLocationConverter
|
|
6
|
+
|
|
7
|
+
init(converter: SourceLocationConverter) {
|
|
8
|
+
self.converter = converter
|
|
9
|
+
super.init(viewMode: .sourceAccurate)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
override func visit(_ node: FunctionCallExprSyntax) -> SyntaxVisitorContinueKind {
|
|
13
|
+
if let member = node.parent?.as(MemberAccessExprSyntax.self),
|
|
14
|
+
member.parent?.is(FunctionCallExprSyntax.self) == true
|
|
15
|
+
{
|
|
16
|
+
return .visitChildren
|
|
17
|
+
}
|
|
18
|
+
let startLine = converter.location(for: node.positionAfterSkippingLeadingTrivia).line
|
|
19
|
+
let endLine = converter.location(for: node.endPositionBeforeTrailingTrivia).line
|
|
20
|
+
guard startLine != endLine else { return .visitChildren }
|
|
21
|
+
var call = node
|
|
22
|
+
while let member = call.calledExpression.as(MemberAccessExprSyntax.self),
|
|
23
|
+
let previous = member.base?.as(FunctionCallExprSyntax.self)
|
|
24
|
+
{
|
|
25
|
+
let previousLine = converter.location(for: previous.endPositionBeforeTrailingTrivia).line
|
|
26
|
+
let dotPosition = member.period.positionAfterSkippingLeadingTrivia
|
|
27
|
+
let dotLine = converter.location(for: dotPosition).line
|
|
28
|
+
if previousLine == dotLine {
|
|
29
|
+
violations.append(LintViolation(position: dotPosition, rule: .multilineChainSpacing))
|
|
30
|
+
}
|
|
31
|
+
call = previous
|
|
32
|
+
}
|
|
33
|
+
return .visitChildren
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import SwiftParser
|
|
2
|
+
import SwiftSyntax
|
|
3
|
+
|
|
4
|
+
enum SwiftLint {
|
|
5
|
+
static func diagnostics(source: String, path: String) -> [String] {
|
|
6
|
+
let tree = Parser.parse(source: source)
|
|
7
|
+
guard !tree.hasError else {
|
|
8
|
+
return ["\(path):1:1: error: Cannot lint Swift source because Swift syntax is invalid."]
|
|
9
|
+
}
|
|
10
|
+
let visitor = MemberSpacingVisitor(viewMode: .sourceAccurate)
|
|
11
|
+
visitor.walk(tree)
|
|
12
|
+
let converter = SourceLocationConverter(fileName: path, tree: tree)
|
|
13
|
+
let chains = MultilineChainSpacingVisitor(converter: converter)
|
|
14
|
+
chains.walk(tree)
|
|
15
|
+
let bodies = ViewBodyDeclarationVisitor(viewMode: .sourceAccurate)
|
|
16
|
+
bodies.walk(tree)
|
|
17
|
+
return (visitor.violations + chains.violations + bodies.violations).sorted { $0.position < $1.position }
|
|
18
|
+
.map { violation in
|
|
19
|
+
let location = converter.location(for: violation.position)
|
|
20
|
+
return
|
|
21
|
+
"\(path):\(location.line):\(location.column): error: \(violation.rule.message) [\(violation.rule.rawValue)]"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private final class MemberSpacingVisitor: SyntaxVisitor {
|
|
27
|
+
private(set) var violations: [LintViolation] = []
|
|
28
|
+
|
|
29
|
+
override func visit(_ node: MemberBlockItemListSyntax) -> SyntaxVisitorContinueKind {
|
|
30
|
+
for (previous, current) in zip(node, node.dropFirst()) {
|
|
31
|
+
guard !previous.decl.is(IfConfigDeclSyntax.self), !current.decl.is(IfConfigDeclSyntax.self),
|
|
32
|
+
Self.isMethod(previous.decl) || Self.isMethod(current.decl)
|
|
33
|
+
else { continue }
|
|
34
|
+
let trivia = previous.trailingTrivia + current.leadingTrivia
|
|
35
|
+
if !Self.hasBlankLineBeforeDeclaration(trivia) {
|
|
36
|
+
violations.append(
|
|
37
|
+
LintViolation(position: current.positionAfterSkippingLeadingTrivia, rule: .memberSpacing)
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return .visitChildren
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private static func isMethod(_ declaration: DeclSyntax) -> Bool {
|
|
45
|
+
declaration.is(FunctionDeclSyntax.self) || declaration.is(InitializerDeclSyntax.self)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private static func hasBlankLineBeforeDeclaration(_ trivia: Trivia) -> Bool {
|
|
49
|
+
var lineBreaks = 0
|
|
50
|
+
for piece in trivia {
|
|
51
|
+
switch piece {
|
|
52
|
+
case .newlines(let count), .carriageReturns(let count), .carriageReturnLineFeeds(let count):
|
|
53
|
+
lineBreaks += count
|
|
54
|
+
if lineBreaks >= 2 { return true }
|
|
55
|
+
case .spaces, .tabs:
|
|
56
|
+
continue
|
|
57
|
+
default:
|
|
58
|
+
// A comment on its own line belongs to the following declaration.
|
|
59
|
+
if lineBreaks > 0 { return false }
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
@main
|
|
4
|
+
private enum SwiftLintChecks {
|
|
5
|
+
static func main() {
|
|
6
|
+
let report = SwiftLintCommand.run(paths: Array(CommandLine.arguments.dropFirst()))
|
|
7
|
+
for diagnostic in report.diagnostics {
|
|
8
|
+
print(diagnostic)
|
|
9
|
+
}
|
|
10
|
+
exit(report.status)
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
struct SwiftLintReport {
|
|
4
|
+
let diagnostics: [String]
|
|
5
|
+
|
|
6
|
+
var status: Int32 { diagnostics.isEmpty ? 0 : 1 }
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
enum SwiftLintCommand {
|
|
10
|
+
static func run(paths: [String]) -> SwiftLintReport {
|
|
11
|
+
guard !paths.isEmpty else {
|
|
12
|
+
return SwiftLintReport(diagnostics: ["error: Usage: kqc-swift-lint <file-or-directory> ..."])
|
|
13
|
+
}
|
|
14
|
+
var diagnostics: [String] = []
|
|
15
|
+
for path in paths {
|
|
16
|
+
do {
|
|
17
|
+
for file in try swiftFiles(at: URL(fileURLWithPath: path)) {
|
|
18
|
+
let source = try String(contentsOf: file, encoding: .utf8)
|
|
19
|
+
diagnostics += SwiftLint.diagnostics(source: source, path: file.path)
|
|
20
|
+
}
|
|
21
|
+
} catch {
|
|
22
|
+
diagnostics.append("\(path):1:1: error: Cannot lint Swift source: \(error.localizedDescription)")
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return SwiftLintReport(diagnostics: diagnostics)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private static func swiftFiles(at url: URL) throws -> [URL] {
|
|
29
|
+
let values = try url.resourceValues(forKeys: [.isDirectoryKey, .isSymbolicLinkKey])
|
|
30
|
+
if values.isSymbolicLink == true { return [] }
|
|
31
|
+
if values.isDirectory == true
|
|
32
|
+
&& (url.lastPathComponent.hasPrefix(".") && url.lastPathComponent != "."
|
|
33
|
+
|| url.lastPathComponent == "node_modules")
|
|
34
|
+
{
|
|
35
|
+
return []
|
|
36
|
+
}
|
|
37
|
+
guard values.isDirectory == true else {
|
|
38
|
+
return url.pathExtension == "swift" ? [url] : []
|
|
39
|
+
}
|
|
40
|
+
let children = try FileManager.default.contentsOfDirectory(
|
|
41
|
+
at: url,
|
|
42
|
+
includingPropertiesForKeys: [.isDirectoryKey, .isSymbolicLinkKey],
|
|
43
|
+
options: [.skipsHiddenFiles]
|
|
44
|
+
)
|
|
45
|
+
return try children.sorted { $0.path < $1.path }.flatMap { try swiftFiles(at: $0) }
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import SwiftSyntax
|
|
2
|
+
|
|
3
|
+
final class ViewBodyDeclarationVisitor: SyntaxVisitor {
|
|
4
|
+
private(set) var violations: [LintViolation] = []
|
|
5
|
+
|
|
6
|
+
override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
|
|
7
|
+
guard node.parent?.is(MemberBlockItemSyntax.self) == true else { return .visitChildren }
|
|
8
|
+
for binding in node.bindings {
|
|
9
|
+
guard binding.pattern.as(IdentifierPatternSyntax.self)?.identifier.text == "body",
|
|
10
|
+
let type = binding.typeAnnotation?.type.as(SomeOrAnyTypeSyntax.self),
|
|
11
|
+
type.someOrAnySpecifier.tokenKind == .keyword(.some),
|
|
12
|
+
["View", "SwiftUI.View"].contains(type.constraint.trimmedDescription),
|
|
13
|
+
let accessors = binding.accessorBlock
|
|
14
|
+
else { continue }
|
|
15
|
+
let locals = BodyLocalDeclarationVisitor(viewMode: .sourceAccurate)
|
|
16
|
+
locals.walk(accessors)
|
|
17
|
+
violations += locals.violations
|
|
18
|
+
}
|
|
19
|
+
return .visitChildren
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
private final class BodyLocalDeclarationVisitor: SyntaxVisitor {
|
|
24
|
+
private(set) var violations: [LintViolation] = []
|
|
25
|
+
|
|
26
|
+
override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
|
|
27
|
+
guard node.parent?.is(MemberBlockItemSyntax.self) != true else { return .skipChildren }
|
|
28
|
+
violations.append(
|
|
29
|
+
LintViolation(
|
|
30
|
+
position: node.bindingSpecifier.positionAfterSkippingLeadingTrivia,
|
|
31
|
+
rule: .viewBodyDeclarations
|
|
32
|
+
)
|
|
33
|
+
)
|
|
34
|
+
return .visitChildren
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Testing
|
|
3
|
+
|
|
4
|
+
@testable import SwiftLintChecks
|
|
5
|
+
|
|
6
|
+
struct MemberSpacingTests {
|
|
7
|
+
@Test
|
|
8
|
+
func `Rejects compact backup document`() {
|
|
9
|
+
let source = """
|
|
10
|
+
struct CollectionBackupDocument: FileDocument {
|
|
11
|
+
static var readableContentTypes: [UTType] { [.json] }
|
|
12
|
+
let data: Data
|
|
13
|
+
init(data: Data) { self.data = data }
|
|
14
|
+
init(configuration: ReadConfiguration) { data = configuration.file.regularFileContents ?? Data() }
|
|
15
|
+
func fileWrapper(configuration: WriteConfiguration) -> FileWrapper {
|
|
16
|
+
FileWrapper(regularFileWithContents: data)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
"""
|
|
20
|
+
#expect(
|
|
21
|
+
SwiftLint.diagnostics(source: source, path: "Backup.swift") == [
|
|
22
|
+
"Backup.swift:4:5: error: Separate initializers and methods from adjacent members with a blank line. [member_spacing]",
|
|
23
|
+
"Backup.swift:5:5: error: Separate initializers and methods from adjacent members with a blank line. [member_spacing]",
|
|
24
|
+
"Backup.swift:6:5: error: Separate initializers and methods from adjacent members with a blank line. [member_spacing]",
|
|
25
|
+
]
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@Test(arguments: [
|
|
30
|
+
"""
|
|
31
|
+
struct Example {
|
|
32
|
+
let first: Int
|
|
33
|
+
let second: Int
|
|
34
|
+
|
|
35
|
+
init() { first = 1; second = 2 }
|
|
36
|
+
|
|
37
|
+
func read() -> Int { first }
|
|
38
|
+
}
|
|
39
|
+
""",
|
|
40
|
+
"""
|
|
41
|
+
struct Example {
|
|
42
|
+
func read() {}
|
|
43
|
+
}
|
|
44
|
+
""",
|
|
45
|
+
"""
|
|
46
|
+
struct Example {
|
|
47
|
+
let first = 1
|
|
48
|
+
|
|
49
|
+
/// Reads the value.
|
|
50
|
+
@MainActor
|
|
51
|
+
func read() {}
|
|
52
|
+
}
|
|
53
|
+
""",
|
|
54
|
+
"""
|
|
55
|
+
struct Example {
|
|
56
|
+
let first = 1 // A trailing property comment.
|
|
57
|
+
|
|
58
|
+
func read() {}
|
|
59
|
+
}
|
|
60
|
+
""",
|
|
61
|
+
"""
|
|
62
|
+
struct Example {
|
|
63
|
+
func read() {}
|
|
64
|
+
|
|
65
|
+
let first = 1
|
|
66
|
+
}
|
|
67
|
+
""",
|
|
68
|
+
"struct Example { let first = 1; let second = 2 }",
|
|
69
|
+
])
|
|
70
|
+
func `Accepts separated methods and grouped properties`(source: String) {
|
|
71
|
+
#expect(SwiftLint.diagnostics(source: source, path: "Example.swift").isEmpty)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@Test(arguments: [
|
|
75
|
+
"""
|
|
76
|
+
struct Example {
|
|
77
|
+
let first = 1
|
|
78
|
+
/// Reads the value.
|
|
79
|
+
@MainActor
|
|
80
|
+
func read() {}
|
|
81
|
+
}
|
|
82
|
+
""",
|
|
83
|
+
"""
|
|
84
|
+
struct Example {
|
|
85
|
+
let first = 1 // A trailing property comment.
|
|
86
|
+
func read() {}
|
|
87
|
+
}
|
|
88
|
+
""",
|
|
89
|
+
"""
|
|
90
|
+
struct Example {
|
|
91
|
+
let first = 1
|
|
92
|
+
/* A comment containing
|
|
93
|
+
|
|
94
|
+
a blank line. */
|
|
95
|
+
func read() {}
|
|
96
|
+
}
|
|
97
|
+
""",
|
|
98
|
+
"""
|
|
99
|
+
struct Example {
|
|
100
|
+
func read() {}
|
|
101
|
+
let first = 1
|
|
102
|
+
}
|
|
103
|
+
""",
|
|
104
|
+
"""
|
|
105
|
+
protocol Example {
|
|
106
|
+
func first()
|
|
107
|
+
func second()
|
|
108
|
+
}
|
|
109
|
+
""",
|
|
110
|
+
"""
|
|
111
|
+
extension Example {
|
|
112
|
+
func first() {}
|
|
113
|
+
func second() {}
|
|
114
|
+
}
|
|
115
|
+
""",
|
|
116
|
+
"""
|
|
117
|
+
struct Outer {
|
|
118
|
+
struct Inner {
|
|
119
|
+
init() {}
|
|
120
|
+
func read() {}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
""",
|
|
124
|
+
"""
|
|
125
|
+
struct Example {
|
|
126
|
+
#if os(macOS)
|
|
127
|
+
func first() {}
|
|
128
|
+
func second() {}
|
|
129
|
+
#else
|
|
130
|
+
func third() {}
|
|
131
|
+
|
|
132
|
+
func fourth() {}
|
|
133
|
+
#endif
|
|
134
|
+
}
|
|
135
|
+
""",
|
|
136
|
+
])
|
|
137
|
+
func `Rejects missing member separation`(source: String) {
|
|
138
|
+
#expect(SwiftLint.diagnostics(source: source, path: "Example.swift").count == 1)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
@Test
|
|
142
|
+
func `Ignores declarations inside strings and comments`() {
|
|
143
|
+
let source = #"""
|
|
144
|
+
struct Example {
|
|
145
|
+
let source = """
|
|
146
|
+
func first() {}
|
|
147
|
+
func second() {}
|
|
148
|
+
"""
|
|
149
|
+
// func first() {}
|
|
150
|
+
// func second() {}
|
|
151
|
+
let another = 1
|
|
152
|
+
}
|
|
153
|
+
"""#
|
|
154
|
+
#expect(SwiftLint.diagnostics(source: source, path: "Example.swift").isEmpty)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
@Test
|
|
158
|
+
func `Handles Windows line endings and whitespace only blank lines`() {
|
|
159
|
+
let source = "struct Example {\r\n let first = 1\r\n \r\n func read() {}\r\n}"
|
|
160
|
+
#expect(SwiftLint.diagnostics(source: source, path: "Example.swift").isEmpty)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
@Test
|
|
164
|
+
func `Rejects invalid Swift syntax`() {
|
|
165
|
+
#expect(
|
|
166
|
+
SwiftLint.diagnostics(source: "struct Example {", path: "Broken.swift") == [
|
|
167
|
+
"Broken.swift:1:1: error: Cannot lint Swift source because Swift syntax is invalid."
|
|
168
|
+
]
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import Testing
|
|
2
|
+
|
|
3
|
+
@testable import SwiftLintChecks
|
|
4
|
+
|
|
5
|
+
struct MultilineChainSpacingTests {
|
|
6
|
+
@Test
|
|
7
|
+
func `Rejects touching modifiers after multiline Button`() {
|
|
8
|
+
let source = """
|
|
9
|
+
Button {
|
|
10
|
+
index -= 1
|
|
11
|
+
} label: {
|
|
12
|
+
Image(systemName: "chevron.left")
|
|
13
|
+
}
|
|
14
|
+
.disabled(index == 0).accessibilityLabel(Text("Previous page", bundle: .module))
|
|
15
|
+
"""
|
|
16
|
+
#expect(
|
|
17
|
+
SwiftLint.diagnostics(source: source, path: "Navigation.swift") == [
|
|
18
|
+
"Navigation.swift:6:22: error: Place each chained call on its own line in a multiline expression. [multiline_chain_spacing]"
|
|
19
|
+
]
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@Test(arguments: [
|
|
24
|
+
"""
|
|
25
|
+
Button {
|
|
26
|
+
index -= 1
|
|
27
|
+
} label: {
|
|
28
|
+
Image(systemName: "chevron.left")
|
|
29
|
+
}
|
|
30
|
+
.disabled(index == 0)
|
|
31
|
+
.accessibilityLabel(Text("Previous page", bundle: .module))
|
|
32
|
+
""",
|
|
33
|
+
"Text(\"Page\").tag(index)",
|
|
34
|
+
"""
|
|
35
|
+
Text("Page")
|
|
36
|
+
.font(.body)
|
|
37
|
+
.padding()
|
|
38
|
+
""",
|
|
39
|
+
#"""
|
|
40
|
+
let source = """
|
|
41
|
+
Text("Page")
|
|
42
|
+
.font(.body).padding()
|
|
43
|
+
"""
|
|
44
|
+
"""#,
|
|
45
|
+
"""
|
|
46
|
+
// Text("Page")
|
|
47
|
+
// .font(.body).padding()
|
|
48
|
+
""",
|
|
49
|
+
])
|
|
50
|
+
func `Accepts separated modifiers and compact single line expressions`(source: String) {
|
|
51
|
+
#expect(SwiftLint.diagnostics(source: source, path: "Navigation.swift").isEmpty)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
@Test
|
|
55
|
+
func `Checks every call when only the last call is multiline`() {
|
|
56
|
+
let source = """
|
|
57
|
+
Text("Page").font(.body).background(
|
|
58
|
+
Color.red
|
|
59
|
+
)
|
|
60
|
+
"""
|
|
61
|
+
#expect(
|
|
62
|
+
SwiftLint.diagnostics(source: source, path: "Navigation.swift") == [
|
|
63
|
+
"Navigation.swift:1:13: error: Place each chained call on its own line in a multiline expression. [multiline_chain_spacing]",
|
|
64
|
+
"Navigation.swift:1:25: error: Place each chained call on its own line in a multiline expression. [multiline_chain_spacing]",
|
|
65
|
+
]
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Testing
|
|
3
|
+
|
|
4
|
+
@testable import SwiftLintChecks
|
|
5
|
+
|
|
6
|
+
struct SwiftLintCommandTests {
|
|
7
|
+
@Test
|
|
8
|
+
func `Returns failure for missing arguments`() {
|
|
9
|
+
let report = SwiftLintCommand.run(paths: [])
|
|
10
|
+
#expect(report.status == 1)
|
|
11
|
+
#expect(report.diagnostics == ["error: Usage: kqc-swift-lint <file-or-directory> ..."])
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
@Test
|
|
15
|
+
func `Returns failure for unreadable input`() throws {
|
|
16
|
+
let directory = try Self.temporaryDirectory()
|
|
17
|
+
defer { try? FileManager.default.removeItem(at: directory) }
|
|
18
|
+
let path = directory.appendingPathComponent("Missing.swift").path
|
|
19
|
+
let report = SwiftLintCommand.run(paths: [path])
|
|
20
|
+
#expect(report.status == 1)
|
|
21
|
+
#expect(report.diagnostics.count == 1)
|
|
22
|
+
#expect(report.diagnostics.first?.hasPrefix("\(path):1:1: error: Cannot lint Swift source:") == true)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@Test
|
|
26
|
+
func `Recursively checks Swift sources and skips build artifacts`() throws {
|
|
27
|
+
let directory = try Self.temporaryDirectory()
|
|
28
|
+
defer { try? FileManager.default.removeItem(at: directory) }
|
|
29
|
+
let nested = directory.appendingPathComponent("Sources")
|
|
30
|
+
let build = directory.appendingPathComponent(".build")
|
|
31
|
+
try FileManager.default.createDirectory(at: nested, withIntermediateDirectories: true)
|
|
32
|
+
try FileManager.default.createDirectory(at: build, withIntermediateDirectories: true)
|
|
33
|
+
let source = """
|
|
34
|
+
struct Example {
|
|
35
|
+
let first = 1
|
|
36
|
+
func read() {}
|
|
37
|
+
}
|
|
38
|
+
"""
|
|
39
|
+
let file = nested.appendingPathComponent("Example.swift")
|
|
40
|
+
try source.write(to: file, atomically: true, encoding: .utf8)
|
|
41
|
+
try source.write(to: build.appendingPathComponent("Ignored.swift"), atomically: true, encoding: .utf8)
|
|
42
|
+
try source.write(to: directory.appendingPathComponent("Ignored.txt"), atomically: true, encoding: .utf8)
|
|
43
|
+
let report = SwiftLintCommand.run(paths: [directory.path])
|
|
44
|
+
#expect(report.status == 1)
|
|
45
|
+
#expect(report.diagnostics.count == 1)
|
|
46
|
+
#expect(
|
|
47
|
+
report.diagnostics.first?.hasSuffix(
|
|
48
|
+
"/Sources/Example.swift:3:5: error: Separate initializers and methods from adjacent members with a blank line. [member_spacing]"
|
|
49
|
+
) == true
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@Test
|
|
54
|
+
func `Returns success for separated members`() throws {
|
|
55
|
+
let directory = try Self.temporaryDirectory()
|
|
56
|
+
defer { try? FileManager.default.removeItem(at: directory) }
|
|
57
|
+
let file = directory.appendingPathComponent("Example.swift")
|
|
58
|
+
let source = """
|
|
59
|
+
struct Example {
|
|
60
|
+
let first = 1
|
|
61
|
+
|
|
62
|
+
func read() {}
|
|
63
|
+
}
|
|
64
|
+
"""
|
|
65
|
+
try source.write(to: file, atomically: true, encoding: .utf8)
|
|
66
|
+
let report = SwiftLintCommand.run(paths: [file.path])
|
|
67
|
+
#expect(report.status == 0)
|
|
68
|
+
#expect(report.diagnostics.isEmpty)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@Test
|
|
72
|
+
func `Returns failure for invalid syntax`() throws {
|
|
73
|
+
let directory = try Self.temporaryDirectory()
|
|
74
|
+
defer { try? FileManager.default.removeItem(at: directory) }
|
|
75
|
+
let file = directory.appendingPathComponent("Broken.swift")
|
|
76
|
+
try "struct Example {".write(to: file, atomically: true, encoding: .utf8)
|
|
77
|
+
let report = SwiftLintCommand.run(paths: [file.path])
|
|
78
|
+
#expect(report.status == 1)
|
|
79
|
+
#expect(
|
|
80
|
+
report.diagnostics == [
|
|
81
|
+
"\(file.path):1:1: error: Cannot lint Swift source because Swift syntax is invalid."
|
|
82
|
+
]
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private static func temporaryDirectory() throws -> URL {
|
|
87
|
+
let url = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
|
88
|
+
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
|
|
89
|
+
return url
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
@Test(arguments: [
|
|
93
|
+
(source: "struct Example { func first() {}\nfunc second() {} }", status: Int32(1)),
|
|
94
|
+
(source: "struct Example { func first() {}\n\nfunc second() {} }", status: Int32(0)),
|
|
95
|
+
])
|
|
96
|
+
func `Executable reports lint exit status`(source: String, status: Int32) throws {
|
|
97
|
+
let directory = try Self.temporaryDirectory()
|
|
98
|
+
defer { try? FileManager.default.removeItem(at: directory) }
|
|
99
|
+
let file = directory.appendingPathComponent("Example.swift")
|
|
100
|
+
try source.write(to: file, atomically: true, encoding: .utf8)
|
|
101
|
+
let package = URL(fileURLWithPath: #filePath)
|
|
102
|
+
.deletingLastPathComponent()
|
|
103
|
+
.deletingLastPathComponent()
|
|
104
|
+
.deletingLastPathComponent()
|
|
105
|
+
let output = Pipe()
|
|
106
|
+
let process = Process()
|
|
107
|
+
process.executableURL = package.appendingPathComponent(".build/debug/kqc-swift-lint")
|
|
108
|
+
process.arguments = [file.path]
|
|
109
|
+
process.standardOutput = output
|
|
110
|
+
try process.run()
|
|
111
|
+
let data = output.fileHandleForReading.readDataToEndOfFile()
|
|
112
|
+
process.waitUntilExit()
|
|
113
|
+
#expect(process.terminationStatus == status)
|
|
114
|
+
#expect(String(decoding: data, as: UTF8.self).contains("[member_spacing]") == (status == 1))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
@Test
|
|
118
|
+
func `Skips dependency directories and symlinks`() throws {
|
|
119
|
+
let directory = try Self.temporaryDirectory()
|
|
120
|
+
defer { try? FileManager.default.removeItem(at: directory) }
|
|
121
|
+
let dependency = directory.appendingPathComponent("node_modules")
|
|
122
|
+
try FileManager.default.createDirectory(at: dependency, withIntermediateDirectories: true)
|
|
123
|
+
let source = """
|
|
124
|
+
struct Example {
|
|
125
|
+
let value = 1
|
|
126
|
+
func read() {}
|
|
127
|
+
}
|
|
128
|
+
"""
|
|
129
|
+
let file = dependency.appendingPathComponent("Ignored.swift")
|
|
130
|
+
try source.write(to: file, atomically: true, encoding: .utf8)
|
|
131
|
+
try FileManager.default.createSymbolicLink(
|
|
132
|
+
at: directory.appendingPathComponent("Linked.swift"),
|
|
133
|
+
withDestinationURL: file
|
|
134
|
+
)
|
|
135
|
+
try FileManager.default.createSymbolicLink(
|
|
136
|
+
at: directory.appendingPathComponent("LinkedSources"),
|
|
137
|
+
withDestinationURL: dependency
|
|
138
|
+
)
|
|
139
|
+
let report = SwiftLintCommand.run(paths: [directory.path])
|
|
140
|
+
#expect(report.status == 0)
|
|
141
|
+
#expect(report.diagnostics.isEmpty)
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import Testing
|
|
2
|
+
|
|
3
|
+
@testable import SwiftLintChecks
|
|
4
|
+
|
|
5
|
+
struct ViewBodyDeclarationTests {
|
|
6
|
+
@Test
|
|
7
|
+
func `Rejects local constants in body`() {
|
|
8
|
+
let source = """
|
|
9
|
+
struct Example: View {
|
|
10
|
+
var body: some View {
|
|
11
|
+
let title = "Page"
|
|
12
|
+
Text(title)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
"""
|
|
16
|
+
#expect(
|
|
17
|
+
SwiftLint.diagnostics(source: source, path: "Example.swift") == [
|
|
18
|
+
"Example.swift:3:9: error: Move local declarations out of SwiftUI body into computed properties or helper functions. [view_body_declarations]"
|
|
19
|
+
]
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@Test(arguments: [
|
|
24
|
+
"""
|
|
25
|
+
struct Example: View {
|
|
26
|
+
var body: some View {
|
|
27
|
+
GeometryReader { geometry in
|
|
28
|
+
let width = geometry.size.width
|
|
29
|
+
Text("Page").frame(width: width)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
""",
|
|
34
|
+
"""
|
|
35
|
+
struct Example: View {
|
|
36
|
+
var body: some View {
|
|
37
|
+
var title = "Page"
|
|
38
|
+
Text(title)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
""",
|
|
42
|
+
"""
|
|
43
|
+
struct Example: View {
|
|
44
|
+
var body: some SwiftUI.View {
|
|
45
|
+
get {
|
|
46
|
+
let title = "Page"
|
|
47
|
+
Text(title)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
""",
|
|
52
|
+
"""
|
|
53
|
+
struct Example: View {
|
|
54
|
+
var body: some View {
|
|
55
|
+
#if os(macOS)
|
|
56
|
+
let title = "Page"
|
|
57
|
+
Text(title)
|
|
58
|
+
#else
|
|
59
|
+
Text("Page")
|
|
60
|
+
#endif
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
""",
|
|
64
|
+
])
|
|
65
|
+
func `Rejects local declarations in nested builders and explicit getters`(source: String) {
|
|
66
|
+
#expect(SwiftLint.diagnostics(source: source, path: "Example.swift").count == 1)
|
|
67
|
+
#expect(
|
|
68
|
+
SwiftLint.diagnostics(source: source, path: "Example.swift").first?.contains("[view_body_declarations]")
|
|
69
|
+
== true
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
@Test(arguments: [
|
|
74
|
+
"""
|
|
75
|
+
struct Example: View {
|
|
76
|
+
let title = "Page"
|
|
77
|
+
var body: some View { Text(title) }
|
|
78
|
+
}
|
|
79
|
+
""",
|
|
80
|
+
"""
|
|
81
|
+
struct Example: View {
|
|
82
|
+
var title: String { "Page" }
|
|
83
|
+
var body: some View { Text(title) }
|
|
84
|
+
}
|
|
85
|
+
""",
|
|
86
|
+
"""
|
|
87
|
+
struct Example: View {
|
|
88
|
+
let title: String?
|
|
89
|
+
var body: some View {
|
|
90
|
+
if let title { Text(title) }
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
""",
|
|
94
|
+
"""
|
|
95
|
+
struct Example: View {
|
|
96
|
+
let title: String?
|
|
97
|
+
var body: some View {
|
|
98
|
+
Button {
|
|
99
|
+
guard let title else { return }
|
|
100
|
+
print(title)
|
|
101
|
+
} label: {
|
|
102
|
+
Text("Page")
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
""",
|
|
107
|
+
"""
|
|
108
|
+
struct Example: View {
|
|
109
|
+
var body: some View { content() }
|
|
110
|
+
|
|
111
|
+
private func content() -> some View {
|
|
112
|
+
let title = "Page"
|
|
113
|
+
return Text(title)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
""",
|
|
117
|
+
"""
|
|
118
|
+
struct Response {
|
|
119
|
+
var body: String {
|
|
120
|
+
let title = "Page"
|
|
121
|
+
return title
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
""",
|
|
125
|
+
])
|
|
126
|
+
func `Accepts computed properties conditional bindings and helper locals`(source: String) {
|
|
127
|
+
#expect(SwiftLint.diagnostics(source: source, path: "Example.swift").isEmpty)
|
|
128
|
+
}
|
|
129
|
+
}
|