Lint and format src/compiler/compile/render_dom/wrappers/shared/add_actions.js

pull/8569/head
Simon Holthausen 3 years ago
parent 44529665f0
commit e5d02f9dd6

@ -7,7 +7,7 @@ import is_contextual from '../../../nodes/shared/is_contextual.js';
* @param {import('../../../nodes/Action.js').default[]} actions * @param {import('../../../nodes/Action.js').default[]} actions
*/ */
export default function add_actions(block, target, actions) { export default function add_actions(block, target, actions) {
actions.forEach((action) => add_action(block, target, action)); actions.forEach((action) => add_action(block, target, action));
} }
const regex_invalid_variable_identifier_characters = /[^a-zA-Z0-9_$]/g; const regex_invalid_variable_identifier_characters = /[^a-zA-Z0-9_$]/g;
@ -17,39 +17,40 @@ const regex_invalid_variable_identifier_characters = /[^a-zA-Z0-9_$]/g;
* @param {import('../../../nodes/Action.js').default} action * @param {import('../../../nodes/Action.js').default} action
*/ */
export function add_action(block, target, action) { export function add_action(block, target, action) {
const { expression, template_scope } = action; const { expression, template_scope } = action;
/** @type {import('estree').Node | undefined} */ /** @type {import('estree').Node | undefined} */
let snippet; let snippet;
/** @type {string[] | undefined} */ /** @type {string[] | undefined} */
let dependencies; let dependencies;
if (expression) { if (expression) {
snippet = expression.manipulate(block); snippet = expression.manipulate(block);
dependencies = expression.dynamic_dependencies(); dependencies = expression.dynamic_dependencies();
} }
const id = block.get_unique_name(`${action.name.replace(regex_invalid_variable_identifier_characters, '_')}_action`); const id = block.get_unique_name(
block.add_variable(id); `${action.name.replace(regex_invalid_variable_identifier_characters, '_')}_action`
const [obj, ...properties] = action.name.split('.'); );
const fn = is_contextual(action.component, template_scope, obj) block.add_variable(id);
? block.renderer.reference(obj) const [obj, ...properties] = action.name.split('.');
: obj; const fn = is_contextual(action.component, template_scope, obj)
if (properties.length) { ? block.renderer.reference(obj)
const member_expression = properties.reduce((lhs, rhs) => x `${lhs}.${rhs}`, fn); : obj;
block.event_listeners.push(x `@action_destroyer(${id} = ${member_expression}(${target}, ${snippet}))`); if (properties.length) {
} const member_expression = properties.reduce((lhs, rhs) => x`${lhs}.${rhs}`, fn);
else { block.event_listeners.push(
block.event_listeners.push(x `@action_destroyer(${id} = ${fn}.call(null, ${target}, ${snippet}))`); x`@action_destroyer(${id} = ${member_expression}(${target}, ${snippet}))`
} );
if (dependencies && dependencies.length > 0) { } else {
let condition = x `${id} && @is_function(${id}.update)`; block.event_listeners.push(
if (dependencies.length > 0) { x`@action_destroyer(${id} = ${fn}.call(null, ${target}, ${snippet}))`
condition = x `${condition} && ${block.renderer.dirty(dependencies)}`; );
} }
block.chunks.update.push(b `if (${condition}) ${id}.update.call(null, ${snippet});`); if (dependencies && dependencies.length > 0) {
} let condition = x`${id} && @is_function(${id}.update)`;
if (dependencies.length > 0) {
condition = x`${condition} && ${block.renderer.dirty(dependencies)}`;
}
block.chunks.update.push(b`if (${condition}) ${id}.update.call(null, ${snippet});`);
}
} }

Loading…
Cancel
Save