-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
7ee92ea
commit db2288b
Showing
5 changed files
with
347 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["es2015"], | ||
"strict": true, | ||
"baseUrl": ".", | ||
"paths": { | ||
"unified-message-control": ["."] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
}) |