Skip to content

Commit

Permalink
Add FrozenProcessor interface to types (#93)
Browse files Browse the repository at this point in the history
* Add FrozenProcessor interface

A frozen processor has the same interface as a regular processor, except `use()`
is always disallowed.

* Fix typo
  • Loading branch information
remcohaszing authored Jul 22, 2020
1 parent 438ba3e commit 56fdba4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
28 changes: 18 additions & 10 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ declare namespace unified {
*
* @typeParam P Processor settings. Useful when packaging unified with a preset parser and compiler.
*/
interface Processor<P = Settings> {
/**
* Clone current processor
*
* @returns New unfrozen processor which is configured to function the same as its ancestor.
* But when the descendant processor is configured in the future it does not affect the ancestral processor.
*/
(): Processor<P>

interface Processor<P = Settings> extends FrozenProcessor<P> {
/**
* Configure the processor to use a plugin and optionally configure that plugin with options.
*
Expand Down Expand Up @@ -61,6 +53,22 @@ declare namespace unified {
* @param processorSettings Settings passed to processor
*/
use(processorSettings: ProcessorSettings<P>): Processor<P>
}

/**
* A frozen processor is just like a regular processor, except no additional plugins can be added.
* A frozen processor can be created by calling `.freeze()` on a processor.
*
* @see Processor
*/
interface FrozenProcessor<P = Settings> {
/**
* Clone current processor
*
* @returns New unfrozen processor which is configured to function the same as its ancestor.
* But when the descendant processor is configured in the future it does not affect the ancestral processor.
*/
(): Processor<P>

/**
* Parse text to a syntax tree.
Expand Down Expand Up @@ -200,7 +208,7 @@ declare namespace unified {
*
* @returns The processor on which freeze is invoked.
*/
freeze(): Processor<P>
freeze(): FrozenProcessor<P>
}

/**
Expand Down
13 changes: 8 additions & 5 deletions types/unified-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let stringValue: string
/**
* `processor()`
*/
let processor: Processor = unified()
const processor: Processor = unified()
const clonedProcessor: Processor = processor()

/**
Expand Down Expand Up @@ -321,7 +321,9 @@ unknownValue = processor.data().randomKey
/**
* `processor.freeze`
*/
processor = processor.freeze()
const frozenProcessor = processor.freeze()
// $ExpectError
frozenProcessor.use(plugin)

/**
* Language specific processors
Expand All @@ -333,15 +335,16 @@ interface RemarkSettings {
const remark = unified<RemarkSettings>()
.use(() => {})
.freeze()
remark
remark.parse('# Hello markdown')
remark()
.use({settings: {gfm: true}})
// $ExpectError
.use({settings: {dne: true}})
remark
remark()
// $ExpectError
.use({settings: {dne: true}})
.use({settings: {gfm: true}})
remark.use(function () {
remark().use(function () {
this
// $ExpectError
.use({settings: {dne: true}})
Expand Down

0 comments on commit 56fdba4

Please sign in to comment.