Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 6, 2023
1 parent ce75ece commit 1470a23
Show file tree
Hide file tree
Showing 50 changed files with 3,100 additions and 2,255 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @typedef {import('./lib/types.js').CharacterReferences} CharacterReferences
* @typedef {import('./lib/types.js').Options} Options
* @typedef {import('./lib/types.js').Quote} Quote
* @typedef {import('./lib/types.js').Space} Space
* @typedef {import('./lib/index.js').CharacterReferences} CharacterReferences
* @typedef {import('./lib/index.js').Options} Options
* @typedef {import('./lib/index.js').Quote} Quote
* @typedef {import('./lib/index.js').Space} Space
*/

export {toHtml} from './lib/index.js'
7 changes: 4 additions & 3 deletions lib/handle/comment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* @typedef {import('../types.js').Comment} Comment
* @typedef {import('../types.js').Parents} Parents
* @typedef {import('../types.js').State} State
* @typedef {import('hast').Comment} Comment
* @typedef {import('hast').Parents} Parents
*
* @typedef {import('../index.js').State} State
*/

import {stringifyEntities} from 'stringify-entities'
Expand Down
12 changes: 8 additions & 4 deletions lib/handle/doctype.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/**
* @typedef {import('../types.js').DocType} DocType
* @typedef {import('../types.js').Parents} Parents
* @typedef {import('../types.js').State} State
* @typedef {import('hast').Doctype} Doctype
* @typedef {import('hast').Parents} Parents
*
* @typedef {import('../index.js').State} State
*/

// Make VS code see references to the above types.
''

/**
* Serialize a doctype.
*
* @param {DocType} _1
* @param {Doctype} _1
* Node to handle.
* @param {number | undefined} _2
* Index of `node` in `parent.
Expand Down
30 changes: 14 additions & 16 deletions lib/handle/element.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* @typedef {import('../types.js').State} State
* @typedef {import('../types.js').Parents} Parents
* @typedef {import('../types.js').Element} Element
* @typedef {import('../types.js').Properties} Properties
* @typedef {import('../types.js').PropertyValue} PropertyValue
* @typedef {import('hast').Element} Element
* @typedef {import('hast').Parents} Parents
* @typedef {import('hast').Properties} Properties
*
* @typedef {import('../index.js').State} State
*/

import {ccount} from 'ccount'
import {stringify as commas} from 'comma-separated-tokens'
import {svg, find} from 'property-information'
import {find, svg} from 'property-information'
import {stringify as spaces} from 'space-separated-tokens'
import {stringifyEntities} from 'stringify-entities'
import {opening} from '../omission/opening.js'
import {closing} from '../omission/closing.js'
import {opening} from '../omission/opening.js'

