diff --git a/lua/splitjoin/languages/ecmascript/defaults.lua b/lua/splitjoin/languages/ecmascript/defaults.lua index cc14bfd..635d7dc 100644 --- a/lua/splitjoin/languages/ecmascript/defaults.lua +++ b/lua/splitjoin/languages/ecmascript/defaults.lua @@ -62,6 +62,11 @@ return { join = ECMAScript.join_arrow_function, }, + comment = { + split = ECMAScript.split_jsdoc_comment, + join = ECMAScript.join_jsdoc_comment, + } + }, } diff --git a/lua/splitjoin/languages/ecmascript/functions.lua b/lua/splitjoin/languages/ecmascript/functions.lua index 38cd95b..fb1a45f 100644 --- a/lua/splitjoin/languages/ecmascript/functions.lua +++ b/lua/splitjoin/languages/ecmascript/functions.lua @@ -98,4 +98,33 @@ function ECMAScript.join_arrow_function(node, options) Node.goto_node(node) end +function ECMAScript.split_jsdoc_comment(node, options) + local indent = options.default_indent or ' ' + for child in node:iter_children() do + if child:type() == 'tag' then return end + end + local append, get = String.append('') + append( + '\n', + indent, + ' *', + Node.get_text(node:child(0)), + '\n' + ) + Node.replace(node, get()) + Node.goto_node(node:child(0)) +end + +function ECMAScript.join_jsdoc_comment(node, options) + local indent = options.default_indent or ' ' + for child in node:iter_children() do + if child:type() == 'tag' then return end + end + local append, get = String.append('') + append('\n'..indent..' *') + append(Node.get_text(node:child(0))) + append('\n') + Node.replace(node, get()) + Node.goto_node(node:child(0)) +end return ECMAScript diff --git a/test/fixtures/fixture.js b/test/fixtures/fixture.js index 31178d4..bd74865 100644 --- a/test/fixtures/fixture.js +++ b/test/fixtures/fixture.js @@ -23,3 +23,10 @@ const g = () => 0 function destruct({ a, b, c }, d) { return { a, b, c, d }; } destruct = ({ a, b, c }, d) => 0; + +/** jsdoc */ +const jsdoc = (thing) => thing + +/** jsdoc @param {number} thing + */ +const jsdoc = (thing) => thing diff --git a/test/javascript_spec.lua b/test/javascript_spec.lua index da6d976..a043b52 100644 --- a/test/javascript_spec.lua +++ b/test/javascript_spec.lua @@ -313,6 +313,22 @@ describe(lang, function() ) end) + describe('jsdoc', function() + H.make_suite(lang, '', + d[[ + /** jsdoc */ + const x = y => y + ]], + d[[ + /** + * jsdoc + */ + const x = y => y + ]], + 'j' + ) + end) + describe('const f = function() { return 0; }', function() H.make_suite(lang, '', d[[