From 7a4a5e1aff8107495daf5464b7838c263277eb0a Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sat, 8 Jul 2023 16:54:34 +0200 Subject: [PATCH] Change to return `undefined` --- index.test-d.ts | 15 --------------- lib/index.js | 43 +++++++++++-------------------------------- package.json | 13 ------------- readme.md | 2 +- test.js | 17 ++++++++++++----- 5 files changed, 24 insertions(+), 66 deletions(-) delete mode 100644 index.test-d.ts diff --git a/index.test-d.ts b/index.test-d.ts deleted file mode 100644 index 924a82e..0000000 --- a/index.test-d.ts +++ /dev/null @@ -1,15 +0,0 @@ -import type {Root, Heading, Paragraph} from 'mdast' -import {expectType} from 'tsd' -import {squeezeParagraphs} from './index.js' - -const root: Root = {type: 'root', children: []} -const paragraph: Paragraph = {type: 'paragraph', children: []} -const heading: Heading = {type: 'heading', depth: 1, children: []} -const headingOrParagraph = {type: 'paragraph', children: []} as - | Heading - | Paragraph - -expectType(squeezeParagraphs(root)) -expectType(squeezeParagraphs(paragraph)) -expectType(squeezeParagraphs(heading)) -expectType(squeezeParagraphs(headingOrParagraph)) diff --git a/lib/index.js b/lib/index.js index 0e8330e..c5f7b83 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,5 @@ /** * @typedef {import('mdast').Nodes} Nodes - * @typedef {import('mdast').Paragraph} Paragraph */ import {visit} from 'unist-util-visit' @@ -8,43 +7,23 @@ import {visit} from 'unist-util-visit' /** * Remove empty paragraphs in `tree`. * - * @template {Nodes} Tree - * Node type. - * @param {Tree} tree + * @param {Nodes} tree * Tree to change. - * @returns {Tree extends Paragraph ? Tree | null : Tree} - * Changed `tree`, or `null` if it was an empty paragraph. + * @returns {undefined} + * Nothing. */ export function squeezeParagraphs(tree) { - if (emptyParagraph(tree)) { - // @ts-expect-error: it’s an empty paragraph. - return null - } - visit(tree, function (node, index, parent) { - if (index !== undefined && parent && emptyParagraph(node)) { + if ( + index !== undefined && + parent && + node.type === 'paragraph' && + node.children.every(function (child) { + return child.type === 'text' && /^\s*$/.test(child.value) + }) + ) { parent.children.splice(index, 1) return index } }) - - // @ts-expect-error: it’s not an empty paragraph. - return tree -} - -/** - * Check if a node is an empty paragraph. - * - * @param {Nodes} node - * Node to check. - * @returns {boolean} - * Whether `node` was an empty paragraph. - */ -function emptyParagraph(node) { - return ( - node.type === 'paragraph' && - node.children.every(function (child) { - return child.type === 'text' && /^\s*$/.test(child.value) - }) - ) } diff --git a/package.json b/package.json index 5b884de..d19c4bd 100644 --- a/package.json +++ b/package.json @@ -49,8 +49,6 @@ "prettier": "^2.0.0", "remark-cli": "^11.0.0", "remark-preset-wooorm": "^9.0.0", - "tape": "^5.0.0", - "tsd": "^0.28.0", "type-coverage": "^2.0.0", "typescript": "^5.0.0", "unist-builder": "^4.0.0", @@ -84,17 +82,6 @@ "strict": true }, "xo": { - "overrides": [ - { - "files": [ - "**/*.ts" - ], - "rules": { - "@typescript-eslint/ban-types": "off", - "@typescript-eslint/consistent-type-assertions": "off" - } - } - ], "prettier": true } } diff --git a/readme.md b/readme.md index 3c7c7f3..65fd625 100644 --- a/readme.md +++ b/readme.md @@ -106,7 +106,7 @@ Remove empty paragraphs in `tree`. ###### Returns -Changed `tree` ([`Node`][node]), or `null` if it was an empty paragraph. +Nothing (`undefined`). ## Types diff --git a/test.js b/test.js index 363bb17..c7415ae 100644 --- a/test.js +++ b/test.js @@ -20,8 +20,10 @@ test('squeezeParagraphs', async function (t) { ]) ]) + squeezeParagraphs(tree) + assert.deepEqual( - squeezeParagraphs(tree), + tree, u('root', [ u('paragraph', [u('text', 'first')]), u('paragraph', [ @@ -37,9 +39,14 @@ test('squeezeParagraphs', async function (t) { ) }) - await t.test('should return `null` for empty paragraphs', async function () { - const tree = u('paragraph', []) + await t.test( + 'should do nothing with a (empty or not) paragraph', + async function () { + const tree = u('paragraph', []) - assert.deepEqual(squeezeParagraphs(tree), null) - }) + squeezeParagraphs(tree) + + assert.deepEqual(tree, u('paragraph', [])) + } + ) })