Skip to content

Commit 22f796b

Browse files
committed
Add floatingValue to FloatLiteralExprSyntax
1 parent 239d369 commit 22f796b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Sources/SwiftSyntax/Convenience.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ extension EnumCaseParameterSyntax {
6868
}
6969
}
7070

71+
extension FloatLiteralExprSyntax {
72+
public var floatingValue: Double? {
73+
let floatingDigitsWithoutUnderscores = literal.text.filter {
74+
$0 != "_"
75+
}
76+
77+
return Double(floatingDigitsWithoutUnderscores)
78+
}
79+
}
80+
7181
extension IntegerLiteralExprSyntax {
7282
public enum Radix {
7383
case binary

Tests/SwiftSyntaxTest/SyntaxTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,17 @@ class SyntaxTests: XCTestCase {
190190
XCTAssertEqual(expr.integerValue, expected, line: line)
191191
}
192192
}
193+
194+
func testFloatLiteralExprSyntax() {
195+
let testCases: [UInt: (String, Double?)] = [
196+
#line: ("2", 2),
197+
#line: ("2_00_00.001", 20000.001),
198+
]
199+
200+
for (line, testCase) in testCases {
201+
let (value, expected) = testCase
202+
let expr = FloatLiteralExprSyntax(literal: .floatLiteral(value))
203+
XCTAssertEqual(expr.floatingValue, expected, line: line)
204+
}
205+
}
193206
}

0 commit comments

Comments
 (0)