Skip to content

Commit

Permalink
Use @types/nlcst
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 27, 2021
1 parent d39fcea commit bceb395
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
7 changes: 4 additions & 3 deletions .gitignore
@@ -1,6 +1,7 @@
.DS_Store
*.d.ts
*.log
coverage/
node_modules/
.DS_Store
test/**/*.d.ts
index.d.ts
*.log
yarn.lock
11 changes: 11 additions & 0 deletions complex-types.d.ts
@@ -0,0 +1,11 @@
import type {Literal} from 'nlcst'

export interface Emoticon extends Literal {
type: 'EmoticonNode'
}

declare module 'nlcst' {
interface SentenceContentMap {
emoticon: Emoticon
}
}
25 changes: 11 additions & 14 deletions index.js
@@ -1,23 +1,28 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('nlcst').Paragraph} Paragraph
* @typedef {import('nlcst').ParagraphContent} ParagraphContent
*
* @typedef {import('./complex-types').Emoticon} Emoticon
*/

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

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

/**
* Merge emoticons into an `EmoticonNode`.
*
* @param {Node} node
* @param {ParagraphContent} node
* @param {number} index
* @param {Parent} ancestor
* @param {Paragraph} ancestor
*/
function mergeAffixEmoticon(node, index, ancestor) {
const previous = ancestor.children[index - 1]

if (index && parent(previous) && parent(node)) {
if (index && 'children' in previous && 'children' in node) {
const children = node.children
let childIndex = -1

Expand All @@ -43,11 +48,3 @@ function mergeAffixEmoticon(node, index, ancestor) {
}
}
}

/**
* @param {Node} node
* @returns {node is Parent}
*/
function parent(node) {
return 'children' in node
}
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -27,10 +27,12 @@
"main": "index.js",
"types": "index.d.ts",
"files": [
"complex-types.d.ts",
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/nlcst": "^1.0.0",
"@types/unist": "^2.0.0",
"unist-util-modify-children": "^3.0.0"
},
Expand All @@ -55,7 +57,7 @@
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"{test/**,}*.d.ts\" && tsc && type-coverage",
"build": "rimraf \"test/**/*.d.ts\" \"index.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
@@ -1,5 +1,5 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('nlcst').Root} Root
*/

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

/** @type {Node} */
/** @type {Root} */
const lollipop = JSON.parse(
String(fs.readFileSync(path.join('test', 'fixtures', 'lollipop.json')))
)

/** @type {Node} */
/** @type {Root} */
const smile = JSON.parse(
String(fs.readFileSync(path.join('test', 'fixtures', 'smile.json')))
)
Expand Down

0 comments on commit bceb395

Please sign in to comment.