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

EJSTemplates: make typealiases accessible from templates #1208

Merged
merged 2 commits into from
Oct 30, 2023
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Sourcery CHANGELOG
## Master
## Changes
- Add support for `typealias`es in EJS templates. ([#1208](https://github.com/krzysztofzablocki/Sourcery/pull/1208))

## 2.1.2
## Changes
Expand Down
1 change: 0 additions & 1 deletion SourceryRuntime/Sources/AST/Type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class Type: NSObject, SourceryModel, Annotated, Documented, Diffable {
}

// All local typealiases
// sourcery: skipJSExport
/// :nodoc:
public var typealiases: [String: Typealias] {
didSet {
Expand Down
1 change: 0 additions & 1 deletion SourceryRuntime/Sources/AST/Type_Linux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public class Type: NSObject, SourceryModel, Annotated, Documented, Diffable, Dyn
}

// All local typealiases
// sourcery: skipJSExport
/// :nodoc:
public var typealiases: [String: Typealias] {
didSet {
Expand Down
7 changes: 7 additions & 0 deletions SourceryRuntime/Sources/Generated/JSExport.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import JavaScriptCore
var module: String? { get }
var imports: [Import] { get }
var allImports: [Import] { get }
var typealiases: [String: Typealias] { get }
var accessLevel: String { get }
var name: String { get }
var isUnknownExtension: Bool { get }
Expand Down Expand Up @@ -106,6 +107,7 @@ extension BytesRange: BytesRangeAutoJSExport {}
var module: String? { get }
var imports: [Import] { get }
var allImports: [Import] { get }
var typealiases: [String: Typealias] { get }
var accessLevel: String { get }
var name: String { get }
var isUnknownExtension: Bool { get }
Expand Down Expand Up @@ -206,6 +208,7 @@ extension DictionaryType: DictionaryTypeAutoJSExport {}
var module: String? { get }
var imports: [Import] { get }
var allImports: [Import] { get }
var typealiases: [String: Typealias] { get }
var accessLevel: String { get }
var name: String { get }
var isUnknownExtension: Bool { get }
Expand Down Expand Up @@ -366,6 +369,7 @@ extension Modifier: ModifierAutoJSExport {}
var module: String? { get }
var imports: [Import] { get }
var allImports: [Import] { get }
var typealiases: [String: Typealias] { get }
var accessLevel: String { get }
var name: String { get }
var isUnknownExtension: Bool { get }
Expand Down Expand Up @@ -415,6 +419,7 @@ extension Protocol: ProtocolAutoJSExport {}
var module: String? { get }
var imports: [Import] { get }
var allImports: [Import] { get }
var typealiases: [String: Typealias] { get }
var accessLevel: String { get }
var name: String { get }
var isUnknownExtension: Bool { get }
Expand Down Expand Up @@ -464,6 +469,7 @@ extension ProtocolComposition: ProtocolCompositionAutoJSExport {}
var module: String? { get }
var imports: [Import] { get }
var allImports: [Import] { get }
var typealiases: [String: Typealias] { get }
var accessLevel: String { get }
var name: String { get }
var isUnknownExtension: Bool { get }
Expand Down Expand Up @@ -563,6 +569,7 @@ extension TupleType: TupleTypeAutoJSExport {}
var module: String? { get }
var imports: [Import] { get }
var allImports: [Import] { get }
var typealiases: [String: Typealias] { get }
var kind: String { get }
var accessLevel: String { get }
var name: String { get }
Expand Down
11 changes: 11 additions & 0 deletions SourceryTests/Generating/JavaScriptTemplateSpecs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ class JavaScriptTemplateTests: QuickSpec {
print("expected:\n\(expectedResult)\n\ngot:\n\(result)")
expect(result).to(equal(expectedResult))
}

it("provides typealias information") {
let templatePath = Stubs.jsTemplates + Path("Typealiases.ejs")
let expectedResult = try? (Stubs.resultDirectory + Path("Typealiases.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("handles includes") {
let templatePath = Stubs.jsTemplates + Path("Includes.ejs")
Expand Down
2 changes: 1 addition & 1 deletion SourceryTests/SourcerySpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ class SourcerySpecTests: QuickSpec {
expect {
let paths = sourceFilesPaths
expect(paths.contains(outputDir + "PerFileGeneration.generated.swift")).to(beTrue())
expect(paths.contains(outputDir + "Generated/Foo.generated.swift")).to(beTrue())
expect(paths.contains(outputDir + "Generated/FooBarBaz.generated.swift")).to(beTrue())
}.toNot(throwError())
}
}
Expand Down
6 changes: 6 additions & 0 deletions SourceryTests/Stub/JavaScriptTemplates/Typealiases.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%_ for (type of types.all) { -%>
// Typealiases in <%= type.name %>
<%_ for (const [aliasName, aliasValue] of Object.entries(type.typealiases)) { -%>
// - name '<%= aliasName %>', type '<%= aliasValue.typeName.name %>'
<% }
} -%>
10 changes: 5 additions & 5 deletions SourceryTests/Stub/Result/Basic+Other+SourceryTemplates.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Generated using Sourcery Major.Minor.Patch — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
extension Bar: Equatable {}
extension BarBaz: Equatable {}

// Bar has Annotations
// BarBaz has Annotations

func == (lhs: Bar, rhs: Bar) -> Bool {
func == (lhs: BarBaz, rhs: BarBaz) -> Bool {
if lhs.parent != rhs.parent { return false }
if lhs.otherVariable != rhs.otherVariable { return false }

return true
}

extension Foo: Equatable {}
extension FooBarBaz: Equatable {}

func == (lhs: Foo, rhs: Foo) -> Bool {
func == (lhs: FooBarBaz, rhs: FooBarBaz) -> Bool {
if lhs.name != rhs.name { return false }
if lhs.value != rhs.value { return false }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Generated using Sourcery Major.Minor.Patch — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
extension Bar: Equatable {}
extension BarBaz: Equatable {}

// Bar has Annotations
// BarBaz has Annotations

func == (lhs: Bar, rhs: Bar) -> Bool {
func == (lhs: BarBaz, rhs: BarBaz) -> Bool {
if lhs.parent != rhs.parent { return false }
if lhs.otherVariable != rhs.otherVariable { return false }

return true
}

extension Foo: Equatable {}
extension FooBarBaz: Equatable {}

func == (lhs: Foo, rhs: Foo) -> Bool {
func == (lhs: FooBarBaz, rhs: FooBarBaz) -> Bool {
if lhs.name != rhs.name { return false }
if lhs.value != rhs.value { return false }

Expand Down
10 changes: 5 additions & 5 deletions SourceryTests/Stub/Result/Basic+Other.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Generated using Sourcery Major.Minor.Patch — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
extension Bar: Equatable {}
extension BarBaz: Equatable {}

// Bar has Annotations
// BarBaz has Annotations

func == (lhs: Bar, rhs: Bar) -> Bool {
func == (lhs: BarBaz, rhs: BarBaz) -> Bool {
if lhs.parent != rhs.parent { return false }
if lhs.otherVariable != rhs.otherVariable { return false }

return true
}

extension Foo: Equatable {}
extension FooBarBaz: Equatable {}

func == (lhs: Foo, rhs: Foo) -> Bool {
func == (lhs: FooBarBaz, rhs: FooBarBaz) -> Bool {
if lhs.name != rhs.name { return false }
if lhs.value != rhs.value { return false }

Expand Down
10 changes: 5 additions & 5 deletions SourceryTests/Stub/Result/Basic.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Generated using Sourcery Major.Minor.Patch — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
extension Bar: Equatable {}
extension BarBaz: Equatable {}

// Bar has Annotations
// BarBaz has Annotations

func == (lhs: Bar, rhs: Bar) -> Bool {
func == (lhs: BarBaz, rhs: BarBaz) -> Bool {
if lhs.parent != rhs.parent { return false }
if lhs.otherVariable != rhs.otherVariable { return false }

return true
}

extension Foo: Equatable {}
extension FooBarBaz: Equatable {}

func == (lhs: Foo, rhs: Foo) -> Bool {
func == (lhs: FooBarBaz, rhs: FooBarBaz) -> Bool {
if lhs.name != rhs.name { return false }
if lhs.value != rhs.value { return false }

Expand Down
6 changes: 3 additions & 3 deletions SourceryTests/Stub/Result/BasicFooExcluded.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Generated using Sourcery Major.Minor.Patch — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
extension Bar: Equatable {}
extension BarBaz: Equatable {}

// Bar has Annotations
// BarBaz has Annotations

func == (lhs: Bar, rhs: Bar) -> Bool {
func == (lhs: BarBaz, rhs: BarBaz) -> Bool {
if lhs.parent != rhs.parent { return false }
if lhs.otherVariable != rhs.otherVariable { return false }

Expand Down
4 changes: 2 additions & 2 deletions SourceryTests/Stub/Result/FooBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
struct AnyFooBar: FooBar {

// MARK: HasFoo properties
var foo: Foo
var foo: FooBarBaz

// MARK: HasBar properties
var bar: Bar
var bar: BarBaz
}
2 changes: 1 addition & 1 deletion SourceryTests/Stub/Result/Function.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Generated using Sourcery Major.Minor.Patch — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
func wrappedPerformFoo(value: Foo) {
func wrappedPerformFoo(value: FooBarBaz) {
performFoo(value: value)
}
7 changes: 7 additions & 0 deletions SourceryTests/Stub/Result/Typealiases.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Generated using Sourcery Major.Minor.Patch — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
// Typealiases in BarBaz
// - name 'List', type '[FooBarBaz]'
// Typealiases in FooBarBaz
// - name 'Name', type 'String'
// Typealiases in FooSubclass
7 changes: 4 additions & 3 deletions SourceryTests/Stub/Source/Bar.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Foundation

// sourcery: this will not appear under Bar
// sourcery: this will not appear under FooBarBaz

/// Documentation for bar
// sourcery: showComment
/// other documentation
class Bar: Foo, AutoEquatable {
var parent: Foo? = nil
class BarBaz: FooBarBaz, AutoEquatable {
typealias List = [FooBarBaz]
var parent: FooBarBaz? = nil
var otherVariable: Int = 0
}
10 changes: 7 additions & 3 deletions SourceryTests/Stub/Source/Foo.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import Foundation

class Foo {
class FooBarBaz {
// Note: when Swift Linux doesn't bug out on [String: String], add a test back for it
// See https://github.com/krzysztofzablocki/Sourcery/pull/1208#issuecomment-1752185381
typealias Name = String

var name: String = ""
var value: Int = 0
}

protocol AutoEquatable {}

class FooSubclass: Foo, AutoEquatable {
class FooSubclass: FooBarBaz, AutoEquatable {
var other: String = ""
}

func performFoo(value: Foo) {
func performFoo(value: FooBarBaz) {

}
4 changes: 2 additions & 2 deletions SourceryTests/Stub/Source/FooBar.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Foundation

protocol HasFoo {
var foo: Foo { get }
var foo: FooBarBaz { get }
}
protocol HasBar {
var bar: Bar { get }
var bar: BarBaz { get }
}

// sourcery: AutoStruct
Expand Down