@@ -103,6 +103,12 @@ function visit<T extends ts.Node>(
103
103
] ) . transformed [ 0 ] as T ;
104
104
}
105
105
106
+ function convertParams ( params ?: ts . ParameterDeclaration [ ] ) :ts . ParameterDeclaration [ ] | undefined {
107
+ return params ?. map ( t => {
108
+ return ts . createParameter ( t . decorators , t . modifiers , t . dotDotDotToken , t . name , t . questionToken , t . type , t . initializer )
109
+ } ) ;
110
+ }
111
+
106
112
/**
107
113
* Examines `extends` and `implements` clauses and removes or replaces them if
108
114
* they refer to a non-exported type. When an export is removed, all members
@@ -159,48 +165,13 @@ function prunePrivateImports<
159
165
// Iterate all members of the private type and add them to the
160
166
// public type if they are not already part of the public type.
161
167
const privateType = typeChecker . getTypeAtLocation ( type ) ;
162
- for ( const property of privateType . getProperties ( ) ) {
163
- const propertyType = typeChecker . getTypeOfSymbolAtLocation ( property , type ) ;
164
- console . log ( "Name:" , property . name , "Type:" , typeChecker . typeToString ( propertyType ) ) ;
165
- let newType = typeChecker . typeToTypeNode ( propertyType , undefined , undefined ) ! ;
166
- additionalMembers . push ( ts . createPropertyAssignment ( property . name ,
167
-
168
- ts . createExpression newType ) ) ;
169
-
170
- //if (!currentMembers || !currentMembers.has(memberName)) {
171
- // additionalMembers.push(
172
- // ...property.declarations.map(d =>
173
- // visit(typeChecker, type, d)
174
- // )
175
- // );
176
- // }
177
- // if (privateType?.symbol?.members) {
178
- //
179
- // privateType.symbol.members!.forEach((definition, memberName) => {
180
- // if (!currentMembers || !currentMembers.has(memberName)) {
181
- // additionalMembers.push(
182
- // ...definition.declarations.slice(0,1).map(d =>
183
- // visit(typeChecker, type, d)
184
- // )
185
- // );
186
- // }
187
- // });
168
+ for ( const privateProperty of privateType . getProperties ( ) ) {
169
+ additionalMembers . push ( ...getPublicPropertyDeclarations ( typeChecker , privateProperty , type ) ) ;
188
170
}
189
171
}
190
172
}
191
173
}
192
-
193
- // for (const property of privateType.getProperties()) {
194
- // for (const declaration of property.declarations) {
195
- // if (ts.isPropertyDeclaration(declaration)) {
196
- //
197
- // const localType = typeChecker.typeToTypeNode(typeChecker.getTypeOfSymbolAtLocation(property, type), node, undefined);
198
- //
199
- // const updatedElement = ts.updateProperty(declaration, declaration.decorators, declaration.modifiers, declaration.name, declaration.questionToken || declaration.exclamationToken, localType, declaration.initializer);
200
- // additionalMembers.push(updatedElement);
201
- // }
202
- // }
203
- // }
174
+
204
175
if ( exportedTypes . length > 0 ) {
205
176
prunedHeritageClauses . push (
206
177
ts . updateHeritageClause ( heritageClause , exportedTypes )
@@ -233,6 +204,52 @@ function prunePrivateImports<
233
204
}
234
205
}
235
206
207
+
208
+ function getPublicPropertyDeclarations ( typeChecker : ts . TypeChecker , property : ts . Symbol , location : ts . Node ) : Array < ts . NamedDeclaration > {
209
+ const declaredType = typeChecker . getTypeOfSymbolAtLocation ( property , location ) ;
210
+ const resolvedType = typeChecker . typeToTypeNode ( declaredType , location , undefined ) ! ;
211
+
212
+ const declaration = property . declarations [ 0 ] ;
213
+ const optional = ! ! ( property . flags & ts . SymbolFlags . Optional ) ? ts . createToken ( ts . SyntaxKind . QuestionToken ) : undefined ;
214
+
215
+ // Based on https://github.com/microsoft/TypeScript/blob/8e9de9bed2e0170f7c43ef17b292cea29b46befb/src/services/codefixes/helpers.ts#L34
216
+
217
+ switch ( declaration . kind ) {
218
+ case ts . SyntaxKind . PropertySignature :
219
+ case ts . SyntaxKind . PropertyDeclaration :
220
+ const modifiers = declaration . modifiers ;
221
+ return [ ts . createProperty (
222
+ /*decorators*/ undefined ,
223
+ modifiers ,
224
+ property . name ,
225
+ optional ,
226
+ resolvedType ,
227
+ /*initializer*/ undefined ) ] ;
228
+ case ts . SyntaxKind . MethodSignature :
229
+ case ts . SyntaxKind . MethodDeclaration :
230
+ return declaredType . getCallSignatures ( ) . map ( callSignature => {
231
+ const methodDeclaration = typeChecker . signatureToSignatureDeclaration ( callSignature , ts . SyntaxKind . MethodDeclaration , location , undefined ) ! as ts . MethodDeclaration ;
232
+ return ts . createMethod (
233
+ methodDeclaration . decorators ,
234
+ methodDeclaration . modifiers ,
235
+ methodDeclaration . asteriskToken ,
236
+ property . name ,
237
+ optional ,
238
+ methodDeclaration . typeParameters ?. map ( t => {
239
+ return ts . createTypeParameterDeclaration ( t . name , t . constraint , t . default ) // Fix
240
+ } ) , // this should be newType
241
+ methodDeclaration . parameters ,
242
+ typeChecker . typeToTypeNode ( callSignature . getReturnType ( ) , undefined , undefined ) ,
243
+ /*block*/ undefined ) ;
244
+ } ) ;
245
+ case ts . SyntaxKind . GetAccessor :
246
+ case ts . SyntaxKind . SetAccessor :
247
+ // TODO: Port set and get accessor support from https://github.com/microsoft/TypeScript/blob/8e9de9bed2e0170f7c43ef17b292cea29b46befb/src/services/codefixes/helpers.ts#L34
248
+ throw new Error ( "get() and set() accessors are not supported yet by the Prune API script." ) ;
249
+ }
250
+
251
+ return [ ] ;
252
+ }
236
253
/**
237
254
* Replaces input types of public APIs that consume non-exported types, which
238
255
* allows us to exclude private types from the pruned definitions. Returns the
0 commit comments