Skip to content

Commit 1470a23

Browse files
committed
Refactor code-style
1 parent ce75ece commit 1470a23

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3100
-2255
lines changed

index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* @typedef {import('./lib/types.js').CharacterReferences} CharacterReferences
3-
* @typedef {import('./lib/types.js').Options} Options
4-
* @typedef {import('./lib/types.js').Quote} Quote
5-
* @typedef {import('./lib/types.js').Space} Space
2+
* @typedef {import('./lib/index.js').CharacterReferences} CharacterReferences
3+
* @typedef {import('./lib/index.js').Options} Options
4+
* @typedef {import('./lib/index.js').Quote} Quote
5+
* @typedef {import('./lib/index.js').Space} Space
66
*/
77

88
export {toHtml} from './lib/index.js'

lib/handle/comment.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
2-
* @typedef {import('../types.js').Comment} Comment
3-
* @typedef {import('../types.js').Parents} Parents
4-
* @typedef {import('../types.js').State} State
2+
* @typedef {import('hast').Comment} Comment
3+
* @typedef {import('hast').Parents} Parents
4+
*
5+
* @typedef {import('../index.js').State} State
56
*/
67

78
import {stringifyEntities} from 'stringify-entities'

lib/handle/doctype.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
/**
2-
* @typedef {import('../types.js').DocType} DocType
3-
* @typedef {import('../types.js').Parents} Parents
4-
* @typedef {import('../types.js').State} State
2+
* @typedef {import('hast').Doctype} Doctype
3+
* @typedef {import('hast').Parents} Parents
4+
*
5+
* @typedef {import('../index.js').State} State
56
*/
67

8+
// Make VS code see references to the above types.
9+
''
10+
711
/**
812
* Serialize a doctype.
913
*
10-
* @param {DocType} _1
14+
* @param {Doctype} _1
1115
* Node to handle.
1216
* @param {number | undefined} _2
1317
* Index of `node` in `parent.

lib/handle/element.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/**
2-
* @typedef {import('../types.js').State} State
3-
* @typedef {import('../types.js').Parents} Parents
4-
* @typedef {import('../types.js').Element} Element
5-
* @typedef {import('../types.js').Properties} Properties
6-
* @typedef {import('../types.js').PropertyValue} PropertyValue
2+
* @typedef {import('hast').Element} Element
3+
* @typedef {import('hast').Parents} Parents
4+
* @typedef {import('hast').Properties} Properties
5+
*
6+
* @typedef {import('../index.js').State} State
77
*/
88

99
import {ccount} from 'ccount'
1010
import {stringify as commas} from 'comma-separated-tokens'
11-
import {svg, find} from 'property-information'
11+
import {find, svg} from 'property-information'
1212
import {stringify as spaces} from 'space-separated-tokens'
1313
import {stringifyEntities} from 'stringify-entities'
14-
import {opening} from '../omission/opening.js'
1514
import {closing} from '../omission/closing.js'
15+
import {opening} from '../omission/opening.js'
1616

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

139138
if (props) {
140139
for (key in props) {
141-
if (props[key] !== undefined && props[key] !== null) {
140+
if (props[key] !== null && props[key] !== undefined) {
142141
const value = serializeAttribute(state, key, props[key])
143142
if (value) values.push(value)
144143
}
@@ -148,7 +147,7 @@ function serializeAttributes(state, props) {
148147
while (++index < values.length) {
149148
const last = state.settings.tightAttributes
150149
? values[index].charAt(values[index].length - 1)
151-
: null
150+
: undefined
152151

153152
// In tight mode, don’t add a space after quoted attributes.
154153
if (index !== values.length - 1 && last !== '"' && last !== "'") {
@@ -162,10 +161,9 @@ function serializeAttributes(state, props) {
162161
/**
163162
* @param {State} state
164163
* @param {string} key
165-
* @param {PropertyValue} value
164+
* @param {Properties[keyof Properties]} value
166165
* @returns {string}
167166
*/
168-
// eslint-disable-next-line complexity
169167
function serializeAttribute(state, key, value) {
170168
const info = find(state.schema, key)
171169
const x =
@@ -185,8 +183,8 @@ function serializeAttribute(state, key, value) {
185183
}
186184

187185
if (
188-
value === undefined ||
189186
value === null ||
187+
value === undefined ||
190188
value === false ||
191189
(typeof value === 'number' && Number.isNaN(value))
192190
) {
@@ -235,8 +233,8 @@ function serializeAttribute(state, key, value) {
235233
result = stringifyEntities(
236234
value,
237235
Object.assign({}, state.settings.characterReferences, {
238-
subset: constants.unquoted[x][y],
239-
attribute: true
236+
attribute: true,
237+
subset: constants.unquoted[x][y]
240238
})
241239
)
242240
}

lib/handle/index.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
2-
* @typedef {import('../types.js').State} State
3-
* @typedef {import('../types.js').Nodes} Nodes
4-
* @typedef {import('../types.js').Parents} Parents
2+
* @typedef {import('hast').Nodes} Nodes
3+
* @typedef {import('hast').Parents} Parents
4+
*
5+
* @typedef {import('../index.js').State} State
56
*/
67

78
import {zwitch} from 'zwitch'
@@ -36,12 +37,13 @@ function invalid(node) {
3637
/**
3738
* Fail when a node with an unknown type is found in the tree.
3839
*
39-
* @param {unknown} node
40+
* @param {unknown} node_
4041
* Unknown node.
4142
* @returns {never}
4243
* Never.
4344
*/
44-
function unknown(node) {
45-
// @ts-expect-error: `type` is defined.
45+
function unknown(node_) {
46+
// `type` is guaranteed by runtime JS.
47+
const node = /** @type {Nodes} */ (node_)
4648
throw new Error('Cannot compile unknown node `' + node.type + '`')
4749
}

lib/handle/raw.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/**
2-
* @typedef {import('../types.js').State} State
3-
* @typedef {import('../types.js').Parents} Parents
4-
* @typedef {import('../types.js').Raw} Raw
2+
* @typedef {import('hast').Parents} Parents
3+
*
4+
* @typedef {import('mdast-util-to-hast').Raw} Raw
5+
*
6+
* @typedef {import('../index.js').State} State
57
*/
68

79
import {text} from './text.js'

lib/handle/root.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
/**
2-
* @typedef {import('../types.js').Root} Root
3-
* @typedef {import('../types.js').Parents} Parents
4-
* @typedef {import('../types.js').State} State
2+
* @typedef {import('hast').Parents} Parents
3+
* @typedef {import('hast').Root} Root
4+
*
5+
* @typedef {import('../index.js').State} State
56
*/
67

8+
// Make VS code see references to the above types.
9+
''
10+
711
/**
812
* Serialize a root.
913
*

lib/handle/text.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
/**
2-
* @typedef {import('../types.js').State} State
3-
* @typedef {import('../types.js').Parents} Parents
4-
* @typedef {import('../types.js').Raw} Raw
5-
* @typedef {import('../types.js').Text} Text
2+
* @typedef {import('hast').Parents} Parents
3+
* @typedef {import('hast').Text} Text
4+
*
5+
* @typedef {import('mdast-util-to-hast').Raw} Raw
6+
*
7+
* @typedef {import('../index.js').State} State
68
*/
79

810
import {stringifyEntities} from 'stringify-entities'
911

1012
/**
1113
* Serialize a text node.
1214
*
13-
* @param {Text | Raw} node
15+
* @param {Raw | Text} node
1416
* Node to handle.
1517
* @param {number | undefined} _
1618
* Index of `node` in `parent.

0 commit comments

Comments
 (0)