Skip to content

Commit 7c9115b

Browse files
committed
Add typed settings
1 parent d8120dd commit 7c9115b

File tree

6 files changed

+25
-21
lines changed

6 files changed

+25
-21
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
"**/*.ts"
7272
],
7373
"rules": {
74-
"@typescript-eslint/ban-types": "off"
74+
"@typescript-eslint/ban-types": "off",
75+
"@typescript-eslint/consistent-type-definitions": "off"
7576
}
7677
}
7778
],

packages/rehype-parse/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ export type {Options} from './lib/index.js'
1717
*/
1818
declare const rehypeParse: Plugin<[(Options | null | undefined)?], string, Root>
1919
export default rehypeParse
20+
21+
// Add custom settings supported when `rehype-parse` is added.
22+
declare module 'unified' {
23+
interface Settings extends Options {}
24+
}

packages/rehype-parse/lib/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ export default function rehypeParse(options) {
3232
/** @type {import('unified').Processor<Root>} */
3333
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
3434
const self = this
35-
const processorSettings = /** @type {Options} */ (self.data('settings'))
36-
const {emitParseErrors, ...settings} = {...processorSettings, ...options}
35+
const {emitParseErrors, ...settings} = {...self.data('settings'), ...options}
3736

3837
self.parser = parser
3938

packages/rehype-stringify/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ declare const rehypeStringify: Plugin<
2020
string
2121
>
2222
export default rehypeStringify
23+
24+
// Add custom settings supported when `rehype-stringify` is added.
25+
declare module 'unified' {
26+
interface Settings extends Options {}
27+
}

packages/rehype-stringify/lib/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ export default function rehypeStringify(options) {
1818
/** @type {import('unified').Processor<undefined, undefined, undefined, Root, string>} */
1919
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
2020
const self = this
21-
const processorSettings = /** @type {Options} */ (self.data('settings'))
22-
const settings = {...processorSettings, ...options}
21+
const settings = {...self.data('settings'), ...options}
2322

2423
self.compiler = compiler
2524

test/api.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
2+
* @typedef {import('unified').Settings} Settings
23
* @typedef {import('hast').Root} Root
34
*/
45

@@ -78,7 +79,6 @@ test('rehype', async function (t) {
7879
async function () {
7980
assert.deepEqual(
8081
unified()
81-
// @ts-expect-error: to do: type `settings`.
8282
.data('settings', {fragment: true})
8383
.use(rehypeParse, {fragment: false})
8484
.use(rehypeStringify)
@@ -94,7 +94,6 @@ test('rehype', async function (t) {
9494
async function () {
9595
assert.deepEqual(
9696
unified()
97-
// @ts-expect-error: to do: type `settings`.
9897
.data('settings', {quote: '"'})
9998
.use(rehypeParse, {fragment: true})
10099
.use(rehypeStringify, {quote: "'"})
@@ -187,8 +186,10 @@ test('rehype', async function (t) {
187186
'should throw when `tree` is not a valid node',
188187
async function () {
189188
assert.throws(function () {
190-
// @ts-expect-error: unknown node.
191-
unified().use(rehypeStringify).stringify({type: 'unicorn'})
189+
unified()
190+
.use(rehypeStringify)
191+
// @ts-expect-error: check how an unknown node is handled.
192+
.stringify({type: 'unicorn'})
192193
}, /unicorn/)
193194
}
194195
)
@@ -308,7 +309,6 @@ test('rehype', async function (t) {
308309
async function () {
309310
assert.deepEqual(
310311
rehype()
311-
// @ts-expect-error: to do: type `settings`.
312312
.data('settings', {emitParseErrors: true})
313313
.processSync('<!doctypehtml>')
314314
.messages.map(String),
@@ -323,7 +323,6 @@ test('rehype', async function (t) {
323323
assert.deepEqual(
324324
rehype()
325325
.data('settings', {
326-
// @ts-expect-error: to do: type `settings`.
327326
emitParseErrors: true,
328327
missingWhitespaceBeforeDoctypeName: false
329328
})
@@ -340,7 +339,6 @@ test('rehype', async function (t) {
340339
assert.deepEqual(
341340
rehype()
342341
.data('settings', {
343-
// @ts-expect-error: to do: type `settings`.
344342
emitParseErrors: true,
345343
missingWhitespaceBeforeDoctypeName: true
346344
})
@@ -357,7 +355,6 @@ test('rehype', async function (t) {
357355
assert.deepEqual(
358356
rehype()
359357
.data('settings', {
360-
// @ts-expect-error: to do: type `settings`.
361358
emitParseErrors: true,
362359
missingWhitespaceBeforeDoctypeName: 2
363360
})
@@ -373,7 +370,6 @@ test('rehype', async function (t) {
373370
assert.deepEqual(
374371
rehype()
375372
.data('settings', {
376-
// @ts-expect-error: to do: type `settings`.
377373
emitParseErrors: true,
378374
missingWhitespaceBeforeDoctypeName: 1
379375
})
@@ -403,15 +399,15 @@ test('fixtures', async function (t) {
403399
const expectedTreeUrl = new URL('index.json', base)
404400
const expectedOutputUrl = new URL('result.html', base)
405401

406-
// To do: types of `Settings`.
407-
/** @type {{fragment?: boolean, reprocess?: boolean}} */
408-
let settings = {}
402+
/** @type {Settings & {reprocess?: boolean | null | undefined}} */
403+
let config = {}
409404

410405
try {
411-
settings = JSON.parse(String(await fs.readFile(configUrl)))
406+
config = JSON.parse(String(await fs.readFile(configUrl)))
412407
} catch {}
413408

414-
// @ts-expect-error: To do: types of `Settings`.
409+
const {reprocess, ...settings} = config
410+
415411
const processor = rehype().data('settings', settings)
416412
const input = new VFile({
417413
basename: 'index.html',
@@ -449,9 +445,8 @@ test('fixtures', async function (t) {
449445
assert.deepEqual(actualTree, expectedTree)
450446
assert.equal(actualOutput, expectedOutput)
451447

452-
if (settings.reprocess !== false) {
448+
if (reprocess !== false) {
453449
const reprocessedTree = rehype()
454-
// @ts-expect-error: to do: type `settings`.
455450
.data('settings', settings)
456451
.parse(actualOutput)
457452
removePosition(actualTree)

0 commit comments

Comments
 (0)