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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
|
local lang = require 'language'
local config = require 'config'
local library = require 'core.library'
local buildGlobal = require 'vm.global'
local DiagnosticSeverity = require 'constant.DiagnosticSeverity'
local DiagnosticDefaultSeverity = require 'constant.DiagnosticDefaultSeverity'
local DiagnosticTag = require 'constant.DiagnosticTag'
local mt = {}
mt.__index = mt
local function isContainPos(obj, start, finish)
if obj.start <= start and obj.finish >= finish then
return true
end
return false
end
function mt:searchUnusedLocals(callback)
self.vm:eachSource(function (source)
local loc = source:bindLocal()
if not loc then
return
end
if loc:get 'emmy arg' then
return
end
local name = loc:getName()
if name == '_' or name == '_ENV' or name == '' then
return
end
if source:action() ~= 'local' then
return
end
if loc:get 'hide' then
return
end
local used = loc:eachInfo(function (info)
if info.type == 'get' then
return true
end
end)
if not used then
callback(source.start, source.finish, name)
end
end)
end
function mt:searchUnusedFunctions(callback)
self.vm:eachSource(function (source)
local loc = source:bindLocal()
if not loc then
return
end
if loc:get 'emmy arg' then
return
end
if source:action() ~= 'local' then
return
end
if loc:get 'hide' then
return
end
local used = loc:eachInfo(function (info)
if info.type == 'get' then
return true
end
end)
if used then
return
end
loc:eachInfo(function (info, src)
if info.type == 'set' or info.type == 'local' then
local v = src:bindValue()
local func = v and v:getFunction()
if func and func:getSource().uri == self.vm.uri then
callback(func:getSource().start, func:getSource().finish)
end
end
end)
end)
end
function mt:searchUndefinedGlobal(callback)
local definedGlobal = {}
for name in pairs(config.config.diagnostics.globals) do
definedGlobal[name] = true
end
local envValue = buildGlobal(self.vm.lsp)
envValue:eachInfo(function (info)
if info.type == 'set child' then
local name = info[1]
definedGlobal[name] = true
end
end)
self.vm:eachSource(function (source)
if not source:get 'global' then
return
end
local name = source:getName()
if name == '' then
return
end
local parent = source:get 'parent'
if not parent then
return
end
if not parent:get 'ENV' and not source:get 'in index' then
return
end
if definedGlobal[name] then
return
end
if type(name) ~= 'string' then
return
end
callback(source.start, source.finish, name)
end)
end
function mt:searchUnusedLabel(callback)
self.vm:eachSource(function (source)
local label = source:bindLabel()
if not label then
return
end
if source:action() ~= 'set' then
return
end
local used = label:eachInfo(function (info)
if info.type == 'get' then
return true
end
end)
if not used then
callback(source.start, source.finish, label:getName())
end
end)
end
function mt:searchUnusedVararg(callback)
self.vm:eachSource(function (source)
local value = source:bindFunction()
if not value then
return
end
local func = value:getFunction()
if not func then
return
end
if func._dotsSource and not func._dotsLoad then
callback(func._dotsSource.start, func._dotsSource.finish)
end
end)
end
local function isInString(vm, start, finish)
return vm:eachSource(function (source)
if source.type == 'string' and isContainPos(source, start, finish) then
return true
end
end)
end
function mt:searchSpaces(callback)
local vm = self.vm
local lines = self.lines
for i = 1, #lines do
local line = lines:line(i)
if line:find '^[ \t]+$' then
local start, finish = lines:range(i)
if isInString(vm, start, finish) then
goto NEXT_LINE
end
callback(start, finish, lang.script.DIAG_LINE_ONLY_SPACE)
goto NEXT_LINE
end
local pos = line:find '[ \t]+$'
if pos then
local start, finish = lines:range(i)
start = start + pos - 1
if isInString(vm, start, finish) then
goto NEXT_LINE
end
callback(start, finish, lang.script.DIAG_LINE_POST_SPACE)
goto NEXT_LINE
end
::NEXT_LINE::
end
end
function mt:searchRedefinition(callback)
local used = {}
local uri = self.uri
self.vm:eachSource(function (source)
local loc = source:bindLocal()
if not loc then
return
end
local shadow = loc:shadow()
if not shadow then
return
end
if used[shadow] then
return
end
used[shadow] = true
if loc:get 'hide' then
return
end
local name = loc:getName()
if name == '_' or name == '_ENV' or name == '' then
return
end
local related = {}
for i = 1, #shadow do
related[i] = {
start = shadow[i]:getSource().start,
finish = shadow[i]:getSource().finish,
uri = uri,
}
end
for i = 2, #shadow do
callback(shadow[i]:getSource().start, shadow[i]:getSource().finish, name, related)
end
end)
end
function mt:searchNewLineCall(callback)
local lines = self.lines
self.vm:eachSource(function (source)
if source.type ~= 'simple' then
return
end
for i = 1, #source - 1 do
local callSource = source[i]
local funcSource = source[i-1]
if callSource.type ~= 'call' then
goto CONTINUE
end
local callLine = lines:rowcol(callSource.start)
local funcLine = lines:rowcol(funcSource.finish)
if callLine > funcLine then
callback(callSource.start, callSource.finish)
end
:: CONTINUE ::
end
end)
end
function mt:searchNewFieldCall(callback)
local lines = self.lines
self.vm:eachSource(function (source)
if source.type ~= 'table' then
return
end
for i = 1, #source do
local field = source[i]
if field.type == 'simple' then
local callSource = field[#field]
local funcSource = field[#field-1]
local callLine = lines:rowcol(callSource.start)
local funcLine = lines:rowcol(funcSource.finish)
if callLine > funcLine then
callback(funcSource.start, callSource.finish
, lines.buf:sub(funcSource.start, funcSource.finish)
, lines.buf:sub(callSource.start, callSource.finish)
)
end
end
end
end)
end
function mt:searchRedundantParameters(callback)
self.vm:eachSource(function (source)
local args = source:bindCall()
if not args then
return
end
-- 回调函数不检查
local simple = source:get 'simple'
if simple and simple[2] == source then
local loc = simple[1]:bindLocal()
if loc then
local source = loc:getSource()
if source:get 'arg' then
return
end
end
end
local value = source:findCallFunction()
if not value then
return
end
local func = value:getFunction()
-- 参数中有 ... ,不用再检查了
if func:hasDots() then
return
end
local max = #func.args
local passed = #args
-- function m.open() end
-- m:open()
-- 这种写法不算错
if passed == 1 and source:get 'has object' then
return
end
for i = max + 1, passed do
local extra = args[i]
callback(extra.start, extra.finish, max, passed)
end
end)
end
local opMap = {
['+'] = true,
['-'] = true,
['*'] = true,
['/'] = true,
['//'] = true,
['^'] = true,
['<<'] = true,
['>>'] = true,
['&'] = true,
['|'] = true,
['~'] = true,
['..'] = true,
}
local literalMap = {
['number'] = true,
['boolean'] = true,
['string'] = true,
['table'] = true,
}
function mt:searchAmbiguity1(callback)
self.vm:eachSource(function (source)
if source.op ~= 'or' then
return
end
local first = source[1]
local second = source[2]
-- a + (b or 0) --> (a + b) or 0
do
if opMap[first.op]
and first.type ~= 'unary'
and not second.op
and literalMap[second.type]
and not first.brackets
then
callback(source.start, source.finish, first.start, first.finish)
end
end
-- (a or 0) + c --> a or (0 + c)
do
if opMap[second.op]
and second.type ~= 'unary'
and not first.op
and literalMap[second[1].type]
and not second.brackets
then
callback(source.start, source.finish, second.start, second.finish)
end
end
end)
end
function mt:searchLowercaseGlobal(callback)
local definedGlobal = {}
for name in pairs(config.config.diagnostics.globals) do
definedGlobal[name] = true
end
for name in pairs(library.global) do
definedGlobal[name] = true
end
self.vm:eachSource(function (source)
if source.type == 'name'
and source:get 'parent'
and not source:get 'simple'
and not source:get 'table index'
and source:action() == 'set'
then
local name = source[1]
if definedGlobal[name] then
return
end
local first = name:match '%w'
if not first then
return
end
if first:match '%l' then
callback(source.start, source.finish)
end
end
end)
end
function mt:searchDuplicateIndex(callback)
self.vm:eachSource(function (source)
if source.type ~= 'table' then
return
end
local mark = {}
for _, obj in ipairs(source) do
if obj.type == 'pair' then
local key = obj[1]
local name
if key.index then
if key.type == 'string' then
name = key[1]
end
elseif key.type == 'name' then
name = key[1]
end
if name then
if mark[name] then
mark[name][#mark[name]+1] = obj
else
mark[name] = { obj }
end
end
end
end
for name, defs in pairs(mark) do
if #defs > 1 then
local related = {}
for i = 1, #defs do
related[i] = {
start = defs[i][1].start,
finish = defs[i][2].finish,
uri = self.uri,
}
end
for i = 1, #defs - 1 do
callback(defs[i][1].start, defs[i][2].finish, name, related, 'unused')
end
for i = #defs, #defs do
callback(defs[i][1].start, defs[i][1].finish, name, related, 'duplicate')
end
end
end
end)
end
function mt:searchDuplicateMethod(callback)
local uri = self.uri
local mark = {}
local map = {}
self.vm:eachSource(function (source)
local parent = source:get 'parent'
if not parent then
return
end
if mark[parent] then
return
end
mark[parent] = true
local relates = {}
parent:eachInfo(function (info, src)
local k = info[1]
if info.type ~= 'set child' then
return
end
if type(k) ~= 'string' then
return
end
if src.start == 0 then
return
end
if not src:get 'object' then
return
end
if map[src] then
return
end
if not relates[k] then
relates[k] = map[src] or {
name = k,
}
end
map[src] = relates[k]
relates[k][#relates[k]+1] = {
start = src.start,
finish = src.finish,
uri = src.uri
}
end)
end)
for src, relate in pairs(map) do
if #relate > 1 and src.uri == uri then
callback(src.start, src.finish, relate.name, relate)
end
end
end
function mt:searchEmptyBlock(callback)
self.vm:eachSource(function (source)
-- 认为空repeat与空while是合法的
-- 要去vm中激活source
if source.type == 'if' then
for _, block in ipairs(source) do
if #block > 0 then
return
end
end
callback(source.start, source.finish)
return
end
if source.type == 'loop'
or source.type == 'in'
then
if #source == 0 then
callback(source.start, source.finish)
end
return
end
end)
end
function mt:searchRedundantValue(callback)
self.vm:eachSource(function (source)
if source.type == 'set' or source.type == 'local' then
local args = source[1]
local values = source[2]
if not source[2] then
return
end
local argCount, valueCount
if args.type == 'list' then
argCount = #args
else
argCount = 1
end
if values.type == 'list' then
valueCount = #values
else
valueCount = 1
end
for i = argCount + 1, valueCount do
local value = values[i]
callback(value.start, value.finish, argCount, valueCount)
end
end
end)
end
function mt:searchUndefinedEnvChild(callback)
self.vm:eachSource(function (source)
if not source:get 'global' then
return
end
local name = source:getName()
if name == '' then
return
end
if source:get 'in index' then
return
end
local parent = source:get 'parent'
if parent:get 'ENV' then
return
end
local value = source:bindValue()
if not value then
return
end
if value:getSource() == source then
callback(source.start, source.finish, name)
end
return
end)
end
function mt:searchGlobalInNilEnv(callback)
self.vm:eachSource(function (source)
if not source:get 'global' then
return
end
local name = source:getName()
if name == '' then
return
end
local parentSource = source:get 'parent' :getSource()
if parentSource and parentSource.type == 'nil' then
callback(source.start, source.finish, {
{
start = parentSource.start,
finish = parentSource.finish,
uri = self.uri,
}
})
end
return
end)
end
function mt:checkEmmyClass(source, callback)
local class = source:get 'emmy.class'
if not class then
return
end
-- class重复定义
local name = class:getName()
local related = {}
self.vm.emmyMgr:eachClass(name, function (class)
if class.type ~= 'emmy.class' and class.type ~= 'emmy.alias' then
return
end
local src = class:getSource()
if src ~= source then
related[#related+1] = {
start = src.start,
finish = src.finish,
uri = src.uri,
}
end
end)
if #related > 0 then
callback(source.start, source.finish, lang.script.DIAG_DUPLICATE_CLASS ,related)
end
-- 继承不存在的class
local extends = class.extends
if not extends then
return
end
local parent = self.vm.emmyMgr:eachClass(extends, function (parent)
if parent.type == 'emmy.class' then
return parent
end
end)
if not parent then
callback(source[2].start, source[2].finish, lang.script.DIAG_UNDEFINED_CLASS)
return
end
-- class循环继承
local related = {}
local current = class
for _ = 1, 10 do
local extends = current.extends
if not extends then
break
end
related[#related+1] = {
start = current:getSource().start,
finish = current:getSource().finish,
uri = current:getSource().uri,
}
current = self.vm.emmyMgr:eachClass(extends, function (parent)
if parent.type == 'emmy.class' then
return parent
end
end)
if not current then
break
end
if current:getName() == class:getName() then
callback(source.start, source.finish, lang.script.DIAG_CYCLIC_EXTENDS, related)
break
end
end
end
function mt:checkEmmyType(source, callback)
for _, tpsource in ipairs(source) do
-- TODO 临时决绝办法,重构后解决
local name
if tpsource.type == 'emmyArrayType' then
name = tpsource[1][1]
else
name = tpsource[1]
end
local class = self.vm.emmyMgr:eachClass(name, function (class)
if class.type == 'emmy.class' or class.type == 'emmy.alias' then
return class
end
end)
if not class then
callback(tpsource.start, tpsource.finish, lang.script.DIAG_UNDEFINED_CLASS)
end
end
end
function mt:checkEmmyAlias(source, callback)
local class = source:get 'emmy.alias'
if not class then
return
end
-- class重复定义
local name = class:getName()
local related = {}
self.vm.emmyMgr:eachClass(name, function (class)
if class.type ~= 'emmy.class' and class.type ~= 'emmy.alias' then
return
end
local src = class:getSource()
if src ~= source then
related[#related+1] = {
start = src.start,
finish = src.finish,
uri = src.uri,
}
end
end)
if #related > 0 then
callback(source.start, source.finish, lang.script.DIAG_DUPLICATE_CLASS ,related)
end
end
function mt:checkEmmyParam(source, callback, mark)
local func = source:get 'emmy function'
if not func then
return
end
if mark[func] then
return
end
mark[func] = true
-- 检查不存在的参数
local emmyParams = func:getEmmyParams()
local funcParams = {}
if func.args then
for _, arg in ipairs(func.args) do
funcParams[arg.name] = true
end
end
for _, param in ipairs(emmyParams) do
local name = param:getName()
if not funcParams[name] then
callback(param:getSource()[1].start, param:getSource()[1].finish, lang.script.DIAG_INEXISTENT_PARAM)
end
end
-- 检查重复的param
local lists = {}
for _, param in ipairs(emmyParams) do
local name = param:getName()
if not lists[name] then
lists[name] = {}
end
lists[name][#lists[name]+1] = param:getSource()[1]
end
for _, list in pairs(lists) do
if #list > 1 then
local related = {}
for _, src in ipairs(list) do
related[#related+1] = {
src.start,
src.finish,
src.uri,
}
callback(src.start, src.finish, lang.script.DIAG_DUPLICATE_PARAM)
end
end
end
end
function mt:checkEmmyField(source, callback, mark)
---@type EmmyClass
local class = source:get 'target class'
-- 必须写在 class 的后面
if not class then
callback(source.start, source.finish, lang.script.DIAG_NEED_CLASS)
end
-- 检查重复的 field
if class and not mark[class] then
mark[class] = true
local lists = {}
class:eachField(function (field)
local name = field:getName()
if not lists[name] then
lists[name] = {}
end
lists[name][#lists[name]+1] = field:getSource()[2]
end)
for _, list in pairs(lists) do
if #list > 1 then
local related = {}
for _, src in ipairs(list) do
related[#related+1] = {
src.start,
src.finish,
src.uri,
}
callback(src.start, src.finish, lang.script.DIAG_DUPLICATE_FIELD)
end
end
end
end
end
function mt:searchEmmyLua(callback)
local mark = {}
self.vm:eachSource(function (source)
if source.type == 'emmyClass' then
self:checkEmmyClass(source, callback)
elseif source.type == 'emmyType' then
self:checkEmmyType(source, callback)
elseif source.type == 'emmyAlias' then
self:checkEmmyAlias(source, callback)
elseif source.type == 'emmyParam' then
self:checkEmmyParam(source, callback, mark)
elseif source.type == 'emmyField' then
self:checkEmmyField(source, callback, mark)
end
end)
end
function mt:searchSetConstLocal(callback)
local mark = {}
self.vm:eachSource(function (source)
local loc = source:bindLocal()
if not loc then
return
end
if mark[loc] then
return
end
mark[loc] = true
if not loc.tags then
return
end
local const
for _, tag in ipairs(loc.tags) do
if tag[1] == 'const' then
const = true
break
end
end
if not const then
return
end
loc:eachInfo(function (info, src)
if info.type == 'set' then
callback(src.start, src.finish)
end
end)
end)
end
function mt:doDiagnostics(func, code, callback)
if config.config.diagnostics.disable[code] then
return
end
local level = config.config.diagnostics.severity[code]
if not DiagnosticSeverity[level] then
level = DiagnosticDefaultSeverity[code]
end
func(self, function (start, finish, ...)
local data = callback(...)
data.code = code
data.start = start
data.finish = finish
data.level = data.level or DiagnosticSeverity[level]
self.datas[#self.datas+1] = data
end)
if coroutine.isyieldable() then
if self.vm:isRemoved() then
coroutine.yield('stop')
else
coroutine.yield()
end
end
end
return function (vm, lines, uri)
local session = setmetatable({
vm = vm,
lines = lines,
uri = uri,
datas = {},
}, mt)
-- 未使用的局部变量
session:doDiagnostics(session.searchUnusedLocals, 'unused-local', function (key)
return {
message = lang.script('DIAG_UNUSED_LOCAL', key),
tags = {DiagnosticTag.Unnecessary},
}
end)
-- 未使用的函数
session:doDiagnostics(session.searchUnusedFunctions, 'unused-function', function ()
return {
message = lang.script.DIAG_UNUSED_FUNCTION,
tags = {DiagnosticTag.Unnecessary},
}
end)
-- 读取未定义全局变量
session:doDiagnostics(session.searchUndefinedGlobal, 'undefined-global', function (key)
local message = lang.script('DIAG_UNDEF_GLOBAL', key)
local otherVersion = library.other[key]
local customLib = library.custom[key]
if otherVersion then
message = ('%s(%s)'):format(message, lang.script('DIAG_DEFINED_VERSION', table.concat(otherVersion, '/'), config.config.runtime.version))
end
if customLib then
message = ('%s(%s)'):format(message, lang.script('DIAG_DEFINED_CUSTOM', table.concat(customLib, '/')))
end
return {
message = message,
}
end)
-- 未使用的Label
session:doDiagnostics(session.searchUnusedLabel, 'unused-label', function (key)
return {
message = lang.script('DIAG_UNUSED_LABEL', key),
tags = {DiagnosticTag.Unnecessary},
}
end)
-- 未使用的不定参数
session:doDiagnostics(session.searchUnusedVararg, 'unused-vararg', function ()
return {
message = lang.script.DIAG_UNUSED_VARARG,
tags = {DiagnosticTag.Unnecessary},
}
end)
-- 只有空格与制表符的行,以及后置空格
session:doDiagnostics(session.searchSpaces, 'trailing-space', function (message)
return {
message = message,
}
end)
-- 重定义局部变量
session:doDiagnostics(session.searchRedefinition, 'redefined-local', function (key, related)
return {
message = lang.script('DIAG_REDEFINED_LOCAL', key),
related = related,
}
end)
-- 以括号开始的一行(可能被误解析为了上一行的call)
session:doDiagnostics(session.searchNewLineCall, 'newline-call', function ()
return {
message = lang.script.DIAG_PREVIOUS_CALL,
}
end)
-- 以字符串开始的field(可能被误解析为了上一行的call)
session:doDiagnostics(session.searchNewFieldCall, 'newfield-call', function (func, call)
return {
message = lang.script('DIAG_PREFIELD_CALL', func, call),
}
end)
-- 调用函数时的参数数量是否超过函数的接收数量
session:doDiagnostics(session.searchRedundantParameters, 'redundant-parameter', function (max, passed)
return {
message = lang.script('DIAG_OVER_MAX_ARGS', max, passed),
tags = {DiagnosticTag.Unnecessary},
}
end)
-- x or 0 + 1
session:doDiagnostics(session.searchAmbiguity1, 'ambiguity-1', function (start, finish)
return {
message = lang.script('DIAG_AMBIGUITY_1', lines.buf:sub(start, finish)),
}
end)
-- 不允许定义首字母小写的全局变量(很可能是拼错或者漏删)
session:doDiagnostics(session.searchLowercaseGlobal, 'lowercase-global', function ()
return {
message = lang.script.DIAG_LOWERCASE_GLOBAL,
}
end)
-- 未定义的变量(重载了 `_ENV`)
session:doDiagnostics(session.searchUndefinedEnvChild, 'undefined-env-child', function (key)
if vm.envType == '_ENV' then
return {
message = lang.script('DIAG_UNDEF_ENV_CHILD', key),
}
else
return {
message = lang.script('DIAG_UNDEF_FENV_CHILD', key),
}
end
end)
-- 全局变量不可用(置空了 `_ENV`)
session:doDiagnostics(session.searchGlobalInNilEnv, 'global-in-nil-env', function (related)
if vm.envType == '_ENV' then
return {
message = lang.script.DIAG_GLOBAL_IN_NIL_ENV,
related = related,
}
else
return {
message = lang.script.DIAG_GLOBAL_IN_NIL_FENV,
related = related,
}
end
end)
-- 构建表时重复定义field
session:doDiagnostics(session.searchDuplicateIndex, 'duplicate-index', function (key, related, type)
if type == 'unused' then
return {
message = lang.script('DIAG_DUPLICATE_INDEX', key),
related = related,
level = DiagnosticSeverity.Hint,
tags = {DiagnosticTag.Unnecessary},
}
else
return {
message = lang.script('DIAG_DUPLICATE_INDEX', key),
related = related,
}
end
end)
-- 往表里面塞重复的method
--session:doDiagnostics(session.searchDuplicateMethod, 'duplicate-method', function (key, related)
-- return {
-- message = lang.script('DIAG_DUPLICATE_METHOD', key),
-- related = related,
-- }
--end)
-- 空代码块
session:doDiagnostics(session.searchEmptyBlock, 'empty-block', function ()
return {
message = lang.script.DIAG_EMPTY_BLOCK,
tags = {DiagnosticTag.Unnecessary},
}
end)
-- 多余的赋值
session:doDiagnostics(session.searchRedundantValue, 'redundant-value', function (max, passed)
return {
message = lang.script('DIAG_OVER_MAX_VALUES', max, passed),
tags = {DiagnosticTag.Unnecessary},
}
end)
-- Emmy相关的检查
session:doDiagnostics(session.searchEmmyLua, 'emmy-lua', function (message, related)
return {
message = message,
related = related,
}
end)
-- 检查给const变量赋值
session:doDiagnostics(session.searchSetConstLocal, 'set-const', function ()
return {
message = lang.script.DIAG_SET_CONST
}
end)
return session.datas
end
|