Skip to content

Commit

Permalink
Change to expect, yield undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 16, 2023
1 parent 9fd2dfa commit ffbb8a8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 37 deletions.
2 changes: 1 addition & 1 deletion lib/handlers/list-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Info passed around.
* @param {ListItem} node
* mdast node.
* @param {Parents | null | undefined} parent
* @param {Parents | undefined} parent
* Parent of `node`.
* @returns {Element}
* hast node.
Expand Down
2 changes: 1 addition & 1 deletion lib/handlers/table-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Info passed around.
* @param {TableRow} node
* mdast node.
* @param {Parents | null | undefined} parent
* @param {Parents | undefined} parent
* Parent of `node`.
* @returns {Element}
* hast node.
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import {createState} from './state.js'
* mdast tree.
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {HastNodes | null | undefined}
* @returns {HastNodes | undefined}
* hast tree.
*/
// To do: next major: always return a single `root`.
Expand Down
47 changes: 16 additions & 31 deletions lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
/**
* @typedef EmbeddedHastFields
* hast fields.
* @property {string | null | undefined} [hName]
* @property {string | undefined} [hName]
* Generate a specific element with this tag name instead.
* @property {HastProperties | null | undefined} [hProperties]
* @property {HastProperties | undefined} [hProperties]
* Generate an element with these properties instead.
* @property {Array<HastElementContent> | null | undefined} [hChildren]
* @property {Array<HastElementContent> | undefined} [hChildren]
* Generate an element with this content instead.
*
* To do: type data!
Expand All @@ -29,36 +29,20 @@
*
* To do: type data!
*
* @typedef {MdastNodes & {data?: MdastData | null | undefined}} MdastNodeWithData
* @typedef {MdastNodes & {data?: MdastData | undefined}} MdastNodeWithData
* mdast node with embedded hast data.
*
* To do: type data!
*
* @typedef PointLike
* Point-like value.
* @property {number | null | undefined} [line]
* Line.
* @property {number | null | undefined} [column]
* Column.
* @property {number | null | undefined} [offset]
* Offset.
*
* @typedef PositionLike
* Position-like value.
* @property {PointLike | null | undefined} [start]
* Point-like value.
* @property {PointLike | null | undefined} [end]
* Point-like value.
*
* @callback Handler
* Handle a node.
* @param {State} state
* Info passed around.
* @param {any} node
* mdast node to handle.
* @param {MdastParents | null | undefined} parent
* @param {MdastParents | undefined} parent
* Parent of `node`.
* @returns {Array<HastElementContent> | HastElementContent | null | undefined}
* @returns {Array<HastElementContent> | HastElementContent | undefined}
* hast node.
*
* @typedef State
Expand All @@ -77,13 +61,13 @@
* Identifiers of order when footnote calls first appear in tree order.
* @property {Handlers} handlers
* Applied handlers.
* @property {(node: MdastNodes, parent: MdastParents | null | undefined) => Array<HastElementContent> | HastElementContent | null | undefined} one
* @property {(node: MdastNodes, parent: MdastParents | undefined) => Array<HastElementContent> | HastElementContent | undefined} one
* Transform an mdast node to hast.
* @property {Options} options
* Configuration.
* @property {(from: MdastNodes, node: HastNodes) => void} patch
* Copy a node’s positional info.
* @property {<Type extends HastRootContent>(nodes: Array<Type>, loose?: boolean | null | undefined) => Array<HastText | Type>} wrap
* @property {<Type extends HastRootContent>(nodes: Array<Type>, loose?: boolean | undefined) => Array<HastText | Type>} wrap
* Wrap `nodes` with line endings between each node, adds initial/final line endings when `loose`.
*
* @typedef Options
Expand Down Expand Up @@ -121,7 +105,7 @@

import {visit} from 'unist-util-visit'
import {position} from 'unist-util-position'
import {handlers} from './handlers/index.js'
import {handlers as defaultHandlers} from './handlers/index.js'

const own = {}.hasOwnProperty

Expand All @@ -143,6 +127,10 @@ export function createState(tree, options) {
const footnoteById = new Map()
/** @type {Map<string, number>} */
const footnoteCounts = new Map()
/** @type {Handlers} */
// @ts-expect-error: the root handler returns a root.
// Hard to type.
const handlers = {...defaultHandlers, ...settings.handlers}

/** @type {State} */
const state = {
Expand All @@ -152,13 +140,10 @@ export function createState(tree, options) {
footnoteById,
footnoteCounts,
footnoteOrder: [],
// @ts-expect-error: fix `null` handling?
handlers: {...handlers, ...settings.handlers},
// @ts-expect-error: fix `null` handling.
handlers,
one: oneBound,
options: settings,
patch,
// @ts-expect-error: fix `null` handling.
wrap
}

Expand All @@ -185,7 +170,7 @@ export function createState(tree, options) {
* mdast node.
* @param {MdastParents | undefined} [parent]
* Parent of `node`.
* @returns {Array<HastElementContent> | HastElementContent | null | undefined}
* @returns {Array<HastElementContent> | HastElementContent | undefined}
* Resulting hast node.
*/
function oneBound(node, parent) {
Expand Down Expand Up @@ -298,7 +283,7 @@ function applyData(from, to) {
* mdast node.
* @param {MdastParents | undefined} [parent]
* Parent of `node`.
* @returns {Array<HastElementContent> | HastElementContent | null | undefined}
* @returns {Array<HastElementContent> | HastElementContent | undefined}
* Resulting hast node.
*/
// To do: next major: do not expose, keep bound.
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Transform mdast to hast.

###### Returns

hast tree ([`HastNode | null | undefined`][hast-node]).
hast tree ([`HastNode | undefined`][hast-node]).

##### Notes

Expand Down Expand Up @@ -214,12 +214,12 @@ Handle a node (TypeScript).
— info passed around
* `node` ([`MdastNode`][mdast-node])
— node to handle
* `parent` ([`MdastNode | null | undefined`][mdast-node])
* `parent` ([`MdastNode | undefined`][mdast-node])
— parent of `node`

###### Returns

Result ([`HastNode | Array<HastNode> | null | undefined`][mdast-node]).
Result ([`Array<HastNode> | HastNode | undefined`][hast-node]).

### `Handlers`

Expand Down

0 comments on commit ffbb8a8

Please sign in to comment.