Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
*   Add more docs to JSDoc
  • Loading branch information
wooorm committed Jan 23, 2023
1 parent 8eca708 commit 401a2de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
19 changes: 10 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,38 @@
*/

/**
* @template {Node} [Tree=Node]
* @typedef {import('./complex-types.js').MapFunction<Tree>} MapFunction
* @template {Node} [Kind=Node]
* Node type.
* @typedef {import('./complex-types.js').MapFunction<Kind>} MapFunction
* Function called with a node, its index, and its parent to produce a new
* node.
*/

/**
* Create a new tree by mapping all nodes with the given function.
*
* @template {Node} Tree
* @template {Node} Kind
* Type of input tree.
* @param {Tree} tree
* @param {Kind} tree
* Tree to map.
* @param {MapFunction<Tree>} mapFunction
* @param {MapFunction<Kind>} mapFunction
* Function called with a node, its index, and its parent to produce a new
* node.
* @returns {Tree}
* @returns {Kind}
* New mapped tree.
*/
export function map(tree, mapFunction) {
// @ts-expect-error Looks like a children.
return preorder(tree, null, null)

/** @type {import('./complex-types.js').MapFunction<Tree>} */
/** @type {import('./complex-types.js').MapFunction<Kind>} */
function preorder(node, index, parent) {
var newNode = Object.assign({}, mapFunction(node, index, parent))
const newNode = Object.assign({}, mapFunction(node, index, parent))

if ('children' in node) {
// @ts-expect-error Looks like a parent.
newNode.children = node.children.map(function (
/** @type {import('./complex-types.js').InclusiveDescendant<Tree>} */ child,
/** @type {import('./complex-types.js').InclusiveDescendant<Kind>} */ child,
/** @type {number} */ index
) {
return preorder(child, index, node)
Expand Down
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@
"trailingComma": "none"
},
"xo": {
"prettier": true,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off"
}
"prettier": true
},
"remarkConfig": {
"plugins": [
Expand Down

0 comments on commit 401a2de

Please sign in to comment.