Skip to content

Commit 38b3aec

Browse files
committed
Adds new formatter
1 parent ccf94bc commit 38b3aec

File tree

61 files changed

+600
-60
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+600
-60
lines changed

MockGenerator.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
/* End PBXAggregateTarget section */
2222

2323
/* Begin PBXBuildFile section */
24+
141BECDC243B8AEB00AE0E2B /* Formatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 141BECDB243B8AEB00AE0E2B /* Formatter.swift */; };
2425
14276C6A1F4F312E00EA7753 /* MockGenerator.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 14276C611F4F312E00EA7753 /* MockGenerator.framework */; };
2526
14276C6F1F4F312E00EA7753 /* MockGeneratorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14276C6E1F4F312E00EA7753 /* MockGeneratorTests.swift */; };
2627
14276C791F4F314600EA7753 /* Generator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14276C781F4F314600EA7753 /* Generator.swift */; };
@@ -29,6 +30,7 @@
2930
14451D351F5F05C500833C61 /* MemberTransformingVisitorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35CDF7A9B1705ADFFC0CB415 /* MemberTransformingVisitorTests.swift */; };
3031
145290EA1F981ED600947144 /* VersionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 145290E91F981ED600947144 /* VersionView.swift */; };
3132
147437B61F79746A00241B8A /* TestProject in Copy Test Project */ = {isa = PBXBuildFile; fileRef = 147437B51F79746000241B8A /* TestProject */; };
33+
14812327243B8554001CA005 /* PositionParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14812326243B8554001CA005 /* PositionParser.swift */; };
3234
14887ABC20FB859B00D50A73 /* MockGenerator.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 14276C611F4F312E00EA7753 /* MockGenerator.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
3335
148D5E08242E801000ED04DC /* TestHelper.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 149A5AEB242BCCD4003DDAE2 /* TestHelper.framework */; };
3436
148D5E0A242E801100ED04DC /* SwiftyPluginTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 148D5E09242E801100ED04DC /* SwiftyPluginTest.framework */; };
@@ -198,6 +200,7 @@
198200
/* End PBXCopyFilesBuildPhase section */
199201

