From 56fdba4d4f35530bcb3e9fa31692ea593aa4384b Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Wed, 22 Jul 2020 15:00:44 +0200 Subject: [PATCH] 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 --- types/index.d.ts | 28 ++++++++++++++++++---------- types/unified-tests.ts | 13 ++++++++----- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index da95b33c..f058d2df 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -9,15 +9,7 @@ declare namespace unified { * * @typeParam P Processor settings. Useful when packaging unified with a preset parser and compiler. */ - interface Processor

{ - /** - * 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

- + interface Processor

extends FrozenProcessor

{ /** * Configure the processor to use a plugin and optionally configure that plugin with options. * @@ -61,6 +53,22 @@ declare namespace unified { * @param processorSettings Settings passed to processor */ use(processorSettings: ProcessorSettings

): Processor

+ } + + /** + * 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

{ + /** + * 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

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

+ freeze(): FrozenProcessor

} /** diff --git a/types/unified-tests.ts b/types/unified-tests.ts index 60341b8e..3bbcd371 100644 --- a/types/unified-tests.ts +++ b/types/unified-tests.ts @@ -11,7 +11,7 @@ let stringValue: string /** * `processor()` */ -let processor: Processor = unified() +const processor: Processor = unified() const clonedProcessor: Processor = processor() /** @@ -321,7 +321,9 @@ unknownValue = processor.data().randomKey /** * `processor.freeze` */ -processor = processor.freeze() +const frozenProcessor = processor.freeze() +// $ExpectError +frozenProcessor.use(plugin) /** * Language specific processors @@ -333,15 +335,16 @@ interface RemarkSettings { const remark = unified() .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}})