/**
* Maps of subsets.
Expand All @@ -21,7 +21,7 @@ import {closing} from '../omission/closing.js'
* The value at `0` causes parse errors, the value at `1` is valid.
* Of both, the value at `0` is unsafe, and the value at `1` is safe.
*
* @type {Record<'name' | 'unquoted' | 'single' | 'double', Array<[Array<string>, Array<string>]>>}
* @type {Record<'double' | 'name' | 'single' | 'unquoted', Array<[Array<string>, Array<string>]>>}
*/
const constants = {
// See: <https://html.spec.whatwg.org/#attribute-name-state>.
Expand Down Expand Up @@ -60,7 +60,6 @@ const constants = {
* @returns {string}
* Serialized node.
*/
// eslint-disable-next-line complexity
export function element(node, index, parent, state) {
const schema = state.schema
const omit = schema.space === 'svg' ? false : state.settings.omitOptionalTags
Expand Down Expand Up @@ -138,7 +137,7 @@ function serializeAttributes(state, props) {

if (props) {
for (key in props) {
if (props[key] !== undefined && props[key] !== null) {
if (props[key] !== null && props[key] !== undefined) {
const value = serializeAttribute(state, key, props[key])
if (value) values.push(value)
}
Expand All @@ -148,7 +147,7 @@ function serializeAttributes(state, props) {
while (++index < values.length) {
const last = state.settings.tightAttributes
? values[index].charAt(values[index].length - 1)
: null
: undefined

// In tight mode, don’t add a space after quoted attributes.
if (index !== values.length - 1 && last !== '"' && last !== "'") {
Expand All @@ -162,10 +161,9 @@ function serializeAttributes(state, props) {
/**
* @param {State} state
* @param {string} key
* @param {PropertyValue} value
* @param {Properties[keyof Properties]} value
* @returns {string}
*/
// eslint-disable-next-line complexity
function serializeAttribute(state, key, value) {
const info = find(state.schema, key)
const x =
Expand All @@ -185,8 +183,8 @@ function serializeAttribute(state, key, value) {
}

if (
value === undefined ||
value === null ||
value === undefined ||
value === false ||
(typeof value === 'number' && Number.isNaN(value))
) {
Expand Down Expand Up @@ -235,8 +233,8 @@ function serializeAttribute(state, key, value) {
result = stringifyEntities(
value,
Object.assign({}, state.settings.characterReferences, {
subset: constants.unquoted[x][y],
attribute: true
attribute: true,
subset: constants.unquoted[x][y]
})
)
}
Expand Down
14 changes: 8 additions & 6 deletions lib/handle/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* @typedef {import('../types.js').State} State
* @typedef {import('../types.js').Nodes} Nodes
* @typedef {import('../types.js').Parents} Parents
* @typedef {import('hast').Nodes} Nodes
* @typedef {import('hast').Parents} Parents
*
* @typedef {import('../index.js').State} State
*/

import {zwitch} from 'zwitch'
Expand Down Expand Up @@ -36,12 +37,13 @@ function invalid(node) {
/**
* Fail when a node with an unknown type is found in the tree.
*
* @param {unknown} node
* @param {unknown} node_
* Unknown node.
* @returns {never}
* Never.
*/
function unknown(node) {
// @ts-expect-error: `type` is defined.
function unknown(node_) {
// `type` is guaranteed by runtime JS.
const node = /** @type {Nodes} */ (node_)
throw new Error('Cannot compile unknown node `' + node.type + '`')
}
8 changes: 5 additions & 3 deletions lib/handle/raw.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/**
* @typedef {import('../types.js').State} State
* @typedef {import('../types.js').Parents} Parents
* @typedef {import('../types.js').Raw} Raw
* @typedef {import('hast').Parents} Parents
*
* @typedef {import('mdast-util-to-hast').Raw} Raw
*
* @typedef {import('../index.js').State} State
*/

import {text} from './text.js'
Expand Down
10 changes: 7 additions & 3 deletions lib/handle/root.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/**
* @typedef {import('../types.js').Root} Root
* @typedef {import('../types.js').Parents} Parents
* @typedef {import('../types.js').State} State
* @typedef {import('hast').Parents} Parents
* @typedef {import('hast').Root} Root
*
* @typedef {import('../index.js').State} State
*/

// Make VS code see references to the above types.
''

/**
* Serialize a root.
*
Expand Down
12 changes: 7 additions & 5 deletions lib/handle/text.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
/**
* @typedef {import('../types.js').State} State
* @typedef {import('../types.js').Parents} Parents
* @typedef {import('../types.js').Raw} Raw
* @typedef {import('../types.js').Text} Text
* @typedef {import('hast').Parents} Parents
* @typedef {import('hast').Text} Text
*
* @typedef {import('mdast-util-to-hast').Raw} Raw
*
* @typedef {import('../index.js').State} State
*/

import {stringifyEntities} from 'stringify-entities'

/**
* Serialize a text node.
*
* @param {Text | Raw} node
* @param {Raw | Text} node
* Node to handle.
* @param {number | undefined} _
* Index of `node` in `parent.
Expand Down
Loading

0 comments on commit 1470a23

Please sign in to comment.