Skip to content

Commit 5383d20

Browse files
committed
Use @types/nlcst
1 parent 6fdd05d commit 5383d20

File tree

5 files changed

+46
-35
lines changed

5 files changed

+46
-35
lines changed

.gitignore

Lines changed: 4 additions & 3 deletions
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+
index.d.ts
5+
test/**/*.d.ts
6+
*.log
67
yarn.lock

complex-types.d.ts

Lines changed: 11 additions & 0 deletions
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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist').Literal<string>} Literal
4-
* @typedef {import('unist').Parent} Parent
2+
* @typedef {import('nlcst').Sentence} Sentence
3+
* @typedef {import('nlcst').SentenceContent} SentenceContent
4+
* @typedef {import('./complex-types').Emoticon} Emoticon
55
*/
66

77
import {toString} from 'nlcst-to-string'
88
import {modifyChildren} from 'unist-util-modify-children'
99
import {emoticon} from 'emoticon'
1010

11-
export const emoticonModifier = modifyChildren(mergeEmoticons)
12-
1311
// Magic numbers.
1412
//
1513
// Emoticons are treated by a parser as multiple nodes.
@@ -30,13 +28,17 @@ const end = []
3028

3129
unpack()
3230

31+
export const emoticonModifier =
32+
/** @type {(node: Sentence) => void} */
33+
// @ts-expect-error: To do: make types in `unist-util-modify-children` smart.
34+
(modifyChildren(mergeEmoticons))
35+
3336
/**
3437
* Merge emoticons into an `EmoticonNode`.
3538
*
36-
* @param {Node} child
39+
* @param {SentenceContent} child
3740
* @param {number} index
38-
* @param {Parent} parent
39-
*
41+
* @param {Sentence} parent
4042
*/
4143
function mergeEmoticons(child, index, parent) {
4244
// Check if `child`s first character could be used to start an emoticon.
@@ -59,7 +61,7 @@ function mergeEmoticons(child, index, parent) {
5961
end.includes(value.charAt(value.length - 1)) &&
6062
emoticons.includes(value)
6163
) {
62-
/** @type {Literal} */
64+
/** @type {Emoticon} */
6365
const emoticonNode = {type: 'EmoticonNode', value}
6466

6567
if (child.position && node.position) {

package.json

Lines changed: 4 additions & 2 deletions
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
"emoticon": "^4.0.0",
3638
"nlcst-to-string": "^3.0.0",
@@ -55,11 +57,11 @@
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",
62-
"test": "npm run format && npm run test-coverage"
64+
"test": "npm run build && npm run format && npm run test-coverage"
6365
},
6466
"prettier": {
6567
"tabWidth": 2,

test/index.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist').Literal<string>} Literal
2+
* @typedef {import('nlcst').Root} Root
3+
* @typedef {import('../complex-types').Emoticon} Emoticon
44
*/
55

66
import fs from 'node:fs'
@@ -40,16 +40,15 @@ test('nlcst-emoticon-modifier()', (t) => {
4040

4141
const files = fs.readdirSync(root)
4242
let index = -1
43-
/** @type {Node} */
44-
let tree
45-
/** @type {string} */
46-
let name
4743

4844
while (++index < files.length) {
4945
if (isHidden(files[index])) continue
5046

51-
tree = JSON.parse(String(fs.readFileSync(path.join(root, files[index]))))
52-
name = path.basename(files[index], path.extname(files[index]))
47+
/** @type {Root} */
48+
const tree = JSON.parse(
49+
String(fs.readFileSync(path.join(root, files[index])))
50+
)
51+
const name = path.basename(files[index], path.extname(files[index]))
5352

5453
t.deepLooseEqual(position.parse(toString(tree)), tree, name)
5554
t.deepLooseEqual(
@@ -64,24 +63,20 @@ test('nlcst-emoticon-modifier()', (t) => {
6463

6564
test('emoticons', (t) => {
6665
let index = -1
67-
let offset = -1
68-
/** @type {Array.<string>} */
69-
let list
70-
/** @type {Node} */
71-
let tree
72-
/** @type {Literal} */
73-
let node
7466

7567
while (++index < emoticon.length) {
76-
list = emoticon[index].emoticons
77-
offset = -1
68+
const list = emoticon[index].emoticons
69+
let offset = -1
7870

7971
while (++offset < list.length) {
80-
tree = position.runSync(
81-
position.parse('Who doesn’t like ' + list[offset] + '?')
72+
const tree = /** @type {Root} */ (
73+
position.runSync(
74+
position.parse('Who doesn’t like ' + list[offset] + '?')
75+
)
8276
)
83-
// @ts-expect-error: hush
84-
node = tree.children[0].children[0].children[6]
77+
/** @type {Emoticon} */
78+
// @ts-expect-error: fine.
79+
const node = tree.children[0].children[0].children[6]
8580

8681
t.strictEqual(node.type, 'EmoticonNode', list[offset] + ' type')
8782
t.strictEqual(node.value, list[offset], list[offset] + ' value')

0 commit comments

Comments
 (0)