@@ -204,7 +204,8 @@ function addCodeAction(
204
204
fixes : CodeFixAction [ ] ,
205
205
context : CodeFixContext | CodeFixAllContext ,
206
206
typePrintMode : TypePrintMode ,
207
- cb : ( fixer : Fixer ) => DiagnosticOrDiagnosticAndArguments | undefined ) {
207
+ cb : ( fixer : Fixer ) => DiagnosticOrDiagnosticAndArguments | undefined ,
208
+ ) {
208
209
const changes = withContext ( context , typePrintMode , cb ) ;
209
210
if ( changes . result && changes . textChanges . length ) {
210
211
fixes . push ( createCodeFixAction (
@@ -236,7 +237,7 @@ function withContext<T>(
236
237
const fixedNodes = new Set < Node > ( ) ;
237
238
const expandoPropertiesAdded = new Set < Node > ( ) ;
238
239
const typePrinter = createPrinter ( {
239
- preserveSourceNewlines : false ,
240
+ preserveSourceNewlines : false ,
240
241
} ) ;
241
242
242
243
const result = cb ( { addTypeAnnotation, addInlineAssertion, extractAsVariable } ) ;
@@ -318,10 +319,10 @@ function withContext<T>(
318
319
}
319
320
320
321
function createSatisfiesAsExpression ( node : Expression , type : TypeNode ) {
321
- if ( needsParenthesizedExpressionForAssertion ( node ) ) {
322
- node = factory . createParenthesizedExpression ( node ) ;
323
- }
324
- return factory . createAsExpression ( factory . createSatisfiesExpression ( node , getSynthesizedDeepClone ( type ) ) , type ) ;
322
+ if ( needsParenthesizedExpressionForAssertion ( node ) ) {
323
+ node = factory . createParenthesizedExpression ( node ) ;
324
+ }
325
+ return factory . createAsExpression ( factory . createSatisfiesExpression ( node , getSynthesizedDeepClone ( type ) ) , type ) ;
325
326
}
326
327
327
328
function addInlineAssertion ( span : TextSpan ) : DiagnosticOrDiagnosticAndArguments | undefined {
@@ -574,22 +575,22 @@ function withContext<T>(
574
575
case SyntaxKind . Parameter :
575
576
case SyntaxKind . PropertyDeclaration :
576
577
case SyntaxKind . VariableDeclaration :
577
- return addTypeToVariableLike ( node as ParameterDeclaration | PropertyDeclaration | VariableDeclaration ) ;
578
+ return addTypeToVariableLike ( node as ParameterDeclaration | PropertyDeclaration | VariableDeclaration ) ;
578
579
case SyntaxKind . ArrowFunction :
579
580
case SyntaxKind . FunctionExpression :
580
581
case SyntaxKind . FunctionDeclaration :
581
582
case SyntaxKind . MethodDeclaration :
582
583
case SyntaxKind . GetAccessor :
583
- return addTypeToSignatureDeclaration ( node as SignatureDeclaration , sourceFile ) ;
584
+ return addTypeToSignatureDeclaration ( node as SignatureDeclaration , sourceFile ) ;
584
585
case SyntaxKind . ExportAssignment :
585
- return transformExportAssignment ( node as ExportAssignment ) ;
586
+ return transformExportAssignment ( node as ExportAssignment ) ;
586
587
case SyntaxKind . ClassDeclaration :
587
- return transformExtendsClauseWithExpression ( node as ClassDeclaration ) ;
588
+ return transformExtendsClauseWithExpression ( node as ClassDeclaration ) ;
588
589
case SyntaxKind . ObjectBindingPattern :
589
590
case SyntaxKind . ArrayBindingPattern :
590
- return transformDestructuringPatterns ( node as BindingPattern ) ;
591
+ return transformDestructuringPatterns ( node as BindingPattern ) ;
591
592
default :
592
- throw new Error ( `Cannot find a fix for the given node ${ node . kind } ` ) ;
593
+ throw new Error ( `Cannot find a fix for the given node ${ node . kind } ` ) ;
593
594
}
594
595
}
595
596
@@ -609,30 +610,30 @@ function withContext<T>(
609
610
}
610
611
611
612
function transformExportAssignment ( defaultExport : ExportAssignment ) : DiagnosticOrDiagnosticAndArguments | undefined {
612
- if ( defaultExport . isExportEquals ) {
613
- return ;
614
- }
615
-
616
- const { typeNode } = inferType ( defaultExport . expression ) ;
617
- if ( ! typeNode ) return undefined ;
618
- changeTracker . replaceNodeWithNodes ( sourceFile , defaultExport , [
619
- factory . createVariableStatement (
620
- /*modifiers*/ undefined ,
621
- factory . createVariableDeclarationList (
622
- [ factory . createVariableDeclaration (
623
- "__default" ,
624
- /*exclamationToken*/ undefined ,
625
- typeNode ,
626
- defaultExport . expression ,
627
- ) ] ,
628
- NodeFlags . Const ,
629
- ) ,
630
- ) ,
631
- factory . updateExportAssignment ( defaultExport , defaultExport ?. modifiers , factory . createIdentifier ( "__default" ) ) ,
632
- ] ) ;
633
- return [
634
- Diagnostics . Extract_default_export_to_variable ,
635
- ] ;
613
+ if ( defaultExport . isExportEquals ) {
614
+ return ;
615
+ }
616
+
617
+ const { typeNode } = inferType ( defaultExport . expression ) ;
618
+ if ( ! typeNode ) return undefined ;
619
+ changeTracker . replaceNodeWithNodes ( sourceFile , defaultExport , [
620
+ factory . createVariableStatement (
621
+ /*modifiers*/ undefined ,
622
+ factory . createVariableDeclarationList (
623
+ [ factory . createVariableDeclaration (
624
+ "__default" ,
625
+ /*exclamationToken*/ undefined ,
626
+ typeNode ,
627
+ defaultExport . expression ,
628
+ ) ] ,
629
+ NodeFlags . Const ,
630
+ ) ,
631
+ ) ,
632
+ factory . updateExportAssignment ( defaultExport , defaultExport ?. modifiers , factory . createIdentifier ( "__default" ) ) ,
633
+ ] ) ;
634
+ return [
635
+ Diagnostics . Extract_default_export_to_variable ,
636
+ ] ;
636
637
}
637
638
638
639
/**
@@ -660,7 +661,7 @@ function withContext<T>(
660
661
/*modifiers*/ undefined ,
661
662
factory . createVariableDeclarationList (
662
663
[ factory . createVariableDeclaration (
663
- baseClassName ,
664
+ baseClassName ,
664
665
/*exclamationToken*/ undefined ,
665
666
heritageTypeNode ,
666
667
heritageExpression . expression ,
@@ -915,7 +916,7 @@ function withContext<T>(
915
916
916
917
function inferType ( node : Node ) : InferenceResult {
917
918
if ( typePrintMode !== TypePrintMode . FULL ) {
918
- return relativeType ( node ) ;
919
+ return relativeType ( node ) ;
919
920
}
920
921
921
922
let type = isValueSignatureDeclaration ( node ) ?
@@ -1123,7 +1124,7 @@ function withContext<T>(
1123
1124
}
1124
1125
}
1125
1126
1126
- function addTypeToVariableLike ( decl : ParameterDeclaration | VariableDeclaration | PropertyDeclaration ) : DiagnosticOrDiagnosticAndArguments | undefined {
1127
+ function addTypeToVariableLike ( decl : ParameterDeclaration | VariableDeclaration | PropertyDeclaration ) : DiagnosticOrDiagnosticAndArguments | undefined {
1127
1128
const { typeNode } = inferType ( decl ) ;
1128
1129
if ( typeNode ) {
1129
1130
if ( decl . type ) {
@@ -1147,25 +1148,24 @@ function withContext<T>(
1147
1148
}
1148
1149
}
1149
1150
1150
-
1151
1151
// Some --isolatedDeclarations errors are not present on the node that directly needs type annotation, so look in the
1152
1152
// ancestors to look for node that needs type annotation. This function can return undefined if the AST is ill-formed.
1153
1153
function findAncestorWithMissingType ( node : Node ) : Node | undefined {
1154
- return findAncestor ( node , ( n ) => {
1155
- return canHaveTypeAnnotation . has ( n . kind ) &&
1156
- ( ( ! isObjectBindingPattern ( n ) && ! isArrayBindingPattern ( n ) ) || isVariableDeclaration ( n . parent ) ) ;
1157
- } ) ;
1154
+ return findAncestor ( node , n => {
1155
+ return canHaveTypeAnnotation . has ( n . kind ) &&
1156
+ ( ( ! isObjectBindingPattern ( n ) && ! isArrayBindingPattern ( n ) ) || isVariableDeclaration ( n . parent ) ) ;
1157
+ } ) ;
1158
1158
}
1159
1159
1160
1160
function findBestFittingNode ( node : Node , span : TextSpan ) {
1161
- while ( node && node . end < span . start + span . length ) {
1162
- node = node . parent ;
1163
- }
1164
- while ( node . parent . pos === node . pos && node . parent . end === node . end ) {
1165
- node = node . parent ;
1166
- }
1167
- if ( isIdentifier ( node ) && hasInitializer ( node . parent ) && node . parent . initializer ) {
1168
- return node . parent . initializer ;
1169
- }
1170
- return node ;
1161
+ while ( node && node . end < span . start + span . length ) {
1162
+ node = node . parent ;
1163
+ }
1164
+ while ( node . parent . pos === node . pos && node . parent . end === node . end ) {
1165
+ node = node . parent ;
1166
+ }
1167
+ if ( isIdentifier ( node ) && hasInitializer ( node . parent ) && node . parent . initializer ) {
1168
+ return node . parent . initializer ;
1169
+ }
1170
+ return node ;
1171
1171
}
0 commit comments