diff --git a/packages/bbob-preset/src/preset.ts b/packages/bbob-preset/src/preset.ts index 7e9d3dbe..13f7f883 100644 --- a/packages/bbob-preset/src/preset.ts +++ b/packages/bbob-preset/src/preset.ts @@ -1,87 +1,64 @@ import type { BBobCoreTagNodeTree, - BbobPluginOptions, - BBobPluginFunction, + BBobPluginOptions, } from "@bbob/core"; import { isTagNode } from "@bbob/plugin-helper"; import type { + PresetFactory, PresetExtendCallback, - PresetFactoryOptions, PresetOptions, PresetTagsDefinition, + ProcessorFunction, } from "./types"; -function process( - tags: PresetTagsDefinition, +export function process( + tags: Tags, tree: BBobCoreTagNodeTree, - core: BbobPluginOptions, - options: PresetFactoryOptions = {} + core: BBobPluginOptions, + options: Options ) { return tree.walk((node) => { - if (isTagNode(node) && typeof tags[node.tag] === "function") { + if (isTagNode(node)) { const tag = node.tag; const tagCallback = tags[tag]; - return tagCallback(node, core, options); + if (typeof tagCallback === "function") { + return tagCallback(node, core, options); + } } return node; }); } -export type ProcessorFunction = typeof process; - -export type ProcessorReturnType = ReturnType; - -export interface PresetExecutor< - TagName extends string = string, - AttrValue = unknown -> extends BBobPluginFunction { - (tree: BBobCoreTagNodeTree, core?: BbobPluginOptions): ProcessorReturnType; - options: PresetOptions; -} - -export interface PresetFactory< - TagName extends string = string, - AttrValue = unknown, - Names extends string = string -> { - (opts?: PresetOptions): PresetExecutor; - options?: PresetOptions; - extend: ( - cb: PresetExtendCallback - ) => PresetFactory; -} - /** * Create a preset plugin for @bbob/core */ -function createPreset( - defTags: PresetTagsDefinition, - processor: ProcessorFunction = process +function createPreset( + defTags: Tags, + processor: ProcessorFunction = process ) { - const presetFactory: PresetFactory = (opts: PresetOptions = {}) => { + const presetFactory: PresetFactory = (opts: Options) => { presetFactory.options = Object.assign(presetFactory.options || {}, opts); function presetExecutor( tree: BBobCoreTagNodeTree, - core: BbobPluginOptions + core: BBobPluginOptions ) { - return processor(defTags, tree, core, presetFactory.options); + return processor(defTags, tree, core, presetFactory.options || {}); } - presetExecutor.options = presetFactory.options; + presetExecutor.options = presetFactory.options as Options; return presetExecutor; }; - presetFactory.extend = function presetExtend( - callback: PresetExtendCallback + presetFactory.extend = function presetExtend( + callback: PresetExtendCallback ) { - return createPreset( - callback(defTags, presetFactory.options || {}), - processor - ); + const newTags = callback(defTags, presetFactory.options) + + return createPreset(newTags, processor as unknown as ProcessorFunction); }; return presetFactory; diff --git a/packages/bbob-preset/src/types.ts b/packages/bbob-preset/src/types.ts index 50e93a3b..ee123723 100644 --- a/packages/bbob-preset/src/types.ts +++ b/packages/bbob-preset/src/types.ts @@ -1,15 +1,41 @@ import type { TagNodeObject } from "@bbob/plugin-helper"; -import type { BbobPluginOptions } from "@bbob/core"; +import type { BBobCoreTagNodeTree, BBobPluginFunction, BBobPluginOptions } from "@bbob/core"; +import type { process } from "./preset"; -export type PresetFactoryOptions = Record +type PartialRecord = Partial> -export type PresetTagFunction = ( - node: Node, - core: BbobPluginOptions, - options: PresetFactoryOptions -) => Node - -export type PresetTagsDefinition = Record +export type PresetTagsDefinition = PartialRecord export type PresetOptions = Record -export type PresetExtendCallback = (defTags: PresetTagsDefinition, options: PresetOptions) => PresetTagsDefinition + +export type ProcessorFunction = typeof process; + +export type ProcessorReturnType = ReturnType>; + +export interface PresetTagFunction { + ( + node: Node, + data: BBobPluginOptions, + options: Options + ): Node +} + +export interface PresetExtendCallback { + (defTags: Tags, options?: Options): NewTags +} + +export interface PresetExecutor extends BBobPluginFunction { + (tree: BBobCoreTagNodeTree, core?: BBobPluginOptions): ProcessorReturnType; + options: Options; +} + +export interface PresetFactory< + Tags extends PresetTagsDefinition = PresetTagsDefinition, + RootOptions extends PresetOptions = PresetOptions, +> { + (opts: Options): PresetExecutor; + options?: RootOptions; + extend: ( + cb: PresetExtendCallback + ) => PresetFactory; +} diff --git a/packages/bbob-preset/test/index.test.ts b/packages/bbob-preset/test/index.test.ts index 6c724086..da00718e 100644 --- a/packages/bbob-preset/test/index.test.ts +++ b/packages/bbob-preset/test/index.test.ts @@ -2,21 +2,22 @@ import { createPreset, PresetTagsDefinition } from '../src'; import { BBobCoreOptions, createTree } from '@bbob/core' describe('@bbob/preset', () => { - const presetFactory = (defTags: PresetTagsDefinition) => { + const presetFactory = (defTags: Tags) => { const processor = jest.fn((tags, tree, core, options) => tags) + const preset = createPreset(defTags, processor) return { - preset: createPreset(defTags, processor), + preset, processor, core: {} as BBobCoreOptions } } test('create', () => { - const defTags = { test: () => 'test' } + const defTags = { test: () => ({ tag: 'test' }) } const options = { foo: 'bar' } const tree = createTree([], {}) - const { preset, processor, core } = presetFactory(defTags); + const { preset, processor } = presetFactory(defTags); expect(preset.extend) .toBeDefined();