Skip to content

Commit bceb395

Browse files
committed
Use @types/nlcst
1 parent d39fcea commit bceb395

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

.gitignore

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
.DS_Store
2-
*.d.ts
3-
*.log
41
coverage/
52
node_modules/
3+
.DS_Store
4+
test/**/*.d.ts
5+
index.d.ts
6+
*.log
67
yarn.lock

complex-types.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type {Literal} from 'nlcst'
2+
3+
export interface Emoticon extends Literal {
4+
type: 'EmoticonNode'
5+
}
6+
7+
declare module 'nlcst' {
8+
interface SentenceContentMap {
9+
emoticon: Emoticon
10+
}
11+
}

index.js

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist').Parent} Parent
2+
* @typedef {import('nlcst').Paragraph} Paragraph
3+
* @typedef {import('nlcst').ParagraphContent} ParagraphContent
4+
*
5+
* @typedef {import('./complex-types').Emoticon} Emoticon
46
*/
57

68
import {modifyChildren} from 'unist-util-modify-children'
79

8-
export const affixEmoticonModifier = modifyChildren(mergeAffixEmoticon)
10+
export const affixEmoticonModifier =
11+
/** @type {(node: Paragraph) => void} */
12+
// @ts-expect-error: To do: make types in `unist-util-modify-children` smart.
13+
modifyChildren(mergeAffixEmoticon)
914

1015
/**
1116
* Merge emoticons into an `EmoticonNode`.
1217
*
13-
* @param {Node} node
18+
* @param {ParagraphContent} node
1419
* @param {number} index
15-
* @param {Parent} ancestor
20+
* @param {Paragraph} ancestor
1621
*/
1722
function mergeAffixEmoticon(node, index, ancestor) {
1823
const previous = ancestor.children[index - 1]
1924

20-
if (index && parent(previous) && parent(node)) {
25+
if (index && 'children' in previous && 'children' in node) {
2126
const children = node.children
2227
let childIndex = -1
2328

@@ -43,11 +48,3 @@ function mergeAffixEmoticon(node, index, ancestor) {
4348
}
4449
}
4550
}
46-
47-
/**
48-
* @param {Node} node
49-
* @returns {node is Parent}
50-
*/
51-
function parent(node) {
52-
return 'children' in node
53-
}

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
"main": "index.js",
2828
"types": "index.d.ts",
2929
"files": [
30+
"complex-types.d.ts",
3031
"index.d.ts",
3132
"index.js"
3233
],
3334
"dependencies": {
35+
"@types/nlcst": "^1.0.0",
3436
"@types/unist": "^2.0.0",
3537
"unist-util-modify-children": "^3.0.0"
3638
},
@@ -55,7 +57,7 @@
5557
},
5658
"scripts": {
5759
"prepack": "npm run build && npm run format",
58-
"build": "rimraf \"{test/**,}*.d.ts\" && tsc && type-coverage",
60+
"build": "rimraf \"test/**/*.d.ts\" \"index.d.ts\" && tsc && type-coverage",
5961
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
6062
"test-api": "node test/index.js",
6163
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",

test/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @typedef {import('unist').Node} Node
2+
* @typedef {import('nlcst').Root} Root
33
*/
44

55
import fs from 'node:fs'
@@ -12,12 +12,12 @@ import {emoticonModifier} from 'nlcst-emoticon-modifier'
1212
import {removePosition} from 'unist-util-remove-position'
1313
import {affixEmoticonModifier} from '../index.js'
1414

15-
/** @type {Node} */
15+
/** @type {Root} */
1616
const lollipop = JSON.parse(
1717
String(fs.readFileSync(path.join('test', 'fixtures', 'lollipop.json')))
1818
)
1919

20-
/** @type {Node} */
20+
/** @type {Root} */
2121
const smile = JSON.parse(
2222
String(fs.readFileSync(path.join('test', 'fixtures', 'smile.json')))
2323
)

0 commit comments

Comments
 (0)