Convert src/compiler/compile/render_dom/wrappers/shared/add_actions.ts to JavaScript

pull/8569/head
Simon Holthausen 3 years ago
parent a3e7f83790
commit c58b47a0d1

@ -14,10 +14,20 @@ import { is_head } from '../shared/is_head.js';
import compiler_warnings from '../../../compiler_warnings.js'; import compiler_warnings from '../../../compiler_warnings.js';
import { namespaces } from '../../../../utils/namespaces.js'; import { namespaces } from '../../../../utils/namespaces.js';
import { extract_ignores_above_node } from '../../../../utils/extract_svelte_ignore.js'; import { extract_ignores_above_node } from '../../../../utils/extract_svelte_ignore.js';
const regex_invalid_variable_identifier_characters = /[^a-zA-Z_$]/g; const regex_invalid_variable_identifier_characters = /[^a-zA-Z_$]/g;
/** @extends Wrapper */ /** @extends Wrapper */
export default class InlineComponentWrapper extends Wrapper { export default class InlineComponentWrapper extends Wrapper {
/**
* @typedef {{
* block: import('../../Block.js').default;
* scope: import('../../../nodes/shared/TemplateScope.js').default;
* get_context?: import('estree').Node;
* get_changes?: import('estree').Node;
* }} SlotDefinition
*/
/** @type {import('estree').Identifier} */ /** @type {import('estree').Identifier} */
var; var;
@ -133,10 +143,10 @@ export default class InlineComponentWrapper extends Wrapper {
block.add_variable(name); block.add_variable(name);
const component_opts = /** @type {import('estree').ObjectExpression} */ (x`{}`); const component_opts = /** @type {import('estree').ObjectExpression} */ (x`{}`);
/** @type {Array<Node | import('estree').Node[]>} */ /** @type {Array<import('estree').Node | import('estree').Node[]>} */
const statements = []; const statements = [];
/** @type {Array<Node | import('estree').Node[]>} */ /** @type {Array<import('estree').Node | import('estree').Node[]>} */
const updates = []; const updates = [];
this.children.forEach((child) => { this.children.forEach((child) => {
this.renderer.add_to_context('$$scope', true); this.renderer.add_to_context('$$scope', true);
@ -235,7 +245,7 @@ export default class InlineComponentWrapper extends Wrapper {
: null; : null;
const unchanged = dependencies.size === 0; const unchanged = dependencies.size === 0;
/** @type {Class<x>>} */ /** @type {import('estree').Node | ReturnType<typeof x>} */
let change_object; let change_object;
if (attr.is_spread) { if (attr.is_spread) {
const value = attr.expression.manipulate(block); const value = attr.expression.manipulate(block);
@ -650,12 +660,3 @@ export default class InlineComponentWrapper extends Wrapper {
}); });
} }
} }
/**
* @typedef {{
* block: Block;
* scope: TemplateScope;
* get_context?: Node;
* get_changes?: Node;
* }} SlotDefinition
*/

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