@@ -233,6 +233,10 @@ namespace ts.refactor.convertParamsToDestructuredObject {
233
233
function getFunctionDeclarationAtPosition ( file : SourceFile , startPosition : number , checker : TypeChecker ) : ValidFunctionDeclaration | undefined {
234
234
const node = getTouchingToken ( file , startPosition ) ;
235
235
const functionDeclaration = getContainingFunction ( node ) ;
236
+
237
+ // don't offer refactor on top-level JSDoc
238
+ if ( isTopLevelJSDoc ( node ) ) return undefined ;
239
+
236
240
if ( functionDeclaration
237
241
&& isValidFunctionDeclaration ( functionDeclaration , checker )
238
242
&& rangeContainsRange ( functionDeclaration , node )
@@ -241,6 +245,15 @@ namespace ts.refactor.convertParamsToDestructuredObject {
241
245
return undefined ;
242
246
}
243
247
248
+ function isTopLevelJSDoc ( node : Node ) : boolean {
249
+ const containingJSDoc = findAncestor ( node , isJSDocNode ) ;
250
+ if ( containingJSDoc ) {
251
+ const containingNonJSDoc = findAncestor ( containingJSDoc , n => ! isJSDocNode ( n ) ) ;
252
+ return ! ! containingNonJSDoc && isFunctionLikeDeclaration ( containingNonJSDoc ) ;
253
+ }
254
+ return false ;
255
+ }
256
+
244
257
function isValidFunctionDeclaration (
245
258
functionDeclaration : SignatureDeclaration ,
246
259
checker : TypeChecker ) : functionDeclaration is ValidFunctionDeclaration {
@@ -308,13 +321,23 @@ namespace ts.refactor.convertParamsToDestructuredObject {
308
321
return parameters ;
309
322
}
310
323
324
+ function createPropertyOrShorthandAssignment ( name : string , initializer : Expression ) : PropertyAssignment | ShorthandPropertyAssignment {
325
+ if ( isIdentifier ( initializer ) && getTextOfIdentifierOrLiteral ( initializer ) === name ) {
326
+ return createShorthandPropertyAssignment ( name ) ;
327
+ }
328
+ return createPropertyAssignment ( name , initializer ) ;
329
+ }
330
+
311
331
function createNewArgument ( functionDeclaration : ValidFunctionDeclaration , functionArguments : NodeArray < Expression > ) : ObjectLiteralExpression {
312
332
const parameters = getRefactorableParameters ( functionDeclaration . parameters ) ;
313
333
const hasRestParameter = isRestParameter ( last ( parameters ) ) ;
314
334
const nonRestArguments = hasRestParameter ? functionArguments . slice ( 0 , parameters . length - 1 ) : functionArguments ;
315
335
const properties = map ( nonRestArguments , ( arg , i ) => {
316
- const property = createPropertyAssignment ( getParameterName ( parameters [ i ] ) , arg ) ;
317
- suppressLeadingAndTrailingTrivia ( property . initializer ) ;
336
+ const parameterName = getParameterName ( parameters [ i ] ) ;
337
+ const property = createPropertyOrShorthandAssignment ( parameterName , arg ) ;
338
+
339
+ suppressLeadingAndTrailingTrivia ( property . name ) ;
340
+ if ( isPropertyAssignment ( property ) ) suppressLeadingAndTrailingTrivia ( property . initializer ) ;
318
341
copyComments ( arg , property ) ;
319
342
return property ;
320
343
} ) ;
0 commit comments