@@ -49,17 +49,15 @@ extension FileDescriptor {
49
49
// Aside: This could be moved into the plugin library, but it doesn't seem
50
50
// like anyone else would need the logic. Swift GRPC support probably stick
51
51
// with the support for the module mappings.
52
- public func computeImports(
52
+ func computeImports(
53
53
namer: SwiftProtobufNamer ,
54
- reexportPublicImports : Bool ,
55
- asImplementationOnly : Bool
54
+ directive : GeneratorOptions . ImportDirective ,
55
+ reexportPublicImports : Bool
56
56
) -> String {
57
57
// The namer should be configured with the module this file generated for.
58
58
assert ( namer. targetModule == ( namer. mappings. moduleName ( forFile: self ) ?? " " ) )
59
59
// Both options can't be enabled.
60
- assert ( !reexportPublicImports ||
61
- !asImplementationOnly ||
62
- reexportPublicImports != asImplementationOnly)
60
+ assert ( !reexportPublicImports || directive != . implementationOnly)
63
61
64
62
guard namer. mappings. hasMappings else {
65
63
// No module mappings? Everything must be the same module, so no Swift
@@ -72,7 +70,7 @@ extension FileDescriptor {
72
70
return " "
73
71
}
74
72
75
- let directive = asImplementationOnly ? " @_implementationOnly import " : " import "
73
+ let importSnippet = directive . snippet
76
74
var imports = Set < String > ( )
77
75
for dependency in dependencies {
78
76
if SwiftProtobufInfo . isBundledProto ( file: dependency) {
@@ -86,16 +84,19 @@ extension FileDescriptor {
86
84
if let depModule = namer. mappings. moduleName ( forFile: dependency) ,
87
85
depModule != namer. targetModule {
88
86
// Different module, import it.
89
- imports. insert ( " \( directive ) \( depModule) " )
87
+ imports. insert ( " \( importSnippet ) \( depModule) " )
90
88
}
91
89
}
92
90
93
91
// If not re-exporting imports, then there is nothing special needed for
94
92
// `import public` files, as any transitive `import public` directives
95
93
// would have already re-exported the types, so everything this file needs
96
94
// will be covered by the above imports.
97
- let exportingImports : [ String ] =
98
- reexportPublicImports ? computeSymbolReExports ( namer: namer) : [ String] ( )
95
+ let exportingImports : [ String ] = reexportPublicImports
96
+ ? computeSymbolReExports (
97
+ namer: namer,
98
+ useAccessLevelOnImports: directive. isAccessLevel)
99
+ : [ String] ( )
99
100
100
101
var result = imports. sorted ( ) . joined ( separator: " \n " )
101
102
if !exportingImports. isEmpty {
@@ -109,7 +110,7 @@ extension FileDescriptor {
109
110
}
110
111
111
112
// Internal helper to `computeImports(...)`.
112
- private func computeSymbolReExports( namer: SwiftProtobufNamer ) -> [ String ] {
113
+ private func computeSymbolReExports( namer: SwiftProtobufNamer , useAccessLevelOnImports : Bool ) -> [ String ] {
113
114
var result = [ String] ( )
114
115
115
116
// To handle re-exporting, recursively walk all the `import public` files
@@ -119,6 +120,7 @@ extension FileDescriptor {
119
120
// authored code.
120
121
var toScan = publicDependencies
121
122
var visited = Set < String > ( )
123
+ let exportedImportDirective = " @_exported \( useAccessLevelOnImports ? " public " : " " ) import "
122
124
while let dependency = toScan. popLast ( ) {
123
125
let dependencyName = dependency. name
124
126
if visited. contains ( dependencyName) { continue }
@@ -144,16 +146,16 @@ extension FileDescriptor {
144
146
// chained imports.
145
147
146
148
for m in dependency. messages {
147
- result. append ( " @_exported import struct \( namer. fullName ( message: m) ) " )
149
+ result. append ( " \( exportedImportDirective ) struct \( namer. fullName ( message: m) ) " )
148
150
}
149
151
for e in dependency. enums {
150
- result. append ( " @_exported import enum \( namer. fullName ( enum: e) ) " )
152
+ result. append ( " \( exportedImportDirective ) enum \( namer. fullName ( enum: e) ) " )
151
153
}
152
154
// There is nothing we can do for the Swift extensions declared on the
153
155
// extended Messages, best we can do is expose the raw extensions
154
156
// themselves.
155
157
for e in dependency. extensions {
156
- result. append ( " @_exported import let \( namer. fullName ( extensionField: e) ) " )
158
+ result. append ( " \( exportedImportDirective ) let \( namer. fullName ( extensionField: e) ) " )
157
159
}
158
160
}
159
161
return result
0 commit comments