Skip to content

Commit 4b2388e

Browse files
committed
Add JSDoc based types
1 parent f06b649 commit 4b2388e

File tree

6 files changed

+71
-11
lines changed

6 files changed

+71
-11
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
.DS_Store
2-
*.log
31
coverage/
42
node_modules/
3+
.DS_Store
4+
*.d.ts
5+
*.log
56
yarn.lock

.prettierignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

index.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
/**
2+
* @typedef {import('unist').Node} Node
3+
* @typedef {import('unist').Parent} Parent
4+
* @typedef {import('unified').Plugin<[]>} Plugin
5+
*/
6+
17
import {toString} from 'nlcst-to-string'
28
import {convert} from 'unist-util-is'
3-
import {visit} from 'unist-util-visit'
9+
import {visit, SKIP} from 'unist-util-visit'
410
import {pointStart, pointEnd} from 'unist-util-position'
511

612
const source = 'retext-repeated-words'
@@ -21,12 +27,27 @@ const list = new Set([
2127
'mau'
2228
])
2329

24-
// Check for for repeated words.
30+
/**
31+
* A retext plugin to check for for repeated words.
32+
*
33+
* * Doesn’t warn for some words which *do* occur twice (`the best exhibition
34+
* they had had since`)
35+
* * Doesn’t warn for initialisms (`D. D. will pop up with…`)
36+
* * Doesn’t warn for capitalised words (`Duran Duran…`)
37+
*
38+
* @type {Plugin}
39+
*/
2540
export default function retextRepeatedWords() {
41+
/**
42+
* @typedef {{value: string, child: Node, index: number}} Info
43+
*/
44+
2645
return (tree, file) => {
27-
visit(tree, 'SentenceNode', (parent) => {
46+
visit(tree, 'SentenceNode', (/** @type {Parent} */ parent) => {
2847
let index = -1
48+
/** @type {Info|undefined} */
2949
let previous
50+
/** @type {Info|undefined} */
3051
let current
3152

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

3859
current = {child, index, value}
3960

40-
if (previous && previous.value === value && !ignore(value)) {
61+
if (
62+
previous &&
63+
previous.value.toLowerCase() === value.toLowerCase() &&
64+
!ignore(value)
65+
) {
4166
Object.assign(
4267
file.message(
4368
'Expected `' + value + '` once, not twice',
@@ -61,12 +86,17 @@ export default function retextRepeatedWords() {
6186
}
6287
}
6388

64-
return visit.SKIP
89+
return SKIP
6590
})
6691
}
6792
}
6893

69-
// Check if `value`, a word which occurs twice, should be ignored.
94+
/**
95+
* Check if `value`, a word which occurs twice, should be ignored.
96+
*
97+
* @param {string} value
98+
* @returns {boolean}
99+
*/
70100
function ignore(value) {
71101
// …the most heartening exhibition they had had since…
72102
if (list.has(value.toLowerCase())) {

package.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,37 @@
2727
"sideEffects": false,
2828
"type": "module",
2929
"main": "index.js",
30+
"types": "index.d.ts",
3031
"files": [
32+
"index.d.ts",
3133
"index.js"
3234
],
3335
"dependencies": {
3436
"nlcst-to-string": "^3.0.0",
37+
"unified": "^10.0.0",
3538
"unist-util-is": "^5.0.0",
3639
"unist-util-position": "^4.0.0",
3740
"unist-util-visit": "^3.0.0"
3841
},
3942
"devDependencies": {
43+
"@types/tape": "^4.0.0",
4044
"c8": "^7.0.0",
4145
"prettier": "^2.0.0",
4246
"remark-cli": "^9.0.0",
4347
"remark-preset-wooorm": "^8.0.0",
4448
"retext": "^8.0.0",
49+
"rimraf": "^3.0.0",
4550
"tape": "^5.0.0",
51+
"type-coverage": "^2.0.0",
52+
"typescript": "^4.0.0",
4653
"xo": "^0.39.0"
4754
},
4855
"scripts": {
56+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4957
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5058
"test-api": "node --conditions development test.js",
5159
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
52-
"test": "npm run format && npm run test-coverage"
60+
"test": "npm run build && npm run format && npm run test-coverage"
5361
},
5462
"prettier": {
5563
"tabWidth": 2,
@@ -66,5 +74,11 @@
6674
"plugins": [
6775
"preset-wooorm"
6876
]
77+
},
78+
"typeCoverage": {
79+
"atLeast": 100,
80+
"detail": true,
81+
"strict": true,
82+
"ignoreCatch": true
6983
}
7084
}

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import test from 'tape'
22
import {retext} from 'retext'
33
import retextRepeatedWords from './index.js'
44

5-
test('repeatedWords()', (t) => {
5+
test('retextRepeatedWords()', (t) => {
66
t.deepEqual(
77
JSON.parse(
88
JSON.stringify(

tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"include": ["*.js"],
3+
"compilerOptions": {
4+
"target": "ES2020",
5+
"lib": ["ES2020"],
6+
"module": "ES2020",
7+
"moduleResolution": "node",
8+
"allowJs": true,
9+
"checkJs": true,
10+
"declaration": true,
11+
"emitDeclarationOnly": true,
12+
"allowSyntheticDefaultImports": true,
13+
"skipLibCheck": true,
14+
"strict": true
15+
}
16+
}

0 commit comments

Comments
 (0)