From 4a36f84d9f620eb2c9e4b0d5eb533e8b070cf4ce Mon Sep 17 00:00:00 2001 From: Krzysztof Rodak Date: Fri, 17 Oct 2025 13:20:26 +0200 Subject: [PATCH 1/2] BridgeJS: Fix support for negative enum values for raw value types --- .../Sources/BridgeJSCore/ExportSwift.swift | 21 ++++++++++++++----- .../Inputs/EnumRawType.swift | 2 +- .../BridgeJSLinkTests/EnumRawType.Export.d.ts | 2 +- .../BridgeJSLinkTests/EnumRawType.Export.js | 2 +- .../ExportSwiftTests/EnumRawType.json | 2 +- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift b/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift index 2a10a360..00902ee9 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift @@ -981,12 +981,23 @@ public class ExportSwift { rawValue = stringLiteral.segments.first?.as(StringSegmentSyntax.self)?.content.text } else if let boolLiteral = element.rawValue?.value.as(BooleanLiteralExprSyntax.self) { rawValue = boolLiteral.literal.text - } else if let intLiteral = element.rawValue?.value.as(IntegerLiteralExprSyntax.self) { - rawValue = intLiteral.literal.text - } else if let floatLiteral = element.rawValue?.value.as(FloatLiteralExprSyntax.self) { - rawValue = floatLiteral.literal.text } else { - rawValue = nil + var numericExpr = element.rawValue?.value + var isNegative = false + if let prefixExpr = numericExpr?.as(PrefixOperatorExprSyntax.self), + prefixExpr.operator.text == "-" + { + numericExpr = prefixExpr.expression + isNegative = true + } + + if let intLiteral = numericExpr?.as(IntegerLiteralExprSyntax.self) { + rawValue = isNegative ? "-\(intLiteral.literal.text)" : intLiteral.literal.text + } else if let floatLiteral = numericExpr?.as(FloatLiteralExprSyntax.self) { + rawValue = isNegative ? "-\(floatLiteral.literal.text)" : floatLiteral.literal.text + } else { + rawValue = nil + } } } else { rawValue = nil diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/EnumRawType.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/EnumRawType.swift index 7e2c17ac..13b502ea 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/EnumRawType.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/EnumRawType.swift @@ -28,7 +28,7 @@ } @JS enum Priority: Int32 { - case lowest = 1 + case lowest = -1 case low = 2 case medium = 3 case high = 4 diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.d.ts b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.d.ts index b92c54ce..d321b99f 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.d.ts +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.d.ts @@ -37,7 +37,7 @@ export enum TSHttpStatus { } export const PriorityValues: { - readonly Lowest: 1; + readonly Lowest: -1; readonly Low: 2; readonly Medium: 3; readonly High: 4; diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.js b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.js index 3c7239b7..6b56108a 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.js +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumRawType.Export.js @@ -34,7 +34,7 @@ export const TSHttpStatus = { }; export const PriorityValues = { - Lowest: 1, + Lowest: -1, Low: 2, Medium: 3, High: 4, diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumRawType.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumRawType.json index 207fd11a..334c8f05 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumRawType.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/EnumRawType.json @@ -178,7 +178,7 @@ ], "name" : "lowest", - "rawValue" : "1" + "rawValue" : "-1" }, { "associatedValues" : [ From cf5187323dec1ed76a4a5560759dac5dd371e765 Mon Sep 17 00:00:00 2001 From: Krzysztof Rodak Date: Fri, 17 Oct 2025 13:47:03 +0200 Subject: [PATCH 2/2] Update runtime tests --- Tests/BridgeJSRuntimeTests/ExportAPITests.swift | 1 + .../Generated/JavaScript/BridgeJS.ExportSwift.json | 7 +++++++ Tests/prelude.mjs | 1 + 3 files changed, 9 insertions(+) diff --git a/Tests/BridgeJSRuntimeTests/ExportAPITests.swift b/Tests/BridgeJSRuntimeTests/ExportAPITests.swift index a5959e14..3336801d 100644 --- a/Tests/BridgeJSRuntimeTests/ExportAPITests.swift +++ b/Tests/BridgeJSRuntimeTests/ExportAPITests.swift @@ -153,6 +153,7 @@ struct TestError: Error { case ok = 200 case notFound = 404 case serverError = 500 + case unknown = -1 } @JS(enumStyle: .tsEnum) enum TSDirection { diff --git a/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.ExportSwift.json b/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.ExportSwift.json index 069b4332..9af2ee95 100644 --- a/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.ExportSwift.json +++ b/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.ExportSwift.json @@ -1290,6 +1290,13 @@ ], "name" : "serverError", "rawValue" : "500" + }, + { + "associatedValues" : [ + + ], + "name" : "unknown", + "rawValue" : "-1" } ], "emitStyle" : "const", diff --git a/Tests/prelude.mjs b/Tests/prelude.mjs index 32560659..7f561027 100644 --- a/Tests/prelude.mjs +++ b/Tests/prelude.mjs @@ -401,6 +401,7 @@ function BridgeJSRuntimeTests_runJsWorks(instance, exports) { assert.equal(exports.HttpStatus.Ok, 200); assert.equal(exports.HttpStatus.NotFound, 404); assert.equal(HttpStatusValues.ServerError, 500); + assert.equal(HttpStatusValues.Unknown, -1); assert.equal(exports.setTheme(exports.Theme.Light), exports.Theme.Light); assert.equal(exports.setTheme(exports.Theme.Dark), exports.Theme.Dark);