Skip to content

Expansion context #3

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

Merged
merged 5 commits into from
Mar 30, 2025
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
6 changes: 3 additions & 3 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ let package = Package(
),
.testTarget(
name: "PrincipleMacrosTests",
dependencies: ["PrincipleMacros"]
dependencies: [
"PrincipleMacros",
.product(
name: "SwiftSyntaxMacroExpansion",
package: "swift-syntax"
)
]
)
]
)
17 changes: 0 additions & 17 deletions Sources/PrincipleMacros/Diagnostics/DiagnosticContext.swift

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// MacroExpansionContext.swift
// PrincipleMacros
//
// Created by Kamil Strzelecki on 30/03/2025.
// Copyright © 2025 Kamil Strzelecki. All rights reserved.
//

import SwiftSyntaxMacros

extension MacroExpansionContext {

public func diagnose(
node: some SyntaxProtocol,
errorMessage: String
) {
let message = MacroExpansionErrorMessage(errorMessage)
let diagnostic = Diagnostic(node: node, message: message)
diagnose(diagnostic)
}
}
5 changes: 3 additions & 2 deletions Sources/PrincipleMacros/Parsers/Common/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
//

import SwiftSyntax
import SwiftSyntaxMacros

public protocol Parser {

associatedtype ResultsCollection: ParserResultsCollection

static func parse(
declaration: DeclSyntaxProtocol,
in context: DiagnosticContext
in context: MacroExpansionContext
) -> ResultsCollection

static func parse(
memberBlock: MemberBlockSyntax,
in context: DiagnosticContext
in context: MacroExpansionContext
) -> ResultsCollection
}
5 changes: 4 additions & 1 deletion Sources/PrincipleMacros/Parsers/Common/_Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
// Copyright © 2025 Kamil Strzelecki. All rights reserved.
//

import SwiftSyntax
import SwiftSyntaxMacros

internal protocol _Parser: Parser
where ResultsCollection: _ParserResultsCollection {}

extension _Parser {

public static func parse(
memberBlock: MemberBlockSyntax,
in context: DiagnosticContext
in context: MacroExpansionContext
) -> ResultsCollection {
ResultsCollection(
memberBlock.members.flatMap { member in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
//

import SwiftSyntax
import SwiftSyntaxMacros

public enum EnumCasesParser: _Parser {

public static func parse(
declaration: DeclSyntaxProtocol,
in _: DiagnosticContext
in _: MacroExpansionContext
) -> EnumCasesList {
guard let declaration = EnumCaseDeclSyntax(declaration) else {
return .init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
//

import SwiftSyntax
import SwiftSyntaxMacros

public enum PropertiesParser: _Parser {

public static func parse(
declaration: DeclSyntaxProtocol,
in context: DiagnosticContext
in context: MacroExpansionContext
) -> PropertiesList {
guard let declaration = VariableDeclSyntax(declaration) else {
return .init()
Expand Down
22 changes: 0 additions & 22 deletions Tests/PrincipleMacrosTests/Mocks/DiagnosticContextMock.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
//

@testable import PrincipleMacros
import SwiftSyntaxMacroExpansion
import Testing

internal struct EnumCasesParserTests {

private let context = DiagnosticContextMock()
private let context = BasicMacroExpansionContext()

@Test
func testEnumCase() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
//

@testable import PrincipleMacros
import SwiftSyntaxMacroExpansion
import Testing

internal struct PropertiesParserTests {

private let context = DiagnosticContextMock()
private let context = BasicMacroExpansionContext()

@Test
func testStoredLet() throws {
Expand Down