11/**
22 * @import { TemplateOperations } from "../types.js"
3- * @import { Namespace } from "#compiler"
4- * @import { CallExpression, Statement, ObjectExpression, Identifier, ArrayExpression, Property, Expression, Literal } from "estree"
3+ * @import { ObjectExpression, Identifier, ArrayExpression, Property, Expression, Literal } from "estree"
54 */
6- import { NAMESPACE_SVG , NAMESPACE_MATHML } from '../../../../../constants.js' ;
75import * as b from '../../../../utils/builders.js' ;
86import { regex_is_valid_identifier } from '../../../patterns.js' ;
97import fix_attribute_casing from './fix-attribute-casing.js' ;
@@ -30,41 +28,42 @@ export function template_to_functions(items) {
3028 }
3129
3230 for ( let instruction of items ) {
33- // on push element we add the element to the stack, from this moment on every insert will
34- // happen on the last element in the stack
35- if ( instruction . kind === 'push_element' && last_current_element ) {
36- elements_stack . push ( last_current_element ) ;
37- continue ;
38- }
39- // we closed one element, we remove it from the stack and eventually revert back
40- // the namespace to the previous one
41- if ( instruction . kind === 'pop_element' ) {
42- elements_stack . pop ( ) ;
43- continue ;
44- }
45-
46- // @ts -expect-error we can't be here if `swap_current_element` but TS doesn't know that
47- const value = map [ instruction . kind ] (
48- ...[
49- ...( instruction . kind === 'create_element'
50- ? [ ]
51- : [ instruction . kind === 'set_prop' ? last_current_element : elements_stack . at ( - 1 ) ] ) ,
52- ...( instruction . args ?? [ ] )
53- ]
54- ) ;
55-
56- // with set_prop we don't need to do anything else, in all other cases we also need to
57- // append the element/node/anchor to the current active element or push it in the elements array
58- if ( instruction . kind !== 'set_prop' ) {
59- if ( elements_stack . length >= 1 && value !== undefined ) {
60- map . insert ( /** @type {Element } */ ( elements_stack . at ( - 1 ) ) , value ) ;
61- } else if ( value !== undefined ) {
31+ const args = instruction . args ?? [ ] ;
32+ const last_element_stack = /** @type {Element } */ ( elements_stack . at ( - 1 ) ) ;
33+ /**
34+ * @param {Expression | null | void } value
35+ * @returns
36+ */
37+ function push ( value ) {
38+ if ( value === undefined ) return ;
39+ if ( last_element_stack ) {
40+ insert ( last_element_stack , value ) ;
41+ } else {
6242 elements . elements . push ( value ) ;
6343 }
64- // keep track of the last created element (it will be pushed to the stack after the props are set)
65- if ( instruction . kind === 'create_element' ) {
66- last_current_element = /** @type {Element } */ ( value ) ;
67- }
44+ }
45+
46+ switch ( instruction . kind ) {
47+ case 'push_element' :
48+ elements_stack . push ( /** @type {Element } */ ( last_current_element ) ) ;
49+ break ;
50+ case 'pop_element' :
51+ elements_stack . pop ( ) ;
52+ last_current_element = elements_stack . at ( - 1 ) ;
53+ break ;
54+ case 'create_element' :
55+ last_current_element = create_element ( args [ 0 ] ) ;
56+ push ( last_current_element ) ;
57+ break ;
58+ case 'create_text' :
59+ push ( create_text ( last_element_stack , args [ 0 ] ) ) ;
60+ break ;
61+ case 'create_anchor' :
62+ push ( create_anchor ( last_element_stack , args [ 0 ] ) ) ;
63+ break ;
64+ case 'set_prop' :
65+ set_prop ( /** @type {Element } */ ( last_current_element ) , args [ 0 ] , args [ 1 ] ) ;
66+ break ;
6867 }
6968 }
7069
@@ -166,17 +165,9 @@ function set_prop(element, prop, value) {
166165/**
167166 *
168167 * @param {Element } element
169- * @param {Element } child
168+ * @param {Expression | null } child
170169 */
171170function insert ( element , child ) {
172171 const c = get_or_create_prop ( element , 'c' , b . array ( [ ] ) ) ;
173172 /** @type {ArrayExpression } */ ( c . value ) . elements . push ( child ) ;
174173}
175-
176- let map = {
177- create_element,
178- create_text,
179- create_anchor,
180- set_prop,
181- insert
182- } ;
0 commit comments