Skip to content

Commit 8256948

Browse files
blevantovychclaude
andcommitted
overhaul test infrastructure, expand test coverage, and fix arrow function support
Test infrastructure: - rewrite run_tests.sh to auto-clone plenary.nvim and nvim-treesitter, install tree-sitter parsers, and run tests in a clean headless nvim - add minimal_init.lua for isolated test environment setup - extract buffer_to_string helper into tests/helpers.lua (DRY up test specs) - add .gitignore for .tests/ dependency directory New test cases: - simple_variable, arguments, template_string, ternary, named_imports, minified object, return_statement, jsx, and tsx Plugin fixes: - fix arrow function handling: walk up AST to find statement_block in parent node's children so console.log is inserted inside the function body - replace nvim_feedkeys-based indentation (= =) with vim.cmd('normal! ==') for reliable auto-indent in headless/test environments CI & docs: - add GitHub Actions workflow (.github/workflows/test.yml) to run tests on push/PR to main/master - update README.md test instructions to reflect new self-contained runner - add CLAUDE.md with project architecture and conventions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent cba5c08 commit 8256948

29 files changed

Lines changed: 336 additions & 19 deletions

.github/workflows/test.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [main, master]
6+
pull_request:
7+
branches: [main, master]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- uses: rhysd/action-setup-vim@v1
16+
with:
17+
neovim: true
18+
version: stable
19+
20+
- name: Run tests
21+
run: cd tests && ./run_tests.sh

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.tests/

CLAUDE.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# printer.nvim
2+
3+
Neovim plugin that inserts a `console.log()` statement for the identifier under the cursor at a syntactically valid position. Uses tree-sitter to walk up the AST and find the right insertion point — handles cases where the cursor is inside an array, object, JSX, return statement, etc.
4+
5+
Scoped to JavaScript/TypeScript only.
6+
7+
## Architecture
8+
9+
Single module: `lua/printer.lua`. No configuration system, no setup function — just one exported function `M.add_console_log()`.
10+
11+
**Flow:**
12+
1. Validate cursor is on an `identifier` or `shorthand_property_identifier_pattern` node
13+
2. Walk up the AST looking for "restricted" parent nodes (object, array, jsx_element, formal_parameters, etc.)
14+
3. If inside a restricted node, insert the print statement above or below that node (above for return_statement/parenthesized_expression, below for everything else)
15+
4. If not restricted, insert below the current line
16+
5. Auto-indent the inserted line with `==`
17+
18+
**Dependency:** `nvim-treesitter` (uses `nvim-treesitter.ts_utils`)
19+
20+
## Tests
21+
22+
Tests use Plenary (`PlenaryBustedFile`). Each test case is a pair of files in `tests/`:
23+
- `<name>.js` — input fixture (the buffer content)
24+
- `<name>.lua` — test spec that positions cursor, calls `add_console_log()`, and asserts buffer content
25+
26+
Run from the `tests/` directory:
27+
```
28+
./run_tests.sh
29+
```
30+
31+
**Known issues:**
32+
- `arrow_function` test is a known failing test (commit `0e24a06`) — cursor on `index` in `[1,2,3].map((n, index) => {})` should insert inside the function body
33+
- Array/object tests have TODOs for printing the variable on the left side of the declaration (e.g., `arr` in `const arr = [...]`)
34+
35+
## Print statement format
36+
37+
```
38+
console.log({ variableName })
39+
```
40+
41+
Uses JS object shorthand so the logged output shows both the name and value.

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ The statement is placed at a syntactically valid position — if the identifier
5454

5555
## Tests
5656

57-
To run tests, execute the following command in the `tests` directory:
57+
Tests use [plenary.nvim](https://github.com/nvim-lua/plenary.nvim). The test runner automatically clones dependencies (plenary.nvim and nvim-treesitter) on first run.
5858

59-
```
60-
./run_tests.sh
59+
Requirements: Neovim and `git`.
60+
61+
```sh
62+
./tests/run_tests.sh
6163
```

lua/printer.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ local function is_inside_where_we_cannot_add_print_statement()
5151
if temp:type() == 'statement_block' then
5252
break
5353
end
54+
local has_body = false
55+
for child in temp:iter_children() do
56+
if child:type() == 'statement_block' then
57+
has_body = true
58+
break
59+
end
60+
end
61+
if has_body then
62+
break
63+
end
5464
temp = temp:parent()
5565
end
5666

@@ -90,8 +100,7 @@ M.add_console_log = function()
90100
end
91101

92102
-- align line
93-
vim.api.nvim_feedkeys('=', 'n', false)
94-
vim.api.nvim_feedkeys('=', 'n', false)
103+
vim.cmd('normal! ==')
95104
end
96105

97106
return M

tests/arguments.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
doSomething(value)

tests/arguments.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local buffer_to_string = dofile('helpers.lua').buffer_to_string
2+
3+
describe("printer", function()
4+
local printer = require('printer')
5+
6+
it("handles arguments", function()
7+
-- go to value
8+
vim.api.nvim_win_set_cursor(0, {1, 12})
9+
printer.add_console_log()
10+
11+
assert.are.equal([[
12+
doSomething(value)
13+
console.log({ value })]], buffer_to_string())
14+
end)
15+
16+
end)

tests/array.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
local buffer_to_string = function()
2-
local content = vim.api.nvim_buf_get_lines(0, 0, vim.api.nvim_buf_line_count(0), false)
3-
return table.concat(content, "\n")
4-
end
1+
local buffer_to_string = dofile('helpers.lua').buffer_to_string
52

63
describe("printer", function()
74
local printer = require('printer')

tests/arrow_function.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
local buffer_to_string = function()
2-
local content = vim.api.nvim_buf_get_lines(0, 0, vim.api.nvim_buf_line_count(0), false)
3-
return table.concat(content, "\n")
4-
end
1+
local buffer_to_string = dofile('helpers.lua').buffer_to_string
52

63
describe("printer", function()
74
local printer = require('printer')

tests/helpers.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
local M = {}
2+
3+
M.buffer_to_string = function()
4+
local content = vim.api.nvim_buf_get_lines(0, 0, vim.api.nvim_buf_line_count(0), false)
5+
return table.concat(content, "\n")
6+
end
7+
8+
return M

0 commit comments

Comments
 (0)