Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 19, 2021
1 parent a787d84 commit 0374713
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
coverage/
node_modules/
.DS_Store
*.d.ts
*.log
yarn.lock
39 changes: 34 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/**
* @typedef Options
* Configuration.
* @property {'github'|'twitter'|RegExp|null|undefined} [style='github']
* Style can be either `'github'` (for GitHub user and team mentions),
* `'twitter'` (for Twitter handles), or a regular expression (such as
* `/^@\w{1,15}$/i`, which is the Twitter regex).
*/

import {toString} from 'nlcst-to-string'
import {pointStart, pointEnd} from 'unist-util-position'
import {visit} from 'unist-util-visit'
Expand All @@ -8,8 +17,22 @@ const gh =
/^@(?:[a-z\d]{1,2}|[a-z\d][a-z\d-]{1,37}[a-z\d])(\/(?:[a-z\d]{1,2}|[a-z\d][a-z\d-]{1,37}[a-z\d]))?$/i
const tw = /^@\w{1,15}$/i

/**
* Plugin to classify @mentions as source, which represents “external
* (ungrammatical) values” instead of natural language.
* This hides mentions from `retext-spell`, `retext-readability`,
* `retext-equality`.
*
* @type {import('unified').Plugin<[Options?]>}
*/
export default function retextSyntaxMentions(options = {}) {
/**
* @typedef {import('unist').Node<unknown>} Node
* @typedef {import('unist').Literal<string>} Literal
*/

const style = options.style || 'github'
/** @type {RegExp} */
let styleRe

if (style === null || style === undefined || style === 'github') {
Expand All @@ -26,12 +49,11 @@ export default function retextSyntaxMentions(options = {}) {

return (tree) => {
visit(tree, 'SymbolNode', (node, index, parent) => {
const siblings = parent.children

if (toString(node) !== '@') {
if (toString(node) !== '@' || !parent || index === null) {
return
}

const siblings = parent.children
let offset = index + 1

while (offset < siblings.length) {
Expand All @@ -53,17 +75,24 @@ export default function retextSyntaxMentions(options = {}) {
return
}

siblings.splice(index, offset - index, {
/** @type {Literal} */
const replacement = {
type: 'SourceNode',
value: toString(slice),
position: {
start: pointStart(node),
end: pointEnd(slice[slice.length - 1])
}
})
}

siblings.splice(index, offset - index, replacement)
})
}

/**
* @param {Node[]} nodes
* @returns {boolean}
*/
function check(nodes) {
return styleRe.test(toString(nodes).replace(genitive, ''))
}
Expand Down
16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,38 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"nlcst-to-string": "^3.0.0",
"unified": "^10.0.0",
"unist-util-position": "^4.0.0",
"unist-util-visit": "^3.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"retext": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"unist-builder": "^3.0.0",
"unist-util-remove-position": "^4.0.0",
"xo": "^0.39.0"
},
"scripts": {
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
"test": "npm run format && npm run test-coverage"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -69,5 +77,11 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}
21 changes: 14 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import {u} from 'unist-builder'
import {removePosition} from 'unist-util-remove-position'
import retextSyntaxMentions from './index.js'

/** @type {import('unified').Plugin<[]>} */
const strip = () => (tree) => {
removePosition(tree, true)
}

const position = retext().use(retextSyntaxMentions)
const noPosition = retext().use(retextSyntaxMentions).use(strip)

function strip() {
return transformer
function transformer(tree) {
removePosition(tree, true)
}
}

test('retext-syntax-mentions', (t) => {
t.throws(
() => {
// @ts-expect-error: runtime.
retext().use(retextSyntaxMentions, {style: '!'}).freeze()
},
/^Error: Expected known style/,
Expand Down Expand Up @@ -275,6 +274,14 @@ test('retext-syntax-mentions', (t) => {
t.end()
})

/**
* @param {number} l1
* @param {number} c1
* @param {number} o1
* @param {number} l2
* @param {number} c2
* @param {number} o2
*/
// eslint-disable-next-line max-params
function pos(l1, c1, o1, l2, c2, o2) {
return {
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"include": ["*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"strict": true
}
}

0 comments on commit 0374713

Please sign in to comment.