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 Aug 6, 2021
1 parent 0846014 commit 37d92ac
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 97 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
27 changes: 22 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('micromark-extension-gfm').Options & import('mdast-util-gfm').Options} Options
*/

import {gfm} from 'micromark-extension-gfm'
import {gfmFromMarkdown, gfmToMarkdown} from 'mdast-util-gfm'

export default function remarkGfm(options) {
/**
* Plugin to support GitHub Flavored Markdown (GFM).
*
* @type {import('unified').Plugin<[Options?]|void[], Root>}
*/
export default function remarkGfm(options = {}) {
const data = this.data()

add('micromarkExtensions', gfm(options))
add('fromMarkdownExtensions', gfmFromMarkdown)
add('toMarkdownExtensions', gfmToMarkdown(options))

/**
* @param {string} field
* @param {unknown} value
*/
function add(field, value) {
// Other extensions.
/* c8 ignore next */
if (data[field]) data[field].push(value)
else data[field] = [value]
const list = /** @type {unknown[]} */ (
// Other extensions
/* c8 ignore next 2 */
data[field] ? data[field] : (data[field] = [])
)

list.push(value)
}
}
26 changes: 18 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,40 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/mdast": "^3.0.0",
"mdast-util-gfm": "^1.0.0",
"micromark-extension-gfm": "^1.0.0"
"micromark-extension-gfm": "^1.0.0",
"unified": "^10.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"dtslint": "^4.0.0",
"is-hidden": "^2.0.0",
"prettier": "^2.0.0",
"remark": "^14.0.0",
"remark-cli": "^10.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"string-width": "^5.0.0",
"tape": "^5.0.0",
"to-vfile": "^7.0.0",
"unified": "^10.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.39.0"
},
"scripts": {
"build": "rimraf \"test/**/*.d.ts\" \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo --ignore-pattern test/ && prettier . -w --loglevel warn && xo --fix",
"test-api": "node --conditions development test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api",
"test-types": "dtslint types",
"test": "npm run format && npm run test-coverage && npm run test-types"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -67,14 +74,17 @@
"trailingComma": "none"
},
"xo": {
"prettier": true,
"ignore": [
"types/"
]
"prettier": true
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}
18 changes: 12 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('../index.js').Options} Options
*/

import fs from 'fs'
import path from 'path'
import test from 'tape'
Expand Down Expand Up @@ -31,32 +36,33 @@ test('fixtures', (t) => {
const file = readSync(path.join(base, entries[index], 'input.md'))
const input = String(file.value)
const treePath = path.join(base, entries[index], 'tree.json')
/** @type {Options|undefined} */
let config

try {
config = JSON.parse(
fs.readFileSync(path.join(base, entries[index], 'config.json'))
String(fs.readFileSync(path.join(base, entries[index], 'config.json')))
)
} catch {
config = undefined
}
} catch {}

if (entries[index] === 'table-string-length') {
config = {stringLength: stringWidth}
}

const proc = remark().use(gfm, config).freeze()
const actual = proc.parse(file)
/** @type {Root} */
let expected

try {
expected = JSON.parse(fs.readFileSync(treePath))
expected = JSON.parse(String(fs.readFileSync(treePath)))
} catch {
// New fixture.
fs.writeFileSync(treePath, JSON.stringify(actual, 0, 2) + '\n')
fs.writeFileSync(treePath, JSON.stringify(actual, null, 2) + '\n')
expected = actual
}

/** @type {string} */
let output

try {
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": ["test/**/*.js", "*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"strict": true
}
}
39 changes: 0 additions & 39 deletions types/index.d.ts

This file was deleted.

22 changes: 0 additions & 22 deletions types/test.ts

This file was deleted.

10 changes: 0 additions & 10 deletions types/tsconfig.json

This file was deleted.

7 changes: 0 additions & 7 deletions types/tslint.json

This file was deleted.

0 comments on commit 37d92ac

Please sign in to comment.