Convert src/compiler/compile/nodes/shared/map_children.ts to JavaScript

pull/8569/head
Simon Holthausen 3 years ago
parent 67b7e9d27f
commit a5b5ecc508

@ -1,96 +1,101 @@
import AwaitBlock from '../AwaitBlock'; import AwaitBlock from '../AwaitBlock.js';
import Body from '../Body'; import Body from '../Body.js';
import ConstTag from '../ConstTag'; import ConstTag from '../ConstTag.js';
import Comment from '../Comment'; import Comment from '../Comment.js';
import EachBlock from '../EachBlock'; import EachBlock from '../EachBlock.js';
import Document from '../Document'; import Document from '../Document.js';
import Element from '../Element'; import Element from '../Element.js';
import Head from '../Head'; import Head from '../Head.js';
import IfBlock from '../IfBlock'; import IfBlock from '../IfBlock.js';
import InlineComponent from '../InlineComponent'; import InlineComponent from '../InlineComponent.js';
import KeyBlock from '../KeyBlock'; import KeyBlock from '../KeyBlock.js';
import MustacheTag from '../MustacheTag'; import MustacheTag from '../MustacheTag.js';
import Options from '../Options'; import Options from '../Options.js';
import RawMustacheTag from '../RawMustacheTag'; import RawMustacheTag from '../RawMustacheTag.js';
import DebugTag from '../DebugTag'; import DebugTag from '../DebugTag.js';
import Slot from '../Slot'; import Slot from '../Slot.js';
import SlotTemplate from '../SlotTemplate'; import SlotTemplate from '../SlotTemplate.js';
import Text from '../Text'; import Text from '../Text.js';
import Title from '../Title'; import Title from '../Title.js';
import Window from '../Window'; import Window from '../Window.js';
import { TemplateNode } from '../../../interfaces'; import { push_array } from '../../../utils/push_array.js';
import { push_array } from '../../../utils/push_array';
export type Children = ReturnType<typeof map_children>;
/** @param {any} type */
function get_constructor(type) { function get_constructor(type) {
switch (type) { switch (type) {
case 'AwaitBlock': case 'AwaitBlock':
return AwaitBlock; return AwaitBlock;
case 'Body': case 'Body':
return Body; return Body;
case 'Comment': case 'Comment':
return Comment; return Comment;
case 'ConstTag': case 'ConstTag':
return ConstTag; return ConstTag;
case 'Document': case 'Document':
return Document; return Document;
case 'EachBlock': case 'EachBlock':
return EachBlock; return EachBlock;
case 'Element': case 'Element':
return Element; return Element;
case 'Head': case 'Head':
return Head; return Head;
case 'IfBlock': case 'IfBlock':
return IfBlock; return IfBlock;
case 'InlineComponent': case 'InlineComponent':
return InlineComponent; return InlineComponent;
case 'KeyBlock': case 'KeyBlock':
return KeyBlock; return KeyBlock;
case 'MustacheTag': case 'MustacheTag':
return MustacheTag; return MustacheTag;
case 'Options': case 'Options':
return Options; return Options;
case 'RawMustacheTag': case 'RawMustacheTag':
return RawMustacheTag; return RawMustacheTag;
case 'DebugTag': case 'DebugTag':
return DebugTag; return DebugTag;
case 'Slot': case 'Slot':
return Slot; return Slot;
case 'SlotTemplate': case 'SlotTemplate':
return SlotTemplate; return SlotTemplate;
case 'Text': case 'Text':
return Text; return Text;
case 'Title': case 'Title':
return Title; return Title;
case 'Window': case 'Window':
return Window; return Window;
default: default:
throw new Error(`Not implemented: ${type}`); throw new Error(`Not implemented: ${type}`);
} }
} }
export default function map_children(component, parent, scope, children: TemplateNode[]) { /**
let last = null; * @param {any} component
let ignores = []; * @param {any} parent
* @param {any} scope
return children.map((child) => { * @param {import('../../../interfaces.js').TemplateNode[]} children
const constructor = get_constructor(child.type); */
export default function map_children(component, parent, scope, children) {
const use_ignores = child.type !== 'Text' && child.type !== 'Comment' && ignores.length; let last = null;
let ignores = [];
if (use_ignores) component.push_ignores(ignores); return children.map(/** @param {any} child */ (child) => {
const node = new constructor(component, parent, scope, child); const constructor = get_constructor(child.type);
if (use_ignores) component.pop_ignores(), (ignores = []); const use_ignores = child.type !== 'Text' && child.type !== 'Comment' && ignores.length;
if (use_ignores)
component.push_ignores(ignores);
const node = new constructor(component, parent, scope, child);
if (use_ignores)
component.pop_ignores(), (ignores = []);
if (node.type === 'Comment' && node.ignores.length) {
push_array(ignores, node.ignores);
}
if (last)
last.next = node;
node.prev = last;
last = node;
return node;
});
}
if (node.type === 'Comment' && node.ignores.length) {
push_array(ignores, node.ignores);
}
if (last) last.next = node; /** @typedef {Class<map_children>>} Children */
node.prev = last;
last = node;
return node;
});
}

Loading…
Cancel
Save