Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ecma): object and array destructuring #29

Merged
merged 2 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/splitjoin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local function get_operable_node_under_cursor(bufnr, winnr)
local tsparser = get_parser(bufnr)
tsparser:parse()
local langtree = tsparser:language_for_range(cursor_range);
local tstree = langtree:tree_for_range(cursor_range, { ignore_injections = false })
local tstree = langtree:tree_for_range(cursor_range, { ignore_injections = false }) or langtree:trees()[1]
if not tstree then return nil, nil end
local lang = langtree:lang()
local query = get_query(lang, 'splitjoin')
Expand Down
13 changes: 13 additions & 0 deletions lua/splitjoin/languages/ecmascript/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@ return {
join = Node.join,
},

object_pattern = {
surround = { '{', '}' },
padding = ' ',
split = Node.split,
join = Node.join,
},

array = {
surround = { '[', ']' },
split = Node.split,
join = Node.join,
},

array_pattern = {
surround = { '[', ']' },
split = Node.split,
join = Node.join,
},

named_imports = {
surround = { '{', '}' },
},
Expand Down
3 changes: 3 additions & 0 deletions lua/splitjoin/util/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ function Node.split(node, options)
Node.goto_node(node)
end

--- default, child-aware joiner
---@param node TSNode
---@param options SplitjoinLanguageOptions
function Node.join(node, options)
local replacement = ''
local sep = options.separator or ','
Expand Down
6 changes: 5 additions & 1 deletion queries/javascript/splitjoin.scm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; collections
(object) @splitjoin.javascript.object
(array) @splitjoin.javascript.array
(object) @splitjoin.javascript.object

; imports
(named_imports) @splitjoin.javascript.imports
Expand All @@ -11,3 +11,7 @@
(arrow_function) @splitjoin.javascript.function.arrow
(arguments) @splitjoin.javascript.function.arguments
(formal_parameters) @splitjoin.javascript.function.parameters

; assignment patterns
(array_pattern) @splitjoin.javascript.array
(object_pattern) @splitjoin.javascript.object
4 changes: 4 additions & 0 deletions queries/typescript/splitjoin.scm
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
(type_arguments)
(named_imports)
] @_splitjoin

; assignment patterns
(array_pattern) @splitjoin.typescript.array
(object_pattern) @splitjoin.typescript.object
4 changes: 4 additions & 0 deletions test/fixtures/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ function f() { return 0; }
const f = function() { return 0; }

const g = () => 0

function destruct({ a, b, c }, d) { return { a, b, c, d }; }

destruct = ({ a, b, c }, d) => 0;
18 changes: 10 additions & 8 deletions test/javascript_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,11 @@ describe(lang, function()
function({ a, b, c }, d) {}
]],
d[[
function(
{ a, b, c },
d,
) {}
function({
a,
b,
c,
}, d) {}
]],
','
)
Expand All @@ -175,10 +176,11 @@ describe(lang, function()
({ a, b, c }, d) => 0
]],
d[[
(
{ a, b, c },
d,
) => 0
({
a,
b,
c,
}, d) => 0
]],
','
)
Expand Down