Skip to content

Commit c74c6dd

Browse files
committed
Remove and refactor Context.init that was only used in tests.
1 parent 97334f2 commit c74c6dd

File tree

4 files changed

+48
-48
lines changed

4 files changed

+48
-48
lines changed

Sources/SwiftFormatCore/Context.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,6 @@ public class Context {
6060
)
6161
}
6262

63-
/// Creates a new Context with the provided configuration, diagnostic engine, and source text.
64-
public init(
65-
configuration: Configuration,
66-
diagnosticEngine: DiagnosticEngine?,
67-
fileURL: URL,
68-
sourceText: String
69-
) {
70-
self.configuration = configuration
71-
self.diagnosticEngine = diagnosticEngine
72-
self.fileURL = fileURL
73-
self.importsXCTest = false
74-
self.didSetImportsXCTest = false
75-
self.sourceLocationConverter = SourceLocationConverter(file: fileURL.path, source: sourceText)
76-
let syntax = try! SyntaxParser.parse(source: sourceText)
77-
self.ruleMask = RuleMask(syntaxNode: syntax, sourceLocationConverter: sourceLocationConverter)
78-
}
79-
8063
/// Given a rule's name and the node it is examining, determine if the rule is disabled at this
8164
/// location or not.
8265
public func isRuleDisabled(_ ruleName: String, node: Syntax) -> Bool {

Tests/SwiftFormatPrettyPrintTests/PrettyPrintTestCase.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,40 @@ public class PrettyPrintTestCase: XCTestCase {
1717
) {
1818
configuration.lineLength = linelength
1919

20-
let context = Context(
21-
configuration: configuration,
22-
diagnosticEngine: nil,
23-
fileURL: URL(fileURLWithPath: "/tmp/file.swift"),
24-
sourceText: input)
25-
2620
// Assert that the input, when formatted, is what we expected.
27-
if let formatted = prettyPrintedSource(input, context: context) {
21+
if let formatted = prettyPrintedSource(input, configuration: configuration) {
2822
XCTAssertEqual(
2923
expected, formatted,
3024
"Pretty-printed result was not what was expected",
3125
file: file, line: line)
3226

3327
// Idempotency check: Running the formatter multiple times should not change the outcome.
3428
// Assert that running the formatter again on the previous result keeps it the same.
35-
if let reformatted = prettyPrintedSource(formatted, context: context) {
29+
if let reformatted = prettyPrintedSource(formatted, configuration: configuration) {
3630
XCTAssertEqual(
3731
formatted, reformatted, "Pretty printer is not idempotent", file: file, line: line)
3832
}
3933
}
4034
}
4135

4236
/// Returns the given source code reformatted with the pretty printer.
43-
private func prettyPrintedSource(_ original: String, context: Context) -> String? {
37+
private func prettyPrintedSource(_ source: String, configuration: Configuration) -> String?
38+
{
39+
let sourceFileSyntax: SourceFileSyntax
4440
do {
45-
let syntax = try SyntaxParser.parse(source: original)
46-
let printer = PrettyPrinter(context: context, node: syntax, printTokenStream: false)
47-
return printer.prettyPrint()
41+
sourceFileSyntax = try SyntaxParser.parse(source: source)
4842
} catch {
4943
XCTFail("Parsing failed with error: \(error)")
5044
return nil
5145
}
46+
47+
let context = Context(
48+
configuration: configuration,
49+
diagnosticEngine: nil,
50+
fileURL: URL(fileURLWithPath: "/tmp/file.swift"),
51+
sourceFileSyntax: sourceFileSyntax)
52+
53+
let printer = PrettyPrinter(context: context, node: sourceFileSyntax, printTokenStream: false)
54+
return printer.prettyPrint()
5255
}
5356
}

Tests/SwiftFormatRulesTests/DiagnosingTestCase.swift

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ public class DiagnosingTestCase: XCTestCase {
3737
}
3838
}
3939

40-
/// Creates and returns a new `Context` from the given source text.
41-
private func makeContext(sourceText: String) -> Context {
40+
/// Creates and returns a new `Context` from the given syntax tree.
41+
private func makeContext(sourceFileSyntax: SourceFileSyntax) -> Context {
4242
let context = Context(
4343
configuration: Configuration(),
4444
diagnosticEngine: DiagnosticEngine(),
4545
fileURL: URL(fileURLWithPath: "/tmp/test.swift"),
46-
sourceText: sourceText)
46+
sourceFileSyntax: sourceFileSyntax)
4747
consumer = DiagnosticTrackingConsumer()
4848
context.diagnosticEngine?.addConsumer(consumer)
4949
return context
@@ -62,19 +62,22 @@ public class DiagnosingTestCase: XCTestCase {
6262
file: StaticString = #file,
6363
line: UInt = #line
6464
) {
65-
context = makeContext(sourceText: input)
65+
let sourceFileSyntax: SourceFileSyntax
66+
do {
67+
sourceFileSyntax = try SyntaxParser.parse(source: input)
68+
} catch {
69+
XCTFail("\(error)", file: file, line: line)
70+
return
71+
}
72+
73+
self.context = makeContext(sourceFileSyntax: sourceFileSyntax)
6674

6775
// If we're linting, then indicate that we want to fail for unasserted diagnostics when the test
6876
// is torn down.
6977
shouldCheckForUnassertedDiagnostics = true
7078

71-
do {
72-
let syntax = try SyntaxParser.parse(source: input)
73-
var linter = type.init(context: context!)
74-
syntax.walk(&linter)
75-
} catch {
76-
XCTFail("\(error)", file: file, line: line)
77-
}
79+
var linter = type.init(context: context!)
80+
sourceFileSyntax.walk(&linter)
7881
}
7982

8083
/// Asserts that the result of applying a formatter to the provided input code yields the output.
@@ -97,17 +100,20 @@ public class DiagnosingTestCase: XCTestCase {
97100
line: UInt = #line,
98101
checkForUnassertedDiagnostics: Bool = false
99102
) {
100-
context = makeContext(sourceText: input)
101-
103+
let sourceFileSyntax: SourceFileSyntax
102104
do {
103-
shouldCheckForUnassertedDiagnostics = checkForUnassertedDiagnostics
104-
let syntax = try SyntaxParser.parse(source: input)
105-
let formatter = formatType.init(context: context!)
106-
let result = formatter.visit(syntax)
107-
XCTAssertDiff(result: result.description, expected: expected, file: file, line: line)
105+
sourceFileSyntax = try SyntaxParser.parse(source: input)
108106
} catch {
109107
XCTFail("\(error)", file: file, line: line)
108+
return
110109
}
110+
111+
context = makeContext(sourceFileSyntax: sourceFileSyntax)
112+
113+
shouldCheckForUnassertedDiagnostics = checkForUnassertedDiagnostics
114+
let formatter = formatType.init(context: context!)
115+
let result = formatter.visit(sourceFileSyntax)
116+
XCTAssertDiff(result: result.description, expected: expected, file: file, line: line)
111117
}
112118

113119
/// Asserts that the two expressions have the same value, and provides a detailed

Tests/SwiftFormatWhitespaceLinterTests/WhitespaceTestCase.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,19 @@ public class WhitespaceTestCase: XCTestCase {
5050
expected: String,
5151
linelength: Int? = nil
5252
) {
53+
let sourceFileSyntax: SourceFileSyntax
54+
do {
55+
sourceFileSyntax = try SyntaxParser.parse(source: input)
56+
} catch {
57+
XCTFail("Parsing failed with error: \(error)")
58+
return
59+
}
60+
5361
context = Context(
5462
configuration: Configuration(),
5563
diagnosticEngine: DiagnosticEngine(),
5664
fileURL: URL(fileURLWithPath: "/tmp/test.swift"),
57-
sourceText: input
65+
sourceFileSyntax: sourceFileSyntax
5866
)
5967
if let linelength = linelength {
6068
context?.configuration.lineLength = linelength

0 commit comments

Comments
 (0)