Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ declare namespace unified {
*
* `Parser` can also be a constructor function (a function with keys in its `prototype`) in which case it’s invoked with `new`. Instances must have a parse method which is invoked without arguments and must return a `Node`.
*/
Parser: ParserFunction | typeof Parser
Parser: ParserConstructor | ParserFunction

/**
* Compile a syntax tree to text.
Expand All @@ -98,7 +98,7 @@ declare namespace unified {
* `Compiler` can also be a constructor function (a function with keys in its `prototype`) in which case it’s invoked with `new`.
* Instances must have a `compile` method which is invoked without arguments and must return a `string`.
*/
Compiler: CompilerFunction | typeof Compiler
Compiler: CompilerConstructor | CompilerFunction

/**
* Transform a syntax tree by applying plugins to it.
Expand Down Expand Up @@ -318,43 +318,72 @@ declare namespace unified {
/**
* Transform file contents into an AST
*/
class Parser {
interface Parser {
/**
* Transform file contents into an AST
*
* @param file File to transform into AST node(s)
* @returns Parsed AST node/tree
*/
parse(file: VFileCompatible): Node
parse(): Node
}

/**
* A constructor function (a function with keys in its `prototype`) or class that implements a
* `parse` method.
*/
interface ParserConstructor {
/**
* Creates a Parser
*
* @param text Text to transform into AST node(s)
* @param file File associated with text
*/
new (text: string, file: VFile): Parser
}

/**
* Transform file contents into an AST
* @param file File to transform into AST node(s)
*
* @param text Text to transform into AST node(s)
* @param file File associated with text
* @returns Parsed AST node/tree
*/
type ParserFunction = (file: VFileCompatible) => Node
type ParserFunction = (text: string, file: VFile) => Node

/**
* Transform an AST node/tree into text
*/
class Compiler {
interface Compiler {
/**
* Transform an AST node/tree into text
*
* @param node Node to be stringified
* @returns Compiled text
*/
compile(): string
}

/**
* A constructor function (a function with keys in its `prototype`) or class that implements a
* `compile` method.
*/
interface CompilerConstructor {
/**
* Creates a Compiler
*
* @param node Node/tree to be stringified
* @param file File associated with node
* @returns transformed text
*/
compile(node: Node, file?: VFileCompatible): string
new (node: Node, file: VFile): Compiler
}

/**
* Transform an AST node/tree into text
*
* @param node Node to be stringified
* @param node Node/tree to be stringified
* @param file File associated with node
* @returns transformed text
* @returns Compiled text
*/
type CompilerFunction = (node: Node, file?: VFileCompatible) => string
type CompilerFunction = (node: Node, file: VFile) => string

/**
* Access results from transforms
Expand Down
23 changes: 17 additions & 6 deletions types/unified-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ProcessCallback
} from 'unified'
import vfile = require('vfile')
import {VFile} from 'vfile'

let fileValue: vfile.VFile
let nodeValue: Node
Expand Down Expand Up @@ -37,8 +38,10 @@ const implicitlyTypedPlugin = (settings?: ExamplePluginSettings) => {}

const transformerPlugin = (settings?: ExamplePluginSettings) => (
tree: Node,
file: vfile.VFile
) => tree
file: VFile
) => ({
type: 'random node'
})

const pluginWithTwoSettings = (
processor?: Processor,
Expand Down Expand Up @@ -177,11 +180,15 @@ processor.parse(new Buffer('random buffer'))
/**
* processor.Parser
*/
processor.Parser = (file: VFileCompatible) => ({
processor.Parser = (text: string, file: VFile) => ({
type: 'random node'
})
processor.Parser = class CustomParser {
parse(file: VFileCompatible) {
constructor(text: string, file: VFile) {
// nothing
}

parse(): Node {
return {
type: 'random node'
}
Expand All @@ -196,11 +203,15 @@ stringValue = processor.stringify(nodeValue)
/**
* processor.Compiler
*/
processor.Compiler = (node: Node, file?: VFileCompatible) => {
processor.Compiler = (node: Node, file: VFile) => {
return 'random string'
}
processor.Compiler = class CustomCompiler {
compile(node: Node, file?: vfile.VFile) {
constructor(node: Node, file: VFile) {
// nothing
}

compile() {
return 'random string'
}
}
Expand Down