Skip to content

Commit 627c626

Browse files
authored
Merge pull request #455 from PassiveLogic/fix/negative-enum-value
BridgeJS: Fix support for negative enum values for raw value types
2 parents 2c183f3 + cf51873 commit 627c626

File tree

8 files changed

+29
-9
lines changed

8 files changed

+29
-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" : [

Tests/BridgeJSRuntimeTests/ExportAPITests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ struct TestError: Error {
153153
case ok = 200
154154
case notFound = 404
155155
case serverError = 500
156+
case unknown = -1
156157
}
157158

158159
@JS(enumStyle: .tsEnum) enum TSDirection {

Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.ExportSwift.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,13 @@
12901290
],
12911291
"name" : "serverError",
12921292
"rawValue" : "500"
1293+
},
1294+
{
1295+
"associatedValues" : [
1296+
1297+
],
1298+
"name" : "unknown",
1299+
"rawValue" : "-1"
12931300
}
12941301
],
12951302
"emitStyle" : "const",

Tests/prelude.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ function BridgeJSRuntimeTests_runJsWorks(instance, exports) {
401401
assert.equal(exports.HttpStatus.Ok, 200);
402402
assert.equal(exports.HttpStatus.NotFound, 404);
403403
assert.equal(HttpStatusValues.ServerError, 500);
404+
assert.equal(HttpStatusValues.Unknown, -1);
404405

405406
assert.equal(exports.setTheme(exports.Theme.Light), exports.Theme.Light);
406407
assert.equal(exports.setTheme(exports.Theme.Dark), exports.Theme.Dark);

0 commit comments

Comments
 (0)