diff --git a/.gitignore b/.gitignore index c977c85..07e5d0a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ -.DS_Store -*.d.ts -*.log coverage/ node_modules/ +.DS_Store +test/**/*.d.ts +index.d.ts +*.log yarn.lock diff --git a/complex-types.d.ts b/complex-types.d.ts new file mode 100644 index 0000000..6c22a3c --- /dev/null +++ b/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 + } +} diff --git a/index.js b/index.js index 0c6e8c1..cd8ab84 100644 --- a/index.js +++ b/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 @@ -43,11 +48,3 @@ function mergeAffixEmoticon(node, index, ancestor) { } } } - -/** - * @param {Node} node - * @returns {node is Parent} - */ -function parent(node) { - return 'children' in node -} diff --git a/package.json b/package.json index b1d0c94..9a930ac 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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", diff --git a/test/index.js b/test/index.js index 59d1695..3174a36 100644 --- a/test/index.js +++ b/test/index.js @@ -1,5 +1,5 @@ /** - * @typedef {import('unist').Node} Node + * @typedef {import('nlcst').Root} Root */ import fs from 'node:fs' @@ -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'))) )