summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/count-down-loop.lua
diff options
context:
space:
mode:
Diffstat (limited to 'script/core/diagnostics/count-down-loop.lua')
-rw-r--r--script/core/diagnostics/count-down-loop.lua37
1 files changed, 37 insertions, 0 deletions
diff --git a/script/core/diagnostics/count-down-loop.lua b/script/core/diagnostics/count-down-loop.lua
new file mode 100644
index 00000000..b64391fd
--- /dev/null
+++ b/script/core/diagnostics/count-down-loop.lua
@@ -0,0 +1,37 @@
+local files = require "files"
+local guide = require "parser.guide"
+local lang = require 'language'
+
+return function (uri, callback)
+ local state = files.getAst(uri)
+ local text = files.getText(uri)
+ if not state or not text then
+ return
+ end
+
+ guide.eachSourceType(state.ast, 'loop', function (source)
+ if not source.loc or not source.loc.value then
+ return
+ end
+ local maxNumer = source.max and source.max.type == 'number' and tonumber(source.max[1])
+ if maxNumer ~= 1 then
+ return
+ end
+ if not source.step then
+ callback {
+ start = source.loc.value.start,
+ finish = source.max.finish,
+ message = lang.script('DIAG_COUNT_DOWN_LOOP', ('%s, %s'):format(text:sub(source.loc.value.start, source.max.finish), '-1'))
+ }
+ else
+ local stepNumber = source.step.type == 'number' and tonumber(source.step[1])
+ if stepNumber and stepNumber > 0 then
+ callback {
+ start = source.loc.value.start,
+ finish = source.step.finish,
+ message = lang.script('DIAG_COUNT_DOWN_LOOP', ('%s, -%s'):format(text:sub(source.loc.value.start, source.max.finish), source.step[1]))
+ }
+ end
+ end
+ end)
+end