Skip to content

Commit

Permalink
Add typed settings
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 25, 2023
1 parent d8120dd commit 7c9115b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 21 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -71,7 +71,8 @@
"**/*.ts"
],
"rules": {
"@typescript-eslint/ban-types": "off"
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/consistent-type-definitions": "off"
}
}
],
Expand Down
5 changes: 5 additions & 0 deletions packages/rehype-parse/index.d.ts
Expand Up @@ -17,3 +17,8 @@ export type {Options} from './lib/index.js'
*/
declare const rehypeParse: Plugin<[(Options | null | undefined)?], string, Root>
export default rehypeParse

// Add custom settings supported when `rehype-parse` is added.
declare module 'unified' {
interface Settings extends Options {}
}
3 changes: 1 addition & 2 deletions packages/rehype-parse/lib/index.js
Expand Up @@ -32,8 +32,7 @@ export default function rehypeParse(options) {
/** @type {import('unified').Processor<Root>} */
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
const self = this
const processorSettings = /** @type {Options} */ (self.data('settings'))
const {emitParseErrors, ...settings} = {...processorSettings, ...options}
const {emitParseErrors, ...settings} = {...self.data('settings'), ...options}

self.parser = parser

Expand Down
5 changes: 5 additions & 0 deletions packages/rehype-stringify/index.d.ts
Expand Up @@ -20,3 +20,8 @@ declare const rehypeStringify: Plugin<
string
>
export default rehypeStringify

// Add custom settings supported when `rehype-stringify` is added.
declare module 'unified' {
interface Settings extends Options {}
}
3 changes: 1 addition & 2 deletions packages/rehype-stringify/lib/index.js
Expand Up @@ -18,8 +18,7 @@ export default function rehypeStringify(options) {
/** @type {import('unified').Processor<undefined, undefined, undefined, Root, string>} */
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
const self = this
const processorSettings = /** @type {Options} */ (self.data('settings'))
const settings = {...processorSettings, ...options}
const settings = {...self.data('settings'), ...options}

self.compiler = compiler

Expand Down
27 changes: 11 additions & 16 deletions test/api.js
@@ -1,4 +1,5 @@
/**
* @typedef {import('unified').Settings} Settings
* @typedef {import('hast').Root} Root
*/

Expand Down Expand Up @@ -78,7 +79,6 @@ test('rehype', async function (t) {
async function () {
assert.deepEqual(
unified()
// @ts-expect-error: to do: type `settings`.
.data('settings', {fragment: true})
.use(rehypeParse, {fragment: false})
.use(rehypeStringify)
Expand All @@ -94,7 +94,6 @@ test('rehype', async function (t) {
async function () {
assert.deepEqual(
unified()
// @ts-expect-error: to do: type `settings`.
.data('settings', {quote: '"'})
.use(rehypeParse, {fragment: true})
.use(rehypeStringify, {quote: "'"})
Expand Down Expand Up @@ -187,8 +186,10 @@ test('rehype', async function (t) {
'should throw when `tree` is not a valid node',
async function () {
assert.throws(function () {
// @ts-expect-error: unknown node.
unified().use(rehypeStringify).stringify({type: 'unicorn'})
unified()
.use(rehypeStringify)
// @ts-expect-error: check how an unknown node is handled.
.stringify({type: 'unicorn'})
}, /unicorn/)
}
)
Expand Down Expand Up @@ -308,7 +309,6 @@ test('rehype', async function (t) {
async function () {
assert.deepEqual(
rehype()
// @ts-expect-error: to do: type `settings`.
.data('settings', {emitParseErrors: true})
.processSync('<!doctypehtml>')
.messages.map(String),
Expand All @@ -323,7 +323,6 @@ test('rehype', async function (t) {
assert.deepEqual(
rehype()
.data('settings', {
// @ts-expect-error: to do: type `settings`.
emitParseErrors: true,
missingWhitespaceBeforeDoctypeName: false
})
Expand All @@ -340,7 +339,6 @@ test('rehype', async function (t) {
assert.deepEqual(
rehype()
.data('settings', {
// @ts-expect-error: to do: type `settings`.
emitParseErrors: true,
missingWhitespaceBeforeDoctypeName: true
})
Expand All @@ -357,7 +355,6 @@ test('rehype', async function (t) {
assert.deepEqual(
rehype()
.data('settings', {
// @ts-expect-error: to do: type `settings`.
emitParseErrors: true,
missingWhitespaceBeforeDoctypeName: 2
})
Expand All @@ -373,7 +370,6 @@ test('rehype', async function (t) {
assert.deepEqual(
rehype()
.data('settings', {
// @ts-expect-error: to do: type `settings`.
emitParseErrors: true,
missingWhitespaceBeforeDoctypeName: 1
})
Expand Down Expand Up @@ -403,15 +399,15 @@ test('fixtures', async function (t) {
const expectedTreeUrl = new URL('index.json', base)
const expectedOutputUrl = new URL('result.html', base)

// To do: types of `Settings`.
/** @type {{fragment?: boolean, reprocess?: boolean}} */
let settings = {}
/** @type {Settings & {reprocess?: boolean | null | undefined}} */
let config = {}

try {
settings = JSON.parse(String(await fs.readFile(configUrl)))
config = JSON.parse(String(await fs.readFile(configUrl)))
} catch {}

// @ts-expect-error: To do: types of `Settings`.
const {reprocess, ...settings} = config

const processor = rehype().data('settings', settings)
const input = new VFile({
basename: 'index.html',
Expand Down Expand Up @@ -449,9 +445,8 @@ test('fixtures', async function (t) {
assert.deepEqual(actualTree, expectedTree)
assert.equal(actualOutput, expectedOutput)

if (settings.reprocess !== false) {
if (reprocess !== false) {
const reprocessedTree = rehype()
// @ts-expect-error: to do: type `settings`.
.data('settings', settings)
.parse(actualOutput)
removePosition(actualTree)
Expand Down

0 comments on commit 7c9115b

Please sign in to comment.