Skip to content

Commit 56fdba4

Browse files
authored
Add FrozenProcessor interface to types (#93)
* Add FrozenProcessor interface A frozen processor has the same interface as a regular processor, except `use()` is always disallowed. * Fix typo
1 parent 438ba3e commit 56fdba4

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

types/index.d.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@ declare namespace unified {
99
*
1010
* @typeParam P Processor settings. Useful when packaging unified with a preset parser and compiler.
1111
*/
12-
interface Processor<P = Settings> {
13-
/**
14-
* Clone current processor
15-
*
16-
* @returns New unfrozen processor which is configured to function the same as its ancestor.
17-
* But when the descendant processor is configured in the future it does not affect the ancestral processor.
18-
*/
19-
(): Processor<P>
20-
12+
interface Processor<P = Settings> extends FrozenProcessor<P> {
2113
/**
2214
* Configure the processor to use a plugin and optionally configure that plugin with options.
2315
*
@@ -61,6 +53,22 @@ declare namespace unified {
6153
* @param processorSettings Settings passed to processor
6254
*/
6355
use(processorSettings: ProcessorSettings<P>): Processor<P>
56+
}
57+
58+
/**
59+
* A frozen processor is just like a regular processor, except no additional plugins can be added.
60+
* A frozen processor can be created by calling `.freeze()` on a processor.
61+
*
62+
* @see Processor
63+
*/
64+
interface FrozenProcessor<P = Settings> {
65+
/**
66+
* Clone current processor
67+
*
68+
* @returns New unfrozen processor which is configured to function the same as its ancestor.
69+
* But when the descendant processor is configured in the future it does not affect the ancestral processor.
70+
*/
71+
(): Processor<P>
6472

6573
/**
6674
* Parse text to a syntax tree.
@@ -200,7 +208,7 @@ declare namespace unified {
200208
*
201209
* @returns The processor on which freeze is invoked.
202210
*/
203-
freeze(): Processor<P>
211+
freeze(): FrozenProcessor<P>
204212
}
205213

206214
/**

types/unified-tests.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let stringValue: string
1111
/**
1212
* `processor()`
1313
*/
14-
let processor: Processor = unified()
14+
const processor: Processor = unified()
1515
const clonedProcessor: Processor = processor()
1616

1717
/**
@@ -321,7 +321,9 @@ unknownValue = processor.data().randomKey
321321
/**
322322
* `processor.freeze`
323323
*/
324-
processor = processor.freeze()
324+
const frozenProcessor = processor.freeze()
325+
// $ExpectError
326+
frozenProcessor.use(plugin)
325327

326328
/**
327329
* Language specific processors
@@ -333,15 +335,16 @@ interface RemarkSettings {
333335
const remark = unified<RemarkSettings>()
334336
.use(() => {})
335337
.freeze()
336-
remark
338+
remark.parse('# Hello markdown')
339+
remark()
337340
.use({settings: {gfm: true}})
338341
// $ExpectError
339342
.use({settings: {dne: true}})
340-
remark
343+
remark()
341344
// $ExpectError
342345
.use({settings: {dne: true}})
343346
.use({settings: {gfm: true}})
344-
remark.use(function () {
347+
remark().use(function () {
345348
this
346349
// $ExpectError
347350
.use({settings: {dne: true}})

0 commit comments

Comments
 (0)