Skip to content

Commit

Permalink
fix: add treesitter support for JS / TS / TSX generator functions (#289)
Browse files Browse the repository at this point in the history
* fix: add JS as TS generator functions

* feat: add TSX generator functions
  • Loading branch information
madlabman committed Sep 15, 2023
1 parent f34defe commit 9bcfbaf
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lua/aerial/backends/treesitter/extensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ M.typescript = {
postprocess = function(bufnr, item, match)
local value_node = (utils.get_at_path(match, "var_type") or {}).node
if value_node then
if value_node:type() == "generator_function" then
item.kind = "Function"
end
if value_node:type() == "arrow_function" then
item.kind = "Function"
end
Expand Down
7 changes: 6 additions & 1 deletion queries/javascript/aerial.scm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
(#set! "kind" "Function")
) @type

(generator_function_declaration
name: (identifier) @name
(#set! "kind" "Function")
) @type

(method_definition
name: (property_identifier) @name
(#set! "kind" "Method")
Expand All @@ -23,7 +28,7 @@
(lexical_declaration
(variable_declarator
name: (identifier) @name
value: [(arrow_function) (function)] @type
value: [(arrow_function) (function) (generator_function)] @type
)
(#set! "kind" "Function")
) @start
Expand Down
5 changes: 5 additions & 0 deletions queries/tsx/aerial.scm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
(#set! "kind" "Function")
) @type

(generator_function_declaration
name: (identifier) @name
(#set! "kind" "Function")
) @type

(interface_declaration
name: (type_identifier) @name
(#set! "kind" "Interface")
Expand Down
5 changes: 5 additions & 0 deletions queries/typescript/aerial.scm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
(#set! "kind" "Function")
) @type

(generator_function_declaration
name: (identifier) @name
(#set! "kind" "Function")
) @type

(interface_declaration
name: (type_identifier) @name
(#set! "kind" "Interface")
Expand Down
18 changes: 18 additions & 0 deletions tests/treesitter/js_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ describe("treesitter js", function()
end_lnum = 23,
end_col = 28,
},
{
kind = "Function",
name = "g",
level = 0,
lnum = 25,
col = 0,
end_lnum = 25,
end_col = 16,
},
{
kind = "Function",
name = "g_1",
level = 0,
lnum = 26,
col = 0,
end_lnum = 26,
end_col = 28,
},
})
end)
end)
3 changes: 3 additions & 0 deletions tests/treesitter/js_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ describe("UnitTest", () => {

const fn_2 = () => {};
const fn_3 = function () {};

function* g() {}
const g_1 = function* () {};
18 changes: 18 additions & 0 deletions tests/treesitter/ts_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,24 @@ describe("treesitter ts", function()
},
},
},
{
kind = "Function",
name = "g",
level = 0,
lnum = 33,
col = 0,
end_lnum = 33,
end_col = 16,
},
{
kind = "Function",
name = "g_1",
level = 0,
lnum = 34,
col = 0,
end_lnum = 34,
end_col = 28,
},
})
end)
end)
3 changes: 3 additions & 0 deletions tests/treesitter/ts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ describe("UnitTest", () => {
test.each([])("runs multiple times", () => {});
});
});

function* g() {}
const g_1 = function* () {};
18 changes: 18 additions & 0 deletions tests/treesitter/tsx_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,24 @@ describe("treesitter tsx", function()
},
},
},
{
kind = "Function",
name = "g",
level = 0,
lnum = 35,
col = 0,
end_lnum = 35,
end_col = 16,
},
{
kind = "Function",
name = "g_1",
level = 0,
lnum = 36,
col = 0,
end_lnum = 36,
end_col = 28,
},
})
end)
end)
3 changes: 3 additions & 0 deletions tests/treesitter/tsx_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ describe("UnitTest", () => {
test.each([])("runs multiple times", () => {});
});
});

function* g() {}
const g_1 = function* () {};

0 comments on commit 9bcfbaf

Please sign in to comment.