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

pull/8569/head
Simon Holthausen 3 years ago
parent a72601d939
commit a8a7cc6d50

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

Loading…
Cancel
Save