Skip to content

Commit ebf189c

Browse files
committed
Refactor types
1 parent 05e6eb9 commit ebf189c

File tree

9 files changed

+84
-73
lines changed

9 files changed

+84
-73
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
coverage/
55
node_modules/
66
yarn.lock
7-
test/error-codes-from-p5.js
7+
/test/error-codes-from-p5.js
8+
!/lib/types.d.ts
9+
!/index.d.ts

index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// eslint-disable-next-line @typescript-eslint/triple-slash-reference -- needed to register data.
2+
/// <reference types="hast-util-from-parse5" />
3+
export {fromHtml} from './lib/index.js'
4+
export type {ErrorCode, ErrorSeverity, OnError, Options} from './lib/types.js'

index.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
1-
/**
2-
* @typedef {import('hast-util-from-parse5')} DoNotTouchItRegistersData
3-
*
4-
* @typedef {import('./lib/index.js').ErrorCode} ErrorCode
5-
* @typedef {import('./lib/index.js').ErrorSeverity} ErrorSeverity
6-
* @typedef {import('./lib/index.js').OnError} OnError
7-
* @typedef {import('./lib/index.js').Options} Options
8-
*/
9-
1+
// Note: types exposed from `index.d.ts`.
102
export {fromHtml} from './lib/index.js'

lib/index.js

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,8 @@
11
/**
2-
* @typedef {import('hast').Root} Root
3-
* @typedef {import('parse5').ParserError} ParserError
4-
* @typedef {import('vfile').Value} Value
5-
*/
6-
7-
/**
8-
* @typedef {keyof errors} ErrorCode
9-
* Known names of parse errors.
10-
*
11-
* @typedef {Partial<Record<ErrorCode, ErrorSeverity | null | undefined>>} ErrorOptions
12-
* Options that define the severity of errors.
13-
*
14-
* @typedef {boolean | 0 | 1 | 2} ErrorSeverity
15-
* Error severity:
16-
*
17-
* * `0` or `false`
18-
* — turn the parse error off
19-
* * `1` or `true`
20-
* — turn the parse error into a warning
21-
* * `2`
22-
* — turn the parse error into an actual error: processing stops.
23-
*
24-
* @typedef ExtraOptions
25-
* Options that define how to parse HTML.
26-
* @property {boolean | null | undefined} [fragment=false]
27-
* Specify whether to parse a fragment, instead of a complete document
28-
* (default: `false`).
29-
*
30-
* In document mode, unopened `html`, `head`, and `body` elements are opened
31-
* in just the right places.
32-
* @property {OnError | null | undefined} [onerror]
33-
* Call `onerror` with parse errors while parsing (optional).
34-
*
35-
* > 👉 **Note**: parse errors are currently being added to HTML.
36-
* > Not all errors emitted by parse5 (or us) are specced yet.
37-
* > Some documentation may still be missing.
38-
*
39-
* Specific rules can be turned off by setting them to `false` (or `0`).
40-
* The default, when `emitParseErrors: true`, is `true` (or `1`), and means
41-
* that rules emit as warnings.
42-
* Rules can also be configured with `2`, to turn them into fatal errors.
43-
*
44-
* @typedef {Omit<import('hast-util-from-parse5').Options, 'file'>} FromParse5Options
45-
* Options that can be passed through to `hast-util-from-parse5`.
46-
*
47-
* @callback OnError
48-
* Handle parse errors.
49-
* @param {VFileMessage} error
50-
* Message.
51-
* @returns {undefined | void}
52-
* Nothing.
53-
*
54-
* Note: `void` included until TS infers `undefined` nicely.
55-
*
56-
* @typedef {FromParse5Options & ErrorOptions & ExtraOptions} Options
57-
* Configuration.
2+
* @import {Root} from 'hast'
3+
* @import {ParserError} from 'parse5'
4+
* @import {Value} from 'vfile'
5+
* @import {ErrorCode, Options} from './types.js'
586
*/
597

608
import {ok as assert} from 'devlop'

