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 5, 2021
1 parent 7d1341b commit 3f125a9
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 129 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
28 changes: 24 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('micromark-extension-frontmatter').Options} Options
*/

import {frontmatter} from 'micromark-extension-frontmatter'
import {
frontmatterFromMarkdown,
frontmatterToMarkdown
} from 'mdast-util-frontmatter'

export default function remarkFrontmatter(options) {
/**
* Plugin to add support for frontmatter.
*
* @type {import('unified').Plugin<[Options?]|void[], Root>}
*/
export default function remarkFrontmatter(options = 'yaml') {
const data = this.data()

add('micromarkExtensions', frontmatter(options))
add('fromMarkdownExtensions', frontmatterFromMarkdown(options))
add('toMarkdownExtensions', frontmatterToMarkdown(options))

/**
* @param {string} field
* @param {unknown} value
*/
function add(field, value) {
/* istanbul ignore if - other extensions. */
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)
}
}
20 changes: 17 additions & 3 deletions 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": {
"@types/mdast": "^3.0.0",
"mdast-util-frontmatter": "^1.0.0",
"micromark-extension-frontmatter": "^1.0.0"
"micromark-extension-frontmatter": "^1.0.0",
"unified": "^10.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.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",
"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": "npm run format && npm run test-coverage"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -79,5 +87,11 @@
],
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}
49 changes: 31 additions & 18 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/**
* @typedef {import('vfile').VFile} VFile
* @typedef {import('mdast').Root} Root
* @typedef {import('../index.js').Options} Options
*/

import fs from 'fs'
import path from 'path'
import test from 'tape'
import {readSync} from 'to-vfile'
import {readSync, writeSync} from 'to-vfile'
import {unified} from 'unified'
import {remark} from 'remark'
import {isHidden} from 'is-hidden'
import remarkFrontmatter from '../index.js'

const join = path.join
const read = fs.readFileSync
const write = fs.writeFileSync
const dir = fs.readdirSync

test('remarkFrontmatter', (t) => {
t.doesNotThrow(() => {
remark().use(remarkFrontmatter).freeze()
Expand All @@ -23,6 +24,7 @@ test('remarkFrontmatter', (t) => {

t.throws(
() => {
// @ts-expect-error: invalid input.
unified().use(remarkFrontmatter, [1]).freeze()
},
/^Error: Expected matter to be an object, not `1`/,
Expand All @@ -31,6 +33,7 @@ test('remarkFrontmatter', (t) => {

t.throws(
() => {
// @ts-expect-error: invalid input.
unified().use(remarkFrontmatter, ['jsonml']).freeze()
},
/^Error: Missing matter definition for `jsonml`/,
Expand All @@ -40,6 +43,7 @@ test('remarkFrontmatter', (t) => {
t.throws(
() => {
unified()
// @ts-expect-error: invalid input.
.use(remarkFrontmatter, [{marker: '*'}])
.freeze()
},
Expand All @@ -50,6 +54,7 @@ test('remarkFrontmatter', (t) => {
t.throws(
() => {
unified()
// @ts-expect-error: invalid input.
.use(remarkFrontmatter, [{type: 'jsonml'}])
.freeze()
},
Expand All @@ -61,45 +66,53 @@ test('remarkFrontmatter', (t) => {
})

test('fixtures', (t) => {
const base = join('test', 'fixtures')
const entries = dir(base).filter((d) => !isHidden(d))
const base = path.join('test', 'fixtures')
const entries = fs.readdirSync(base).filter((d) => !isHidden(d))
let index = -1

t.plan(entries.length)

while (++index < entries.length) {
const fixture = entries[index]
t.test(fixture, (st) => {
const input = readSync(join(base, fixture, 'input.md'))
const treePath = join(base, fixture, 'tree.json')
const outputPath = join(base, fixture, 'output.md')
const input = readSync(path.join(base, fixture, 'input.md'))
const treePath = path.join(base, fixture, 'tree.json')
const outputPath = path.join(base, fixture, 'output.md')
/** @type {VFile} */
let output
/** @type {Root} */
let expected
/** @type {Options|undefined} */
let config

try {
config = JSON.parse(read(join(base, fixture, 'config.json')))
config = JSON.parse(
String(readSync(path.join(base, fixture, 'config.json')))
)
} catch {}

const proc = remark().use(remarkFrontmatter, config)
const actual = JSON.parse(JSON.stringify(proc.parse(input)))
const actual = proc.parse(input)

try {
output = read(outputPath, 'utf8')
output = readSync(outputPath)
} catch {
output = String(input)
output = input
}

try {
expected = JSON.parse(read(treePath))
expected = JSON.parse(String(readSync(treePath)))
} catch {
// New fixture.
write(treePath, JSON.stringify(actual, 0, 2) + '\n')
writeSync({
path: treePath,
value: JSON.stringify(actual, null, 2) + '\n'
})
expected = actual
}

st.deepEqual(actual, expected, 'tree')
st.equal(String(proc.processSync(input)), output, 'process')
st.equal(String(proc.processSync(input)), String(output), 'process')
st.end()
})
}
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
}
}
63 changes: 0 additions & 63 deletions types/index.d.ts

This file was deleted.

23 changes: 0 additions & 23 deletions types/remark-frontmatter-tests.ts

This file was deleted.

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

This file was deleted.

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

This file was deleted.

0 comments on commit 3f125a9

Please sign in to comment.