Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 8, 2023
1 parent 779cb6c commit 0803bf2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 25 deletions.
70 changes: 47 additions & 23 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,60 @@
/**
* @typedef {import('nlcst').Root} Root
* @typedef {import('nlcst').Word} Word
*
* @typedef {import('vfile').VFile} VFile
*/

/**
* @typedef Options
* Configuration.
* @property {Array<string>} [ignore]
* Phrases *not* to warn about.
* @property {ReadonlyArray<string> | 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<Options>} */
const emptyOptions = {}
/** @type {ReadonlyArray<never>} */
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<Options> | 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
Expand All @@ -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'
})
}
}
1 change: 1 addition & 0 deletions lib/list.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {ReadonlyArray<string>} */
export const list = [
'awoken',
'awoke',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 0803bf2

Please sign in to comment.