Skip to content

Commit a86b65b

Browse files
committed
Use @types/nlcst
1 parent ce2ae68 commit a86b65b

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

index.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
2-
* @typedef {import('unist').Parent} Parent
3-
* @typedef {import('unist').Node} Node
2+
* @typedef {import('nlcst').Root} Root
3+
* @typedef {import('nlcst').Sentence} Sentence
4+
* @typedef {import('nlcst').Word} Word
5+
* @typedef {import('nlcst').SentenceContent} SentenceContent
46
*
57
* @typedef Options
68
* Configuration.
@@ -25,13 +27,13 @@
2527
* @property {string} stem
2628
*
2729
* @typedef PhraseMatch
28-
* @property {Node[]} nodes
29-
* @property {Parent} parent
30+
* @property {SentenceContent[]} nodes
31+
* @property {Sentence} parent
3032
*
3133
* @typedef KeywordMatch
32-
* @property {Node} node
34+
* @property {Word} node
3335
* @property {number} index
34-
* @property {Parent} parent
36+
* @property {Sentence} parent
3537
*/
3638

3739
import {stemmer} from 'stemmer'
@@ -43,7 +45,7 @@ const own = {}.hasOwnProperty
4345
/**
4446
* Plugin to extract keywords and key-phrases.
4547
*
46-
* @type {import('unified').Plugin<[Options?]>}
48+
* @type {import('unified').Plugin<[Options?]|[], Root>}
4749
*/
4850
export default function retextKeywords(options = {}) {
4951
const maximum = options.maximum || 5
@@ -58,17 +60,17 @@ export default function retextKeywords(options = {}) {
5860
/**
5961
* Get following or preceding important words or white space.
6062
*
61-
* @param {Parent} parent
63+
* @param {Sentence} parent
6264
* @param {number} index
6365
* @param {number} offset
6466
*/
6567
function findPhraseInDirection(parent, index, offset) {
6668
const children = parent.children
67-
/** @type {Node[]} */
69+
/** @type {SentenceContent[]} */
6870
const nodes = []
6971
/** @type {string[]} */
7072
const stems = []
71-
/** @type {Node[]} */
73+
/** @type {SentenceContent[]} */
7274
const queue = []
7375

7476
while (children[(index += offset)]) {
@@ -97,7 +99,7 @@ function findPhraseInDirection(parent, index, offset) {
9799
function getKeyphrases(results, maximum) {
98100
/** @type {Record<string, Keyphrase>} */
99101
const stemmedPhrases = {}
100-
/** @type {Node[]} */
102+
/** @type {Word[]} */
101103
const initialWords = []
102104
/** @type {string} */
103105
let keyword
@@ -112,7 +114,7 @@ function getKeyphrases(results, maximum) {
112114
while (++index < matches.length) {
113115
const phrase = findPhrase(matches[index])
114116
const stemmedPhrase = stemmedPhrases[phrase.value]
115-
const first = phrase.nodes[0]
117+
const first = /** @type {Word} */ (phrase.nodes[0])
116118
const match = {nodes: phrase.nodes, parent: matches[index].parent}
117119

118120
// If we've detected the same stemmed phrase somewhere.
@@ -260,13 +262,14 @@ function merge(previous, current, next) {
260262
/**
261263
* Get most important words in `node`.
262264
*
263-
* @param {Node} node
265+
* @param {Root} node
264266
*/
265267
function getImportantWords(node) {
266268
/** @type {Record<string, Keyword>} */
267269
const words = {}
268270

269-
visit(node, 'WordNode', (word, index, parent) => {
271+
visit(node, 'WordNode', (word, index, parent_) => {
272+
const parent = /** @type {Sentence} */ (parent_)
270273
if (parent && index !== null && important(word)) {
271274
const stem = stemNode(word)
272275
const match = {node: word, index, parent}
@@ -309,7 +312,7 @@ function cloneMatches(words) {
309312
/**
310313
* Check if `node` is important.
311314
*
312-
* @param {Node} node
315+
* @param {SentenceContent} node
313316
* @returns {boolean}
314317
*/
315318
function important(node) {
@@ -336,7 +339,7 @@ function uppercase(value) {
336339
/**
337340
* Get the stem of a node.
338341
*
339-
* @param {Node} node
342+
* @param {SentenceContent} node
340343
* @returns {string}
341344
*/
342345
function stemNode(node) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"index.js"
3535
],
3636
"dependencies": {
37+
"@types/nlcst": "^1.0.0",
3738
"nlcst-to-string": "^3.0.0",
3839
"stemmer": "^2.0.0",
3940
"unified": "^10.0.0",

test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import test from 'tape'
88
import {retext} from 'retext'
9-
// @ts-expect-error: To type.
109
import retextPos from 'retext-pos'
1110
import retextKeywords from './index.js'
1211

0 commit comments

Comments
 (0)