Skip to content

Commit

Permalink
Fixed typealias resolution breaking resolution of real types. (#1325)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianmuecke authored Apr 30, 2024
1 parent fd86480 commit 6ecf791
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ internal struct ParserResultsComposed {
// let variable: Module.ID.ID // should be resolved as MyView.ID type
let finalLookup = typeName.actualTypeName ?? typeName
var resolvedIdentifier = finalLookup.generic?.name ?? finalLookup.unwrappedTypeName
if let type = unique[resolvedIdentifier] {
return type
}

for alias in resolvedTypealiases {
/// iteratively replace all typealiases from the resolvedIdentifier to get to the actual type name requested
if resolvedIdentifier.contains(alias.value.name), let range = resolvedIdentifier.range(of: alias.value.name) {
Expand Down
48 changes: 48 additions & 0 deletions SourceryTests/Parsing/ComposerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2249,6 +2249,54 @@ class ParserComposerSpec: QuickSpec {
check(variable: "batDouble", typeName: "Double", type: "Double", onType: "ModuleA.Foo.Bar")
check(variable: "batInt", typeName: "Int", type: nil, onType: "ModuleA.Foo.Bar")
}

it("resolves variable type correctly when typealias is used") {
let expectedBar = Class(name: "Bar", variables: [])

let expectedContainsTopLevelTypealias = Struct(name: "ContainsTopLevelTypealias")

let expectedBaz = Struct(name: "Baz", variables: [
Variable(name: "bar", typeName: TypeName(name: "Foo.Bar"), type: expectedBar, accessLevel: (.internal, .none), definedInTypeName: TypeName(name: "Foo.Baz")),
Variable(name: "topLevelTypealias", typeName: TypeName(name: "TopLevelTypealias"), type: expectedBar, accessLevel: (.internal, .none), definedInTypeName: TypeName(name: "Foo.Baz")),
Variable(name: "topLevelType", typeName: TypeName(name: "ContainsTopLevelTypealias"), type: expectedContainsTopLevelTypealias, accessLevel: (.internal, .none), definedInTypeName: TypeName(name: "Foo.Baz")),
Variable(name: "usingTypealias", typeName: TypeName(name: "MyEnum.MyTypealias.Bar"), type: expectedBar, accessLevel: (.internal, .none), definedInTypeName: TypeName(name: "Foo.Baz"))
])

let expectedFoo = Struct(name: "Foo", containedTypes: [expectedBar, expectedBaz])

let types = parseModules(
(name: nil, contents:
"""
enum MyEnum {
typealias MyTypealias = Foo
}
typealias TopLevelTypealias = Foo.Bar
struct ContainsTopLevelTypealias {}
struct Foo {
class Bar {}
struct Baz {
let bar: Foo.Bar
let topLevelTypealias: TopLevelTypealias
let topLevelType: ContainsTopLevelTypealias
let usingTypealias: MyEnum.MyTypealias.Bar
}
}
"""
)).types

let parsedFoo = types.first(where: { $0.globalName == "Foo" })
expect(parsedFoo).to(equal(expectedFoo))
let parsedBaz = parsedFoo?.containedTypes.last
expect(parsedBaz).toNot(beNil())
expect(parsedBaz!.variables.first?.type).to(equal(expectedBar))
expect(parsedBaz!.variables.last?.type).to(equal(expectedBar))
for variable in parsedBaz!.variables {
expect(variable.type).toNot(beNil(), description: "\(variable.typeName.name)")
}
}
}
}

Expand Down

0 comments on commit 6ecf791

Please sign in to comment.