@@ -20,6 +20,7 @@ import { LSAndTSDocResolver } from '../../../../src/plugins/typescript/LSAndTSDo
20
20
import { __resetCache } from '../../../../src/plugins/typescript/service' ;
21
21
import { pathToUrl } from '../../../../src/utils' ;
22
22
import { recursiveServiceWarmup } from '../test-utils' ;
23
+ import { DiagnosticCode } from '../../../../src/plugins/typescript/features/DiagnosticsProvider' ;
23
24
24
25
const testDir = path . join ( __dirname , '..' ) ;
25
26
const indent = ' ' . repeat ( 4 ) ;
@@ -64,7 +65,7 @@ describe('CodeActionsProvider', function () {
64
65
uri : pathToUrl ( filePath ) ,
65
66
text : harmonizeNewLines ( ts . sys . readFile ( filePath ) || '' )
66
67
} ) ;
67
- return { provider, document, docManager } ;
68
+ return { provider, document, docManager, lsAndTsDocResolver } ;
68
69
}
69
70
70
71
it ( 'provides quickfix' , async ( ) => {
@@ -661,6 +662,81 @@ describe('CodeActionsProvider', function () {
661
662
] ) ;
662
663
} ) ;
663
664
665
+ it ( 'provides quickfix for component import with "did you mean" diagnostics' , async ( ) => {
666
+ const { provider, document } = setup ( 'codeaction-component-import.svelte' ) ;
667
+
668
+ const codeActions = await provider . getCodeActions (
669
+ document ,
670
+ Range . create ( Position . create ( 4 , 1 ) , Position . create ( 4 , 6 ) ) ,
671
+ {
672
+ diagnostics : [
673
+ {
674
+ code : 2552 ,
675
+ message : "Cannot find name 'Empty'. Did you mean 'EMpty'?" ,
676
+ range : Range . create ( Position . create ( 4 , 1 ) , Position . create ( 4 , 6 ) ) ,
677
+ source : 'ts'
678
+ }
679
+ ] ,
680
+ only : [ CodeActionKind . QuickFix ]
681
+ }
682
+ ) ;
683
+
684
+ ( < TextDocumentEdit > codeActions [ 0 ] ?. edit ?. documentChanges ?. [ 0 ] ) ?. edits . forEach (
685
+ ( edit ) => ( edit . newText = harmonizeNewLines ( edit . newText ) )
686
+ ) ;
687
+
688
+ assert . deepStrictEqual ( codeActions , < CodeAction [ ] > [
689
+ {
690
+ edit : {
691
+ documentChanges : [
692
+ {
693
+ edits : [
694
+ {
695
+ newText : harmonizeNewLines (
696
+ `\n${ indent } import Empty from "../empty.svelte";\n`
697
+ ) ,
698
+ range : {
699
+ end : Position . create ( 0 , 18 ) ,
700
+ start : Position . create ( 0 , 18 )
701
+ }
702
+ }
703
+ ] ,
704
+ textDocument : {
705
+ uri : getUri ( 'codeaction-component-import.svelte' ) ,
706
+ version : null
707
+ }
708
+ }
709
+ ]
710
+ } ,
711
+ kind : 'quickfix' ,
712
+ title : 'Add import from "../empty.svelte"'
713
+ } ,
714
+ {
715
+ edit : {
716
+ documentChanges : [
717
+ {
718
+ edits : [
719
+ {
720
+ newText : 'EMpty' ,
721
+ range : {
722
+ end : Position . create ( 4 , 6 ) ,
723
+ start : Position . create ( 4 , 1 )
724
+ }
725
+ }
726
+ ] ,
727
+ textDocument : {
728
+ uri : getUri ( 'codeaction-component-import.svelte' ) ,
729
+ version : null
730
+ }
731
+ }
732
+ ]
733
+ } ,
734
+ kind : 'quickfix' ,
735
+ title : "Change spelling to 'EMpty'"
736
+ }
737
+ ] ) ;
738
+ } ) ;
739
+
664
740
it ( 'remove import inline with script tag' , async ( ) => {
665
741
const { provider, document } = setup ( 'remove-imports-inline.svelte' ) ;
666
742
@@ -863,13 +939,15 @@ describe('CodeActionsProvider', function () {
863
939
} ) ;
864
940
865
941
it ( 'provide quick fix to fix all missing import component' , async ( ) => {
866
- const { provider, document } = setup ( 'codeaction-custom-fix-all-component.svelte' ) ;
942
+ const { provider, document, docManager, lsAndTsDocResolver } = setup (
943
+ 'codeaction-custom-fix-all-component.svelte'
944
+ ) ;
867
945
868
946
const range = Range . create ( Position . create ( 4 , 1 ) , Position . create ( 4 , 15 ) ) ;
869
947
const codeActions = await provider . getCodeActions ( document , range , {
870
948
diagnostics : [
871
949
{
872
- code : 2304 ,
950
+ code : DiagnosticCode . CANNOT_FIND_NAME ,
873
951
message : "Cannot find name 'FixAllImported'." ,
874
952
range : range ,
875
953
source : 'ts'
@@ -912,6 +990,73 @@ describe('CodeActionsProvider', function () {
912
990
}
913
991
]
914
992
} ) ;
993
+
994
+ // fix-all has some "creative" workaround. Testing if it won't affect the document synchronization after applying the fix
995
+ docManager . updateDocument (
996
+ document ,
997
+ resolvedFixAll . edit . documentChanges [ 0 ] . edits . map ( ( edit ) => ( {
998
+ range : edit . range ,
999
+ text : edit . newText
1000
+ } ) )
1001
+ ) ;
1002
+
1003
+ const { lang, tsDoc } = await lsAndTsDocResolver . getLSAndTSDoc ( document ) ;
1004
+ const cannotFindNameDiagnostics = lang
1005
+ . getSemanticDiagnostics ( tsDoc . filePath )
1006
+ . filter ( ( diagnostic ) => diagnostic . code === DiagnosticCode . CANNOT_FIND_NAME ) ;
1007
+ assert . strictEqual ( cannotFindNameDiagnostics . length , 0 ) ;
1008
+ } ) ;
1009
+
1010
+ it ( 'provide quick fix to fix all missing import component with "did you mean" diagnostics' , async ( ) => {
1011
+ const { provider, document } = setup ( 'codeaction-custom-fix-all-component4.svelte' ) ;
1012
+
1013
+ const range = Range . create ( Position . create ( 4 , 1 ) , Position . create ( 4 , 15 ) ) ;
1014
+ const codeActions = await provider . getCodeActions ( document , range , {
1015
+ diagnostics : [
1016
+ {
1017
+ code : DiagnosticCode . CANNOT_FIND_NAME_X_DID_YOU_MEAN_Y ,
1018
+ message : "Cannot find name 'FixAllImported'. Did you mean 'FixAllImported3'?" ,
1019
+ range : range ,
1020
+ source : 'ts'
1021
+ }
1022
+ ] ,
1023
+ only : [ CodeActionKind . QuickFix ]
1024
+ } ) ;
1025
+
1026
+ const fixAll = codeActions . find ( ( action ) => action . data ) ;
1027
+ const resolvedFixAll = await provider . resolveCodeAction ( document , fixAll ! ) ;
1028
+
1029
+ ( < TextDocumentEdit > resolvedFixAll ?. edit ?. documentChanges ?. [ 0 ] ) ?. edits . forEach (
1030
+ ( edit ) => ( edit . newText = harmonizeNewLines ( edit . newText ) )
1031
+ ) ;
1032
+
1033
+ assert . deepStrictEqual ( resolvedFixAll . edit , {
1034
+ documentChanges : [
1035
+ {
1036
+ edits : [
1037
+ {
1038
+ newText :
1039
+ `\n${ indent } import FixAllImported from \"./importing/FixAllImported.svelte\";\n` +
1040
+ `${ indent } import FixAllImported2 from \"./importing/FixAllImported2.svelte\";\n` ,
1041
+ range : {
1042
+ start : {
1043
+ character : 18 ,
1044
+ line : 0
1045
+ } ,
1046
+ end : {
1047
+ character : 18 ,
1048
+ line : 0
1049
+ }
1050
+ }
1051
+ }
1052
+ ] ,
1053
+ textDocument : {
1054
+ uri : getUri ( 'codeaction-custom-fix-all-component4.svelte' ) ,
1055
+ version : null
1056
+ }
1057
+ }
1058
+ ]
1059
+ } ) ;
915
1060
} ) ;
916
1061
917
1062
it ( 'provide quick fix to fix all missing import component without duplicate (script)' , async ( ) => {
0 commit comments