Skip to content

Commit

Permalink
Refactor to simplify types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 5, 2021
1 parent b15cecf commit c48b477
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
with:
node-version: ${{matrix.node}}
- run: npm install
- run: '# npm test'
- run: npm test
- uses: codecov/codecov-action@v1
strategy:
matrix:
Expand Down
55 changes: 12 additions & 43 deletions lib/from-mdast.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,7 @@ function linkReference(node, context) {
*/
function list(node, context) {
// @ts-ignore always valid content.
return inherit(node, {
type: 'list',
children: parentOfNodes(node, context)
})
return inherit(node, {type: 'list', children: parent(node, context)})
}

/**
Expand Down Expand Up @@ -323,7 +320,8 @@ function root(node, context) {
type: 'root',
children: wrap(
context,
parentOfNodes(node, context).concat(flush(context, true))
// @ts-ignore assume valid content.
parent(node, context).concat(flush(context, true))
)
})
}
Expand All @@ -337,7 +335,7 @@ function table(node, context) {
return inherit(node, {
type: 'pre',
alt: context.dsvName,
value: parentOfStrings(node, context).join('\n') || ''
value: parent(node, context).join('\n') || ''
})
}

Expand All @@ -360,7 +358,7 @@ function tableCell(node, context) {
* @returns {string}
*/
function tableRow(node, context) {
return parentOfStrings(node, context).join(context.dsvDelimiter)
return parent(node, context).join(context.dsvDelimiter)
}

function ignore() {}
Expand All @@ -378,7 +376,7 @@ function literal(node) {
* @returns {string}
*/
function flow(node, context) {
var nodes = parentOfNodes(node, context)
var nodes = parent(node, context)
/** @type {string[]} */
var results = []
var index = -1
Expand All @@ -397,49 +395,20 @@ function flow(node, context) {
* @returns {string}
*/
function phrasing(node, context) {
return parentOfStrings(node, context).join('').replace(/\r?\n/g, ' ')
}

/**
* @param {MdastTable|MdastTableRow|MdastHeading|MdastLink|MdastLinkReference|MdastParagraph|MdastTableCell} node
* @param {Context} context
* @returns {string[]}
*/
function parentOfStrings(node, context) {
var children = node.children || []
/** @type {string[]} */
var results = []
var index = -1
/** @type {string|string[]|void} */
var value

while (++index < children.length) {
// @ts-ignore too many overloads
value = handle(children[index], context)

if (value) {
if (typeof value === 'object' && 'length' in value) {
push.apply(results, value)
} else {
results.push(value)
}
}
}

return results
return parent(node, context).join('').replace(/\r?\n/g, ' ')
}

/**
* @param {MdastRoot|MdastList|MdastBlockquote|MdastListItem|MdastFootnoteDefinition} node
* @param {UnistParent} node
* @param {Context} context
* @returns {Array.<GtastNode>}
* @returns {Array.<unknown>}
*/
function parentOfNodes(node, context) {
function parent(node, context) {
var children = node.children || []
/** @type {GtastNode[]} */
/** @type {Array.<unknown>} */
var results = []
var index = -1
/** @type {GtastNode|GtastNode[]|void} */
/** @type {Array.<unknown>|unknown} */
var value

while (++index < children.length) {
Expand Down

0 comments on commit c48b477

Please sign in to comment.