Skip to content

Commit

Permalink
feat(javascript): support switch statement
Browse files Browse the repository at this point in the history
  • Loading branch information
Goose97 committed Nov 21, 2024
1 parent ce490ac commit 12e7130
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 4 deletions.
22 changes: 22 additions & 0 deletions queries/javascript/neolog-log-container.scm
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,25 @@
condition: (parenthesized_expression) @log_container
consequence: (statement_block) @logable_range (#adjust-range! @logable_range 1 -1)
)

(switch_statement
value: (parenthesized_expression) @log_container
)

(switch_statement
body: (switch_body
(switch_case
value: (_) @log_container
body: (statement_block) @logable_range (#adjust-range! @logable_range 1 -1)
)
)
)

(switch_statement
body: (switch_body
(switch_case
value: (_) @log_container
body: (_) @logable_range (#not-match? @logable_range "^\\{")
)
)
)
116 changes: 114 additions & 2 deletions tests/neolog/actions/lang/javascript_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ describe("javascript", function()
end)
end)

describe("supports try/catch clause", function()
describe("supports try/catch statement", function()
it("supports plain parameters", function()
local actions = require("neolog.actions")

Expand Down Expand Up @@ -434,7 +434,7 @@ describe("javascript", function()
end)
end)

it("supports if clause", function()
it("supports if statement", function()
local actions = require("neolog.actions")

local input = [[
Expand Down Expand Up @@ -475,6 +475,118 @@ describe("javascript", function()
})
end)

describe("supports switch statement", function()
it("supports switch head", function()
local actions = require("neolog.actions")

local input = [[
switch (fo|o) {
case bar:
break
case "baz":
break
}
]]

-- This is invalid syntax but it's a delibarate choice
-- We want the switch statement log contaienr to be more granular
-- So instead of matching the whole switch statement, we match against switch head
-- and individual clauses
helper.assert_scenario({
input = input,
filetype = "javascript",
action = function()
actions.add_log({ log_template = [[console.log("%identifier", %identifier)]], position = "below" })
end,
expected = [[
switch (foo) {
console.log("foo", foo)
case bar:
break
case "baz":
break
}
]],
})

helper.assert_scenario({
input = input,
filetype = "javascript",
action = function()
actions.add_log({ log_template = [[console.log("%identifier", %identifier)]], position = "above" })
end,
expected = [[
console.log("foo", foo)
switch (foo) {
case bar:
break
case "baz":
break
}
]],
})
end)

it("supports switch clause", function()
local actions = require("neolog.actions")

helper.assert_scenario({
input = [[
switch (foo) {
case ba|r:
break
case "baz":
break
}
]],
filetype = "javascript",
action = function()
actions.add_log({ log_template = [[console.log("%identifier", %identifier)]], position = "below" })
end,
expected = [[
switch (foo) {
case bar:
console.log("bar", bar)
break
case "baz":
break
}
]],
})

helper.assert_scenario({
input = [[
switch (foo) {
case (ba|r + baz): {
break
}
case "baz":
const baz = "baz"
break
}
]],
filetype = "javascript",
action = function()
vim.cmd("normal! vi{V")
actions.add_log({ log_template = [[console.log("%identifier", %identifier)]], position = "below" })
end,
expected = [[
switch (foo) {
case (bar + baz): {
console.log("bar", bar)
console.log("baz", baz)
break
}
case "baz":
const baz = "baz"
console.log("baz", baz)
break
}
]],
})
end)
end)

describe("supports import statements", function()
it("supports plain imports", function()
local actions = require("neolog.actions")
Expand Down
117 changes: 115 additions & 2 deletions tests/neolog/actions/lang/typescript_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ describe("typescript", function()
end)
end)

describe("supports try/catch clause", function()
describe("supports try/catch statement", function()
it("supports plain parameters", function()
local actions = require("neolog.actions")

Expand Down Expand Up @@ -486,7 +486,7 @@ describe("typescript", function()
end)
end)

it("supports if clause", function()
it("supports if statement", function()
local actions = require("neolog.actions")

local input = [[
Expand Down Expand Up @@ -527,6 +527,119 @@ describe("typescript", function()
})
end)

describe("supports switch statement", function()
it("supports switch head", function()
local actions = require("neolog.actions")

local input = [[
switch (fo|o) {
case bar:
break
case "baz":
break
}
]]

-- This is invalid syntax but it's a delibarate choice
-- We want the switch statement log contaienr to be more granular
-- So instead of matching the whole switch statement, we match against switch head
-- and individual clauses
helper.assert_scenario({
input = input,
filetype = "typescript",
action = function()
actions.add_log({ log_template = [[console.log("%identifier", %identifier)]], position = "below" })
end,
expected = [[
switch (foo) {
console.log("foo", foo)
case bar:
break
case "baz":
break
}
]],
})

helper.assert_scenario({
input = input,
filetype = "typescript",
action = function()
actions.add_log({ log_template = [[console.log("%identifier", %identifier)]], position = "above" })
end,
expected = [[
console.log("foo", foo)
switch (foo) {
case bar:
break
case "baz":
break
}
]],
})
end)

it("supports switch clause", function()
local actions = require("neolog.actions")

helper.assert_scenario({
input = [[
switch (foo) {
case ba|r:
break
case "baz":
break
}
]],
filetype = "typescript",
action = function()
actions.add_log({ log_template = [[console.log("%identifier", %identifier)]], position = "below" })
end,
expected = [[
switch (foo) {
case bar:
console.log("bar", bar)
break
case "baz":
break
}
]],
})

-- TODO: figure out why indentation is off with inner switch clause
helper.assert_scenario({
input = [[
switch (foo) {
case (ba|r + baz): {
break
}
case "baz":
const baz = "baz"
break
}
]],
filetype = "typescript",
action = function()
vim.cmd("normal! vi{V")
actions.add_log({ log_template = [[console.log("%identifier", %identifier)]], position = "below" })
end,
expected = [[
switch (foo) {
case (bar + baz): {
console.log("bar", bar)
console.log("baz", baz)
break
}
case "baz":
const baz = "baz"
console.log("baz", baz)
break
}
]],
})
end)
end)

describe("supports import statements", function()
it("supports plain imports", function()
local actions = require("neolog.actions")
Expand Down

0 comments on commit 12e7130

Please sign in to comment.