diff --git a/lib/index.js b/lib/index.js index ed48834..8b22c12 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,37 +1,60 @@ /** * @typedef {import('nlcst').Root} Root - * @typedef {import('nlcst').Word} Word * + * @typedef {import('vfile').VFile} VFile + */ + +/** * @typedef Options * Configuration. - * @property {Array} [ignore] - * Phrases *not* to warn about. + * @property {ReadonlyArray | null | undefined} [ignore] + * Phrases *not* to warn about (optional). */ import {search} from 'nlcst-search' import {toString} from 'nlcst-to-string' import {findBefore} from 'unist-util-find-before' -import {pointStart, pointEnd} from 'unist-util-position' +import {pointEnd, pointStart} from 'unist-util-position' import {list} from './list.js' -const source = 'retext-passive' -const url = 'https://github.com/retextjs/retext-passive#readme' +/** @type {Readonly} */ +const emptyOptions = {} +/** @type {ReadonlyArray} */ +const emptyIgnore = [] const verbs = new Set(['am', 'are', 'were', 'being', 'is', 'been', 'was', 'be']) /** - * Plugin to check for passive voice. + * Check for passive voice. * - * @type {import('unified').Plugin<[Options?], Root>} + * @param {Readonly | null | undefined} [options] + * Configuration (optional). + * @returns + * Transform. */ -export default function retextPassive(options = {}) { - const ignore = options.ignore || [] +export default function retextPassive(options) { + const settings = options || emptyOptions + const ignore = settings.ignore || emptyIgnore const phrases = - ignore.length > 0 ? list.filter((d) => !ignore.includes(d)) : list + ignore.length > 0 + ? list.filter(function (d) { + return !ignore.includes(d) + }) + : [...list] - return (tree, file) => { - search(tree, phrases, (match, index, parent, phrase) => { - const before = /** @type {Word} */ (findBefore(parent, index, 'WordNode')) + /** + * Transform. + * + * @param {Root} tree + * Tree. + * @param {VFile} file + * File. + * @returns {undefined} + * Nothing. + */ + return function (tree, file) { + search(tree, phrases, function (match, index, parent, phrase) { + const before = findBefore(parent, index, 'WordNode') if (!before || !verbs.has(toString(before).toLowerCase())) { return @@ -40,15 +63,16 @@ export default function retextPassive(options = {}) { const start = pointStart(match[0]) const end = pointEnd(match[match.length - 1]) - Object.assign( - file.message('Don’t use the passive voice', { - /* c8 ignore next -- hard to test */ - place: start && end ? {start, end} : undefined, - source, - ruleId: phrase.replace(/\s+/g, '-').toLowerCase() - }), - {actual: toString(match), expected: [], url} - ) + const message = file.message('Don’t use the passive voice', { + /* c8 ignore next -- hard to test */ + place: start && end ? {start, end} : undefined, + source: 'retext-passive', + ruleId: phrase.replace(/\s+/g, '-').toLowerCase() + }) + + message.actual = toString(match) + message.expected = [] + message.url = 'https://github.com/retextjs/retext-passive#readme' }) } } diff --git a/lib/list.js b/lib/list.js index 58ab2de..8429516 100644 --- a/lib/list.js +++ b/lib/list.js @@ -1,3 +1,4 @@ +/** @type {ReadonlyArray} */ export const list = [ 'awoken', 'awoke', diff --git a/package.json b/package.json index d443339..e9f3bea 100644 --- a/package.json +++ b/package.json @@ -35,9 +35,9 @@ "@types/nlcst": "^2.0.0", "nlcst-search": "^4.0.0", "nlcst-to-string": "^4.0.0", - "unified": "^11.0.0", "unist-util-find-before": "^4.0.0", - "unist-util-position": "^5.0.0" + "unist-util-position": "^5.0.0", + "vfile": "^6.0.0" }, "devDependencies": { "@types/node": "^20.0.0", diff --git a/test.js b/test.js index 63c298a..c92e2eb 100644 --- a/test.js +++ b/test.js @@ -4,6 +4,12 @@ import {retext} from 'retext' import retextPassive from './index.js' test('retext-passive', async function (t) { + await t.test('should expose the public api', async function () { + assert.deepEqual(Object.keys(await import('./index.js')).sort(), [ + 'default' + ]) + }) + const doc = 'He was withheld while we were being fed.\nFed.\nThe fed.' const file = await retext().use(retextPassive).process(doc)