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

pull/8569/head
Simon Holthausen 3 years ago
parent 766165d210
commit 6817b87417

@ -1,119 +1,112 @@
import Let from '../../../nodes/Let';
import { x, p } from 'code-red'; import { x, p } from 'code-red';
import Block from '../../Block';
import TemplateScope from '../../../nodes/shared/TemplateScope';
import { BinaryExpression, Identifier } from 'estree';
export function get_slot_definition(block: Block, scope: TemplateScope, lets: Let[]) { /**
if (lets.length === 0) return { block, scope }; * @param {import('../../Block.js').default} block
* @param {import('../../../nodes/shared/TemplateScope.js').default} scope
const context_input = { * @param {import('../../../nodes/Let.js').default[]} lets
type: 'ObjectPattern', */
properties: lets.map((l) => ({ export function get_slot_definition(block, scope, lets) {
type: 'Property', if (lets.length === 0)
kind: 'init', return { block, scope };
key: l.name, const context_input = {
value: l.value || l.name type: 'ObjectPattern',
})) properties: lets.map((l) => ({
}; type: 'Property',
kind: 'init',
const properties = []; key: l.name,
const value_map = new Map(); value: l.value || l.name
}))
lets.forEach((l) => { };
let value: Identifier; const properties = [];
if (l.names.length > 1) { const value_map = new Map();
// more than one, probably destructuring lets.forEach((l) => {
const unique_name = block.get_unique_name(l.names.join('_')).name;
value_map.set(l.value, unique_name); /** @type {import('estree').Identifier} */
value = { type: 'Identifier', name: unique_name }; let value;
} else { if (l.names.length > 1) {
value = l.value || l.name; // more than one, probably destructuring
} const unique_name = block.get_unique_name(l.names.join('_')).name;
properties.push({ value_map.set(l.value, unique_name);
type: 'Property', value = { type: 'Identifier', name: unique_name };
kind: 'init', }
key: l.name, else {
value value = l.value || l.name;
}); }
}); properties.push({
type: 'Property',
const changes_input = { kind: 'init',
type: 'ObjectPattern', key: l.name,
properties value
}; });
});
const names: Set<string> = new Set(); const changes_input = {
const names_lookup: Map<string, string> = new Map(); type: 'ObjectPattern',
properties
lets.forEach((l) => { };
l.names.forEach((name) => {
names.add(name); /** @type {Set<string>} */
if (value_map.has(l.value)) { const names = new Set();
names_lookup.set(name, value_map.get(l.value));
} /** @type {Map<string, string>} */
}); const names_lookup = new Map();
}); lets.forEach((l) => {
l.names.forEach((name) => {
const context = { names.add(name);
type: 'ObjectExpression', if (value_map.has(l.value)) {
properties: Array.from(names).map( names_lookup.set(name, value_map.get(l.value));
(name) => p`${block.renderer.context_lookup.get(name).index}: ${name}` }
) });
}; });
const context = {
const { context_lookup } = block.renderer; type: 'ObjectExpression',
properties: Array.from(names).map((name) => p `${block.renderer.context_lookup.get(name).index}: ${name}`)
// i am well aware that this code is gross };
// TODO: context-overflow make it less gross const { context_lookup } = block.renderer;
const changes = { // i am well aware that this code is gross
type: 'ParenthesizedExpression', // TODO: context-overflow make it less gross
get expression() { const changes = {
if (block.renderer.context_overflow) { type: 'ParenthesizedExpression',
const grouped = []; get expression() {
if (block.renderer.context_overflow) {
Array.from(names).forEach((name) => { const grouped = [];
const i = context_lookup.get(name).index.value as number; Array.from(names).forEach((name) => {
const g = Math.floor(i / 31); const i = /** @type {number} */ (context_lookup.get(name).index.value);
const g = Math.floor(i / 31);
const lookup_name = names_lookup.has(name) ? names_lookup.get(name) : name; const lookup_name = names_lookup.has(name) ? names_lookup.get(name) : name;
if (!grouped[g])
if (!grouped[g]) grouped[g] = []; grouped[g] = [];
grouped[g].push({ name: lookup_name, n: i % 31 }); grouped[g].push({ name: lookup_name, n: i % 31 });
}); });
const elements = [];
const elements = []; for (let g = 0; g < grouped.length; g += 1) {
elements[g] = grouped[g]
? grouped[g]
.map(({ name, n }) => x `${name} ? ${1 << n} : 0`)
.reduce((lhs, rhs) => x `${lhs} | ${rhs}`)
: x `0`;
}
return {
type: 'ArrayExpression',
elements
};
}
return /** @type {import('estree').BinaryExpression} */ (Array.from(names)
.map((name) => {
const lookup_name = names_lookup.has(name) ? names_lookup.get(name) : name;
const i = /** @type {number} */ (context_lookup.get(name).index.value);
return x `${lookup_name} ? ${1 << i} : 0`;
})
.reduce((lhs, rhs) => x `${lhs} | ${rhs}`));
}
};
return {
block,
scope,
get_context: x `${context_input} => ${context}`,
get_changes: x `${changes_input} => ${changes}`
};
}
for (let g = 0; g < grouped.length; g += 1) {
elements[g] = grouped[g]
? grouped[g]
.map(({ name, n }) => x`${name} ? ${1 << n} : 0`)
.reduce(
(lhs: ReturnType<typeof x>, rhs: ReturnType<typeof x>) => x`${lhs} | ${rhs}`
)
: x`0`;
}
return {
type: 'ArrayExpression',
elements
};
}
return Array.from(names)
.map((name) => {
const lookup_name = names_lookup.has(name) ? names_lookup.get(name) : name;
const i = context_lookup.get(name).index.value as number;
return x`${lookup_name} ? ${1 << i} : 0`;
})
.reduce((lhs, rhs) => x`${lhs} | ${rhs}`) as BinaryExpression;
}
};
return {
block,
scope,
get_context: x`${context_input} => ${context}`,
get_changes: x`${changes_input} => ${changes}`
};
}

Loading…
Cancel
Save