diff --git a/types/index.d.ts b/types/index.d.ts index f4ead217..a8f35f70 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -5,8 +5,15 @@ import {VFile, VFileContents, VFileOptions} from 'vfile' import vfile = require('vfile') declare namespace unified { + /** + * Processor allows plugins, parsers, and compilers to be chained together to transform content. + * + * @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

@@ -17,6 +24,8 @@ declare namespace unified { * @param plugin unified plugin * @param settings Configuration for plugin * @param extraSettings Additional configuration for plugin + * @typeParam S Plugin settings + * @typeParam S2 Extra plugin settings * @returns The processor on which use is invoked */ use( @@ -26,28 +35,33 @@ declare namespace unified { ): Processor

/** + * Configure the processor with a preset to use + * * @param preset `Object` with an plugins (set to list), and/or an optional settings object */ use(preset: Preset

): Processor

/** + * Configure using a tuple of plugin and setting(s) + * * @param pluginTuple pairs, plugin and settings in an array + * @typeParam S Plugin settings + * @typeParam S2 Extra plugin settings */ - use(pluginTuple: PluginTuple): Processor

- - /** - * @param pluginTriple plugin, setting, and extraSettings in an array - */ - use( - pluginTriple: PluginTriple + use( + pluginTuple: PluginTuple ): Processor

/** + * A list of plugins and presets to be applied to processor + * * @param list List of plugins, presets, and pairs */ use(list: PluggableList

): Processor

/** + * Configuration passed to a frozen processor + * * @param processorSettings Settings passed to processor */ use(processorSettings: ProcessorSettings

): Processor

@@ -93,15 +107,35 @@ declare namespace unified { /** * Transform a syntax tree by applying plugins to it. * - * @param node - * @param file `VFile` or anything which can be given to `vfile()` - * @param done Invoked when transformation is complete. - * Either invoked with an error or a syntax tree and a file. + * @param node Node to transform * @returns `Promise` if `done` is not given. Rejected with an error, or resolved with the resulting syntax tree. */ run(node: Node): Promise + + /** + * Transform a syntax tree by applying plugins to it. + * + * @param node Node to transform + * @param file `VFile` or anything which can be given to `vfile()` + * @returns `Promise` if `done` is not given. Rejected with an error, or resolved with the resulting syntax tree. + */ run(node: Node, file: VFileCompatible): Promise + + /** + * Transform a syntax tree by applying plugins to it. + * + * @param node Node to transform + * @param done Invoked when transformation is complete. + */ run(node: Node, done: RunCallback): void + + /** + * Transform a syntax tree by applying plugins to it. + * + * @param node Node to transform + * @param file `VFile` or anything which can be given to `vfile()` + * @param done Invoked when transformation is complete. + */ run(node: Node, file: VFileCompatible, done: RunCallback): void /** @@ -109,7 +143,7 @@ declare namespace unified { * * If asynchronous plugins are configured an error is thrown. * - * @param node + * @param node Node to transform * @param file `VFile` or anything which can be given to `vfile()` * @returns The given syntax tree. */ @@ -117,12 +151,17 @@ declare namespace unified { /** * Process the given representation of a file as configured on the processor. The process invokes `parse`, `run`, and `stringify` internally. - * @param file - * @param done Invoked when the process is complete. Invoked with a fatal error, if any, and the VFile. + * @param file `VFile` or anything which can be given to `vfile()` * @returns `Promise` if `done` is not given. * Rejected with an error or resolved with the resulting file. */ process(file: VFileCompatible): Promise + + /** + * Process the given representation of a file as configured on the processor. The process invokes `parse`, `run`, and `stringify` internally. + * @param file `VFile` or anything which can be given to `vfile()` + * @param done Invoked when the process is complete. Invoked with a fatal error, if any, and the VFile. + */ process(file: VFileCompatible, done: ProcessCallback): void /** @@ -130,7 +169,7 @@ declare namespace unified { * * If asynchronous plugins are configured an error is thrown. * - * @param file + * @param file `VFile` or anything which can be given to `vfile()` * @returns Virtual file with modified contents. */ processSync(file: VFileCompatible): VFile @@ -142,11 +181,13 @@ declare namespace unified { * @returns key-value store object */ data(): {[key: string]: unknown} + /** * @param key Identifier * @returns If getting, the value at key */ data(key: string): unknown + /** * @param value Value to set. Omit if getting key * @returns If setting, the processor on which data is invoked @@ -165,37 +206,77 @@ declare namespace unified { freeze(): Processor

} + /** + * A Plugin (Attacher) is the thing passed to `use`. + * It configures the processor and in turn can receive options. + * + * Attachers can configure processors, such as by interacting with parsers and compilers, linking them to other processors, or by specifying how the syntax tree is handled. + * + * @this Processor context object is set to the invoked on processor. + * @param settings Configuration + * @param extraSettings Secondary configuration + * @typeParam S Plugin settings + * @typeParam S2 Extra plugin settings + * @typeParam P Processor settings + * @returns Optional Transformer. + */ type Plugin = Attacher + + /** + * Configuration passed to a Plugin or Processor + */ type Settings = { [key: string]: unknown } + /** * Presets provide a potentially sharable way to configure processors. * They can contain multiple plugins and optionally settings as well. + * + * @typeParam P Processor settings */ - interface Preset { + interface Preset

{ plugins: PluggableList

- settings?: S + settings?: Settings } /** * Settings can be passed directly to the processor + * + * @typeParam P Settings applied to a processor. Useful when packaging unified with a preset parser and compiler. */ - interface ProcessorSettings { - settings: S + interface ProcessorSettings

{ + settings: P } - type PluginTuple = [Plugin, S] - type PluginTriple = [ - Plugin, - S, - S2 - ] + /** + * A pairing of a plugin with its settings + * + * @typeParam S Plugin settings + * @typeParam S2 Extra plugin settings + * @typeParam P Processor settings + */ + type PluginTuple = + | [Plugin, S] + | [Plugin, S, S2] + + /** + * A union of the different ways to add plugins to unified + * + * @typeParam S Plugin settings + * @typeParam S2 Extra plugin settings + * @typeParam P Processor settings + */ type Pluggable = | Plugin - | Preset - | PluginTuple - | PluginTriple + | Preset

+ | PluginTuple + + /** + * A list of plugins and presets + * + * @typeParam P Processor settings + */ type PluggableList

= Array> /** @@ -207,7 +288,10 @@ declare namespace unified { * @this Processor context object is set to the invoked on processor. * @param settings Configuration * @param extraSettings Secondary configuration - * @returns Optional. + * @typeParam S Plugin settings + * @typeParam S2 Extra plugin settings + * @typeParam P Processor settings + * @returns Optional Transformer. */ interface Attacher { (this: Processor

, settings?: S, extraSettings?: S2): Transformer | void @@ -219,8 +303,8 @@ declare namespace unified { * * The transformation process in unified is handled by `trough`, see it’s documentation for the exact semantics of transformers. * - * @param node - * @param file + * @param node Node or tree to be transformed + * @param file File associated with node or tree * @param next If the signature of a transformer includes `next` (third argument), the function may finish asynchronous, and must invoke `next()`. * @returns * - `Error` — Can be returned to stop the process @@ -235,26 +319,74 @@ declare namespace unified { ): Error | Node | Promise } + /** + * Transform file contents into an AST + */ class Parser { + /** + * Transform file contents into an AST + * + * @param file File to transform into AST node(s) + */ parse(file: VFileCompatible): Node } + /** + * Transform file contents into an AST + * @param file File to transform into AST node(s) + */ type ParserFunction = (file: VFileCompatible) => Node + /** + * Transform an AST node/tree into text + */ class Compiler { + /** + * Transform an AST node/tree into text + * + * @param node Node to be stringified + * @param file File associated with node + * @returns transformed text + */ compile(node: Node, file?: VFileCompatible): string } + + /** + * Transform an AST node/tree into text + * + * @param node Node to be stringified + * @param file File associated with node + * @returns transformed text + */ type CompilerFunction = (node: Node, file?: VFileCompatible) => string + /** + * Access results from transforms + * + * @param error Error if any occurred + * @param node Transformed AST tree/node + * @param vfile File associated with node + */ type RunCallback = (error: Error | null, node: Node, file: VFile) => void + /** + * Access results from transforms + * + * @param error Error if any occurred + * @param vfile File with updated content + */ type ProcessCallback = (error: Error | null, file: VFile) => void + /** + * A union of the VFile types unified supports + */ type VFileCompatible = VFile | VFileOptions | VFileContents } /** - * Object describing how to process text. + * Unified processor allows plugins, parsers, and compilers to be chained together to transform content. + * + * @typeParam P Processor settings. Useful when packaging unified with a preset parser and compiler. */ declare function unified

(): unified.Processor

export = unified