Skip to content

Commit d1fbb6c

Browse files
committed
Refactor code-style
* Add support for `null` in API input types * Add more docs to JSDoc
1 parent 9c05bd4 commit d1fbb6c

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

Diff for: lib/index.js

+43-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
/**
22
* @typedef {import('hast').Root} Root
33
* @typedef {import('vfile').VFileCompatible} VFileCompatible
4-
* @typedef {Pick<import('hast-util-from-parse5').Options, 'space'|'verbose'>} FromParse5Options
4+
*/
5+
6+
/**
7+
* @typedef {Pick<import('hast-util-from-parse5').Options, 'space' | 'verbose'>} FromParse5Options
58
*
69
* @typedef {keyof errors} ErrorCode
7-
* @typedef {0|1|2|boolean|null|undefined} ErrorSeverity
8-
* @typedef {Partial<Record<ErrorCode, ErrorSeverity>>} ErrorFields
10+
* Known names of parse errors.
11+
* @typedef {0 | 1 | 2 | boolean} ErrorSeverity
12+
* Error severity:
13+
*
14+
* * `0` or `false`
15+
* — turn the parse error off
16+
* * `1` or `true`
17+
* — turn the parse error into a warning
18+
* * `2`
19+
* — turn the parse error into an actual error: processing stops.
20+
* @typedef {Partial<Record<ErrorCode, ErrorSeverity | null | undefined>>} ErrorFields
21+
* Error configuration.
922
*
1023
* @typedef Error
24+
* Error from `parse5`.
1125
* @property {string} code
1226
* @property {number} startLine
1327
* @property {number} startCol
@@ -17,27 +31,34 @@
1731
* @property {number} endOffset
1832
*
1933
* @callback OnError
34+
* Handle errors.
2035
* @param {VFileMessage} error
36+
* Message.
2137
* @returns {void}
38+
* Nothing.
2239
*
2340
* @typedef ParseFields
24-
* @property {boolean|undefined} [fragment=false]
41+
* @property {boolean | null | undefined} [fragment=false]
2542
* Specify whether to parse a fragment, instead of a complete document.
43+
*
2644
* In document mode, unopened `html`, `head`, and `body` elements are opened
2745
* in just the right places.
28-
* @property {OnError|undefined} [onerror=false]
46+
* @property {OnError | null | undefined} [onerror]
47+
* Call `onerror` with parse errors while parsing.
48+
*
2949
* > 👉 **Note**: parse errors are currently being added to HTML.
3050
* > Not all errors emitted by parse5 (or us) are specced yet.
3151
* > Some documentation may still be missing.
3252
*
33-
* Call `onerror` with parse errors while parsing.
34-
*
3553
* Specific rules can be turned off by setting them to `false` (or `0`).
3654
* The default, when `emitParseErrors: true`, is `true` (or `1`), and means
3755
* that rules emit as warnings.
3856
* Rules can also be configured with `2`, to turn them into fatal errors.
39-
*
57+
*/
58+
59+
/**
4060
* @typedef {FromParse5Options & ParseFields & ErrorFields} Options
61+
* Configuration.
4162
*/
4263

4364
import {parse, parseFragment} from 'parse5'
@@ -51,27 +72,33 @@ const base = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-'
5172
const fatalities = {2: true, 1: false, 0: null}
5273

5374
/**
75+
* Turn serialized HTML into a hast tree.
76+
*
5477
* @param {VFileCompatible} value
55-
* @param {Options} [options={}]
78+
* Serialized HTML to parse.
79+
* @param {Options | null | undefined} [options={}]
80+
* Configuration (optional).
5681
* @returns {Root}
82+
* Tree.
5783
*/
58-
export function fromHtml(value, options = {}) {
59-
const warn = options.onerror || null
84+
export function fromHtml(value, options) {
85+
const settings = options || {}
86+
const warn = settings.onerror || null
6087
const file = value instanceof VFile ? value : new VFile(value)
61-
const fn = options.fragment ? parseFragment : parse
88+
const fn = settings.fragment ? parseFragment : parse
6289
const doc = String(file)
6390
const p5doc = fn(doc, {
6491
sourceCodeLocationInfo: true,
65-
onParseError: options.onerror ? onerror : null,
92+
onParseError: settings.onerror ? onerror : null,
6693
scriptingEnabled: false
6794
})
6895

6996
// @ts-expect-error: `parse5` returns document or fragment, which are always
7097
// mapped to roots.
7198
return fromParse5(p5doc, {
7299
file,
73-
space: options.space,
74-
verbose: options.verbose
100+
space: settings.space,
101+
verbose: settings.verbose
75102
})
76103

77104
/**
@@ -80,7 +107,7 @@ export function fromHtml(value, options = {}) {
80107
function onerror(error) {
81108
const code = error.code
82109
const name = camelcase(code)
83-
const setting = options[name]
110+
const setting = settings[name]
84111
const config = setting === undefined || setting === null ? true : setting
85112
const level = typeof config === 'number' ? config : config ? 1 : 0
86113
const start = {

Diff for: test/index.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*/
44

55
import fs from 'node:fs/promises'
6-
import path from 'node:path'
76
import test from 'tape'
87
import {VFile} from 'vfile'
98
import {toVFile, read} from 'to-vfile'
@@ -321,7 +320,7 @@ test('parse errors: coverage', async (t) => {
321320

322321
test('parse-errors: working', async (t) => {
323322
let index = -1
324-
const root = path.join('test', 'parse-error')
323+
const root = new URL('parse-error/', import.meta.url)
325324
const fixtures = await fs.readdir(root)
326325

327326
t.test('surrogate-in-input-stream', (t) => {
@@ -379,15 +378,13 @@ test('parse-errors: working', async (t) => {
379378
return
380379
}
381380

382-
const fp = path.join(root, fixture)
383-
384381
setImmediate(next) // Queue next.
385382

386383
t.test(fixture, async (t) => {
387-
const file = await read(path.join(fp, 'index.html'), 'utf8')
384+
const file = await read(new URL(fixture + '/index.html', root), 'utf8')
388385
/** @type {Array<Error>} */
389386
const messages = JSON.parse(
390-
String(await fs.readFile(path.join(fp, 'messages.json')))
387+
String(await fs.readFile(new URL(fixture + '/messages.json', root)))
391388
)
392389

393390
file.dirname = ''

0 commit comments

Comments
 (0)