Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 16, 2021
1 parent f06b649 commit 4b2388e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 11 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
*.log
coverage/
node_modules/
.DS_Store
*.d.ts
*.log
yarn.lock
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
coverage/
*.json
*.md
42 changes: 36 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('unified').Plugin<[]>} Plugin
*/

import {toString} from 'nlcst-to-string'
import {convert} from 'unist-util-is'
import {visit} from 'unist-util-visit'
import {visit, SKIP} from 'unist-util-visit'
import {pointStart, pointEnd} from 'unist-util-position'

const source = 'retext-repeated-words'
Expand All @@ -21,12 +27,27 @@ const list = new Set([
'mau'
])

// Check for for repeated words.
/**
* A retext plugin to check for for repeated words.
*
* * Doesn’t warn for some words which *do* occur twice (`the best exhibition
* they had had since`)
* * Doesn’t warn for initialisms (`D. D. will pop up with…`)
* * Doesn’t warn for capitalised words (`Duran Duran…`)
*
* @type {Plugin}
*/
export default function retextRepeatedWords() {
/**
* @typedef {{value: string, child: Node, index: number}} Info
*/

return (tree, file) => {
visit(tree, 'SentenceNode', (parent) => {
visit(tree, 'SentenceNode', (/** @type {Parent} */ parent) => {
let index = -1
/** @type {Info|undefined} */
let previous
/** @type {Info|undefined} */
let current

while (++index < parent.children.length) {
Expand All @@ -37,7 +58,11 @@ export default function retextRepeatedWords() {

current = {child, index, value}

if (previous && previous.value === value && !ignore(value)) {
if (
previous &&
previous.value.toLowerCase() === value.toLowerCase() &&
!ignore(value)
) {
Object.assign(
file.message(
'Expected `' + value + '` once, not twice',
Expand All @@ -61,12 +86,17 @@ export default function retextRepeatedWords() {
}
}

return visit.SKIP
return SKIP
})
}
}

// Check if `value`, a word which occurs twice, should be ignored.
/**
* Check if `value`, a word which occurs twice, should be ignored.
*
* @param {string} value
* @returns {boolean}
*/
function ignore(value) {
// …the most heartening exhibition they had had since…
if (list.has(value.toLowerCase())) {
Expand Down
16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,37 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"nlcst-to-string": "^3.0.0",
"unified": "^10.0.0",
"unist-util-is": "^5.0.0",
"unist-util-position": "^4.0.0",
"unist-util-visit": "^3.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"retext": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.39.0"
},
"scripts": {
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
"test": "npm run format && npm run test-coverage"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -66,5 +74,11 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'tape'
import {retext} from 'retext'
import retextRepeatedWords from './index.js'

test('repeatedWords()', (t) => {
test('retextRepeatedWords()', (t) => {
t.deepEqual(
JSON.parse(
JSON.stringify(
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"include": ["*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"strict": true
}
}

0 comments on commit 4b2388e

Please sign in to comment.