-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[swift2objc] Support properties that throw #1939
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import '../../ast/_core/shared/parameter.dart'; | |
import '../../ast/declarations/built_in/built_in_declaration.dart'; | ||
import '../../ast/declarations/compounds/class_declaration.dart'; | ||
import '../../ast/declarations/compounds/members/initializer_declaration.dart'; | ||
import '../../ast/declarations/compounds/members/method_declaration.dart'; | ||
import '../../ast/declarations/compounds/members/property_declaration.dart'; | ||
import '../../parser/_core/utils.dart'; | ||
import '../_core/unique_namer.dart'; | ||
|
@@ -52,6 +53,7 @@ ClassDeclaration transformCompound( | |
.fillNestingParents(transformedCompound); | ||
|
||
transformedCompound.properties = originalCompound.properties | ||
.where((property) => !property.throws) | ||
.map((property) => transformProperty( | ||
property, | ||
wrappedCompoundInstance, | ||
|
@@ -72,7 +74,8 @@ ClassDeclaration transformCompound( | |
.toList() | ||
..sort((Declaration a, Declaration b) => a.id.compareTo(b.id)); | ||
|
||
transformedCompound.methods = originalCompound.methods | ||
transformedCompound.methods = (originalCompound.methods + | ||
_convertPropertiesToMethods(originalCompound.properties)) | ||
.map((method) => transformMethod( | ||
method, | ||
wrappedCompoundInstance, | ||
|
@@ -106,3 +109,23 @@ InitializerDeclaration _buildWrapperInitializer( | |
hasObjCAnnotation: wrappedClassInstance.hasObjCAnnotation, | ||
); | ||
} | ||
|
||
List<MethodDeclaration> _convertPropertiesToMethods( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if I understand correctly, you've added a It would be cleaner to remove // transformedProperties is a List<AstNode?>
final transformedProperties = originalCompound.properties
.map((property) => transformProperty(
property,
wrappedCompoundInstance,
parentNamer,
transformationMap,
)).nonNulls.toList();
final transformedMethods = originalCompound.methods
.map((method) => transformMethod(
method,
wrappedCompoundInstance,
parentNamer,
transformationMap,
)).nonNulls.toList();
transformedCompound.properties =
transformedProperties.whereType<PropertyDeclaration>()
..sort((Declaration a, Declaration b) => a.id.compareTo(b.id));
transformedCompound.methods = (transformedMethods +
transformedProperties.whereType<MethodDeclaration>())
..sort((Declaration a, Declaration b) => a.id.compareTo(b.id)); The same goes for |
||
List<PropertyDeclaration> properties, | ||
) { | ||
return properties | ||
.where((property) => property.throws) | ||
.map((property) => MethodDeclaration( | ||
id: property.id, | ||
name: property.name, | ||
returnType: property.type, | ||
params: [], | ||
hasObjCAnnotation: true, | ||
statements: property.getter?.statements ?? [], | ||
isStatic: property.isStatic, | ||
throws: property.throws, | ||
async: property.async, | ||
isCallingProperty: true, | ||
)) | ||
.toList(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Foundation | ||
|
||
public class MyClass { | ||
public init(y: Int) throws {} | ||
|
||
public var classGetter: MyClass { | ||
get throws { | ||
try MyClass(y: 3) | ||
} | ||
} | ||
public var otherClassGetter: OtherClass { | ||
get throws { | ||
OtherClass() | ||
} | ||
} | ||
} | ||
|
||
public class OtherClass {} | ||
|
||
public var globalClassGetter: MyClass { | ||
get throws { | ||
try MyClass(y: 4) | ||
} | ||
} | ||
public var globalOtherClassGetter: OtherClass { | ||
get throws { | ||
OtherClass() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Test preamble text | ||
|
||
import Foundation | ||
|
||
@objc public class GlobalsWrapper: NSObject { | ||
@objc static public func globalClassGetterWrapper() throws -> MyClassWrapper { | ||
let result = try globalClassGetter | ||
return MyClassWrapper(result) | ||
} | ||
|
||
@objc static public func globalOtherClassGetterWrapper() throws -> OtherClassWrapper { | ||
let result = try globalOtherClassGetter | ||
return OtherClassWrapper(result) | ||
} | ||
|
||
} | ||
|
||
@objc public class OtherClassWrapper: NSObject { | ||
var wrappedInstance: OtherClass | ||
|
||
init(_ wrappedInstance: OtherClass) { | ||
self.wrappedInstance = wrappedInstance | ||
} | ||
|
||
} | ||
|
||
@objc public class MyClassWrapper: NSObject { | ||
var wrappedInstance: MyClass | ||
|
||
init(_ wrappedInstance: MyClass) { | ||
self.wrappedInstance = wrappedInstance | ||
} | ||
|
||
@objc init(y: Int) throws { | ||
wrappedInstance = try MyClass(y: y) | ||
} | ||
|
||
@objc public func otherClassGetter() throws -> OtherClassWrapper { | ||
let result = try wrappedInstance.otherClassGetter | ||
return OtherClassWrapper(result) | ||
} | ||
|
||
@objc public func classGetter() throws -> MyClassWrapper { | ||
let result = try wrappedInstance.classGetter | ||
return MyClassWrapper(result) | ||
} | ||
|
||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can delete this whole if statement and just return
throws
.