Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added annotations to typealiases and typealiases property to EJS template context. #1379

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public final class FileParserSyntax: SyntaxVisitor, FileParserType {
$0.imports = collector.imports
$0.path = path
}

collector.typealiases.forEach {
$0.imports = collector.imports
}

return FileParserResult(
path: path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ class SyntaxTreeCollector: SyntaxVisitor {
accessLevel: baseModifiers.readAccess,
parent: visitingType,
module: module,
annotations: annotations,
documentation: documentation
)

Expand Down
10 changes: 10 additions & 0 deletions SourceryRuntime/Sources/Common/AST/Typealias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public final class Typealias: NSObject, Typed, SourceryModel, Diffable {

/// module in which this typealias was declared
public var module: String?

/// Imports that existed in the file that contained this typealias declaration
public var imports: [Import] = []

/// typealias annotations
public var annotations: Annotations = [:]
Expand Down Expand Up @@ -130,6 +133,12 @@ public final class Typealias: NSObject, Typed, SourceryModel, Diffable {
}; self.typeName = typeName
self.type = aDecoder.decode(forKey: "type")
self.module = aDecoder.decode(forKey: "module")
guard let imports: [Import] = aDecoder.decode(forKey: "imports") else {
withVaList(["imports"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
fatalError()
}; self.imports = imports
guard let annotations: Annotations = aDecoder.decode(forKey: "annotations") else {
withVaList(["annotations"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
Expand Down Expand Up @@ -158,6 +167,7 @@ public final class Typealias: NSObject, Typed, SourceryModel, Diffable {
aCoder.encode(self.typeName, forKey: "typeName")
aCoder.encode(self.type, forKey: "type")
aCoder.encode(self.module, forKey: "module")
aCoder.encode(self.imports, forKey: "imports")
aCoder.encode(self.annotations, forKey: "annotations")
aCoder.encode(self.documentation, forKey: "documentation")
aCoder.encode(self.parent, forKey: "parent")
Expand Down
3 changes: 2 additions & 1 deletion SourceryRuntime/Sources/Common/TemplateContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public final class TemplateContext: NSObject, SourceryModel, NSCoding, Diffable
"based": types.based,
"inheriting": types.inheriting,
"implementing": types.implementing,
"protocolCompositions": types.protocolCompositions
"protocolCompositions": types.protocolCompositions,
"typealiases": types.typealiases
] as [String : Any],
"functions": functions,
"type": types.typesByName,
Expand Down
2 changes: 2 additions & 0 deletions SourceryRuntime/Sources/Generated/JSExport.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ extension Type: TypeAutoJSExport {}
var set: SetType? { get }
var asSource: String { get }
var description: String { get }
var hash: Int { get }
var debugDescription: String { get }
}

Expand All @@ -683,6 +684,7 @@ extension TypeName: TypeNameAutoJSExport {}
var typeName: TypeName { get }
var type: Type? { get }
var module: String? { get }
var imports: [Import] { get }
var annotations: Annotations { get }
var documentation: Documentation { get }
var parent: Type? { get }
Expand Down
13 changes: 12 additions & 1 deletion SourceryTests/Generating/JavaScriptTemplateSpecs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ class JavaScriptTemplateTests: QuickSpec {

it("provides protocol compositions") {
let templatePath = Stubs.jsTemplates + Path("ProtocolCompositions.ejs")
let expectedResult = try? (Stubs.resultDirectory + Path("FooBar.swift")).read(.utf8)
let expectedResult = try? (Stubs.resultDirectory + Path("ProtocolCompositions.swift")).read(.utf8)

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

let result = (try? (outputDir + Sourcery().generatedPath(for: templatePath)).read(.utf8))
print("expected:\n\(expectedResult)\n\ngot:\n\(result)")
expect(result).to(equal(expectedResult))
}

it("provides protocol all typealiases") {
let templatePath = Stubs.jsTemplates + Path("AllTypealiases.ejs")
let expectedResult = try? (Stubs.resultDirectory + Path("AllTypealiases.swift")).read(.utf8)

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

Expand Down
8 changes: 8 additions & 0 deletions SourceryTests/Stub/JavaScriptTemplates/AllTypealiases.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%_
for (alias of types.typealiases.filter((t) => t.annotations.AutoStruct != null)) { %>
struct Any<%- alias.name %>: <%- alias.type.name %> {
<%_ for (variable of alias.type.instanceVariables) { -%>
var <%- variable.name %>: <%- variable.typeName.asSource %>
<% } -%>
}
<%_ } -%>
10 changes: 10 additions & 0 deletions SourceryTests/Stub/Result/AllTypealiases.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Generated using Sourcery Major.Minor.Patch — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT

struct AnyBarAlias: HasBar {
var bar: BarBaz
}

struct AnyFooAlias: HasFoo {
var foo: FooBarBaz
}
6 changes: 6 additions & 0 deletions SourceryTests/Stub/Source/FooBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ protocol HasBar {

// sourcery: AutoStruct
typealias FooBar = HasFoo & HasBar

// sourcery: AutoStruct
typealias FooAlias = HasFoo

// sourcery: AutoStruct
typealias BarAlias = HasBar
Loading