From 9b8d79b4c67def0347a0024a139bd86a68f9bd08 Mon Sep 17 00:00:00 2001 From: Liam Appelbe Date: Wed, 20 Nov 2024 09:04:28 +1100 Subject: [PATCH] Support globals with explicit getters/setters (#1733) --- .../parse_variable_declaration.dart | 4 +++- .../global_variables_and_functions_input.swift | 3 +++ .../global_variables_and_functions_output.swift | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkgs/swift2objc/lib/src/parser/parsers/declaration_parsers/parse_variable_declaration.dart b/pkgs/swift2objc/lib/src/parser/parsers/declaration_parsers/parse_variable_declaration.dart index 783c6bb48..171fc2acf 100644 --- a/pkgs/swift2objc/lib/src/parser/parsers/declaration_parsers/parse_variable_declaration.dart +++ b/pkgs/swift2objc/lib/src/parser/parsers/declaration_parsers/parse_variable_declaration.dart @@ -29,11 +29,13 @@ GlobalVariableDeclaration parseGlobalVariableDeclaration( ParsedSymbolgraph symbolgraph, { bool isStatic = false, }) { + final isConstant = _parseVariableIsConstant(variableSymbolJson); + final hasSetter = _parsePropertyHasSetter(variableSymbolJson); return GlobalVariableDeclaration( id: parseSymbolId(variableSymbolJson), name: parseSymbolName(variableSymbolJson), type: _parseVariableType(variableSymbolJson, symbolgraph), - isConstant: _parseVariableIsConstant(variableSymbolJson), + isConstant: isConstant || !hasSetter, ); } diff --git a/pkgs/swift2objc/test/integration/global_variables_and_functions_input.swift b/pkgs/swift2objc/test/integration/global_variables_and_functions_input.swift index cd838a0df..8f7a222a6 100644 --- a/pkgs/swift2objc/test/integration/global_variables_and_functions_input.swift +++ b/pkgs/swift2objc/test/integration/global_variables_and_functions_input.swift @@ -6,6 +6,9 @@ public let globalRepresentableConstant = 1 public var globalCustomVariable = MyOtherClass() public let globalCustomConstant = MyOtherClass() +public var globalGetterVariable: Double { get { 123 } } +public var globalSetterVariable: Double { get { 123 } set {} } + public func globalCustomFunction(label1 param1: Int, param2: MyOtherClass) -> MyOtherClass { return MyOtherClass() } diff --git a/pkgs/swift2objc/test/integration/global_variables_and_functions_output.swift b/pkgs/swift2objc/test/integration/global_variables_and_functions_output.swift index 79e680af0..9409f4d2d 100644 --- a/pkgs/swift2objc/test/integration/global_variables_and_functions_output.swift +++ b/pkgs/swift2objc/test/integration/global_variables_and_functions_output.swift @@ -18,6 +18,21 @@ import Foundation } } + @objc static public var globalGetterVariableWrapper: Double { + get { + globalGetterVariable + } + } + + @objc static public var globalSetterVariableWrapper: Double { + get { + globalSetterVariable + } + set { + globalSetterVariable = newValue + } + } + @objc static public var globalRepresentableConstantWrapper: Int { get { globalRepresentableConstant