Skip to content

Commit

Permalink
Fix struct var parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kryštof Matěj committed Aug 14, 2024
1 parent 15fa080 commit ee84284
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Sources/Rubicon/Syntactic analysis/StructParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ private class StructVisitor: SyntaxVisitor {
result.append(
StructDeclaration(
name: (nestedInItemsNames + [name]).joined(separator: "."),
variables: varsVisitor.execute(node: node),
notes: node.leadingTrivia.pieces.compactMap(makeLineComment),
variables: varsVisitor.execute(node: node.memberBlock),
notes: node.leadingTrivia.pieces.compactMap(makeLineComment),
accessLevel: parseAccessLevel(from: node)
)
)
Expand Down Expand Up @@ -127,4 +127,16 @@ private class VariablesVisitor: SyntaxVisitor {
} catch {}
return .visitChildren
}

override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind {
.skipChildren
}

override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
return .skipChildren
}

override func visit(_ node: EnumDeclSyntax) -> SyntaxVisitorContinueKind {
return .skipChildren
}
}
62 changes: 62 additions & 0 deletions Tests/RubiconTests/Syntactic analysis/StructParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,66 @@ final class StructParserTests: XCTestCase {

XCTAssertEqual(structs.first?.accessLevel, .private)
}

func test_givenStructWithVariablesAndNestedStruct_whenParse_thenReturnStruct() throws {
let text = """
struct A {
struct B {
var c: Int
let d: Int
}
var a: Int
let b: Int
}
"""

let structs = try sut.parse(text: text)

XCTAssertEqual(structs.count, 2)
XCTAssertEqual(structs.first?.name, "A")
XCTAssertEqual(structs.first?.variables.count, 2)
XCTAssertEqual(structs.first?.variables.first, .makeStub())
}

func test_givenStructWithVariablesAndNestedClass_whenParse_thenReturnStruct() throws {
let text = """
struct A {
class B {
var c: Int
let d: Int
}
var a: Int
let b: Int
}
"""

let structs = try sut.parse(text: text)

XCTAssertEqual(structs.count, 1)
XCTAssertEqual(structs.first?.name, "A")
XCTAssertEqual(structs.first?.variables.count, 2)
XCTAssertEqual(structs.first?.variables.first, .makeStub())
}

func test_givenStructWithVariablesAndNestedEnum_whenParse_thenReturnStruct() throws {
let text = """
struct A {
enum B {
static var c: Int = 0
}
var a: Int
let b: Int
}
"""

let structs = try sut.parse(text: text)

XCTAssertEqual(structs.count, 1)
XCTAssertEqual(structs.first?.name, "A")
XCTAssertEqual(structs.first?.variables.count, 2)
XCTAssertEqual(structs.first?.variables.first, .makeStub())
}
}

0 comments on commit ee84284

Please sign in to comment.