From 2d28166d21288d00a54d757ccdbb5be142ff95e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danny=20M=C3=B6sch?= Date: Sat, 22 Jun 2024 11:22:55 +0200 Subject: [PATCH] Enable upcoming feature "Concise Magic File" (#5637) --- BUILD | 2 ++ Package.swift | 3 ++- .../DiscouragedOptionalBooleanRuleExamples.swift | 5 ++++- .../DiscouragedOptionalCollectionExamples.swift | 5 ++++- .../Idiomatic/UnneededBreakInSwitchRule.swift | 2 +- .../Lint/ProhibitedInterfaceBuilderRule.swift | 2 +- .../Rules/Lint/StrongIBOutletRule.swift | 2 +- .../Metrics/ClosureBodyLengthRuleExamples.swift | 14 +++++++------- .../Rules/Metrics/TypeBodyLengthRule.swift | 2 +- .../Rules/Style/EmptyEnumArgumentsRule.swift | 4 ++-- .../Rules/Style/SwitchCaseOnNewlineRule.swift | 2 +- Source/SwiftLintCore/Models/Example.swift | 2 +- Tests/IntegrationTests/IntegrationTests.swift | 6 +++--- .../FileNameNoSpaceRuleTests.swift | 2 +- .../FunctionBodyLengthRuleTests.swift | 4 ++-- .../FunctionParameterCountRuleTests.swift | 2 +- .../IndentationWidthRuleTests.swift | 8 ++++---- .../LinterCacheTests.swift | 6 +++--- .../XCTestCase+BundlePath.swift | 2 +- Tests/SwiftLintTestHelpers/TestHelpers.swift | 15 +++++++-------- 20 files changed, 49 insertions(+), 41 deletions(-) diff --git a/BUILD b/BUILD index 747580856..086b7db35 100644 --- a/BUILD +++ b/BUILD @@ -28,6 +28,8 @@ config_setting( copts = [ "-enable-upcoming-feature", "ExistentialAny", + "-enable-upcoming-feature", + "ConciseMagicFile", ] strict_concurrency_copts = [ diff --git a/Package.swift b/Package.swift index f86d250cd..cb3645a4f 100644 --- a/Package.swift +++ b/Package.swift @@ -3,7 +3,8 @@ import CompilerPluginSupport import PackageDescription let swiftFeatures: [SwiftSetting] = [ - .enableUpcomingFeature("ExistentialAny") + .enableUpcomingFeature("ExistentialAny"), + .enableUpcomingFeature("ConciseMagicFile") ] let swiftLintPluginDependencies: [Target.Dependency] diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DiscouragedOptionalBooleanRuleExamples.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DiscouragedOptionalBooleanRuleExamples.swift index 7a309d258..883717df2 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DiscouragedOptionalBooleanRuleExamples.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DiscouragedOptionalBooleanRuleExamples.swift @@ -163,6 +163,9 @@ internal struct DiscouragedOptionalBooleanRuleExamples { // MARK: - Private -private func wrapExample(_ type: String, _ test: String, file: StaticString = #file, line: UInt = #line) -> Example { +private func wrapExample(_ type: String, + _ test: String, + file: StaticString = #filePath, + line: UInt = #line) -> Example { return Example("\(type) Foo {\n\t\(test)\n}", file: file, line: line) } diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DiscouragedOptionalCollectionExamples.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DiscouragedOptionalCollectionExamples.swift index 56030d980..e2aaeee15 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DiscouragedOptionalCollectionExamples.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DiscouragedOptionalCollectionExamples.swift @@ -210,7 +210,10 @@ internal struct DiscouragedOptionalCollectionExamples { // MARK: - Private -private func wrapExample(_ type: String, _ test: String, file: StaticString = #file, line: UInt = #line) -> Example { +private func wrapExample(_ type: String, + _ test: String, + file: StaticString = #filePath, + line: UInt = #line) -> Example { return Example(""" \(type) Foo { \(test) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededBreakInSwitchRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededBreakInSwitchRule.swift index 85019f1d7..1c9bb4a65 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededBreakInSwitchRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/UnneededBreakInSwitchRule.swift @@ -3,7 +3,7 @@ import SwiftSyntax private func embedInSwitch( _ text: String, case: String = "case .bar", - file: StaticString = #file, line: UInt = #line) -> Example { + file: StaticString = #filePath, line: UInt = #line) -> Example { return Example(""" switch foo { \(`case`): diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/ProhibitedInterfaceBuilderRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/ProhibitedInterfaceBuilderRule.swift index cd0e3af3b..a03d75986 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/ProhibitedInterfaceBuilderRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/ProhibitedInterfaceBuilderRule.swift @@ -36,7 +36,7 @@ private extension ProhibitedInterfaceBuilderRule { } } -private func wrapExample(_ text: String, file: StaticString = #file, line: UInt = #line) -> Example { +private func wrapExample(_ text: String, file: StaticString = #filePath, line: UInt = #line) -> Example { return Example(""" class ViewController: UIViewController { \(text) diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/StrongIBOutletRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/StrongIBOutletRule.swift index 3a5a298ef..516aff689 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/StrongIBOutletRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/StrongIBOutletRule.swift @@ -68,7 +68,7 @@ private extension VariableDeclSyntax { } } -private func wrapExample(_ text: String, file: StaticString = #file, line: UInt = #line) -> Example { +private func wrapExample(_ text: String, file: StaticString = #filePath, line: UInt = #line) -> Example { return Example(""" class ViewController: UIViewController { \(text) diff --git a/Source/SwiftLintBuiltInRules/Rules/Metrics/ClosureBodyLengthRuleExamples.swift b/Source/SwiftLintBuiltInRules/Rules/Metrics/ClosureBodyLengthRuleExamples.swift index d5e7cda8b..899c01356 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Metrics/ClosureBodyLengthRuleExamples.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Metrics/ClosureBodyLengthRuleExamples.swift @@ -29,7 +29,7 @@ internal struct ClosureBodyLengthRuleExamples { // MARK: - Private -private func singleLineClosure(file: StaticString = #file, line: UInt = #line) -> Example { +private func singleLineClosure(file: StaticString = #filePath, line: UInt = #line) -> Example { return Example("foo.bar { $0 }", file: file, line: line) } @@ -37,7 +37,7 @@ private func trailingClosure(_ violationSymbol: String = "", codeLinesCount: Int, commentLinesCount: Int, emptyLinesCount: Int, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line) -> Example { return Example(""" foo.bar \(violationSymbol){ toto in @@ -50,7 +50,7 @@ private func trailingClosure(_ violationSymbol: String = "", private func argumentClosure(_ violationSymbol: String = "", codeLinesCount: Int, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line) -> Example { return Example(""" foo.bar(\(violationSymbol){ toto in @@ -61,7 +61,7 @@ private func argumentClosure(_ violationSymbol: String = "", private func labeledArgumentClosure(_ violationSymbol: String = "", codeLinesCount: Int, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line) -> Example { return Example(""" foo.bar(label: \(violationSymbol){ toto in @@ -72,7 +72,7 @@ private func labeledArgumentClosure(_ violationSymbol: String = "", private func multiLabeledArgumentClosures(_ violationSymbol: String = "", codeLinesCount: Int, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line) -> Example { return Example(""" foo.bar(label: \(violationSymbol){ toto in @@ -85,7 +85,7 @@ private func multiLabeledArgumentClosures(_ violationSymbol: String = "", private func labeledAndTrailingClosures(_ violationSymbol: String = "", codeLinesCount: Int, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line) -> Example { return Example(""" foo.bar(label: \(violationSymbol){ toto in @@ -98,7 +98,7 @@ private func labeledAndTrailingClosures(_ violationSymbol: String = "", private func lazyInitialization(_ violationSymbol: String = "", codeLinesCount: Int, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line) -> Example { return Example(""" let foo: Bar = \(violationSymbol){ toto in diff --git a/Source/SwiftLintBuiltInRules/Rules/Metrics/TypeBodyLengthRule.swift b/Source/SwiftLintBuiltInRules/Rules/Metrics/TypeBodyLengthRule.swift index 1852f791e..18486f8f2 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Metrics/TypeBodyLengthRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Metrics/TypeBodyLengthRule.swift @@ -4,7 +4,7 @@ private func wrapExample( _ template: String, _ count: Int, _ add: String = "", - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line) -> Example { return Example("\(prefix)\(type) Abc {\n" + repeatElement(template, count: count).joined() + "\(add)}\n", file: file, line: line) diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/EmptyEnumArgumentsRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/EmptyEnumArgumentsRule.swift index 63c22fa8f..70ab89d64 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/EmptyEnumArgumentsRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/EmptyEnumArgumentsRule.swift @@ -3,7 +3,7 @@ import SwiftSyntax private func wrapInSwitch( variable: String = "foo", _ str: String, - file: StaticString = #file, line: UInt = #line) -> Example { + file: StaticString = #filePath, line: UInt = #line) -> Example { return Example( """ switch \(variable) { @@ -12,7 +12,7 @@ private func wrapInSwitch( """, file: file, line: line) } -private func wrapInFunc(_ str: String, file: StaticString = #file, line: UInt = #line) -> Example { +private func wrapInFunc(_ str: String, file: StaticString = #filePath, line: UInt = #line) -> Example { return Example(""" func example(foo: Foo) { switch foo { diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/SwitchCaseOnNewlineRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/SwitchCaseOnNewlineRule.swift index 6581f5ca9..3d8254cc1 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/SwitchCaseOnNewlineRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/SwitchCaseOnNewlineRule.swift @@ -1,6 +1,6 @@ import SwiftSyntax -private func wrapInSwitch(_ str: String, file: StaticString = #file, line: UInt = #line) -> Example { +private func wrapInSwitch(_ str: String, file: StaticString = #filePath, line: UInt = #line) -> Example { return Example(""" switch foo { \(str) diff --git a/Source/SwiftLintCore/Models/Example.swift b/Source/SwiftLintCore/Models/Example.swift index 5a8a84eed..bd4964fef 100644 --- a/Source/SwiftLintCore/Models/Example.swift +++ b/Source/SwiftLintCore/Models/Example.swift @@ -61,7 +61,7 @@ public extension Example { /// Defaults to the line where this initializer is called. init(_ code: String, configuration: [String: any Sendable]? = nil, testMultiByteOffsets: Bool = true, testWrappingInComment: Bool = true, testWrappingInString: Bool = true, testDisableCommand: Bool = true, - testOnLinux: Bool = true, file: StaticString = #file, line: UInt = #line, + testOnLinux: Bool = true, file: StaticString = #filePath, line: UInt = #line, excludeFromDocumentation: Bool = false) { self.code = code self.configuration = configuration diff --git a/Tests/IntegrationTests/IntegrationTests.swift b/Tests/IntegrationTests/IntegrationTests.swift index 7a116f265..ff49b170c 100644 --- a/Tests/IntegrationTests/IntegrationTests.swift +++ b/Tests/IntegrationTests/IntegrationTests.swift @@ -5,7 +5,7 @@ import XCTest private let config: Configuration = { let bazelWorkspaceDirectory = ProcessInfo.processInfo.environment["BUILD_WORKSPACE_DIRECTORY"] - let rootProjectDirectory = bazelWorkspaceDirectory ?? #file.bridge() + let rootProjectDirectory = bazelWorkspaceDirectory ?? #filePath.bridge() .deletingLastPathComponent.bridge() .deletingLastPathComponent.bridge() .deletingLastPathComponent @@ -21,7 +21,7 @@ final class IntegrationTests: SwiftLintTestCase { forceExclude: false, excludeBy: .paths(excludedPaths: config.excludedPaths())) XCTAssert( - swiftFiles.contains(where: { #file.bridge().absolutePathRepresentation() == $0.path }), + swiftFiles.contains(where: { #filePath.bridge().absolutePathRepresentation() == $0.path }), "current file should be included" ) @@ -64,7 +64,7 @@ final class IntegrationTests: SwiftLintTestCase { """ } .joined(separator: "\n") - let referenceFile = URL(fileURLWithPath: #file) + let referenceFile = URL(fileURLWithPath: #filePath) .deletingLastPathComponent() .appendingPathComponent("default_rule_configurations.yml") XCTAssertEqual(defaultConfig + "\n", try String(contentsOf: referenceFile)) diff --git a/Tests/SwiftLintFrameworkTests/FileNameNoSpaceRuleTests.swift b/Tests/SwiftLintFrameworkTests/FileNameNoSpaceRuleTests.swift index d3cc87143..4aee8dcc7 100644 --- a/Tests/SwiftLintFrameworkTests/FileNameNoSpaceRuleTests.swift +++ b/Tests/SwiftLintFrameworkTests/FileNameNoSpaceRuleTests.swift @@ -2,7 +2,7 @@ import SourceKittenFramework @testable import SwiftLintBuiltInRules import XCTest -private let fixturesDirectory = #file.bridge() +private let fixturesDirectory = #filePath.bridge() .deletingLastPathComponent.bridge() .appendingPathComponent("Resources/FileNameNoSpaceRuleFixtures") diff --git a/Tests/SwiftLintFrameworkTests/FunctionBodyLengthRuleTests.swift b/Tests/SwiftLintFrameworkTests/FunctionBodyLengthRuleTests.swift index bd5b72916..73e5eea7c 100644 --- a/Tests/SwiftLintFrameworkTests/FunctionBodyLengthRuleTests.swift +++ b/Tests/SwiftLintFrameworkTests/FunctionBodyLengthRuleTests.swift @@ -3,13 +3,13 @@ import XCTest private func funcWithBody(_ body: String, violates: Bool = false, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line) -> Example { let marker = violates ? "↓" : "" return Example("func \(marker)abc() {\n\(body)}\n", file: file, line: line) } -private func violatingFuncWithBody(_ body: String, file: StaticString = #file, line: UInt = #line) -> Example { +private func violatingFuncWithBody(_ body: String, file: StaticString = #filePath, line: UInt = #line) -> Example { return funcWithBody(body, violates: true, file: file, line: line) } diff --git a/Tests/SwiftLintFrameworkTests/FunctionParameterCountRuleTests.swift b/Tests/SwiftLintFrameworkTests/FunctionParameterCountRuleTests.swift index 979d8872c..c8f3e3ebf 100644 --- a/Tests/SwiftLintFrameworkTests/FunctionParameterCountRuleTests.swift +++ b/Tests/SwiftLintFrameworkTests/FunctionParameterCountRuleTests.swift @@ -2,7 +2,7 @@ private func funcWithParameters(_ parameters: String, violates: Bool = false, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line) -> Example { let marker = violates ? "↓" : "" diff --git a/Tests/SwiftLintFrameworkTests/IndentationWidthRuleTests.swift b/Tests/SwiftLintFrameworkTests/IndentationWidthRuleTests.swift index 26f836fa4..627797f81 100644 --- a/Tests/SwiftLintFrameworkTests/IndentationWidthRuleTests.swift +++ b/Tests/SwiftLintFrameworkTests/IndentationWidthRuleTests.swift @@ -271,7 +271,7 @@ final class IndentationWidthRuleTests: SwiftLintTestCase { includeComments: Bool = true, includeCompilerDirectives: Bool = true, includeMultilineStrings: Bool = true, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line ) -> Int { var configDict: [String: Any] = [:] @@ -297,7 +297,7 @@ final class IndentationWidthRuleTests: SwiftLintTestCase { includeComments: Bool = true, includeCompilerDirectives: Bool = true, includeMultilineStrings: Bool = true, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line ) { XCTAssertEqual( @@ -322,7 +322,7 @@ final class IndentationWidthRuleTests: SwiftLintTestCase { includeComments: Bool = true, includeCompilerDirectives: Bool = true, includeMultilineStrings: Bool = true, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line ) { assertViolations( @@ -343,7 +343,7 @@ final class IndentationWidthRuleTests: SwiftLintTestCase { includeComments: Bool = true, includeCompilerDirectives: Bool = true, includeMultilineStrings: Bool = true, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line ) { assertViolations( diff --git a/Tests/SwiftLintFrameworkTests/LinterCacheTests.swift b/Tests/SwiftLintFrameworkTests/LinterCacheTests.swift index ad57ad2db..b6426186e 100644 --- a/Tests/SwiftLintFrameworkTests/LinterCacheTests.swift +++ b/Tests/SwiftLintFrameworkTests/LinterCacheTests.swift @@ -78,7 +78,7 @@ final class LinterCacheTests: SwiftLintTestCase { } private func cacheAndValidate(violations: [StyleViolation], forFile: String, configuration: Configuration, - file: StaticString = #file, line: UInt = #line) { + file: StaticString = #filePath, line: UInt = #line) { cache.cache(violations: violations, forFile: forFile, configuration: configuration) cache = cache.flushed() XCTAssertEqual(cache.violations(forFile: forFile, configuration: configuration)!, @@ -86,7 +86,7 @@ final class LinterCacheTests: SwiftLintTestCase { } private func cacheAndValidateNoViolationsTwoFiles(configuration: Configuration, - file: StaticString = #file, line: UInt = #line) { + file: StaticString = #filePath, line: UInt = #line) { let (file1, file2) = ("file1.swift", "file2.swift") // swiftlint:disable:next force_cast let fileManager = cache.fileManager as! TestFileManager @@ -97,7 +97,7 @@ final class LinterCacheTests: SwiftLintTestCase { } private func validateNewConfigDoesntHitCache(dict: [String: Any], initialConfig: Configuration, - file: StaticString = #file, line: UInt = #line) throws { + file: StaticString = #filePath, line: UInt = #line) throws { let newConfig = try Configuration(dict: dict) let (file1, file2) = ("file1.swift", "file2.swift") diff --git a/Tests/SwiftLintFrameworkTests/XCTestCase+BundlePath.swift b/Tests/SwiftLintFrameworkTests/XCTestCase+BundlePath.swift index 6aedcb8a4..f20eb5b17 100644 --- a/Tests/SwiftLintFrameworkTests/XCTestCase+BundlePath.swift +++ b/Tests/SwiftLintFrameworkTests/XCTestCase+BundlePath.swift @@ -7,7 +7,7 @@ enum TestResources { return "\(rootProjectDirectory)/Tests/SwiftLintFrameworkTests/Resources" } - return URL(fileURLWithPath: #file, isDirectory: false) + return URL(fileURLWithPath: #filePath, isDirectory: false) .deletingLastPathComponent() .appendingPathComponent("Resources") .path diff --git a/Tests/SwiftLintTestHelpers/TestHelpers.swift b/Tests/SwiftLintTestHelpers/TestHelpers.swift index 7a0c4c521..fffb3146d 100644 --- a/Tests/SwiftLintTestHelpers/TestHelpers.swift +++ b/Tests/SwiftLintTestHelpers/TestHelpers.swift @@ -325,7 +325,7 @@ public extension XCTestCase { skipDisableCommandTests: Bool = false, testMultiByteOffsets: Bool = true, testShebang: Bool = true, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line) { guard ruleDescription.minSwiftVersion <= .current else { return @@ -349,7 +349,8 @@ public extension XCTestCase { self.verifyLint(ruleDescription, config: config, commentDoesntViolate: commentDoesntViolate, stringDoesntViolate: stringDoesntViolate, skipCommentTests: skipCommentTests, skipStringTests: skipStringTests, disableCommands: disableCommands, - testMultiByteOffsets: testMultiByteOffsets, testShebang: testShebang) + testMultiByteOffsets: testMultiByteOffsets, testShebang: testShebang, + file: file, line: line) self.verifyCorrections(ruleDescription, config: config, disableCommands: disableCommands, testMultiByteOffsets: testMultiByteOffsets) } @@ -363,11 +364,11 @@ public extension XCTestCase { disableCommands: [String] = [], testMultiByteOffsets: Bool = true, testShebang: Bool = true, - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line) { func verify(triggers: [Example], nonTriggers: [Example]) { verifyExamples(triggers: triggers, nonTriggers: nonTriggers, configuration: config, - requiresFileOnDisk: ruleDescription.requiresFileOnDisk, file: file, line: line) + requiresFileOnDisk: ruleDescription.requiresFileOnDisk) } func makeViolations(_ example: Example) -> [StyleViolation] { return violations(example, config: config, requiresFileOnDisk: ruleDescription.requiresFileOnDisk) @@ -458,9 +459,7 @@ public extension XCTestCase { } private func verifyExamples(triggers: [Example], nonTriggers: [Example], - configuration config: Configuration, requiresFileOnDisk: Bool, - file callSiteFile: StaticString = #file, - line callSiteLine: UInt = #line) { + configuration config: Configuration, requiresFileOnDisk: Bool) { // Non-triggering examples don't violate for nonTrigger in nonTriggers { let unexpectedViolations = violations(nonTrigger, config: config, @@ -527,7 +526,7 @@ public extension XCTestCase { // file and line parameters are first so we can use trailing closure syntax with the closure func checkError( - file: StaticString = #file, + file: StaticString = #filePath, line: UInt = #line, _ error: T, closure: () throws -> Void) {