Skip to content

Commit 74c5315

Browse files
committed
[Macros] Add back the #column implementation, now that it works
1 parent 8fff8dd commit 74c5315

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ public struct ImageLiteralMacro: ExpressionMacro {
100100
}
101101
}
102102

103+
public struct ColumnMacro: ExpressionMacro {
104+
public static func expansion(
105+
of macro: MacroExpansionExprSyntax,
106+
in context: any MacroExpansionContext
107+
) throws -> ExprSyntax {
108+
guard let sourceLoc = context.location(of: macro),
109+
let column = sourceLoc.column else {
110+
throw CustomError.message("can't find location for macro")
111+
}
112+
113+
114+
let fileLiteral: ExprSyntax = "\(literal: column)"
115+
if let leadingTrivia = macro.leadingTrivia {
116+
return fileLiteral.with(\.leadingTrivia, leadingTrivia)
117+
}
118+
return fileLiteral
119+
}
120+
}
121+
103122
public struct FileIDMacro: ExpressionMacro {
104123
public static func expansion(
105124
of macro: MacroExpansionExprSyntax,
@@ -602,6 +621,7 @@ public func AssertMacroExpansion(
602621
public let testMacros: [String: Macro.Type] = [
603622
"checkContext": CheckContextIndependenceMacro.self,
604623
"colorLiteral": ColorLiteralMacro.self,
624+
"column": ColumnMacro.self,
605625
"fileID": FileIDMacro.self,
606626
"imageLiteral": ImageLiteralMacro.self,
607627
"stringify": StringifyMacro.self,
@@ -648,16 +668,18 @@ final class MacroSystemTests: XCTestCase {
648668
)
649669
}
650670

651-
func testFileExpansions() {
671+
func testLocationExpansions() {
652672
AssertMacroExpansion(
653673
macros: testMacros,
654674
testModuleName: "MyModule",
655675
testFileName: "taylor.swift",
656676
"""
657677
let b = #fileID
678+
let c = #column
658679
""",
659680
"""
660681
let b = "MyModule/taylor.swift"
682+
let c = 9
661683
"""
662684
)
663685
}

0 commit comments

Comments
 (0)