Skip to content

Commit

Permalink
Add types
Browse files Browse the repository at this point in the history
Closes GH-7.
Closes GH-8.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
Reviewed-by: Junyoung Choi <fluke8259@gmail.com>

Co-authored-by: Thomas Neil James Shadwell <zemnmez+gbkt@googlemail.com>
  • Loading branch information
ChristianMurphy and Zemnmez committed Jan 30, 2020
1 parent 7ee92ea commit db2288b
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 11 deletions.
116 changes: 116 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// TypeScript Version: 3.4

import {Node} from 'unist'
import {Transformer} from 'unified'
import {Test} from 'unist-util-is'

declare namespace messageControl {
/**
* A comment marker.
*/
interface Marker<N extends Node> {
/**
* Name of marker
*/
name: string

/**
* Value after name
*/
attributes: string

/**
* Parsed attributes
*/
parameters: Record<string, unknown>

/**
* Reference to given node
*/
node: N
}

/**
* Parse a possible comment marker node to a Marker
*/
type MarkerParser<N extends Node> = (node: N) => Marker<N> | null

interface MessageControlOptionsWithReset<T extends Node>
extends BaseMessageControlOptions<T> {
/**
* Whether to treat all messages as turned off initially
*/
reset: true

/**
* List of `ruleId`s to initially turn on.
*/
enable?: string[]
}

interface MessageControlOptionsWithoutReset<T extends Node>
extends BaseMessageControlOptions<T> {
/**
* Whether to treat all messages as turned off initially
*/
reset?: false

/**
* List of `ruleId`s to turn off
*/
disable?: string[]
}

interface BaseMessageControlOptions<T extends Node> {
/**
* Name of markers that can control the message sources.
*
* For example. `{name: 'alpha'}` controls `alpha` markers:
*
* `<!--alpha ignore-->`
*/
name: string

/**
* Test for possible markers
*/
test: Test<T>

/**
* Parse a possible marker to a comment marker object (Marker)
* if possible the marker isn't a marker, should return `null`.
*/
marker: MarkerParser<T>

/**
* List of allowed `ruleId`s. When given a warning is shown
* when someone tries to control an unknown rule.
*
* For example, `{name: 'alpha', known: ['bravo']}` results
* in a warning if `charlie is configured:
*
* `<!--alpha ignore charlie-->`
*/
known?: string[]

/**
* Sources that can be controlled with `name` markers.
*
* @defaultValue `MessageControlOptions.name`
*/
sources?: string | string[]
}

type MessageControlOptions<T extends Node> =
| MessageControlOptionsWithoutReset<T>
| MessageControlOptionsWithReset<T>
}

/**
* Enable, disable, and ignore messages with unified.
*/
declare function messageControl<T extends Node>(
options?: messageControl.MessageControlOptions<T>
): Transformer

export = messageControl
31 changes: 20 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,45 @@
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"Christian Murphy <christian.murphy.42@gmail.com> (https://github.com/ChristianMurphy)"
"Christian Murphy <christian.murphy.42@gmail.com> (https://github.com/ChristianMurphy)",
"Thomas 'zemnmez' Shadwell (https://github.com/zemnmez)"
],
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"dependencies": {
"unist-util-visit": "^1.0.0",
"vfile-location": "^2.0.0"
"unist-util-visit": "^2.0.0",
"vfile-location": "^3.0.0"
},
"devDependencies": {
"@types/hast": "^2.0.0",
"@types/mdast": "^3.0.0",
"@types/unist": "^2.0.0",
"browserify": "^16.0.0",
"dtslint": "^2.0.0",
"esmangle": "^1.0.0",
"mdast-comment-marker": "^1.0.0",
"nyc": "^14.0.0",
"nyc": "^15.0.0",
"prettier": "^1.0.0",
"remark": "^10.0.0",
"remark-cli": "^6.0.0",
"remark-preset-wooorm": "^5.0.0",
"remark-toc": "^5.0.0",
"remark": "^11.0.0",
"remark-cli": "^7.0.0",
"remark-preset-wooorm": "^6.0.0",
"remark-toc": "^7.0.0",
"tape": "^4.0.0",
"unified": "^8.0.0",
"unist-util-is": "^4.0.0",
"xo": "^0.24.0"
},
"scripts": {
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"format": "remark . -qfo && prettier --write '**/*.{js,ts}' && xo --fix",
"build-bundle": "browserify index.js --bare -s unifiedMessageControl > unified-message-control.js",
"build-mangle": "esmangle unified-message-control.js > unified-message-control.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run build && npm run test-coverage"
"test-types": "dtslint",
"test": "npm run test-types && npm run format && npm run build && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
Expand Down
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"baseUrl": ".",
"paths": {
"unified-message-control": ["."]
}
}
}
8 changes: 8 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"semicolon": false,
"whitespace": false,
"no-unnecessary-generics": false
}
}
193 changes: 193 additions & 0 deletions type-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/**
* This file is purely for typechecking and does not produce code
*/

