From 7922b63a3d0108f58d7238b17cea37e8208ab4db Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Fri, 12 May 2023 14:58:40 +0200 Subject: [PATCH] Convert src/compiler/compile/render_dom/wrappers/shared/add_const_tags.ts to JavaScript --- .../render_dom/wrappers/shared/add_actions.js | 1 + .../wrappers/shared/add_const_tags.ts | 85 ++++++++++--------- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/src/compiler/compile/render_dom/wrappers/shared/add_actions.js b/src/compiler/compile/render_dom/wrappers/shared/add_actions.js index 2d2c0fce4f..a089e9284d 100644 --- a/src/compiler/compile/render_dom/wrappers/shared/add_actions.js +++ b/src/compiler/compile/render_dom/wrappers/shared/add_actions.js @@ -9,6 +9,7 @@ import is_contextual from '../../../nodes/shared/is_contextual.js'; export default function add_actions(block, target, actions) { actions.forEach((action) => add_action(block, target, action)); } + const regex_invalid_variable_identifier_characters = /[^a-zA-Z0-9_$]/g; /** diff --git a/src/compiler/compile/render_dom/wrappers/shared/add_const_tags.ts b/src/compiler/compile/render_dom/wrappers/shared/add_const_tags.ts index 5599457f85..446900f016 100644 --- a/src/compiler/compile/render_dom/wrappers/shared/add_const_tags.ts +++ b/src/compiler/compile/render_dom/wrappers/shared/add_const_tags.ts @@ -1,47 +1,48 @@ -import ConstTag from '../../../nodes/ConstTag'; -import Block from '../../Block'; -import { b, Node, x } from 'code-red'; -import Renderer from '../../Renderer'; -import Expression from '../../../nodes/shared/Expression'; +import { b, x } from 'code-red'; +import Expression from '../../../nodes/shared/Expression.js'; -export function add_const_tags(block: Block, const_tags: ConstTag[], ctx: string) { - const const_tags_props = []; - const_tags.forEach((const_tag, i) => { - const name = `#constants_${i}`; - const_tags_props.push(b`const ${name} = ${const_tag.expression.manipulate(block, ctx)}`); - const to_ctx = (name: string) => - block.renderer.context_lookup.has(name) - ? x`${ctx}[${block.renderer.context_lookup.get(name).index}]` - : ({ type: 'Identifier', name } as Node); +/** + * @param {import('../../Block.js').default} block + * @param {import('../../../nodes/ConstTag.js').default[]} const_tags + * @param {string} ctx + */ +export function add_const_tags(block, const_tags, ctx) { + const const_tags_props = []; + const_tags.forEach((const_tag, i) => { + const name = `#constants_${i}`; + const_tags_props.push(b `const ${name} = ${const_tag.expression.manipulate(block, ctx)}`); - const_tag.contexts.forEach((context) => { - if (context.type === 'DestructuredVariable') { - const_tags_props.push( - b`${ctx}[${ - block.renderer.context_lookup.get(context.key.name).index - }] = ${context.default_modifier(context.modifier({ type: 'Identifier', name }), to_ctx)}` - ); - } else { - const expression = new Expression( - block.renderer.component, - const_tag, - const_tag.scope, - context.key - ); - const_tags_props.push( - b`const ${context.property_name} = ${expression.manipulate(block, ctx)}` - ); - } - }); - }); - return const_tags_props; + /** @param {string} name */ + const to_ctx = (name) => block.renderer.context_lookup.has(name) + ? x `${ctx}[${block.renderer.context_lookup.get(name).index}]` + : ( /** @type {import('code-red').Node} */({ type: 'Identifier', name })); + const_tag.contexts.forEach((context) => { + if (context.type === 'DestructuredVariable') { + const_tags_props.push(b `${ctx}[${block.renderer.context_lookup.get(context.key.name).index}] = ${context.default_modifier(context.modifier({ type: 'Identifier', name }), to_ctx)}`); + } + else { + const expression = new Expression(block.renderer.component, const_tag, const_tag.scope, context.key); + const_tags_props.push(b `const ${context.property_name} = ${expression.manipulate(block, ctx)}`); + } + }); + }); + return const_tags_props; } -export function add_const_tags_context(renderer: Renderer, const_tags: ConstTag[]) { - const_tags.forEach((const_tag) => { - const_tag.contexts.forEach((context) => { - if (context.type !== 'DestructuredVariable') return; - renderer.add_to_context(context.key.name, true); - }); - }); +/** + * @param {import('../../Renderer.js').default} renderer + * @param {import('../../../nodes/ConstTag.js').default[]} const_tags + */ +export function add_const_tags_context(renderer, const_tags) { + const_tags.forEach((const_tag) => { + const_tag.contexts.forEach((context) => { + if (context.type !== 'DestructuredVariable') + return; + renderer.add_to_context(context.key.name, true); + }); + }); } + + + +