Skip to content

Commit 6d4c01c

Browse files
clavinwooorm
authored andcommitted
Fix types of Parser, Compiler
Closes GH-64. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Junyoung Choi <fluke8259@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 6c84a1f commit 6d4c01c

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

types/index.d.ts

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ declare namespace unified {
7878
*
7979
* `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`.
8080
*/
81-
Parser: ParserFunction | typeof Parser
81+
Parser: ParserConstructor | ParserFunction
8282

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

103103
/**
104104
* Transform a syntax tree by applying plugins to it.
@@ -318,43 +318,72 @@ declare namespace unified {
318318
/**
319319
* Transform file contents into an AST
320320
*/
321-
class Parser {
321+
interface Parser {
322322
/**
323323
* Transform file contents into an AST
324324
*
325-
* @param file File to transform into AST node(s)
325+
* @returns Parsed AST node/tree
326326
*/
327-
parse(file: VFileCompatible): Node
327+
parse(): Node
328+
}
329+
330+
/**
331+
* A constructor function (a function with keys in its `prototype`) or class that implements a
332+
* `parse` method.
333+
*/
334+
interface ParserConstructor {
335+
/**
336+
* Creates a Parser
337+
*
338+
* @param text Text to transform into AST node(s)
339+
* @param file File associated with text
340+
*/
341+
new (text: string, file: VFile): Parser
328342
}
329343

330344
/**
331345
* Transform file contents into an AST
332-
* @param file File to transform into AST node(s)
346+
*
347+
* @param text Text to transform into AST node(s)
348+
* @param file File associated with text
349+
* @returns Parsed AST node/tree
333350
*/
334-
type ParserFunction = (file: VFileCompatible) => Node
351+
type ParserFunction = (text: string, file: VFile) => Node
335352

336353
/**
337354
* Transform an AST node/tree into text
338355
*/
339-
class Compiler {
356+
interface Compiler {
340357
/**
341358
* Transform an AST node/tree into text
342359
*
343-
* @param node Node to be stringified
360+
* @returns Compiled text
361+
*/
362+
compile(): string
363+
}
364+
365+
/**
366+
* A constructor function (a function with keys in its `prototype`) or class that implements a
367+
* `compile` method.
368+
*/
369+
interface CompilerConstructor {
370+
/**
371+
* Creates a Compiler
372+
*
373+
* @param node Node/tree to be stringified
344374
* @param file File associated with node
345-
* @returns transformed text
346375
*/
347-
compile(node: Node, file?: VFileCompatible): string
376+
new (node: Node, file: VFile): Compiler
348377
}
349378

350379
/**
351380
* Transform an AST node/tree into text
352381
*
353-
* @param node Node to be stringified
382+
* @param node Node/tree to be stringified
354383
* @param file File associated with node
355-
* @returns transformed text
384+
* @returns Compiled text
356385
*/
357-
type CompilerFunction = (node: Node, file?: VFileCompatible) => string
386+
type CompilerFunction = (node: Node, file: VFile) => string
358387

359388
/**
360389
* Access results from transforms

types/unified-tests.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ProcessCallback
99
} from 'unified'
1010
import vfile = require('vfile')
11+
import {VFile} from 'vfile'
1112

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

3839
const transformerPlugin = (settings?: ExamplePluginSettings) => (
3940
tree: Node,
40-
file: vfile.VFile
41-
) => tree
41+
file: VFile
42+
) => ({
43+
type: 'random node'
44+
})
4245

4346
const pluginWithTwoSettings = (
4447
processor?: Processor,
@@ -177,11 +180,15 @@ processor.parse(new Buffer('random buffer'))
177180
/**
178181
* processor.Parser
179182
*/
180-
processor.Parser = (file: VFileCompatible) => ({
183+
processor.Parser = (text: string, file: VFile) => ({
181184
type: 'random node'
182185
})
183186
processor.Parser = class CustomParser {
184-
parse(file: VFileCompatible) {
187+
constructor(text: string, file: VFile) {
188+
// nothing
189+
}
190+
191+
parse(): Node {
185192
return {
186193
type: 'random node'
187194
}
@@ -196,11 +203,15 @@ stringValue = processor.stringify(nodeValue)
196203
/**
197204
* processor.Compiler
198205
*/
199-
processor.Compiler = (node: Node, file?: VFileCompatible) => {
206+
processor.Compiler = (node: Node, file: VFile) => {
200207
return 'random string'
201208
}
202209
processor.Compiler = class CustomCompiler {
203-
compile(node: Node, file?: vfile.VFile) {
210+
constructor(node: Node, file: VFile) {
211+
// nothing
212+
}
213+
214+
compile() {
204215
return 'random string'
205216
}
206217
}

0 commit comments

Comments
 (0)