Skip to content

Commit 4a36f84

Browse files
committed
BridgeJS: Fix support for negative enum values for raw value types
1 parent 2c183f3 commit 4a36f84

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -981,12 +981,23 @@ public class ExportSwift {
981981
rawValue = stringLiteral.segments.first?.as(StringSegmentSyntax.self)?.content.text
982982
} else if let boolLiteral = element.rawValue?.value.as(BooleanLiteralExprSyntax.self) {
983983
rawValue = boolLiteral.literal.text
984-
} else if let intLiteral = element.rawValue?.value.as(IntegerLiteralExprSyntax.self) {
985-
rawValue = intLiteral.literal.text
986-
} else if let floatLiteral = element.rawValue?.value.as(FloatLiteralExprSyntax.self) {
987-
rawValue = floatLiteral.literal.text
988984
} else {
989-
rawValue = nil
985+
var numericExpr = element.rawValue?.value
986+
var isNegative = false
987+
if let prefixExpr = numericExpr?.as(PrefixOperatorExprSyntax.self),
988+
prefixExpr.operator.text == "-"
989+
{
990+
numericExpr = prefixExpr.expression
991+
isNegative = true
992+
}
993+
994+
if let intLiteral = numericExpr?.as(IntegerLiteralExprSyntax.self) {
995+
rawValue = isNegative ? "-\(intLiteral.literal.text)" : intLiteral.literal.text
996+
} else if let floatLiteral = numericExpr?.as(FloatLiteralExprSyntax.self) {
997+
rawValue = isNegative ? "-\(floatLiteral.literal.text)" : floatLiteral.literal.text
998+
} else {
999+
rawValue = nil
1000+
}
9901001
}
9911002
} else {
9921003
rawValue = nil

Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/EnumRawType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
}
2929

3030
@JS enum Priority: Int32 {
31-
case lowest = 1
31+
case lowest = -1
3232
case low = 2
3333
case medium = 3
3434
case high = 4

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export enum TSHttpStatus {
3737
}
3838

3939
export const PriorityValues: {
40-
readonly Lowest: 1;
40+
readonly Lowest: -1;
4141
readonly Low: 2;
4242
readonly Medium: 3;
4343
readonly High: 4;

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const TSHttpStatus = {
3434
};
3535

3636
export const PriorityValues = {
37-
Lowest: 1,
37+
Lowest: -1,
3838
Low: 2,
3939
Medium: 3,
4040
High: 4,

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumRawType.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178

179179
],
180180
"name" : "lowest",
181-
"rawValue" : "1"
181+
"rawValue" : "-1"
182182
},
183183
{
184184
"associatedValues" : [

0 commit comments

Comments
 (0)