Skip to content

Commit

Permalink
Add improved internal types for zwitch
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Dec 13, 2022
1 parent 651ce97 commit 515977a
Show file tree
Hide file tree
Showing 21 changed files with 198 additions and 59 deletions.
12 changes: 10 additions & 2 deletions lib/handle/blockquote.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* @typedef {import('mdast').Blockquote} Blockquote
* @typedef {import('../types.js').Handle} Handle
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Content} Content
* @typedef {Root|Content} Node
* @typedef {Extract<Node, import('mdast').Parent>} Parent
* @typedef {import('../types.js').Context} Context
* @typedef {import('../types.js').SafeOptions} SafeOptions
* @typedef {import('../util/indent-lines.js').Map} Map
*/

Expand All @@ -9,8 +14,11 @@ import {indentLines} from '../util/indent-lines.js'
import {track} from '../util/track.js'

/**
* @type {Handle}
* @param {Blockquote} node
* @param {Parent|undefined} _
* @param {Context} context
* @param {SafeOptions} safeOptions
* @returns {string}
*/
export function blockquote(node, _, context, safeOptions) {
const exit = context.enter('blockquote')
Expand Down
16 changes: 12 additions & 4 deletions lib/handle/break.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
/**
* @typedef {import('../types.js').Handle} Handle
* @typedef {import('mdast').Break} Break
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Content} Content
* @typedef {Root|Content} Node
* @typedef {Extract<Node, import('mdast').Parent>} Parent
* @typedef {import('../types.js').Context} Context
* @typedef {import('../types.js').SafeOptions} SafeOptions
*/

import {patternInScope} from '../util/pattern-in-scope.js'

/**
* @type {Handle}
* @param {Break} _
* @param {Parent|undefined} _1
* @param {Context} context
* @param {SafeOptions} safeOptions
* @returns {string}
*/
export function hardBreak(_, _1, context, safe) {
export function hardBreak(_, _1, context, safeOptions) {
let index = -1

while (++index < context.unsafe.length) {
Expand All @@ -19,7 +27,7 @@ export function hardBreak(_, _1, context, safe) {
context.unsafe[index].character === '\n' &&
patternInScope(context.stack, context.unsafe[index])
) {
return /[ \t]/.test(safe.before) ? '' : ' '
return /[ \t]/.test(safeOptions.before) ? '' : ' '
}
}

Expand Down
13 changes: 10 additions & 3 deletions lib/handle/code.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/**
* @typedef {import('mdast').Code} Code
* @typedef {import('../types.js').Handle} Handle
* @typedef {import('../types.js').Exit} Exit
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Content} Content
* @typedef {Root|Content} Node
* @typedef {Extract<Node, import('mdast').Parent>} Parent
* @typedef {import('../types.js').Context} Context
* @typedef {import('../types.js').SafeOptions} SafeOptions
* @typedef {import('../util/indent-lines.js').Map} Map
*/

Expand All @@ -13,8 +17,11 @@ import {safe} from '../util/safe.js'
import {track} from '../util/track.js'

/**
* @type {Handle}
* @param {Code} node
* @param {Parent|undefined} _
* @param {Context} context
* @param {SafeOptions} safeOptions
* @returns {string}
*/
export function code(node, _, context, safeOptions) {
const marker = checkFence(context)
Expand Down
12 changes: 10 additions & 2 deletions lib/handle/definition.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* @typedef {import('mdast').Definition} Definition
* @typedef {import('../types.js').Handle} Handle
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Content} Content
* @typedef {Root|Content} Node
* @typedef {Extract<Node, import('mdast').Parent>} Parent
* @typedef {import('../types.js').Context} Context
* @typedef {import('../types.js').SafeOptions} SafeOptions
*/

import {association} from '../util/association.js'
Expand All @@ -9,8 +14,11 @@ import {safe} from '../util/safe.js'
import {track} from '../util/track.js'

/**
* @type {Handle}
* @param {Definition} node
* @param {Parent|undefined} _
* @param {Context} context
* @param {SafeOptions} safeOptions
* @returns {string}
*/
export function definition(node, _, context, safeOptions) {
const quote = checkQuote(context)
Expand Down
16 changes: 13 additions & 3 deletions lib/handle/emphasis.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* @typedef {import('mdast').Emphasis} Emphasis
* @typedef {import('../types.js').Handle} Handle
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Content} Content
* @typedef {Root|Content} Node
* @typedef {Extract<Node, import('mdast').Parent>} Parent
* @typedef {import('../types.js').Context} Context
* @typedef {import('../types.js').SafeOptions} SafeOptions
*/

import {checkEmphasis} from '../util/check-emphasis.js'
Expand All @@ -14,8 +19,11 @@ emphasis.peek = emphasisPeek
// There’s no way around that though, except for injecting zero-width stuff.
// Do we need to safeguard against that?
/**
* @type {Handle}
* @param {Emphasis} node
* @param {Parent|undefined} _
* @param {Context} context
* @param {SafeOptions} safeOptions
* @returns {string}
*/
export function emphasis(node, _, context, safeOptions) {
const marker = checkEmphasis(context)
Expand All @@ -35,8 +43,10 @@ export function emphasis(node, _, context, safeOptions) {
}

/**
* @type {Handle}
* @param {Emphasis} _
* @param {Parent|undefined} _1
* @param {Context} context
* @returns {string}
*/
function emphasisPeek(_, _1, context) {
return context.options.emphasis || '*'
Expand Down
13 changes: 10 additions & 3 deletions lib/handle/heading.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
/**
* @typedef {import('mdast').Heading} Heading
* @typedef {import('../types.js').Handle} Handle
* @typedef {import('../types.js').Exit} Exit
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Content} Content
* @typedef {Root|Content} Node
* @typedef {Extract<Node, import('mdast').Parent>} Parent
* @typedef {import('../types.js').Context} Context
* @typedef {import('../types.js').SafeOptions} SafeOptions
*/

import {formatHeadingAsSetext} from '../util/format-heading-as-setext.js'
import {containerPhrasing} from '../util/container-phrasing.js'
import {track} from '../util/track.js'

/**
* @type {Handle}
* @param {Heading} node
* @param {Parent|undefined} _
* @param {Context} context
* @param {SafeOptions} safeOptions
* @returns {string}
*/
export function heading(node, _, context, safeOptions) {
const rank = Math.max(Math.min(6, node.depth || 1), 1)
Expand Down
8 changes: 5 additions & 3 deletions lib/handle/html.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
/**
* @typedef {import('mdast').HTML} HTML
* @typedef {import('../types.js').Handle} Handle
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Content} Content
* @typedef {Root|Content} Node
*/

html.peek = htmlPeek

/**
* @type {Handle}
* @param {HTML} node
* @returns {string}
*/
export function html(node) {
return node.value || ''
}

/**
* @type {Handle}
* @returns {string}
*/
function htmlPeek() {
return '<'
Expand Down
14 changes: 11 additions & 3 deletions lib/handle/image-reference.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* @typedef {import('mdast').ImageReference} ImageReference
* @typedef {import('../types.js').Handle} Handle
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Content} Content
* @typedef {Root|Content} Node
* @typedef {Extract<Node, import('mdast').Parent>} Parent
* @typedef {import('../types.js').Context} Context
* @typedef {import('../types.js').SafeOptions} SafeOptions
*/

import {association} from '../util/association.js'
Expand All @@ -10,8 +15,11 @@ import {track} from '../util/track.js'
imageReference.peek = imageReferencePeek

/**
* @type {Handle}
* @param {ImageReference} node
* @param {Parent|undefined} _
* @param {Context} context
* @param {SafeOptions} safeOptions
* @returns {string}
*/
export function imageReference(node, _, context, safeOptions) {
const type = node.referenceType
Expand Down Expand Up @@ -57,7 +65,7 @@ export function imageReference(node, _, context, safeOptions) {
}

/**
* @type {Handle}
* @returns {string}
*/
function imageReferencePeek() {
return '!'
Expand Down
14 changes: 11 additions & 3 deletions lib/handle/image.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* @typedef {import('mdast').Image} Image
* @typedef {import('../types.js').Handle} Handle
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Content} Content
* @typedef {Root|Content} Node
* @typedef {Extract<Node, import('mdast').Parent>} Parent
* @typedef {import('../types.js').Context} Context
* @typedef {import('../types.js').SafeOptions} SafeOptions
*/

import {checkQuote} from '../util/check-quote.js'
Expand All @@ -10,8 +15,11 @@ import {track} from '../util/track.js'
image.peek = imagePeek

/**
* @type {Handle}
* @param {Image} node
* @param {Parent|undefined} _
* @param {Context} context
* @param {SafeOptions} safeOptions
* @returns {string}
*/
export function image(node, _, context, safeOptions) {
const quote = checkQuote(context)
Expand Down Expand Up @@ -74,7 +82,7 @@ export function image(node, _, context, safeOptions) {
}

/**
* @type {Handle}
* @returns {string}
*/
function imagePeek() {
return '!'
Expand Down
12 changes: 9 additions & 3 deletions lib/handle/inline-code.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
/**
* @typedef {import('mdast').InlineCode} InlineCode
* @typedef {import('../types.js').Handle} Handle
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Content} Content
* @typedef {Root|Content} Node
* @typedef {Extract<Node, import('mdast').Parent>} Parent
* @typedef {import('../types.js').Context} Context
*/

import {patternCompile} from '../util/pattern-compile.js'

inlineCode.peek = inlineCodePeek

/**
* @type {Handle}
* @param {InlineCode} node
* @param {Parent|undefined} _
* @param {Context} context
* @returns {string}
*/
export function inlineCode(node, _, context) {
let value = node.value || ''
Expand Down Expand Up @@ -69,7 +75,7 @@ export function inlineCode(node, _, context) {
}

/**
* @type {Handle}
* @returns {string}
*/
function inlineCodePeek() {
return '`'
Expand Down
14 changes: 11 additions & 3 deletions lib/handle/link-reference.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* @typedef {import('mdast').LinkReference} LinkReference
* @typedef {import('../types.js').Handle} Handle
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Content} Content
* @typedef {Root|Content} Node
* @typedef {Extract<Node, import('mdast').Parent>} Parent
* @typedef {import('../types.js').Context} Context
* @typedef {import('../types.js').SafeOptions} SafeOptions
*/

import {association} from '../util/association.js'
Expand All @@ -11,8 +16,11 @@ import {track} from '../util/track.js'
linkReference.peek = linkReferencePeek

/**
* @type {Handle}
* @param {LinkReference} node
* @param {Parent|undefined} _
* @param {Context} context
* @param {SafeOptions} safeOptions
* @returns {string}
*/
export function linkReference(node, _, context, safeOptions) {
const type = node.referenceType
Expand Down Expand Up @@ -58,7 +66,7 @@ export function linkReference(node, _, context, safeOptions) {
}

/**
* @type {Handle}
* @returns {string}
*/
function linkReferencePeek() {
return '['
Expand Down
16 changes: 13 additions & 3 deletions lib/handle/link.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* @typedef {import('mdast').Link} Link
* @typedef {import('../types.js').Handle} Handle
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Content} Content
* @typedef {Root|Content} Node
* @typedef {Extract<Node, import('mdast').Parent>} Parent
* @typedef {import('../types.js').Context} Context
* @typedef {import('../types.js').SafeOptions} SafeOptions
* @typedef {import('../types.js').Exit} Exit
*/

Expand All @@ -13,8 +18,11 @@ import {track} from '../util/track.js'
link.peek = linkPeek

/**
* @type {Handle}
* @param {Link} node
* @param {Parent|undefined} _
* @param {Context} context
* @param {SafeOptions} safeOptions
* @returns {string}
*/
export function link(node, _, context, safeOptions) {
const quote = checkQuote(context)
Expand Down Expand Up @@ -104,8 +112,10 @@ export function link(node, _, context, safeOptions) {
}

/**
* @type {Handle}
* @param {Link} node
* @param {Parent|undefined} _
* @param {Context} context
* @returns {string}
*/
function linkPeek(node, _, context) {
return formatLinkAsAutolink(node, context) ? '<' : '['
Expand Down
Loading

0 comments on commit 515977a

Please sign in to comment.