Skip to content

Fix code generation#7

Merged
FlickerSoul merged 2 commits intomainfrom
fix/strip-comments
Nov 10, 2025
Merged

Fix code generation#7
FlickerSoul merged 2 commits intomainfrom
fix/strip-comments

Conversation

@FlickerSoul
Copy link
Owner

@FlickerSoul FlickerSoul commented Nov 10, 2025

This PR

(1) strips comments in macro code generation output, which had led to invalid syntax. For instance

struct Header {
  var value: Int // some comment
}

generates __assertParsable(Int // some comment.self)

(2) wraps types in () before applying .self

For instance, A & B will previously generate A & B.self instead of (A & B).self

@FlickerSoul FlickerSoul changed the title Strip comments in macro output Fix code generation Nov 10, 2025
@FlickerSoul FlickerSoul requested a review from Copilot November 10, 2025 18:55
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a macro code generation issue where comments and extra whitespace in type annotations were being incorrectly included in the generated code. The fix wraps type syntax nodes with parentheses and applies .trimmed to remove leading/trailing trivia (comments and spaces).

  • Wraps all type references in generated assertion calls with parentheses to handle types that contain trivia
  • Applies .trimmed to TokenSyntax and TypeSyntax nodes to remove comments and whitespace
  • Removes nonisolated(unsafe) modifier from test macros constant

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
Tests/BinaryParseKitMacroTests/Misc.swift Removed nonisolated(unsafe) modifier from testMacros constant
Tests/BinaryParseKitMacroTests/BinaryParseKitStructTests.swift Updated test expectations with parentheses around types and added new test for comment/space handling
Tests/BinaryParseKitMacroTests/BinaryParseKitEnumTests.swift Updated test expectations with parentheses around types and added new test for comment handling
Sources/BinaryParseKitMacros/Macros/Supports/Utilities.swift Wrapped type syntax in parentheses in all assertion calls to handle trivia correctly
Sources/BinaryParseKitMacros/Macros/Supports/ParseMacroInfo.swift Applied .trimmed to endianness and byteCount expressions to remove trivia
Sources/BinaryParseKitMacros/Macros/ParseStruct/ParseStructField.swift Added init that trims type and variable name, applied trimming to variable name
Sources/BinaryParseKitMacros/Macros/ParseEnum/EnumCaseParseInfo.swift Added inits that trim firstName, type, and caseElementName tokens

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@FlickerSoul FlickerSoul merged commit 1190f9e into main Nov 10, 2025
8 checks passed
@FlickerSoul FlickerSoul deleted the fix/strip-comments branch November 10, 2025 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants