Skip to content

Commit

Permalink
fix(jsx): correctly comment first line of JSX
Browse files Browse the repository at this point in the history
The first and last lines of JSX should be commented with `// %s`, rather
than `{/* %s */}`. However, the current way of configuring the plugin
does not allow us to check if the current node is a `jsx_element` with a
parent of `parenthesized_expression`. We could allow checking the parent
node in the configuration, but that would be a bit tricky and a lot of
work.

Since this is (hopefully) a 1-time exception, then it seems okay to add
an explicit check for this. If we have similar cases in the future
though, then we should think of making this configurable somehow.

Fixes #29
  • Loading branch information
JoosepAlviste committed Dec 31, 2022
1 parent 4a42b30 commit 3d1f244
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lua/ts_context_commentstring/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,21 @@ function M.check_node(node, language_config, commentstring_key)
end

local node_type = node:type()
local match = language_config[node_type]

-- A workaround for an edge case in JSX.
-- The first line of JSX should be commented with `// %s` rather than
-- `{/* %s */}` but the general logic does not allow us to configure that
-- behaviour. This is a bit of an edge case though, and so we have this
-- workaround here rather than allowing for more complex configuration.
-- For more information, see:
-- https://github.com/JoosepAlviste/nvim-ts-context-commentstring/issues/29
-- NOTE: We should NOT add any more of these sort of workarounds
local is_first_line_of_jsx = node_type == 'jsx_element' and node:parent():type() == 'parenthesized_expression'
if is_first_line_of_jsx then
return language_config[commentstring_key] or language_config.__default or language_config
end

local match = language_config[node_type]
if match then
return match[commentstring_key] or match.__default or match
end
Expand Down

0 comments on commit 3d1f244

Please # to comment.