Skip to content

Commit 4e674f4

Browse files
authored
[swift2objc] Generate initializers as public (#2667)
1 parent 0ff4b67 commit 4e674f4

12 files changed

+39
-43
lines changed

pkgs/swift2objc/lib/src/generator/generators/class_generator.dart

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,35 +72,31 @@ String? _generateClassWrappedInstance(ClassDeclaration declaration) {
7272
}
7373

7474
List<String> _generateInitializers(ClassDeclaration declaration) {
75-
final initializers = [
76-
declaration.wrapperInitializer,
77-
...declaration.initializers,
78-
].nonNulls;
79-
return [for (final init in initializers) ..._generateInitializer(init)];
75+
return [
76+
..._generateInitializer(declaration.wrapperInitializer, isPublic: false),
77+
for (final init in declaration.initializers)
78+
..._generateInitializer(init, isPublic: true),
79+
];
8080
}
8181

82-
List<String> _generateInitializer(InitializerDeclaration initializer) {
83-
final header = StringBuffer();
84-
85-
if (initializer.hasObjCAnnotation) {
86-
header.write('@objc ');
87-
}
88-
89-
if (initializer.isOverriding) {
90-
header.write('override ');
91-
}
92-
93-
header.write('init');
94-
95-
if (initializer.isFailable) {
96-
header.write('?');
97-
}
98-
99-
header.write('(${generateParameters(initializer.params)})');
82+
List<String> _generateInitializer(
83+
InitializerDeclaration? initializer, {
84+
required bool isPublic,
85+
}) {
86+
if (initializer == null) return [];
87+
final header = [
88+
if (initializer.hasObjCAnnotation) '@objc ',
89+
if (initializer.isOverriding) 'override ',
90+
if (isPublic) 'public ',
91+
'init',
92+
if (initializer.isFailable) '?',
93+
'(${generateParameters(initializer.params)}) ',
94+
'${generateAnnotations(initializer)}{',
95+
].join('');
10096

10197
return [
10298
...generateAvailability(initializer),
103-
'$header ${generateAnnotations(initializer)}{',
99+
header,
104100
...initializer.statements.indent(),
105101
'}\n',
106102
];

pkgs/swift2objc/test/integration/async_init_output.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99
self.wrappedInstance = wrappedInstance
1010
}
1111

12-
@objc override init() {
12+
@objc override public init() {
1313
wrappedInstance = MyClass()
1414
}
1515

pkgs/swift2objc/test/integration/available_output.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import Foundation
5555

5656
@available(macOS, introduced: 456)
5757
@available(iOS, introduced: 101)
58-
@objc init(x: Int) {
58+
@objc public init(x: Int) {
5959
wrappedInstance = NewApi(x: x)
6060
}
6161

pkgs/swift2objc/test/integration/classes_and_initializers_output.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ import Foundation
3030
self.wrappedInstance = wrappedInstance
3131
}
3232

33-
@objc init(outerLabel representableProperty: Int, customProperty: MyOtherClassWrapper) {
33+
@objc public init(outerLabel representableProperty: Int, customProperty: MyOtherClassWrapper) {
3434
wrappedInstance = MyClass(outerLabel: representableProperty, customProperty: customProperty.wrappedInstance)
3535
}
3636

37-
@objc init?(outerLabel x: Int) {
37+
@objc public init?(outerLabel x: Int) {
3838
if let instance = MyClass(outerLabel: x) {
3939
wrappedInstance = instance
4040
} else {
4141
return nil
4242
}
4343
}
4444

45-
@objc init(label1 name1: Int, label2: Int, _ name3: Int) {
45+
@objc public init(label1 name1: Int, label2: Int, _ name3: Int) {
4646
wrappedInstance = MyClass(label1: name1, label2: label2, name3)
4747
}
4848

pkgs/swift2objc/test/integration/optional_output.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ import Foundation
5353
self.wrappedInstance = wrappedInstance
5454
}
5555

56-
@objc init(label param: MyClassWrapper?) {
56+
@objc public init(label param: MyClassWrapper?) {
5757
wrappedInstance = MyClass(label: param?.wrappedInstance)
5858
}
5959

60-
@objc init(label1 param1: MyClassWrapper?, label2: Int, label3 param3: MyStructWrapper?) {
60+
@objc public init(label1 param1: MyClassWrapper?, label2: Int, label3 param3: MyStructWrapper?) {
6161
wrappedInstance = MyClass(label1: param1?.wrappedInstance, label2: label2, label3: param3?.wrappedInstance)
6262
}
6363

@@ -88,7 +88,7 @@ import Foundation
8888
self.wrappedInstance = wrappedInstance
8989
}
9090

91-
@objc init(label param: MyClassWrapper?) {
91+
@objc public init(label param: MyClassWrapper?) {
9292
wrappedInstance = MyStruct(label: param?.wrappedInstance)
9393
}
9494

pkgs/swift2objc/test/integration/structs_and_initializers_output.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ import Foundation
3030
self.wrappedInstance = wrappedInstance
3131
}
3232

33-
@objc init(outerLabel representableProperty: Int, customProperty: MyOtherStructWrapper) {
33+
@objc public init(outerLabel representableProperty: Int, customProperty: MyOtherStructWrapper) {
3434
wrappedInstance = MyStruct(outerLabel: representableProperty, customProperty: customProperty.wrappedInstance)
3535
}
3636

37-
@objc init?(outerLabel x: Int) {
37+
@objc public init?(outerLabel x: Int) {
3838
if let instance = MyStruct(outerLabel: x) {
3939
wrappedInstance = instance
4040
} else {
4141
return nil
4242
}
4343
}
4444

45-
@objc init(label1 name1: Int, label2: Int, _ name3: Int) {
45+
@objc public init(label1 name1: Int, label2: Int, _ name3: Int) {
4646
wrappedInstance = MyStruct(label1: name1, label2: label2, name3)
4747
}
4848

pkgs/swift2objc/test/integration/throwing_getters_output.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import Foundation
3131
self.wrappedInstance = wrappedInstance
3232
}
3333

34-
@objc init(y: Int) throws {
34+
@objc public init(y: Int) throws {
3535
wrappedInstance = try MyClass(y: y)
3636
}
3737

pkgs/swift2objc/test/integration/throws_output.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Foundation
2121
self.wrappedInstance = wrappedInstance
2222
}
2323

24-
@objc init(y: Int) throws {
24+
@objc public init(y: Int) throws {
2525
wrappedInstance = try MyClass(y: y)
2626
}
2727

pkgs/swift2objc/test/unit/filter_test_output_a.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Foundation
2121
self.wrappedInstance = wrappedInstance
2222
}
2323

24-
@objc init(type: String, horsepower: Int) {
24+
@objc public init(type: String, horsepower: Int) {
2525
wrappedInstance = Engine(type: type, horsepower: horsepower)
2626
}
2727

pkgs/swift2objc/test/unit/filter_test_output_b.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ import Foundation
7575
self.wrappedInstance = wrappedInstance
7676
}
7777

78-
@objc override init() {
78+
@objc override public init() {
7979
wrappedInstance = Garage()
8080
}
8181

@@ -141,7 +141,7 @@ import Foundation
141141
self.wrappedInstance = wrappedInstance
142142
}
143143

144-
@objc init(brand: String, gearCount: Int, dimensions: DimensionsWrapper) {
144+
@objc public init(brand: String, gearCount: Int, dimensions: DimensionsWrapper) {
145145
wrappedInstance = Bicycle(brand: brand, gearCount: gearCount, dimensions: dimensions.wrappedInstance)
146146
}
147147

@@ -194,7 +194,7 @@ import Foundation
194194
self.wrappedInstance = wrappedInstance
195195
}
196196

197-
@objc init(make: String, model: String, engine: EngineWrapper, dimensions: DimensionsWrapper) {
197+
@objc public init(make: String, model: String, engine: EngineWrapper, dimensions: DimensionsWrapper) {
198198
wrappedInstance = Vehicle(make: make, model: model, engine: engine.wrappedInstance, dimensions: dimensions.wrappedInstance)
199199
}
200200

0 commit comments

Comments
 (0)