Skip to content

Commit 2347ceb

Browse files
committed
Refactor code-style
1 parent fc34c7b commit 2347ceb

40 files changed

+862
-698
lines changed

Diff for: index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2+
* @typedef {import('./lib/state.js').Handle} Handle
3+
* @typedef {import('./lib/state.js').NodeHandle} NodeHandle
4+
* @typedef {import('./lib/state.js').Options} Options
25
* @typedef {import('./lib/state.js').State} State
3-
* @typedef {import('./lib/types.js').Handle} Handle
4-
* @typedef {import('./lib/types.js').NodeHandle} NodeHandle
5-
* @typedef {import('./lib/types.js').Options} Options
66
*/
77

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

Diff for: lib/handlers/a.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
/**
2-
* @typedef {import('mdast').Link} Link
32
* @typedef {import('hast').Element} Element
3+
*
4+
* @typedef {import('mdast').Link} Link
5+
* @typedef {import('mdast').PhrasingContent} PhrasingContent
6+
*
47
* @typedef {import('../state.js').State} State
58
*/
69

10+
// Fix to let VS Code see references to the above types.
11+
''
12+
713
/**
814
* @param {State} state
915
* State.
10-
* @param {Element} node
16+
* @param {Readonly<Element>} node
1117
* hast element to transform.
1218
* @returns {Link}
1319
* mdast node.
1420
*/
1521
export function a(state, node) {
1622
const properties = node.properties || {}
23+
// Allow potentially “invalid” nodes, they might be unknown.
24+
// We also support straddling later.
25+
const children = /** @type {Array<PhrasingContent>} */ (state.all(node))
1726

1827
/** @type {Link} */
1928
const result = {
2029
type: 'link',
2130
url: state.resolve(String(properties.href || '') || null),
2231
title: properties.title ? String(properties.title) : null,
23-
// @ts-expect-error: allow potentially “invalid” nodes, they might be unknown.
24-
// We also support straddling later.
25-
children: state.all(node)
32+
children
2633
}
2734
state.patch(node, result)
2835
return result

Diff for: lib/handlers/base.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
/**
22
* @typedef {import('hast').Element} Element
3+
*
34
* @typedef {import('../state.js').State} State
45
*/
56

7+
// Fix to let VS Code see references to the above types.
8+
''
9+
610
/**
711
* @param {State} state
812
* State.
9-
* @param {Element} node
13+
* @param {Readonly<Element>} node
1014
* hast element to transform.
11-
* @returns {void}
15+
* @returns {undefined}
1216
* Nothing.
1317
*/
1418
export function base(state, node) {

Diff for: lib/handlers/blockquote.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
/**
22
* @typedef {import('hast').Element} Element
3+
*
34
* @typedef {import('mdast').Blockquote} Blockquote
5+
*
46
* @typedef {import('../state.js').State} State
57
*/
68

9+
// Fix to let VS Code see references to the above types.
10+
''
11+
712
/**
813
* @param {State} state
914
* State.
10-
* @param {Element} node
15+
* @param {Readonly<Element>} node
1116
* hast element to transform.
1217
* @returns {Blockquote}
1318
* mdast node.

Diff for: lib/handlers/br.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
/**
22
* @typedef {import('hast').Element} Element
3+
*
34
* @typedef {import('mdast').Break} Break
5+
*
46
* @typedef {import('../state.js').State} State
57
*/
68

9+
// Fix to let VS Code see references to the above types.
10+
''
11+
712
/**
813
* @param {State} state
914
* State.
10-
* @param {Element} node
15+
* @param {Readonly<Element>} node
1116
* hast element to transform.
1217
* @returns {Break}
1318
* mdast node.

Diff for: lib/handlers/code.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* @typedef {import('hast').Element} Element
3+
*
34
* @typedef {import('mdast').Code} Code
5+
*
46
* @typedef {import('../state.js').State} State
57
*/
68

@@ -12,15 +14,15 @@ const prefix = 'language-'
1214
/**
1315
* @param {State} state
1416
* State.
15-
* @param {Element} node
17+
* @param {Readonly<Element>} node
1618
* hast element to transform.
1719
* @returns {Code}
1820
* mdast node.
1921
*/
2022
export function code(state, node) {
2123
const children = node.children
2224
let index = -1
23-
/** @type {Array<string | number> | undefined} */
25+
/** @type {Array<number | string> | undefined} */
2426
let classList
2527
/** @type {string | undefined} */
2628
let lang

Diff for: lib/handlers/comment.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
/**
22
* @typedef {import('hast').Comment} Comment
3-
* @typedef {import('mdast').HTML} HTML
3+
*
4+
* @typedef {import('mdast').Html} Html
5+
*
46
* @typedef {import('../state.js').State} State
57
*/
68

9+
// Fix to let VS Code see references to the above types.
10+
''
11+
712
/**
813
* @param {State} state
914
* State.
10-
* @param {Comment} node
15+
* @param {Readonly<Comment>} node
1116
* hast element to transform.
12-
* @returns {HTML}
17+
* @returns {Html}
1318
* mdast node.
1419
*/
1520
export function comment(state, node) {
16-
/** @type {HTML} */
21+
/** @type {Html} */
1722
const result = {
1823
type: 'html',
1924
value: '<!--' + node.value + '-->'

Diff for: lib/handlers/del.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
/**
22
* @typedef {import('hast').Element} Element
3+
*
34
* @typedef {import('mdast').Delete} Delete
5+
* @typedef {import('mdast').PhrasingContent} PhrasingContent
6+
*
47
* @typedef {import('../state.js').State} State
58
*/
69

10+
// Fix to let VS Code see references to the above types.
11+
''
12+
713
/**
814
* @param {State} state
915
* State.
10-
* @param {Element} node
16+
* @param {Readonly<Element>} node
1117
* hast element to transform.
1218
* @returns {Delete}
1319
* mdast node.
1420
*/
1521
export function del(state, node) {
22+
// Allow potentially “invalid” nodes, they might be unknown.
23+
// We also support straddling later.
24+
const children = /** @type {Array<PhrasingContent>} */ (state.all(node))
1625
/** @type {Delete} */
17-
const result = {
18-
type: 'delete',
19-
// @ts-expect-error: allow potentially “invalid” nodes, they might be unknown.
20-
// We also support straddling later.
21-
children: state.all(node)
22-
}
26+
const result = {type: 'delete', children}
2327
state.patch(node, result)
2428
return result
2529
}

Diff for: lib/handlers/dl.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('hast').ElementContent} ElementContent
4-
* @typedef {import('mdast').List} List
4+
*
55
* @typedef {import('mdast').BlockContent} BlockContent
66
* @typedef {import('mdast').DefinitionContent} DefinitionContent
7+
* @typedef {import('mdast').List} List
78
* @typedef {import('mdast').ListContent} ListContent
9+
* @typedef {import('mdast').ListItem} ListItem
10+
*
811
* @typedef {import('../state.js').State} State
912
*
13+
*/
14+
15+
/**
1016
* @typedef Group
1117
* Title/definition group.
1218
* @property {Array<Element>} titles
@@ -20,7 +26,7 @@ import {listItemsSpread} from '../util/list-items-spread.js'
2026
/**
2127
* @param {State} state
2228
* State.
23-
* @param {Element} node
29+
* @param {Readonly<Element>} node
2430
* hast element to transform.
2531
* @returns {List | undefined}
2632
* mdast node.
@@ -44,7 +50,7 @@ export function dl(state, node) {
4450
}
4551

4652
/** @type {Group} */
47-
let group = {titles: [], definitions: []}
53+
let group = {definitions: [], titles: []}
4854
index = -1
4955

5056
// Group titles and definitions.
@@ -60,7 +66,7 @@ export function dl(state, node) {
6066
previous.tagName === 'dd'
6167
) {
6268
groups.push(group)
63-
group = {titles: [], definitions: []}
69+
group = {definitions: [], titles: []}
6470
}
6571

6672
group.titles.push(child)
@@ -116,12 +122,7 @@ export function dl(state, node) {
116122
* mdast nodes.
117123
*/
118124
function handle(state, children) {
119-
const nodes = state.all({
120-
type: 'element',
121-
tagName: 'x',
122-
properties: {},
123-
children
124-
})
125+
const nodes = state.all({type: 'root', children})
125126
const listItems = state.toSpecificContent(nodes, create)
126127

127128
if (listItems.length === 0) {
@@ -144,7 +145,7 @@ function handle(state, children) {
144145
}
145146

146147
/**
147-
* @returns {ListContent}
148+
* @returns {ListItem}
148149
*/
149150
function create() {
150151
return {type: 'listItem', spread: false, checked: null, children: []}

Diff for: lib/handlers/em.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
/**
22
* @typedef {import('hast').Element} Element
3+
*
34
* @typedef {import('mdast').Emphasis} Emphasis
5+
* @typedef {import('mdast').PhrasingContent} PhrasingContent
6+
*
47
* @typedef {import('../state.js').State} State
58
*/
69

10+
// Fix to let VS Code see references to the above types.
11+
''
12+
713
/**
814
* @param {State} state
915
* State.
10-
* @param {Element} node
16+
* @param {Readonly<Element>} node
1117
* hast element to transform.
1218
* @returns {Emphasis}
1319
* mdast node.
1420
*/
1521
export function em(state, node) {
22+
// Allow potentially “invalid” nodes, they might be unknown.
23+
// We also support straddling later.
24+
const children = /** @type {Array<PhrasingContent>} */ (state.all(node))
25+
1626
/** @type {Emphasis} */
17-
// @ts-expect-error: allow potentially “invalid” nodes, they might be unknown.
18-
const result = {type: 'emphasis', children: state.all(node)}
27+
const result = {type: 'emphasis', children}
1928
state.patch(node, result)
2029
return result
2130
}

Diff for: lib/handlers/heading.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
/**
22
* @typedef {import('hast').Element} Element
3+
*
34
* @typedef {import('mdast').Heading} Heading
5+
* @typedef {import('mdast').PhrasingContent} PhrasingContent
6+
*
47
* @typedef {import('../state.js').State} State
58
*/
69

10+
// Fix to let VS Code see references to the above types.
11+
''
12+
713
/**
814
* @param {State} state
915
* State.
10-
* @param {Element} node
16+
* @param {Readonly<Element>} node
1117
* hast element to transform.
1218
* @returns {Heading}
1319
* mdast node.
1420
*/
1521
export function heading(state, node) {
16-
/* c8 ignore next */
17-
const depth = Number(node.tagName.charAt(1)) || 1
22+
const depth = /** @type {Heading['depth']} */ (
23+
/* c8 ignore next */
24+
Number(node.tagName.charAt(1)) || 1
25+
)
26+
const children = /** @type {Array<PhrasingContent>} */ (state.all(node))
1827

1928
/** @type {Heading} */
20-
const result = {
21-
type: 'heading',
22-
// @ts-expect-error: fine.
23-
depth,
24-
// @ts-expect-error: allow potentially “invalid” nodes, they might be unknown.
25-
children: state.all(node)
26-
}
29+
const result = {type: 'heading', depth, children}
2730
state.patch(node, result)
2831
return result
2932
}

Diff for: lib/handlers/hr.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
/**
22
* @typedef {import('hast').Element} Element
3+
*
34
* @typedef {import('mdast').ThematicBreak} ThematicBreak
5+
*
46
* @typedef {import('../state.js').State} State
57
*/
68

9+
// Fix to let VS Code see references to the above types.
10+
''
11+
712
/**
813
* @param {State} state
914
* State.
10-
* @param {Element} node
15+
* @param {Readonly<Element>} node
1116
* hast element to transform.
1217
* @returns {ThematicBreak}
1318
* mdast node.

Diff for: lib/handlers/iframe.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
/**
22
* @typedef {import('hast').Element} Element
3+
*
34
* @typedef {import('mdast').Link} Link
5+
*
46
* @typedef {import('../state.js').State} State
57
*/
68

9+
// Fix to let VS Code see references to the above types.
10+
''
11+
712
/**
813
* @param {State} state
914
* State.
10-
* @param {Element} node
15+
* @param {Readonly<Element>} node
1116
* hast element to transform.
12-
* @returns {Link | void}
17+
* @returns {Link | undefined}
1318
* mdast node.
1419
*/
1520
export function iframe(state, node) {

0 commit comments

Comments
 (0)