lib/types.d.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import type {VFileMessage} from 'vfile-message'
2+
import type {Options as FromParse5Options} from 'hast-util-from-parse5'
3+
import type {errors} from './errors.js'
4+
5+
/**
6+
* Known names of parse errors.
7+
*/
8+
export type ErrorCode = keyof typeof errors
9+
10+
/**
11+
* Error severity:
12+
*
13+
* * `0` or `false`
14+
* — turn the parse error off
15+
* * `1` or `true`
16+
* — turn the parse error into a warning
17+
* * `2`
18+
* — turn the parse error into an actual error: processing stops.
19+
*/
20+
export type ErrorSeverity = boolean | 0 | 1 | 2
21+
22+
/**
23+
* Handle parse errors.
24+
*/
25+
export type OnError = (error: VFileMessage) => undefined | void
26+
27+
/**
28+
* Options that define the severity of errors.
29+
*/
30+
export type ErrorOptions = Partial<
31+
Record<ErrorCode, ErrorSeverity | null | undefined>
32+
>
33+
34+
/**
35+
* Configuration.
36+
*/
37+
export interface Options extends ErrorOptions, FromParse5Options {
38+
/**
39+
* The `file` field from `hast-util-from-parse5` is not supported.
40+
*/
41+
file?: never
42+
/**
43+
* Specify whether to parse a fragment, instead of a complete document
44+
* (default: `false`).
45+
*
46+
* In document mode, unopened `html`, `head`, and `body` elements are opened
47+
* in just the right places.
48+
*/
49+
fragment?: boolean | null | undefined
50+
/**
51+
* Call `onerror` with parse errors while parsing (optional).
52+
*
53+
* > 👉 **Note**: parse errors are currently being added to HTML.
54+
* > Not all errors emitted by parse5 (or us) are specced yet.
55+
* > Some documentation may still be missing.
56+
*
57+
* Specific rules can be turned off by setting them to `false` (or `0`).
58+
* The default, when `emitParseErrors: true`, is `true` (or `1`), and means
59+
* that rules emit as warnings.
60+
* Rules can also be configured with `2`, to turn them into fatal errors.
61+
*/
62+
onerror?: OnError | null | undefined
63+
}

lib/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Note: types exposed from `lib/types.d.ts`.
2+
export {}

script/parse-error.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* @typedef {import('mdast').Root} Root
3-
* @typedef {import('mdast').ListItem} ListItem
4-
* @typedef {import('mdast').PhrasingContent} PhrasingContent
2+
* @import {ListItem, PhrasingContent, Root} from 'mdast'
3+
* @import {PackageJson} from 'type-fest'
4+
* @import {Plugin} from 'unified'
55
*/
66

77
import fs from 'node:fs/promises'
@@ -12,7 +12,7 @@ import {errors} from '../lib/errors.js'
1212

1313
const own = {}.hasOwnProperty
1414

15-
/** @type {import('type-fest').PackageJson} */
15+
/** @type {PackageJson} */
1616
const packageJson = JSON.parse(String(await fs.readFile('package.json')))
1717

1818
const repo = packageJson.repository
@@ -26,7 +26,7 @@ const ignoreFixture = {
2626
surrogateInInputStream: true
2727
}
2828

29-
/** @satisfies {import('unified').Plugin<[], Root>} */
29+
/** @satisfies {Plugin<[], Root>} */
3030
export default function remarkParseErrors() {
3131
/**
3232
* @param {Root} tree

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable unicorn/prefer-structured-clone -- JSON is simplified */
22

33
/**
4-
* @typedef {import('vfile-message').VFileMessage} VFileMessage
4+
* @import {VFileMessage} from 'vfile-message'
55
*/
66

77
import assert from 'node:assert/strict'

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"target": "es2022"
1212
},
1313
"exclude": ["coverage/", "node_modules/"],
14-
"include": ["**/*.js"]
14+
"include": ["**/*.js", "lib/types.d.ts", "index.d.ts"]
1515
}

0 commit comments

Comments
 (0)