From 106f1b963296cfb1ffd78a1ead2b0bbc8df0a134 Mon Sep 17 00:00:00 2001 From: Nikita Kryukov Date: Tue, 20 Jul 2021 17:41:11 +0300 Subject: [PATCH] Fix internal circular dependency Closes GH-23. Reviewed-by: Titus Wormer --- lib/all.js | 24 ---------------- lib/index.js | 2 +- lib/one.js | 41 --------------------------- lib/{element.js => tree.js} | 56 ++++++++++++++++++++++++++++++++++++- 4 files changed, 56 insertions(+), 67 deletions(-) delete mode 100644 lib/all.js delete mode 100644 lib/one.js rename lib/{element.js => tree.js} (84%) diff --git a/lib/all.js b/lib/all.js deleted file mode 100644 index dc8e824..0000000 --- a/lib/all.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * @typedef {import('./types.js').Handle} Handle - * @typedef {import('./types.js').Parent} Parent - */ -import {one} from './one.js' - -/** - * Serialize all children of `parent`. - * - * @type {Handle} - * @param {Parent} parent - */ -export function all(ctx, parent) { - /** @type {Array.} */ - var results = [] - var children = (parent && parent.children) || [] - var index = -1 - - while (++index < children.length) { - results[index] = one(ctx, children[index], index, parent) - } - - return results.join('') -} diff --git a/lib/index.js b/lib/index.js index 4327218..7e9de6b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,7 +8,7 @@ import {html, svg} from 'property-information' import {htmlVoidElements} from 'html-void-elements' import {omission} from './omission/index.js' -import {one} from './one.js' +import {one} from './tree.js' /** * @param {Node|Array.} node diff --git a/lib/one.js b/lib/one.js deleted file mode 100644 index c3373fe..0000000 --- a/lib/one.js +++ /dev/null @@ -1,41 +0,0 @@ -/** - * @typedef {import('./types.js').Handle} Handle - */ - -import {comment} from './comment.js' -import {doctype} from './doctype.js' -import {element} from './element.js' -import {raw} from './raw.js' -import {all} from './all.js' -import {text} from './text.js' - -/** - * @type {Object.} - */ -var handlers = { - comment, - doctype, - element, - // @ts-ignore `raw` is nonstandard - raw, - // @ts-ignore `root` is a parent. - root: all, - text -} - -var own = {}.hasOwnProperty - -/** - * @type {Handle} - */ -export function one(ctx, node, index, parent) { - if (!node || !node.type) { - throw new Error('Expected node, not `' + node + '`') - } - - if (!own.call(handlers, node.type)) { - throw new Error('Cannot compile unknown node `' + node.type + '`') - } - - return handlers[node.type](ctx, node, index, parent) -} diff --git a/lib/element.js b/lib/tree.js similarity index 84% rename from lib/element.js rename to lib/tree.js index 1039014..d55596a 100644 --- a/lib/element.js +++ b/lib/tree.js @@ -4,6 +4,7 @@ * @typedef {import('./types.js').Context} Context * @typedef {import('./types.js').Properties} Properties * @typedef {import('./types.js').PropertyValue} PropertyValue + * @typedef {import('./types.js').Parent} Parent */ import {svg, find} from 'property-information' @@ -11,8 +12,61 @@ import {stringify as spaces} from 'space-separated-tokens' import {stringify as commas} from 'comma-separated-tokens' import {stringifyEntities} from 'stringify-entities' import {ccount} from 'ccount' -import {all} from './all.js' import {constants} from './constants.js' +import {comment} from './comment' +import {doctype} from './doctype' +import {raw} from './raw' +import {text} from './text' + +/** + * @type {Object.} + */ +var handlers = { + comment, + doctype, + element, + // @ts-ignore `raw` is nonstandard + raw, + // @ts-ignore `root` is a parent. + root: all, + text +} + +var own = {}.hasOwnProperty + +/** + * @type {Handle} + */ +export function one(ctx, node, index, parent) { + if (!node || !node.type) { + throw new Error('Expected node, not `' + node + '`') + } + + if (!own.call(handlers, node.type)) { + throw new Error('Cannot compile unknown node `' + node.type + '`') + } + + return handlers[node.type](ctx, node, index, parent) +} + +/** + * Serialize all children of `parent`. + * + * @type {Handle} + * @param {Parent} parent + */ +export function all(ctx, parent) { + /** @type {Array.} */ + var results = [] + var children = (parent && parent.children) || [] + var index = -1 + + while (++index < children.length) { + results[index] = one(ctx, children[index], index, parent) + } + + return results.join('') +} /** * @type {Handle}