Skip to content

Commit

Permalink
Add AbstractConvert & ConverterConstructor
Browse files Browse the repository at this point in the history
Please note: this changes opts of the convert method as well. It's now
'unknown', so consumer are able to redeclare the type for their need.
  • Loading branch information
bitPogo authored and ggrossetie committed Oct 31, 2020
1 parent fac8480 commit 8c2a2d4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
25 changes: 22 additions & 3 deletions packages/core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2980,7 +2980,26 @@ export namespace Asciidoctor {
function create(): Html5Converter;
}

class Converter {
interface AbstractConverter {
/**
* Converts an {AbstractNode} using the given transform.
* This method must be implemented by a concrete converter class.
*
* @param node - The concrete instance of AbstractNode to convert.
* @param [transform] - An optional String transform that hints at which transformation should be applied to this node.
* If a transform is not given, the transform is often derived from the value of the {AbstractNode#getNodeName} property. (optional, default: undefined)
* @param [opts]- An optional JSON of options hints about how to convert the node. (optional, default: undefined)
*
* @returns the {String} result.
*/
convert(node: AbstractNode, transform?: string, opts?: unknown): string;
}

interface ConverterConstructor {
new(backend?: string, opts?: unknown): AbstractConverter;
}

class Converter implements AbstractConverter {
/**
* Converts an {AbstractNode} using the given transform.
* This method must be implemented by a concrete converter class.
Expand Down Expand Up @@ -3009,7 +3028,7 @@ export namespace Asciidoctor {
* @param opts - a JSON of options to pass to the converter (default: {})
* @returns a {Converter} instance for converting nodes in an Asciidoctor AST.
*/
function create(backend: string, opts?: any): Converter;
function create(backend: string, opts?: unknown): Converter;
}

/**
Expand All @@ -3027,7 +3046,7 @@ export namespace Asciidoctor {
* @param converter - The {Converter} instance to register
* @param backends- A {string} {Array} of backend names that this converter should be registered to handle (optional, default: ['*'])
*/
register(converter: any, backends?: string[]): void;
register(converter: AbstractConverter | ConverterConstructor, backends?: string[]): void;

/**
* Retrieves the singleton instance of the converter factory.
Expand Down
13 changes: 11 additions & 2 deletions packages/core/types/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ interface Transforms {
[key: string]: (node: Asciidoctor.AbstractNode) => string;
}

class BlogConverter {
class BlogConverter implements Asciidoctor.AbstractConverter {
private readonly baseConverter: Asciidoctor.Html5Converter;
private readonly transforms: Transforms;

Expand Down Expand Up @@ -1076,12 +1076,21 @@ assert(blockSubs5[0] === 'specialcharacters');
let converterRegistry = processor.ConverterFactory.getRegistry();
assert(typeof converterRegistry.html5 === 'function');

class BlankConverter {
class BlankConverter implements Asciidoctor.AbstractConverter {
convert() {
return '';
}
}

const BlankConstructor = BlankConverter as Asciidoctor.ConverterConstructor;
processor.ConverterFactory.register(BlankConstructor, ['blank']);
converterRegistry = processor.ConverterFactory.getRegistry();
assert(typeof converterRegistry.html5 === 'function');
assert(typeof converterRegistry.blank === 'function');
assert(typeof processor.ConverterFactory.for('html5') === 'function');
assert(typeof processor.ConverterFactory.for('blank') === 'function');
assert(typeof processor.ConverterFactory.for('foo') === 'undefined');

processor.ConverterFactory.register(new BlankConverter(), ['blank']);
converterRegistry = processor.ConverterFactory.getRegistry();
assert(typeof converterRegistry.html5 === 'function');
Expand Down

0 comments on commit 8c2a2d4

Please sign in to comment.