-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathCodeActionsProcessor.spec.ts
More file actions
1270 lines (1156 loc) · 53.9 KB
/
Copy pathCodeActionsProcessor.spec.ts
File metadata and controls
1270 lines (1156 loc) · 53.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { expect } from '../../chai-config.spec';
import { URI } from 'vscode-uri';
import type { CodeAction, Range } from 'vscode-languageserver';
import type { BscFile, OnGetCodeActionsEvent } from '../../interfaces';
import { Program } from '../../Program';
import { expectCodeActions, trim } from '../../testHelpers.spec';
import { standardizePath as s, util } from '../../util';
import { rootDir } from '../../testHelpers.spec';
import { CodeActionsProcessor } from './CodeActionsProcessor';
describe('CodeActionsProcessor', () => {
let program: Program;
beforeEach(() => {
program = new Program({
rootDir: rootDir,
autoImportComponentScript: true
});
});
afterEach(() => {
program.dispose();
});
/**
* Helper function for testing code actions. By default it filters out the generic
* `Disable {code} for this line/file` quick fixes because every diagnostic gets them
* and the per-feature tests below only care about feature-specific actions.
*/
function testGetCodeActions(file: BscFile | string, range: Range, expected: string[], options?: { includeDisableDirectives?: boolean }) {
program.validate();
const titles = program.getCodeActions(
typeof file === 'string' ? file : file.srcPath,
range
).map(x => x.title);
expect(filterDisableActions(titles, options).sort()).to.eql(expected);
}
/**
* Drops the generic `Disable {code} for this line/file` titles from a list so feature-level
* assertions can stay focused on their own actions.
*/
function filterDisableActions(titles: string[], options?: { includeDisableDirectives?: boolean }) {
if (options?.includeDisableDirectives) {
return titles;
}
return titles.filter(t => !/^Disable .+ for this (line|file)/.test(t));
}
describe('getCodeActions', () => {
it('suggests `extends=Group`', () => {
const file = program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="comp1">
</component>
`);
program.validate();
expectCodeActions(() => {
program.getCodeActions(
file.srcPath,
//<comp|onent name="comp1">
util.createRange(1, 5, 1, 5)
);
}, [{
title: `Extend "Group"`,
isPreferred: true,
kind: 'quickfix',
changes: [{
filePath: s`${rootDir}/components/comp1.xml`,
newText: ' extends="Group"',
type: 'insert',
//<component name="comp1"|>
position: util.createPosition(1, 23)
}]
}, {
title: `Extend "Task"`,
kind: 'quickfix',
changes: [{
filePath: s`${rootDir}/components/comp1.xml`,
newText: ' extends="Task"',
type: 'insert',
//<component name="comp1"|>
position: util.createPosition(1, 23)
}]
}, {
title: `Extend "ContentNode"`,
kind: 'quickfix',
changes: [{
filePath: s`${rootDir}/components/comp1.xml`,
newText: ' extends="ContentNode"',
type: 'insert',
//<component name="comp1"|>
position: util.createPosition(1, 23)
}]
}]);
});
it('adds attribute at end of component with multiple attributes`', () => {
const file = program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="comp1" attr2="attr3" attr3="attr3">
</component>
`);
program.validate();
const codeActions = program.getCodeActions(
file.srcPath,
//<comp|onent name="comp1">
util.createRange(1, 5, 1, 5)
);
expect(
codeActions[0].edit!.changes![URI.file(s`${rootDir}/components/comp1.xml`).toString()][0].range
).to.eql(
util.createRange(1, 51, 1, 51)
);
});
it('does not produce duplicate code actions for bs imports', () => {
//define the function in two files
program.setFile('components/lib1.brs', `
sub doSomething()
end sub
`);
program.setFile('components/lib2.brs', `
sub doSomething()
end sub
`);
//use the function in this file
const componentCommonFile = program.setFile('components/ComponentCommon.bs', `
sub init()
doSomething()
end sub
`);
//import the file in two scopes
program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="ChildScene">
<script uri="ComponentCommon.bs" />
</component>
`);
program.setFile('components/comp2.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="ChildScene">
<script uri="ComponentCommon.bs" />
</component>
`);
program.validate();
//we should only get each file import suggestion exactly once
const codeActions = program.getCodeActions(
componentCommonFile.srcPath,
// doSome|thing()
util.createRange(2, 22, 2, 22)
);
expect(filterDisableActions(codeActions.map(x => x.title)).sort()).to.eql([
`import "pkg:/components/lib1.brs"`,
`import "pkg:/components/lib2.brs"`
]);
});
it('does not suggest imports for brs files', () => {
//import the file in two scopes
program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="ChildScene">
<script uri="comp1.brs" />
</component>
`);
//import the function here
const file = program.setFile('components/comp1.brs', `
sub init()
DoSomething()
end sub
`);
//define the function here
program.setFile('source/lib.brs', `
sub DoSomething()
end sub
`);
program.validate();
//there should be no import code actions since this is a brs file (disable directives still appear for any diagnostic)
const codeActions = program.getCodeActions(
file.srcPath,
// DoSometh|ing()
util.createRange(2, 28, 2, 28)
);
expect(filterDisableActions(codeActions.map(x => x.title))).to.be.empty;
});
it('suggests class imports', () => {
//import the file in two scopes
program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="ChildScene">
<script uri="comp1.bs" />
</component>
`);
const file = program.setFile('components/comp1.bs', `
sub init()
dude = new Person()
end sub
`);
program.setFile('source/Person.bs', `
class Person
end class
`);
program.validate();
expect(
filterDisableActions(program.getCodeActions(
file.srcPath,
// new Per|son()
util.createRange(2, 34, 2, 34)
).map(x => x.title)).sort()
).to.eql([
`import "pkg:/source/Person.bs"`
]);
});
it('suggests class imports', () => {
//import the file in two scopes
program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="ChildScene">
<script uri="comp1.bs" />
</component>
`);
//import the function here
const file = program.setFile('components/comp1.bs', `
sub init()
kitty = new Animals.Cat()
end sub
`);
program.setFile('source/Animals.bs', `
namespace Animals
class Cat
end class
end namespace
`);
program.validate();
expect(
filterDisableActions(program.getCodeActions(
file.srcPath,
// new Anim|als.Cat()
util.createRange(2, 36, 2, 36)
).map(x => x.title)).sort()
).to.eql([
`import "pkg:/source/Animals.bs"`
]);
});
it('suggests all files for a root namespace name', () => {
program.setFile('source/first.bs', `
namespace alpha
function firstAction()
end function
end namespace
`);
program.setFile('source/second.bs', `
namespace alpha.beta
function secondAction()
end function
end namespace
`);
program.setFile('components/MainScene.xml', trim`<component name="MainScene"></component>`);
const file = program.setFile('components/MainScene.bs', `
sub init()
print alpha.secondAction()
end sub
`);
// print al|pha.secondAction()
testGetCodeActions(file, util.createRange(2, 28, 2, 28), [
`import "pkg:/source/first.bs"`,
`import "pkg:/source/second.bs"`
]);
});
it('clears suggestedImports after process() so the same instance could be reused in the future', () => {
program.setFile('source/lib.bs', `
function doSomething()
end function
`);
program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="comp1" extends="Scene">
<script uri="comp1.bs" />
</component>
`);
const file = program.setFile('components/comp1.bs', `
sub init()
doSomething()
end sub
`);
program.validate();
const range = util.createRange(2, 24, 2, 24);
const event: OnGetCodeActionsEvent = {
program: program,
file: file,
range: range,
scopes: program.getScopesForFile(file),
diagnostics: program.getDiagnostics().filter(d => d.file === file && util.rangesIntersectOrTouch(d.range, range)),
codeActions: []
};
const processor = new CodeActionsProcessor(event);
processor.process();
const firstCallTitles = event.codeActions.map(x => x.title).sort();
// Reset codeActions and call process() again on the same instance.
// If suggestedImports were NOT cleared, the second call would return no import suggestions.
event.codeActions = [];
processor.process();
const secondCallTitles = event.codeActions.map(x => x.title).sort();
expect(secondCallTitles).to.eql(firstCallTitles);
});
it('suggests files for second part of missing namespace', () => {
program.setFile('source/first.bs', `
namespace alpha
function firstAction()
end function
end namespace
`);
program.setFile('source/second.bs', `
namespace alpha.beta
function secondAction()
end function
end namespace
`);
program.setFile('components/MainScene.xml', trim`<component name="MainScene"></component>`);
const file = program.setFile('components/MainScene.bs', `
import "pkg:/source/first.bs"
sub init()
print alpha.beta.secondAction()
end sub
`);
// print alpha.be|ta.secondAction()
testGetCodeActions(file, util.createRange(3, 34, 3, 34), [`import "pkg:/source/second.bs"`]);
});
});
describe('Fix all: Auto fixable missing imports', () => {
it('offers fix-all when multiple unambiguous imports are missing', () => {
program.setFile('source/lib1.bs', `
function doSomething()
end function
`);
program.setFile('source/lib2.bs', `
function doSomethingElse()
end function
`);
program.setFile('components/MainScene.xml', trim`<component name="MainScene"></component>`);
const file = program.setFile('components/MainScene.bs', `
sub init()
doSomething()
doSomethingElse()
end sub
`);
// doSome|thing()
testGetCodeActions(file, util.createRange(2, 26, 2, 26), [
`Fix all: Auto fixable missing imports`,
`import "pkg:/source/lib1.bs"`
]);
});
it('does not offer fix-all when only one import is missing', () => {
program.setFile('source/lib1.bs', `
function doSomething()
end function
`);
program.setFile('components/MainScene.xml', trim`<component name="MainScene"></component>`);
const file = program.setFile('components/MainScene.bs', `
sub init()
doSomething()
end sub
`);
// doSome|thing()
testGetCodeActions(file, util.createRange(2, 26, 2, 26), [
`import "pkg:/source/lib1.bs"`
]);
});
it('excludes ambiguous names from fix-all', () => {
program.setFile('source/lib1.bs', `
function doSomething()
end function
`);
program.setFile('source/lib2.bs', `
function doSomething()
end function
`);
program.setFile('source/lib3.bs', `
function doSomethingUnambiguous()
end function
`);
program.setFile('components/MainScene.xml', trim`<component name="MainScene"></component>`);
const file = program.setFile('components/MainScene.bs', `
sub init()
doSomething()
doSomethingUnambiguous()
end sub
`);
program.validate();
const actions = program.getCodeActions(file.srcPath, util.createRange(2, 26, 2, 26));
// fix-all only has the unambiguous import, not doSomething (which has 2 options)
const fixAll = actions.find(a => a.title === 'Fix all: Auto fixable missing imports');
expect(fixAll).to.be.undefined;
});
it('includes class imports in fix-all', () => {
program.setFile('source/lib1.bs', `
function doSomething()
end function
`);
program.setFile('source/MyClass.bs', `
class MyClass
end class
`);
program.setFile('components/MainScene.xml', trim`<component name="MainScene"></component>`);
const file = program.setFile('components/MainScene.bs', `
sub init()
doSomething()
obj = new MyClass()
end sub
`);
// doSome|thing()
testGetCodeActions(file, util.createRange(2, 26, 2, 26), [
`Fix all: Auto fixable missing imports`,
`import "pkg:/source/lib1.bs"`
]);
});
it('deduplicates when multiple missing names resolve to the same file', () => {
program.setFile('source/lib1.bs', `
function doSomething()
end function
function doSomethingElse()
end function
`);
program.setFile('source/lib2.bs', `
function doThird()
end function
`);
program.setFile('components/MainScene.xml', trim`<component name="MainScene"></component>`);
const file = program.setFile('components/MainScene.bs', `
sub init()
doSomething()
doSomethingElse()
doThird()
end sub
`);
program.validate();
const actions = program.getCodeActions(file.srcPath, util.createRange(2, 26, 2, 26));
const fixAll = actions.find(a => a.title === 'Fix all: Auto fixable missing imports');
// lib1.bs and lib2.bs — only 2 changes despite 3 missing names
expect(Object.values(fixAll!.edit!.changes!)[0]).to.have.lengthOf(2);
});
});
describe('named argument ordering', () => {
it('offers quick fix for out-of-order named arguments', () => {
const file = program.setFile('source/main.bs', `
sub greet(name as string, excited as boolean)
end sub
sub main()
greet(excited: true, name: "Bob")
end sub
`);
testGetCodeActions(file, util.createRange(5, 41, 5, 41), [
'Reorder named arguments to match function declaration'
]);
});
it('reorders mixed positional and named args to declaration order', () => {
const file = program.setFile('source/main.bs', `
sub greet(name as string, title = "Mr" as string, excited = false as boolean)
end sub
sub main()
greet("Bob", excited: true, title: "Dr")
end sub
`);
program.validate();
const actions = program.getCodeActions(file.srcPath, util.createRange(5, 50, 5, 50));
const action = actions.find(x => x.title === 'Reorder named arguments to match function declaration');
const edit = Object.values(action.edit.changes)[0][0];
expect(edit.newText).to.equal(`"Bob", title: "Dr", excited: true`);
});
});
it('suggests imports at very start and very end of diagnostic', () => {
program.setFile('source/first.bs', `
namespace alpha
function firstAction()
end function
end namespace
`);
program.setFile('components/MainScene.xml', trim`<component name="MainScene"></component>`);
const file = program.setFile('components/MainScene.bs', `
sub init()
print alpha.firstAction()
end sub
`);
// print |alpha.firstAction()
testGetCodeActions(file, util.createRange(2, 22, 2, 22), [`import "pkg:/source/first.bs"`]);
// print alpha|.firstAction()
testGetCodeActions(file, util.createRange(2, 27, 2, 27), [`import "pkg:/source/first.bs"`]);
});
describe('void function return value', () => {
it('suggests deleting the return value and converting the sub to a function', () => {
const file = program.setFile('source/main.brs', `
sub test()
'should not have a return value here...
return true
end sub
`);
// return tr|ue
testGetCodeActions(file, util.createRange(3, 29, 3, 29), [`Convert sub to function`, `Remove return value`]);
});
it('suggests converting sub to function with inline body', () => {
const file = program.setFile('source/main.brs', `
sub test() : print "onItemContentChange"
return true
end sub
`);
// return tr|ue
testGetCodeActions(file, util.createRange(2, 28, 2, 28), [`Convert sub to function`, `Remove return value`]);
// verify only the `sub` and `end sub` keywords are replaced, not the `: print ...` inline body
program.validate();
const actions = program.getCodeActions(file.srcPath, util.createRange(2, 28, 2, 28));
const convertAction = actions.find(a => a.title === 'Convert sub to function');
const changes = Object.values(convertAction!.edit!.changes!)[0];
// change[0] replaces `sub` keyword only
expect(changes[0].range).to.eql(util.createRange(1, 16, 1, 19));
expect(changes[0].newText).to.eql('function');
// change[1] replaces `end sub` keyword only
expect(changes[1].range).to.eql(util.createRange(3, 16, 3, 23));
expect(changes[1].newText).to.eql('end function');
});
it('suggests deleting the return type from void function', () => {
const file = program.setFile('source/main.brs', `
function test() as void
'should not have a return value here...
return true
end function
`);
// return tr|ue
testGetCodeActions(file, util.createRange(3, 29, 3, 29), [`Remove return type from function declaration`, `Remove return value`]);
});
it('suggests deleting only the return type from void function with inline body', () => {
const file = program.setFile('source/main.brs', `
function test() as void : print "onItemContentChange"
return "test"
end function
`);
// return |"test"
testGetCodeActions(file, util.createRange(2, 28, 2, 28), [`Remove return type from function declaration`, `Remove return value`]);
// verify only ` as void` is deleted, not the `: print ...` inline body
program.validate();
const actions = program.getCodeActions(file.srcPath, util.createRange(2, 28, 2, 28));
const removeTypeAction = actions.find(a => a.title === 'Remove return type from function declaration');
const changes = Object.values(removeTypeAction!.edit!.changes!)[0];
// range should span ` as void` only, starting after `)` and ending at the close of `void`
expect(changes[0].range).to.eql(util.createRange(1, 31, 1, 39));
});
it('offers fix-all when multiple void-return violations exist in the file', () => {
const file = program.setFile('source/main.brs', `
sub test1()
return true
end sub
sub test2()
return false
end sub
`);
// return tr|ue (first violation)
testGetCodeActions(file, util.createRange(2, 27, 2, 27), [
`Convert sub to function`,
`Fix all: Remove void return values`,
`Remove return value`
]);
});
it('does not offer fix-all when only one void-return violation exists', () => {
const file = program.setFile('source/main.brs', `
sub test1()
return true
end sub
sub test2()
end sub
`);
// return tr|ue
testGetCodeActions(file, util.createRange(2, 27, 2, 27), [`Convert sub to function`, `Remove return value`]);
});
it('does not duplicate fix-all when multiple violations are at the cursor range', () => {
const file = program.setFile('source/main.brs', `
sub test1()
return true
end sub
sub test2()
return false
end sub
`);
program.validate();
const actions = program.getCodeActions(file.srcPath, util.createRange(2, 20, 4, 20));
expect(actions.filter(a => a.title === 'Fix all: Remove void return values')).to.have.lengthOf(1);
});
});
describe('typed function/sub empty return', () => {
it('suggests deleting the return value and converting the sub to a function', () => {
const file = program.setFile('source/main.brs', `
function test()
'need a return value here...
return
end function
`);
// ret|urn
testGetCodeActions(file, util.createRange(3, 23, 3, 23), [`Add void return type to function declaration`, `Convert function to sub`]);
});
it('suggests adding void return type to function with inline body', () => {
const file = program.setFile('source/main.brs', `
function test() : print "onItemContentChange"
return
end function
`);
// ret|urn
testGetCodeActions(file, util.createRange(2, 23, 2, 23), [`Add void return type to function declaration`, `Convert function to sub`]);
// verify ` as void` is inserted after `)`, not after the inline body
program.validate();
const actions = program.getCodeActions(file.srcPath, util.createRange(2, 23, 2, 23));
const addVoidAction = actions.find(a => a.title === 'Add void return type to function declaration');
const changes = Object.values(addVoidAction!.edit!.changes!)[0];
// insert position should be immediately after `)`, before ` : print ...`
expect(changes[0].range).to.eql(util.createRange(1, 31, 1, 31));
expect(changes[0].newText).to.eql(' as void');
});
it('suggests deleting the return type from void function', () => {
const file = program.setFile('source/main.brs', `
sub test() as integer
'need a return value here...
return
end sub
`);
// ret|urn
testGetCodeActions(file, util.createRange(3, 23, 3, 23), [`Remove return type from sub declaration`]);
});
it('suggests deleting only the return type from sub with inline body', () => {
const file = program.setFile('source/main.brs', `
sub test() as integer : print "onItemContentChange"
return
end sub
`);
// ret|urn
testGetCodeActions(file, util.createRange(2, 23, 2, 23), [`Remove return type from sub declaration`]);
// verify only ` as integer` is deleted, not the `: print ...` inline body
program.validate();
const actions = program.getCodeActions(file.srcPath, util.createRange(2, 23, 2, 23));
const removeTypeAction = actions.find(a => a.title === 'Remove return type from sub declaration');
const changes = Object.values(removeTypeAction!.edit!.changes!)[0];
// range should span ` as integer` only, starting after `)` and ending at the close of `integer`
expect(changes[0].range).to.eql(util.createRange(1, 26, 1, 37));
});
it('offers fix-all for multiple subs with return types', () => {
const file = program.setFile('source/main.brs', `
sub test1() as integer
return
end sub
sub test2() as string
return
end sub
`);
// ret|urn (first violation)
testGetCodeActions(file, util.createRange(2, 24, 2, 24), [
`Fix all: Remove return type from sub declarations`,
`Remove return type from sub declaration`
]);
});
it('offers fix-all for multiple functions with missing return types', () => {
const file = program.setFile('source/main.brs', `
function test1()
return
end function
function test2()
return
end function
`);
// ret|urn (first violation)
testGetCodeActions(file, util.createRange(2, 24, 2, 24), [
`Add void return type to function declaration`,
`Convert function to sub`,
`Fix all: Add void return type to function declarations`
]);
});
it('does not offer fix-all when only one non-void-return violation exists', () => {
const file = program.setFile('source/main.brs', `
sub test1() as integer
return
end sub
`);
// ret|urn
testGetCodeActions(file, util.createRange(2, 24, 2, 24), [`Remove return type from sub declaration`]);
});
it('deduplicates fix-all changes when one function has multiple bare returns', () => {
const file = program.setFile('source/main.brs', `
sub test1() as integer
return
return
end sub
sub test2() as string
return
end sub
`);
program.validate();
const actions = program.getCodeActions(file.srcPath, util.createRange(2, 24, 2, 24));
const fixAll = actions.find(a => a.title === 'Fix all: Remove return type from sub declarations');
// Two unique functions → two changes (not three)
expect(Object.values(fixAll!.edit!.changes!)[0]).to.have.lengthOf(2);
});
});
describe('referencedFileDoesNotExist', () => {
it('offers to remove the script import line', () => {
const file = program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="comp1" extends="Scene">
<script type="text/brightscript" uri="pkg:/source/missing.brs" />
</component>
`);
// uri="pkg:/source/missing.brs" is on line 2
testGetCodeActions(file, util.createRange(2, 50, 2, 50), ['Remove script import']);
});
it('deletes the entire script tag line', () => {
const file = program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="comp1" extends="Scene">
<script type="text/brightscript" uri="pkg:/source/missing.brs" />
</component>
`);
program.validate();
expectCodeActions(() => {
program.getCodeActions(file.srcPath, util.createRange(2, 50, 2, 50));
}, [{
title: 'Remove script import',
kind: 'quickfix',
changes: [{
type: 'delete',
filePath: s`${rootDir}/components/comp1.xml`,
range: util.createRange(2, 0, 3, 0)
}]
}]);
});
it('offers fix-all when multiple missing imports exist', () => {
const file = program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="comp1" extends="Scene">
<script type="text/brightscript" uri="pkg:/source/missing1.brs" />
<script type="text/brightscript" uri="pkg:/source/missing2.brs" />
</component>
`);
testGetCodeActions(file, util.createRange(2, 50, 2, 50), [
'Fix all: Remove script imports',
'Remove script import'
]);
});
});
describe('unnecessaryScriptImportInChildFromParent', () => {
it('offers to remove the redundant script import', () => {
program.setFile('components/parent.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="ParentScene" extends="Scene">
<script type="text/brightscript" uri="pkg:/source/lib.brs" />
</component>
`);
program.setFile('source/lib.brs', '');
const file = program.setFile('components/child.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="ChildScene" extends="ParentScene">
<script type="text/brightscript" uri="pkg:/source/lib.brs" />
</component>
`);
// uri on line 2 of child.xml
testGetCodeActions(file, util.createRange(2, 50, 2, 50), ['Remove redundant script import']);
});
it('offers fix-all when multiple redundant imports exist', () => {
program.setFile('components/parent.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="ParentScene" extends="Scene">
<script type="text/brightscript" uri="pkg:/source/lib1.brs" />
<script type="text/brightscript" uri="pkg:/source/lib2.brs" />
</component>
`);
program.setFile('source/lib1.brs', '');
program.setFile('source/lib2.brs', '');
const file = program.setFile('components/child.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="ChildScene" extends="ParentScene">
<script type="text/brightscript" uri="pkg:/source/lib1.brs" />
<script type="text/brightscript" uri="pkg:/source/lib2.brs" />
</component>
`);
testGetCodeActions(file, util.createRange(2, 50, 2, 50), [
'Fix all: Remove redundant script imports',
'Remove redundant script import'
]);
});
});
describe('unnecessaryCodebehindScriptImport', () => {
it('offers to remove the unnecessary codebehind import', () => {
const file = program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="comp1" extends="Scene">
<script type="text/brightscript" uri="pkg:/components/comp1.brs" />
</component>
`);
program.setFile('components/comp1.brs', '');
// uri on line 2
testGetCodeActions(file, util.createRange(2, 50, 2, 50), ['Remove unnecessary codebehind import']);
});
});
describe('scriptImportCaseMismatch', () => {
it('offers to fix the casing of the script import path', () => {
const file = program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="comp1" extends="Scene">
<script type="text/brightscript" uri="pkg:/SOURCE/lib.brs" />
</component>
`);
program.setFile('source/lib.brs', '');
testGetCodeActions(file, util.createRange(2, 50, 2, 50), ['Fix script import path casing']);
});
it('replaces the URI value with the correctly-cased path', () => {
const file = program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="comp1" extends="Scene">
<script type="text/brightscript" uri="pkg:/SOURCE/lib.brs" />
</component>
`);
program.setFile('source/lib.brs', '');
program.validate();
const actions = program.getCodeActions(file.srcPath, util.createRange(2, 50, 2, 50));
const fix = actions.find(a => a.title === 'Fix script import path casing');
const changes = Object.values(fix!.edit!.changes!)[0];
expect(changes[0].newText).to.equal('pkg:/source/lib.brs');
});
it('offers fix-all when multiple case-mismatched imports exist', () => {
const file = program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="comp1" extends="Scene">
<script type="text/brightscript" uri="pkg:/SOURCE/lib1.brs" />
<script type="text/brightscript" uri="pkg:/SOURCE/lib2.brs" />
</component>
`);
program.setFile('source/lib1.brs', '');
program.setFile('source/lib2.brs', '');
testGetCodeActions(file, util.createRange(2, 50, 2, 50), [
'Fix all: Fix script import path casing',
'Fix script import path casing'
]);
});
it('offers to fix the casing of a relative script import path', () => {
const file = program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="comp1" extends="Scene">
<script type="text/brightscript" uri="LIB.brs" />
</component>
`);
program.setFile('components/lib.brs', '');
testGetCodeActions(file, util.createRange(2, 45, 2, 45), ['Fix script import path casing']);
});
it('replaces a relative URI with the correctly-cased relative path', () => {
const file = program.setFile('components/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="comp1" extends="Scene">
<script type="text/brightscript" uri="LIB.brs" />
</component>
`);
program.setFile('components/lib.brs', '');
program.validate();
const actions = program.getCodeActions(file.srcPath, util.createRange(2, 45, 2, 45));
const fix = actions.find(a => a.title === 'Fix script import path casing');
const changes = Object.values(fix!.edit!.changes!)[0];
expect(changes[0].newText).to.equal('lib.brs');
});
it('replaces a cross-directory relative URI with the correctly-cased relative path', () => {
const file = program.setFile('components/sub/comp1.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="comp1" extends="Scene">
<script type="text/brightscript" uri="../utils/HELPER.brs" />
</component>
`);
program.setFile('components/utils/helper.brs', '');
program.validate();
const actions = program.getCodeActions(file.srcPath, util.createRange(2, 50, 2, 50));
const fix = actions.find(a => a.title === 'Fix script import path casing');
const changes = Object.values(fix!.edit!.changes!)[0];
expect(changes[0].newText).to.equal('../utils/helper.brs');
});
});
describe('missingOverrideKeyword', () => {
it('offers to add the override keyword', () => {
program.setFile('components/comp1.xml', trim`<component name="comp1" extends="Scene"></component>`);
const file = program.setFile('components/comp1.bs', `
class Animal
function speak()
end function
end class
class Dog extends Animal
function speak()
end function
end class
`);
// "function speak()" in Dog — diagnostic starts at the function keyword on line 6
testGetCodeActions(file, util.createRange(6, 20, 6, 20), [`Add missing 'override' keyword`]);
});
it('inserts override before the function keyword', () => {
program.setFile('components/comp1.xml', trim`<component name="comp1" extends="Scene"></component>`);
const file = program.setFile('components/comp1.bs', `
class Animal
function speak()
end function
end class
class Dog extends Animal
function speak()
end function
end class
`);
program.validate();
const actions = program.getCodeActions(file.srcPath, util.createRange(6, 20, 6, 20));
const fix = actions.find(a => a.title === `Add missing 'override' keyword`);
const changes = Object.values(fix!.edit!.changes!)[0];
expect(changes[0].newText).to.equal('override ');
});
it('offers fix-all when multiple methods are missing override', () => {