Skip to content

Commit fd985a3

Browse files
authored
Added annotations to typealiases and typealiases property to JS template context (#1379)
1 parent ae93315 commit fd985a3

File tree

10 files changed

+55
-2
lines changed

10 files changed

+55
-2
lines changed

SourceryFramework/Sources/Parsing/SwiftSyntax/FileParserSyntax.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public final class FileParserSyntax: SyntaxVisitor, FileParserType {
5555
$0.imports = collector.imports
5656
$0.path = path
5757
}
58+
59+
collector.typealiases.forEach {
60+
$0.imports = collector.imports
61+
}
5862

5963
return FileParserResult(
6064
path: path,

SourceryFramework/Sources/Parsing/SwiftSyntax/SyntaxTreeCollector.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class SyntaxTreeCollector: SyntaxVisitor {
225225
accessLevel: baseModifiers.readAccess,
226226
parent: visitingType,
227227
module: module,
228+
annotations: annotations,
228229
documentation: documentation
229230
)
230231

SourceryRuntime/Sources/Common/AST/Typealias.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public final class Typealias: NSObject, Typed, SourceryModel, Diffable {
1616

1717
/// module in which this typealias was declared
1818
public var module: String?
19+
20+
/// Imports that existed in the file that contained this typealias declaration
21+
public var imports: [Import] = []
1922

2023
/// typealias annotations
2124
public var annotations: Annotations = [:]
@@ -130,6 +133,12 @@ public final class Typealias: NSObject, Typed, SourceryModel, Diffable {
130133
}; self.typeName = typeName
131134
self.type = aDecoder.decode(forKey: "type")
132135
self.module = aDecoder.decode(forKey: "module")
136+
guard let imports: [Import] = aDecoder.decode(forKey: "imports") else {
137+
withVaList(["imports"]) { arguments in
138+
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
139+
}
140+
fatalError()
141+
}; self.imports = imports
133142
guard let annotations: Annotations = aDecoder.decode(forKey: "annotations") else {
134143
withVaList(["annotations"]) { arguments in
135144
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
@@ -158,6 +167,7 @@ public final class Typealias: NSObject, Typed, SourceryModel, Diffable {
158167
aCoder.encode(self.typeName, forKey: "typeName")
159168
aCoder.encode(self.type, forKey: "type")
160169
aCoder.encode(self.module, forKey: "module")
170+
aCoder.encode(self.imports, forKey: "imports")
161171
aCoder.encode(self.annotations, forKey: "annotations")
162172
aCoder.encode(self.documentation, forKey: "documentation")
163173
aCoder.encode(self.parent, forKey: "parent")

SourceryRuntime/Sources/Common/TemplateContext.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ public final class TemplateContext: NSObject, SourceryModel, NSCoding, Diffable
130130
"based": types.based,
131131
"inheriting": types.inheriting,
132132
"implementing": types.implementing,
133-
"protocolCompositions": types.protocolCompositions
133+
"protocolCompositions": types.protocolCompositions,
134+
"typealiases": types.typealiases
134135
] as [String : Any],
135136
"functions": functions,
136137
"type": types.typesByName,

SourceryRuntime/Sources/Generated/JSExport.generated.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ extension Type: TypeAutoJSExport {}
673673
var set: SetType? { get }
674674
var asSource: String { get }
675675
var description: String { get }
676+
var hash: Int { get }
676677
var debugDescription: String { get }
677678
}
678679

@@ -683,6 +684,7 @@ extension TypeName: TypeNameAutoJSExport {}
683684
var typeName: TypeName { get }
684685
var type: Type? { get }
685686
var module: String? { get }
687+
var imports: [Import] { get }
686688
var annotations: Annotations { get }
687689
var documentation: Documentation { get }
688690
var parent: Type? { get }

SourceryTests/Generating/JavaScriptTemplateSpecs.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,18 @@ class JavaScriptTemplateTests: QuickSpec {
3333

3434
it("provides protocol compositions") {
3535
let templatePath = Stubs.jsTemplates + Path("ProtocolCompositions.ejs")
36-
let expectedResult = try? (Stubs.resultDirectory + Path("FooBar.swift")).read(.utf8)
36+
let expectedResult = try? (Stubs.resultDirectory + Path("ProtocolCompositions.swift")).read(.utf8)
37+
38+
expect { try Sourcery(cacheDisabled: true).processFiles(.sources(Paths(include: [Stubs.sourceDirectory])), usingTemplates: Paths(include: [templatePath]), output: output, baseIndentation: 0) }.toNot(throwError())
39+
40+
let result = (try? (outputDir + Sourcery().generatedPath(for: templatePath)).read(.utf8))
41+
print("expected:\n\(expectedResult)\n\ngot:\n\(result)")
42+
expect(result).to(equal(expectedResult))
43+
}
44+
45+
it("provides protocol all typealiases") {
46+
let templatePath = Stubs.jsTemplates + Path("AllTypealiases.ejs")
47+
let expectedResult = try? (Stubs.resultDirectory + Path("AllTypealiases.swift")).read(.utf8)
3748

3849
expect { try Sourcery(cacheDisabled: true).processFiles(.sources(Paths(include: [Stubs.sourceDirectory])), usingTemplates: Paths(include: [templatePath]), output: output, baseIndentation: 0) }.toNot(throwError())
3950

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<%_
2+
for (alias of types.typealiases.filter((t) => t.annotations.AutoStruct != null)) { %>
3+
struct Any<%- alias.name %>: <%- alias.type.name %> {
4+
<%_ for (variable of alias.type.instanceVariables) { -%>
5+
var <%- variable.name %>: <%- variable.typeName.asSource %>
6+
<% } -%>
7+
}
8+
<%_ } -%>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Generated using Sourcery Major.Minor.Patch — https://github.com/krzysztofzablocki/Sourcery
2+
// DO NOT EDIT
3+
4+
struct AnyBarAlias: HasBar {
5+
var bar: BarBaz
6+
}
7+
8+
struct AnyFooAlias: HasFoo {
9+
var foo: FooBarBaz
10+
}

SourceryTests/Stub/Source/FooBar.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ protocol HasBar {
99

1010
// sourcery: AutoStruct
1111
typealias FooBar = HasFoo & HasBar
12+
13+
// sourcery: AutoStruct
14+
typealias FooAlias = HasFoo
15+
16+
// sourcery: AutoStruct
17+
typealias BarAlias = HasBar

0 commit comments

Comments
 (0)