Skip to content

Commit 7f81e9d

Browse files
feat: use selene to prevent accidental print statements
## Details After leaving a print statement in the last push, wanted an automated way to prevent this in the future. While there's no perfect solution I did come up with one that works well enough. Added a `selene.toml` with a `neovim` base. The base defines the `vim` global (as any) along with the various test framework globals like `describe`, `it`, and the various `assert` methods. Important for this change specifically it also marks both `print` and `vim.print` as deprecated. That way any usage of those functions fails the check. Where we do want to allow these methods add `selene: allow(deprecated)`. It's not accurate since the methods are not deprecated, but does allow me to quickly catch any usage that has not been explicitly allowed. Added a `check` step that runs both `selene` & `stylua` over all files. Fixed / improved a few issues: - caught an error where I defined `Providers` globally instead of locally - inline luassert `assert` methods so that errors are found - add allow for mixed table around minimal config - always provide a message argument to regular `assert` method
1 parent bff12b4 commit 7f81e9d

24 files changed

+115
-66
lines changed

benches/util.lua

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
---@module 'luassert'
22

3-
local Eq = assert.are.same
4-
local True = assert.True
5-
63
---@class render.md.bench.Util
74
local M = {}
85

@@ -53,14 +50,14 @@ end
5350
---@param actual number
5451
---@param max number
5552
function M.less_than(actual, max)
56-
True(actual < max, ('expected %f < %f'):format(actual, max))
53+
assert.is_true(actual < max, ('expected %f < %f'):format(actual, max))
5754
end
5855

5956
---@param expected integer
6057
function M.num_marks(expected)
6158
local ui = require('render-markdown.core.ui')
6259
local marks = vim.api.nvim_buf_get_extmarks(0, ui.ns, 0, -1, {})
63-
Eq(expected, #marks)
60+
assert.same(expected, #marks)
6461
end
6562

6663
return M

demo/minit.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local lazypath = vim.fn.stdpath('data') .. '/lazy/lazy.nvim'
1515
assert(vim.uv.fs_stat(lazypath))
1616
vim.opt.rtp:prepend(lazypath)
1717

18+
-- selene: allow(mixed_table)
1819
require('lazy').setup({
1920
dev = { path = '~/dev/repos/personal' },
2021
spec = {
@@ -32,7 +33,12 @@ require('lazy').setup({
3233
config = function()
3334
---@diagnostic disable-next-line: missing-fields
3435
require('nvim-treesitter.configs').setup({
35-
ensure_installed = { 'markdown', 'markdown_inline', 'latex' },
36+
ensure_installed = {
37+
'html',
38+
'latex',
39+
'markdown',
40+
'markdown_inline',
41+
},
3642
highlight = { enable = true },
3743
})
3844
end,
@@ -56,7 +62,10 @@ require('lazy').setup({
5662
{
5763
'MeanderingProgrammer/render-markdown.nvim',
5864
dev = true,
59-
dependencies = { 'nvim-treesitter/nvim-treesitter', 'echasnovski/mini.nvim' },
65+
dependencies = {
66+
'nvim-treesitter/nvim-treesitter',
67+
'echasnovski/mini.nvim',
68+
},
6069
config = function()
6170
require('render-markdown').setup({})
6271
end,

doc/render-markdown.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For NVIM v0.11.1 Last change: 2025 April 28
1+
*render-markdown.txt* For NVIM v0.11.1 Last change: 2025 April 29
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

justfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
init := "tests/minimal_init.lua"
22

3-
default: update test health
3+
default: update check test health
44

55
update:
66
# Updates types.lua & README.md
@@ -11,6 +11,10 @@ update:
1111
--project-name render-markdown \
1212
--input-file README.md
1313

14+
check:
15+
selene --quiet .
16+
stylua --check .
17+
1418
test:
1519
just busted "tests"
1620

lua/render-markdown/api.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ end
4949
function M.config()
5050
local difference = state.difference()
5151
if difference == nil then
52+
-- selene: allow(deprecated)
5253
vim.print('default configuration')
5354
else
55+
-- selene: allow(deprecated)
5456
vim.print(difference)
5557
end
5658
end

lua/render-markdown/core/log.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ function M.runtime(name, callback)
5757
callback()
5858
local end_time = Compat.uv.hrtime()
5959
local elapsed = (end_time - start_time) / 1e+6
60-
assert(elapsed < 1000)
60+
assert(elapsed < 1000, 'invalid elapsed time')
61+
-- selene: allow(deprecated)
6162
vim.print(('%8s : %5.1f ms'):format(name:upper(), elapsed))
6263
end
6364
else

lua/render-markdown/debug/marks.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ end
2525
---@return boolean
2626
function Mark.__lt(a, b)
2727
local as, bs = a:priorities(), b:priorities()
28-
assert(#as == #bs)
28+
assert(#as == #bs, 'priorities must be same length')
2929
for i = 1, #as do
3030
if as[i] ~= bs[i] then
3131
return as[i] < bs[i]
@@ -166,10 +166,13 @@ function M.show()
166166
table.sort(marks)
167167

168168
if #marks == 0 then
169+
-- selene: allow(deprecated)
169170
vim.print(('no marks on row: %d'):format(row))
170171
else
172+
-- selene: allow(deprecated)
171173
vim.print(('marks on row: %d'):format(row))
172174
for _, mark in ipairs(marks) do
175+
-- selene: allow(deprecated)
173176
vim.print(tostring(mark))
174177
end
175178
end

lua/render-markdown/handler/html.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function Handler:parse(root)
3737
local marks = Marks.new(self.context, true)
3838
self.context:query(root, self.query, function(capture, node)
3939
local renderer = self.renderers[capture]
40-
assert(renderer ~= nil, 'Unhandled html capture: ' .. capture)
40+
assert(renderer ~= nil, 'unhandled html capture: ' .. capture)
4141
local render = renderer:new(self.context, marks, node)
4242
if render:setup() then
4343
render:render()

lua/render-markdown/handler/markdown.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function Handler:parse(root)
6969
local marks = Marks.new(self.context, false)
7070
self.context:query(root, self.query, function(capture, node)
7171
local renderer = self.renderers[capture]
72-
assert(renderer ~= nil, 'Unhandled markdown capture: ' .. capture)
72+
assert(renderer ~= nil, 'unhandled markdown capture: ' .. capture)
7373
local render = renderer:new(self.context, marks, node)
7474
if render:setup() then
7575
render:render()

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function Handler:parse(root)
4848
local marks = Marks.new(self.context, true)
4949
self.context:query(root, self.query, function(capture, node)
5050
local renderer = self.renderers[capture]
51-
assert(renderer ~= nil, 'Unhandled inline capture: ' .. capture)
51+
assert(renderer ~= nil, 'unhandled inline capture: ' .. capture)
5252
local render = renderer:new(self.context, marks, node)
5353
if render:setup() then
5454
render:render()

0 commit comments

Comments
 (0)