Enable upcoming feature "Concise Magic File" (#5637)

This commit is contained in:
Danny Mösch
2024-06-22 11:22:55 +02:00
committed by GitHub
parent 914fa02e4e
commit 2d28166d21
20 changed files with 49 additions and 41 deletions
+2
View File
@@ -28,6 +28,8 @@ config_setting(
copts = [
"-enable-upcoming-feature",
"ExistentialAny",
"-enable-upcoming-feature",
"ConciseMagicFile",
]
strict_concurrency_copts = [
+2 -1
View File
@@ -3,7 +3,8 @@ import CompilerPluginSupport
import PackageDescription
let swiftFeatures: [SwiftSetting] = [
.enableUpcomingFeature("ExistentialAny")
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("ConciseMagicFile")
]
let swiftLintPluginDependencies: [Target.Dependency]
@@ -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)
}
@@ -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)
@@ -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`):
@@ -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)
@@ -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)
@@ -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
@@ -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)
@@ -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 {
@@ -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)
+1 -1
View File
@@ -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
@@ -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))
@@ -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")
@@ -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)
}
@@ -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 ? "" : ""
@@ -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(
@@ -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")
@@ -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
+7 -8
View File
@@ -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<T: Error & Equatable>(
file: StaticString = #file,
file: StaticString = #filePath,
line: UInt = #line,
_ error: T,
closure: () throws -> Void) {