import * as control from 'unified-message-control'
import * as unified from 'unified'
import {Node} from 'unist'
import {HTML} from 'mdast'
import {Element} from 'hast'

// $ExpectError
control({})

control({
name: 'foo',
marker: (n: Node) => null,
test: 'html'
})

control({
name: 'foo',
marker: (n: Node) => ({
name: 'foo',
attributes: 'bar=false',
parameters: {
bar: false
},
node: {
type: 'foo',
value: 'bar=false'
}
}),
test: 'foo'
})

control({
name: 'foo',
marker: (n: Element) => null,
test: 'element'
})

control({
name: 'foo',
marker: (n: HTML) => null,
test: 'html'
})

control({
name: 'foo',
marker: (n: Node) => null,
test: 'html',
known: ['rule-1', 'rule-2']
})

control({
name: 'foo',
marker: (n: Node) => null,
test: 'html',
sources: 'example'
})

control({
name: 'foo',
marker: (n: Node) => null,
test: 'html',
sources: ['one', 'two']
})

control({
name: 'foo',
marker: (n: Node) => null,
test: 'html',
reset: false,
disable: ['rule-id']
})

control({
name: 'foo',
marker: (n: Node) => null,
test: 'html',
reset: false,
// $ExpectError
enable: ['rule-id']
})

control({
name: 'foo',
marker: (n: Node) => null,
test: 'html',
reset: true,
enable: ['rule-id']
})

control({
name: 'foo',
marker: (n: Node) => null,
test: 'html',
reset: true,
// $ExpectError
disable: ['rule-id']
})

// $ExpectError
unified().use(control, {})

unified().use(control, {
name: 'foo',
marker: (n: Node) => null,
test: 'html'
})

unified().use(control, {
name: 'foo',
marker: (n: Node) => ({
name: 'foo',
attributes: 'bar=false',
parameters: {
bar: false
},
node: {
type: 'foo',
value: 'bar=false'
}
}),
test: 'foo'
})

unified().use(control, {
name: 'foo',
marker: (n: Element) => null,
test: 'element'
})

unified().use(control, {
name: 'foo',
marker: (n: HTML) => null,
test: 'html'
})

unified().use(control, {
name: 'foo',
marker: (n: Node) => null,
test: 'html',
known: ['rule-1', 'rule-2']
})

unified().use(control, {
name: 'foo',
marker: (n: Node) => null,
test: 'html',
sources: 'example'
})

unified().use(control, {
name: 'foo',
marker: (n: Node) => null,
test: 'html',
sources: ['one', 'two']
})

unified().use(control, {
name: 'foo',
marker: (n: Node) => null,
test: 'html',
reset: false,
disable: ['rule-id']
})

unified().use(control, {
name: 'foo',
marker: (n: Node) => null,
test: 'html',
reset: false,
// $ExpectError
enable: ['rule-id']
})

unified().use(control, {
name: 'foo',
marker: (n: Node) => null,
test: 'html',
reset: true,
enable: ['rule-id']
})

unified().use(control, {
name: 'foo',
marker: (n: Node) => null,
test: 'html',
reset: true,
// $ExpectError
disable: ['rule-id']
})

0 comments on commit db2288b

Please sign in to comment.