200202
/* Begin PBXFileReference section */
203+
141BECDB243B8AEB00AE0E2B /* Formatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Formatter.swift; sourceTree = "<group>"; };
201204
14276C611F4F312E00EA7753 /* MockGenerator.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MockGenerator.framework; sourceTree = BUILT_PRODUCTS_DIR; };
202205
14276C641F4F312E00EA7753 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
203206
14276C691F4F312E00EA7753 /* MockGeneratorTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MockGeneratorTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -208,6 +211,7 @@
208211
142B023921709BC5005DE573 /* libGRMustache7-MacOS.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = "libGRMustache7-MacOS.a"; sourceTree = BUILT_PRODUCTS_DIR; };
209212
145290E91F981ED600947144 /* VersionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionView.swift; sourceTree = "<group>"; };
210213
147437B51F79746000241B8A /* TestProject */ = {isa = PBXFileReference; lastKnownFileType = folder; path = TestProject; sourceTree = SOURCE_ROOT; };
214+
14812326243B8554001CA005 /* PositionParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PositionParser.swift; sourceTree = "<group>"; };
211215
14872E7B1F59DBC50017AF3F /* MockGeneratorAppTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MockGeneratorAppTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
212216
14872E7F1F59DBC50017AF3F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
213217
148D5E09242E801100ED04DC /* SwiftyPluginTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftyPluginTest.framework; path = lib/Debug/SwiftyPluginTest.framework; sourceTree = "<group>"; };
@@ -330,6 +334,8 @@
330334
35CDF512D0EA7664390189DC /* stub.mustache */,
331335
14276C641F4F312E00EA7753 /* Info.plist */,
332336
14276C781F4F314600EA7753 /* Generator.swift */,
337+
14812326243B8554001CA005 /* PositionParser.swift */,
338+
141BECDB243B8AEB00AE0E2B /* Formatter.swift */,
333339
35CDFDB834B634E694CF658C /* MemberTransformingVisitor.swift */,
334340
35CDF33B8A857EED9FEB1F7A /* MustacheView.swift */,
335341
35CDF4695C73C787CC3952DB /* SourceFileFinder.swift */,
@@ -778,7 +784,9 @@
778784
buildActionMask = 2147483647;
779785
files = (
780786
14276C791F4F314600EA7753 /* Generator.swift in Sources */,
787+
14812327243B8554001CA005 /* PositionParser.swift in Sources */,
781788
35CDF3051C92D783E99E519D /* SourceFileFinder.swift in Sources */,
789+
141BECDC243B8AEB00AE0E2B /* Formatter.swift in Sources */,
782790
14E132281F58E6D300D75943 /* MemberTransformingVisitor.swift in Sources */,
783791
35CDF6B3B8F080E7AE114743 /* TypeResolverVisitor.swift in Sources */,
784792
35CDF1D4B3EE5C1ED545361F /* MustacheView.swift in Sources */,

MockGenerator/Formatter.swift

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Formatter
2+
import AST
3+
import SwiftyKit
4+
5+
public class DefaultFormatter: Formatter {
6+
7+
let useTabs: Bool
8+
let indentationWidth: Int
9+
10+
public init(useTabs: Bool, indentationWidth: Int) {
11+
self.useTabs = useTabs
12+
self.indentationWidth = indentationWidth
13+
}
14+
15+
public func format(_ element: Element) {
16+
let makeIndent: (Int) -> String = { [useTabs, indentationWidth] in
17+
if useTabs {
18+
return String(repeating: "\t", count: $0)
19+
} else {
20+
return spaceIndent(width: indentationWidth)($0)
21+
}
22+
}
23+
let indentOptions = IndentStrategyVisitor.Options(
24+
default: RegularIndentStrategy(makeIndent: makeIndent)
25+
)
26+
let newLineBetweenOptions = NewLineSpacingVisitor.Options(
27+
functionDeclaration: (1, 1),
28+
simpleVariableDeclaration: (0, 0),
29+
codeBlockVariableDeclaration: (1, 1),
30+
codeBlockSingleLineDeclaration: (0, 0),
31+
getSetKeywordBlockVariableDeclaration: (0, 0),
32+
typeDeclaration: (1, 1),
33+
operatorDeclaration: (1, 1),
34+
default: (0, 0)
35+
)
36+
let indentVisitor = FormatVisitor(indentStrategy: {
37+
IndentStrategyVisitor.visit($0, context: indentOptions)
38+
})
39+
let newLineInsideOptions = NewLineSpacingVisitor.Options(
40+
typeDeclaration: (1, 0)
41+
)
42+
let visitors: [ElementVisitor] = [
43+
NewLineBetweenDeclaration(spacing: { NewLineSpacingVisitor.visit($0, context: newLineBetweenOptions) }),
44+
NewLineInsideDeclaration(spacing: { NewLineSpacingVisitor.visit($0, context: newLineInsideOptions) }),
45+
NewLineAroundImports(spacing: (1, 1)),
46+
EmptyBraces(),
47+
48+
indentVisitor,
49+
]
50+
visitors.forEach {
51+
element.accept(CompoundRecursiveVisitor($0))
52+
}
53+
}
54+
}

MockGenerator/Generator.swift

+4-14
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,15 @@ public class Generator: ASTCommandImpl {
1313
public override var identifier: String { "codes.seanhenry.mockgenerator" }
1414

1515
private let templateName: String
16-
private let useTabsForIndentation: Bool
17-
private let indentationWidth: Int
1816
private let resolver: Resolver
1917
private let trackLines: (Int) -> Void
2018

2119
public init(
2220
projectURL: URL,
2321
templateName: String,
24-
useTabsForIndentation: Bool,
25-
indentationWidth: Int,
2622
trackLines: @escaping (Int) -> Void
2723
) {
2824
self.templateName = templateName
29-
self.useTabsForIndentation = useTabsForIndentation
30-
self.indentationWidth = indentationWidth
3125
let sourceFiles = SourceFileFinder(projectRoot: projectURL).findSourceFiles()
3226
self.resolver = ResolverFactory.createResolver(filePaths: Generator.filterUniqueFileNames(sourceFiles))
3327
self.trackLines = trackLines
@@ -66,7 +60,7 @@ public class Generator: ASTCommandImpl {
6660
}
6761
try buildMock(toFile: file, atElement: typeElement)
6862
}
69-
63+
7064
private func buildMock(toFile file: Element, atElement element: TypeDeclaration) throws {
7165
let mockClass = transformToMockClass(element: element)
7266
guard !isEmpty(mockClass: mockClass) else {
@@ -76,14 +70,15 @@ public class Generator: ASTCommandImpl {
7670
guard !mockLines.isEmpty else {
7771
throw error("Found inherited types but there was nothing to mock")
7872
}
79-
let formatted = format(mockLines, relativeTo: element).joined(separator: "\n")
80-
let mock = "class Mock {\n\(formatted)\n}"
73+
let mockString = mockLines.joined(separator: "\n")
74+
let mock = "class Mock {\n\(mockString)\n}"
8175
guard let codeBlock = try? parserFactory.make().parseFile(mock, url: nil).typeDeclarations.first?.codeBlock else {
8276
throw error("The resulting mock could not be parsed")
8377
}
8478
trackLines(mockLines.count)
8579
codeBlock.delete()
8680
element.codeBlock.swap(with: codeBlock)
81+
formatterFactory.make().format(element)
8782
}
8883

8984
private func isEmpty(mockClass: UseCasesMockClass) -> Bool {
@@ -106,9 +101,4 @@ public class Generator: ASTCommandImpl {
106101
private func transformToMockClass(element: Element) -> UseCasesMockClass {
107102
return TypeDeclarationTransformingVisitor.transformMock(element, resolver: resolver)
108103
}
109-
110-
private func format(_ lines: [String], relativeTo element: Element) -> [String] {
111-
return FormatUtil(useTabs: useTabsForIndentation, spaces: indentationWidth)
112-
.format(lines, relativeTo: element)
113-
}
114104
}

MockGenerator/MemberTransformingVisitor.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class MemberTransformingVisitor: RecursiveElementVisitor {
7979

8080
private func transformTupleType(_ e: TupleTypeElement) -> UseCasesTupleTypeTupleElement? {
8181
if let type = e.type ?? e.typeAnnotation?.type {
82-
return UseCasesTupleTypeTupleElement(label: e.label, type: transformType(type))
82+
return UseCasesTupleTypeTupleElement(label: e.elementName?.text, type: transformType(type))
8383
}
8484
return nil
8585
}
@@ -128,9 +128,15 @@ class MemberTransformingVisitor: RecursiveElementVisitor {
128128

129129
private func transformParameters(_ parameters: [Parameter]) -> [UseCasesParameter] {
130130
return parameters.map { parameter in
131-
UseCasesParameter(
131+
var internalName = parameter.localParameterName
132+
if internalName == "`let`" {
133+
internalName = "let"
134+
} else if internalName == "`var`" {
135+
internalName = "var"
136+
}
137+
return UseCasesParameter(
132138
externalName: parameter.externalParameterName,
133-
internalName: parameter.localParameterName,
139+
internalName: internalName,
134140
type: UseCasesResolvedType(originalType: transformType(parameter.typeAnnotation.type), resolvedType: resolveAndTransform(parameter.typeAnnotation.type)),
135141
text: parameter.text,
136142
isEscaping: isEscaping(parameter))

MockGenerator/PositionParser.swift

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Parser
2+
import AST
3+
import SwiftyKit
4+
import Algorithms
5+
import Foundation
6+
7+
public class PositionParser: Parser {
8+
9+
public init() {}
10+
public func parseFile(_ string: String, url: URL?) throws -> AST.File {
11+
let file = try ParserImpl().parseFile(string, url: url)
12+
PositionVisitor.calculate(file)
13+
return file
14+
}
15+
16+
public func parseFile(at path: String) throws -> AST.File {
17+
let file = try ParserImpl().parseFile(at: path)
18+
PositionVisitor.calculate(file)
19+
return file
20+
}
21+
22+
public func parseType(_ string: String) throws -> Type {
23+
return try ParserImpl().parseType(string)
24+
}
25+
26+
public func parseWhitespace(_ string: String) throws -> Whitespace {
27+
return try ParserImpl().parseWhitespace(string)
28+
}
29+
30+
public func parseHorizontalWhitespaceItem(_ string: String) throws -> HorizontalWhitespaceItem {
31+
return try ParserImpl().parseHorizontalWhitespaceItem(string)
32+
}
33+
34+
public func parseLineBreaks(_ string: String) throws -> [LineBreak] {
35+
return try ParserImpl().parseLineBreaks(string)
36+
}
37+
}

MockGenerator/SourceFileFinder.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import Foundation
22

33
class SourceFileFinder {
4-
4+
55
private let projectRoot: URL
6-
6+
77
init(projectRoot: URL) {
88
self.projectRoot = projectRoot
99
}
10-
10+
1111
func findSourceFiles() -> [URL] {
1212
let enumerator = FileManager.default.enumerator(at: projectRoot, includingPropertiesForKeys: [.nameKey], options: .skipsHiddenFiles, errorHandler: nil)
1313
var files = [URL]()

MockGeneratorApp/ManualProjectPathView.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import AppKit
22

33
class ManualProjectPathView: NSStackView {
4-
4+
55
@IBOutlet var projectPathHistory: NSPopUpButton!
66
private static let clearTitle = "Clear history"
77
private static let browseTitle = "Browse..."
88
private let preferences = Preferences()
9-
9+
1010
override func viewDidMoveToWindow() {
1111
super.viewDidMoveToWindow()
1212
refreshHistory()
1313
projectPathHistory.target = self
1414
projectPathHistory.action = #selector(didChangeSelection(_:))
1515
}
16-
16+
1717
@IBAction func didTapProjectPathButton(_ sender: Any?) {
1818
let panel = NSOpenPanel()
1919
panel.allowsMultipleSelection = false

TestProject/ArgumentInitializerClassMock_expected.swift

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@testable import TestProject
22

33
class ArgumentInitializerClassMock: ArgumentInitializerClass {
4+
45
convenience init() {
56
self.init(a: 0, b: "", nil)
67
}

TestProject/ArgumentsSubscriptProtocolMock_expected.swift

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@testable import TestProject
22

33
class ArgumentsSubscriptProtocolMock: ArgumentsSubscriptProtocol {
4+
45
var invokedSubscriptGetter = false
56
var invokedSubscriptGetterCount = 0
67
var invokedSubscriptGetterParameters: (a: Int, Void)?
@@ -12,6 +13,7 @@ class ArgumentsSubscriptProtocolMock: ArgumentsSubscriptProtocol {
1213
var invokedSubscriptSetterParametersList = [(a: Int, Void)]()
1314
var invokedSubscript: Int?
1415
var invokedSubscriptList = [Int]()
16+
1517
subscript(a: Int) -> Int {
1618
set {
1719
invokedSubscriptSetter = true

TestProject/AugmentedClassSubscriptMock_expected.swift

+4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
@testable import TestProject
22

33
class AugmentedClassSubscriptMock: AugmentedClassSubscript {
4+
45
var invokedSubscriptGetter = false
56
var invokedSubscriptGetterCount = 0
67
var stubbedSubscriptResult: Int! = 0
78
var invokedSubscriptSetter = false
89
var invokedSubscriptSetterCount = 0
910
var invokedSubscript: Int?
1011
var invokedSubscriptList = [Int]()
12+
1113
override subscript() -> Int {
1214
set {
1315
invokedSubscriptSetter = true
@@ -21,6 +23,7 @@ class AugmentedClassSubscriptMock: AugmentedClassSubscript {
2123
return stubbedSubscriptResult
2224
}
2325
}
26+
2427
var invokedSubscriptBGetter = false
2528
var invokedSubscriptBGetterCount = 0
2629
var invokedSubscriptBGetterParameters: (b: Int, Void)?
@@ -32,6 +35,7 @@ class AugmentedClassSubscriptMock: AugmentedClassSubscript {
3235
var invokedSubscriptBSetterParametersList = [(b: Int, Void)]()
3336
var invokedSubscriptB: Int?
3437
var invokedSubscriptBList = [Int]()
38+
3539
override subscript(b: Int) -> Int {
3640
set {
3741
invokedSubscriptBSetter = true

TestProject/AvailableClassMock_expected.swift

+4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
@testable import TestProject
22

33
class AvailableClassMock: AvailableClass {
4+
45
var invokedPropertyGetter = false
56
var invokedPropertyGetterCount = 0
67
var stubbedProperty: String!
8+
79
override var property: String? {
810
invokedPropertyGetter = true
911
invokedPropertyGetterCount += 1
1012
return stubbedProperty
1113
}
14+
1215
var invokedMethod = false
1316
var invokedMethodCount = 0
17+
1418
override func method() {
1519
invokedMethod = true
1620
invokedMethodCount += 1

TestProject/CaretSuccessTest-1.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
@testable import TestProject
22

33
class AnotherDeclarationInTheFileShouldNotBeAffected {
4-
4+
55
func shouldNotInterfere() {
6-
6+
77
}
88
}
99

TestProject/CaretSuccessTest-2.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
@testable import TestProject
22

33
class AnotherDeclarationInTheFileShouldNotBeAffected {
4-
4+
55
func shouldNotInterfere() {
6-
6+
77
}
88
}
99

TestProject/CaretSuccessTest-3.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
@testable import TestProject
22

33
class AnotherDeclarationInTheFileShouldNotBeAffected {
4-
4+
55
func shouldNotInterfere() {
6-
6+
77
}
88
}
99

TestProject/CaretSuccessTest-4.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
@testable import TestProject
22

33
class AnotherDeclarationInTheFileShouldNotBeAffected {
4-
4+
55
func shouldNotInterfere() {
6-
6+
77
}
88
}
99

0 commit comments

Comments
 (0)