Skip to content

Commit

Permalink
feat: jest support for typescript (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Feb 4, 2023
1 parent 4428a47 commit 960cf86
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lua/aerial/backends/treesitter/extensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@ M.typescript = {
item.kind = "Function"
end
end
local method = (utils.get_at_path(match, "method") or {}).node
local modifier = (utils.get_at_path(match, "modifier") or {}).node
local string = (utils.get_at_path(match, "string") or {}).node
if method and string then
local fn = vim.treesitter.query.get_node_text(method, bufnr) or "<parse error>"
if modifier then
fn = fn .. "." .. (vim.treesitter.query.get_node_text(modifier, bufnr) or "<parse error>")
end
local str = vim.treesitter.query.get_node_text(string, bufnr) or "<parse error>"
item.name = fn .. " " .. str
end
end,
}

Expand Down
35 changes: 35 additions & 0 deletions queries/typescript/aerial.scm
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,38 @@
value: (_) @var_type) @type
(#set! "kind" "Variable")
) @start

; describe("Unit test")
(call_expression
function: (identifier) @method @name (#any-of? @method "describe" "it" "test" "afterAll" "afterEach" "beforeAll" "beforeEach")
arguments: (arguments
(string
(string_fragment) @name @string))?
(#set! "kind" "Function")
) @type

; test.skip("this test")
(call_expression
function: (member_expression
object: (identifier) @method (#any-of? @method "describe" "it" "test")
property: (property_identifier) @modifier (#any-of? @modifier "skip" "todo")
) @name
arguments: (arguments
(string
(string_fragment) @name @string))?
(#set! "kind" "Function")
) @type

; describe.each([])("Test suite")
(call_expression
function: (call_expression
function: (member_expression
object: (identifier) @method (#any-of? @method "describe" "it" "test")
property: (property_identifier) @modifier (#any-of? @modifier "each")
) @name
)
arguments: (arguments
(string
(string_fragment) @name @string))?
(#set! "kind" "Function")
) @type
103 changes: 103 additions & 0 deletions tests/treesitter/ts_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,109 @@ describe("treesitter ts", function()
end_lnum = 16,
end_col = 22,
},
{
kind = "Function",
name = "describe UnitTest",
level = 0,
lnum = 18,
col = 0,
end_lnum = 30,
end_col = 2,
children = {
{
kind = "Function",
name = "afterAll",
level = 1,
lnum = 19,
col = 2,
end_lnum = 19,
end_col = 20,
},
{
kind = "Function",
name = "afterEach",
level = 1,
lnum = 20,
col = 2,
end_lnum = 20,
end_col = 21,
},
{
kind = "Function",
name = "beforeAll",
level = 1,
lnum = 21,
col = 2,
end_lnum = 21,
end_col = 21,
},
{
kind = "Function",
name = "beforeEach",
level = 1,
lnum = 22,
col = 2,
end_lnum = 22,
end_col = 22,
},
{
kind = "Function",
name = "test should describe the test",
level = 1,
lnum = 23,
col = 2,
end_lnum = 23,
end_col = 44,
},
{
kind = "Function",
name = "it is an alias for test",
level = 1,
lnum = 24,
col = 2,
end_lnum = 24,
end_col = 38,
},
{
kind = "Function",
name = "test.skip skip this test",
level = 1,
lnum = 25,
col = 2,
end_lnum = 25,
end_col = 39,
},
{
kind = "Function",
name = "test.todo this is a todo",
level = 1,
lnum = 26,
col = 2,
end_lnum = 26,
end_col = 29,
},
{
kind = "Function",
name = "describe.each Test Suite",
level = 1,
lnum = 27,
col = 2,
end_lnum = 29,
end_col = 4,
children = {
{
kind = "Function",
name = "test.each runs multiple times",
level = 2,
lnum = 28,
col = 4,
end_lnum = 28,
end_col = 50,
},
},
},
},
},
})
end)
end)
14 changes: 14 additions & 0 deletions tests/treesitter/ts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ const fn_3 = () => {};

const const_var = 'value';
let let_var = 'value';

describe("UnitTest", () => {
afterAll(() => {});
afterEach(() => {});
beforeAll(() => {});
beforeEach(() => {});
test("should describe the test", () => {});
it("is an alias for test", () => {});
test.skip("skip this test", () => {});
test.todo("this is a todo");
describe.each([])("Test Suite", () => {
test.each([])("runs multiple times", () => {});
});
});

0 comments on commit 960cf86

Please sign in to comment.