diff --git a/package.json b/package.json index f74582a7..ba9067cb 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,8 @@ "**/*.ts" ], "rules": { - "@typescript-eslint/ban-types": "off" + "@typescript-eslint/ban-types": "off", + "@typescript-eslint/consistent-type-definitions": "off" } } ], diff --git a/packages/rehype-parse/index.d.ts b/packages/rehype-parse/index.d.ts index 9daa1a06..4e34af21 100644 --- a/packages/rehype-parse/index.d.ts +++ b/packages/rehype-parse/index.d.ts @@ -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 {} +} diff --git a/packages/rehype-parse/lib/index.js b/packages/rehype-parse/lib/index.js index 5101e47d..6c0125d9 100644 --- a/packages/rehype-parse/lib/index.js +++ b/packages/rehype-parse/lib/index.js @@ -32,8 +32,7 @@ export default function rehypeParse(options) { /** @type {import('unified').Processor} */ // @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 diff --git a/packages/rehype-stringify/index.d.ts b/packages/rehype-stringify/index.d.ts index effed7e7..8b14a783 100644 --- a/packages/rehype-stringify/index.d.ts +++ b/packages/rehype-stringify/index.d.ts @@ -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 {} +} diff --git a/packages/rehype-stringify/lib/index.js b/packages/rehype-stringify/lib/index.js index 5c1bb7f7..2060e7dc 100644 --- a/packages/rehype-stringify/lib/index.js +++ b/packages/rehype-stringify/lib/index.js @@ -18,8 +18,7 @@ export default function rehypeStringify(options) { /** @type {import('unified').Processor} */ // @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 diff --git a/test/api.js b/test/api.js index 2af1781a..5f296e2a 100644 --- a/test/api.js +++ b/test/api.js @@ -1,4 +1,5 @@ /** + * @typedef {import('unified').Settings} Settings * @typedef {import('hast').Root} Root */ @@ -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) @@ -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: "'"}) @@ -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/) } ) @@ -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('') .messages.map(String), @@ -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 }) @@ -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 }) @@ -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 }) @@ -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 }) @@ -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', @@ -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)