[chore] add missing types to `compiler/compile/render_dom` functions and variables (#7777)

* add missing types to `compiler/compile/render_dom` functions and variables

* add `DetachingOrNull`

* fis `is_head`
pull/7942/head
Hofer Ivan 2 years ago committed by GitHub
parent 0eba57113b
commit 739bfaec12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -168,7 +168,7 @@ export default class Renderer {
return member;
}
invalidate(name: string, value?, main_execution_context: boolean = false) {
invalidate(name: string, value?: unknown, main_execution_context: boolean = false) {
return renderer_invalidate(this, name, value, main_execution_context);
}

@ -83,7 +83,7 @@ export default function dom(
}
const uses_slots = component.var_lookup.has('$$slots');
let compute_slots;
let compute_slots: Node[] | undefined;
if (uses_slots) {
compute_slots = b`
const $$slots = @compute_slots(#slots);
@ -396,7 +396,7 @@ export default function dom(
if (has_definition) {
const reactive_declarations: (Node | Node[]) = [];
const fixed_reactive_declarations = []; // not really 'reactive' but whatever
const fixed_reactive_declarations: Node[] = []; // not really 'reactive' but whatever
component.reactive_declarations.forEach(d => {
const dependencies = Array.from(d.dependencies);
@ -441,7 +441,7 @@ export default function dom(
return b`let ${$name};`;
});
let unknown_props_check;
let unknown_props_check: Node[] | undefined;
if (component.compile_options.dev && !(uses_props || uses_rest)) {
unknown_props_check = b`
const writable_props = [${writable_props.map(prop => x`'${prop.export_name}'`)}];

@ -80,7 +80,7 @@ export function invalidate(renderer: Renderer, scope: Scope, node: Node, names:
return invalidate;
}
export function renderer_invalidate(renderer: Renderer, name: string, value?, main_execution_context: boolean = false) {
export function renderer_invalidate(renderer: Renderer, name: string, value?: unknown, main_execution_context: boolean = false) {
const variable = renderer.component.var_lookup.get(name);
if (variable && (variable.subscribable && (variable.reassigned || variable.export_name))) {

@ -582,7 +582,7 @@ export default class EachBlockWrapper extends Wrapper {
const start = this.block.has_update_method ? 0 : '#old_length';
let remove_old_blocks;
let remove_old_blocks: Node[];
if (this.block.has_outros) {
const out = block.get_unique_name('out');

@ -130,7 +130,7 @@ export default class AttributeWrapper extends BaseAttributeWrapper {
const dependencies = this.get_dependencies();
const value = this.get_value(block);
let updater;
let updater: Node[];
const init = this.get_init(block, value);
if (is_legacy_input_type) {
@ -258,7 +258,7 @@ export default class AttributeWrapper extends BaseAttributeWrapper {
return metadata;
}
get_value(block) {
get_value(block: Block) {
if (this.node.is_true) {
if (this.metadata && boolean_attributes.has(this.metadata.property_name.toLowerCase())) {
return x`true`;
@ -287,7 +287,7 @@ export default class AttributeWrapper extends BaseAttributeWrapper {
return value;
}
get_class_name_text(block) {
get_class_name_text(block: Block) {
const scoped_css = this.node.chunks.some((chunk: Text) => chunk.synthetic);
const rendered = this.render_chunks(block);

@ -299,8 +299,8 @@ function get_binding_group(renderer: Renderer, value: Binding, block: Block) {
for (const dep of contextual_dependencies) {
const context = block.bindings.get(dep);
let key;
let name;
let key: string;
let name: string;
if (context) {
key = context.object.name;
name = context.property.name;
@ -364,7 +364,7 @@ function get_event_handler(
const contextual_dependencies = new Set<string>(binding.node.expression.contextual_dependencies);
const context = block.bindings.get(name);
let set_store;
let set_store: Node[] | undefined;
if (context) {
const { object, property, store, snippet } = context;

@ -26,7 +26,7 @@ export default class EventHandlerWrapper {
}
}
get_snippet(block) {
get_snippet(block: Block) {
const snippet = this.node.expression ? this.node.expression.manipulate(block) : block.renderer.reference(this.node.handler_name);
if (this.node.reassigned) {

@ -6,7 +6,7 @@ svg_attributes.forEach(name => {
svg_attribute_lookup.set(name.toLowerCase(), name);
});
export default function fix_attribute_casing(name) {
export default function fix_attribute_casing(name: string) {
name = name.toLowerCase();
return svg_attribute_lookup.get(name) || name;
}

@ -19,7 +19,7 @@ import { add_event_handler } from '../shared/add_event_handlers';
import { add_action } from '../shared/add_actions';
import bind_this from '../shared/bind_this';
import { is_head } from '../shared/is_head';
import { Identifier, ExpressionStatement, CallExpression } from 'estree';
import { Identifier, ExpressionStatement, CallExpression, Node } from 'estree';
import EventHandler from './EventHandler';
import { extract_names } from 'periscopic';
import Action from '../../../nodes/Action';
@ -534,7 +534,7 @@ export default class ElementWrapper extends Wrapper {
.filter((attr) => !(attr instanceof SpreadAttributeWrapper) && !attr.property_name)
.map((attr) => p`${(attr as StyleAttributeWrapper | AttributeWrapper).name}: true`);
let reference;
let reference: string | ReturnType<typeof x>;
if (this.node.tag_expr.node.type === 'Literal') {
if (this.node.namespace) {
reference = `"${this.node.tag_expr.node.value}"`;
@ -634,7 +634,7 @@ export default class ElementWrapper extends Wrapper {
// media bindings — awkward special case. The native timeupdate events
// fire too infrequently, so we need to take matters into our
// own hands
let animation_frame;
let animation_frame: Identifier | undefined;
if (binding_group.events[0] === 'timeupdate') {
animation_frame = block.get_unique_name(`${this.var.name}_animationframe`);
block.add_variable(animation_frame);
@ -874,9 +874,7 @@ export default class ElementWrapper extends Wrapper {
}
}
add_transitions(
block: Block
) {
add_transitions(block: Block) {
const { intro, outro } = this.node;
if (!intro && !outro) return;
@ -933,7 +931,7 @@ export default class ElementWrapper extends Wrapper {
const fn = this.renderer.reference(intro.name);
let intro_block;
let intro_block: Node[];
if (outro) {
intro_block = b`
@ -1032,7 +1030,7 @@ export default class ElementWrapper extends Wrapper {
${outro && b`@add_transform(${this.var}, ${rect});`}
`);
let params;
let params: Node | ReturnType<typeof x>;
if (this.node.animation.expression) {
params = this.node.animation.expression.manipulate(block);
@ -1060,8 +1058,8 @@ export default class ElementWrapper extends Wrapper {
const has_spread = this.node.attributes.some(attr => attr.is_spread);
this.node.classes.forEach(class_directive => {
const { expression, name } = class_directive;
let snippet;
let dependencies;
let snippet: Node | string;
let dependencies: Set<string>;
if (expression) {
snippet = expression.manipulate(block);
dependencies = expression.dependencies;
@ -1105,7 +1103,7 @@ export default class ElementWrapper extends Wrapper {
const { name, expression, should_cache } = style_directive;
const snippet = expression.manipulate(block);
let cached_snippet;
let cached_snippet: Identifier | undefined;
if (should_cache) {
cached_snippet = block.get_unique_name(`style_${name.replace(regex_minus_signs, '_')}`);
block.add_variable(cached_snippet, snippet);
@ -1136,7 +1134,7 @@ export default class ElementWrapper extends Wrapper {
});
}
add_manual_style_scoping(block) {
add_manual_style_scoping(block: Block) {
if (this.node.needs_manual_style_scoping) {
const updater = b`@toggle_class(${this.var}, "${this.node.component.stylesheet.id}", true);`;
block.chunks.hydrate.push(updater);
@ -1151,7 +1149,7 @@ const regex_dollar_signs = /\$/g;
function to_html(wrappers: Array<ElementWrapper | TextWrapper | MustacheTagWrapper | RawMustacheTagWrapper>, block: Block, literal: any, state: any, can_use_raw_text?: boolean) {
wrappers.forEach(wrapper => {
if (wrapper instanceof TextWrapper) {
// Don't add the <pre>/<textare> newline logic here because pre/textarea.innerHTML
// Don't add the <pre>/<textarea> newline logic here because pre/textarea.innerHTML
// would keep the leading newline, too, only someParent.innerHTML = '..<pre/textarea>..' won't
if ((wrapper as TextWrapper).use_space()) state.quasi.value.raw += ' ';

@ -65,7 +65,7 @@ export default class FragmentWrapper {
this.nodes = [];
let last_child: Wrapper;
let window_wrapper;
let window_wrapper: Window | undefined;
let i = nodes.length;
while (i--) {

@ -33,7 +33,7 @@ export default class HeadWrapper extends Wrapper {
}
render(block: Block, _parent_node: Identifier, _parent_nodes: Identifier) {
let nodes;
let nodes: Identifier;
if (this.renderer.options.hydratable && this.fragment.nodes.length) {
nodes = block.get_unique_name('head_nodes');
block.chunks.claim.push(b`const ${nodes} = @head_selector('${this.node.id}', @_document.head);`);

@ -13,6 +13,8 @@ import { Identifier, Node } from 'estree';
import { push_array } from '../../../utils/push_array';
import { add_const_tags, add_const_tags_context } from './shared/add_const_tags';
type DetachingOrNull = 'detaching' | null;
function is_else_if(node: ElseBlock) {
return (
node && node.children.length === 1 && node.children[0].type === 'IfBlock'
@ -213,7 +215,7 @@ export default class IfBlockWrapper extends Wrapper {
const vars = { name, anchor, if_exists_condition, has_else, has_transitions };
const detaching = parent_node && !is_head(parent_node) ? null : 'detaching';
const detaching: DetachingOrNull = parent_node && !is_head(parent_node) ? null : 'detaching';
if (this.node.else) {
this.branches.forEach(branch => {
@ -275,9 +277,9 @@ export default class IfBlockWrapper extends Wrapper {
block: Block,
parent_node: Identifier,
_parent_nodes: Identifier,
dynamic,
dynamic: boolean,
{ name, anchor, has_else, if_exists_condition, has_transitions },
detaching
detaching: DetachingOrNull
) {
const select_block_type = this.renderer.component.get_unique_name('select_block_type');
const current_block_type = block.get_unique_name('current_block_type');
@ -414,9 +416,9 @@ export default class IfBlockWrapper extends Wrapper {
block: Block,
parent_node: Identifier,
_parent_nodes: Identifier,
dynamic,
dynamic: boolean,
{ name, anchor, has_else, has_transitions, if_exists_condition },
detaching
detaching: DetachingOrNull
) {
const select_block_type = this.renderer.component.get_unique_name('select_block_type');
const current_block_type_index = block.get_unique_name('current_block_type_index');
@ -428,8 +430,8 @@ export default class IfBlockWrapper extends Wrapper {
const if_ctx = select_block_ctx ? x`${select_block_ctx}(#ctx, ${current_block_type_index})` : x`#ctx`;
const if_current_block_type_index = has_else
? nodes => nodes
: nodes => b`if (~${current_block_type_index}) { ${nodes} }`;
? (nodes: Node[]) => nodes
: (nodes: Node[]) => b`if (~${current_block_type_index}) { ${nodes} }`;
block.add_variable(current_block_type_index);
block.add_variable(name);
@ -593,9 +595,9 @@ export default class IfBlockWrapper extends Wrapper {
block: Block,
parent_node: Identifier,
_parent_nodes: Identifier,
dynamic,
dynamic: boolean,
{ name, anchor, if_exists_condition, has_transitions },
detaching
detaching: DetachingOrNull
) {
const branch = this.branches[0];
const if_ctx = branch.get_ctx_name ? x`${branch.get_ctx_name}(#ctx)` : x`#ctx`;

@ -139,7 +139,7 @@ export default class InlineComponentWrapper extends Wrapper {
child.render(block, null, x`#nodes` as Identifier);
});
let props;
let props: Identifier | undefined;
const name_changes = block.get_unique_name(`${name.name}_changes`);
const uses_spread = !!this.node.attributes.find(a => a.is_spread);
@ -234,7 +234,7 @@ export default class InlineComponentWrapper extends Wrapper {
: null;
const unchanged = dependencies.size === 0;
let change_object;
let change_object: Node | ReturnType<typeof x>;
if (attr.is_spread) {
const value = attr.expression.manipulate(block);
initial_props.push(value);

@ -8,7 +8,7 @@ import Element from '../../nodes/Element';
import MustacheTag from '../../nodes/MustacheTag';
import RawMustacheTag from '../../nodes/RawMustacheTag';
import { is_head } from './shared/is_head';
import { Identifier } from 'estree';
import { Identifier, Node } from 'estree';
export default class RawMustacheTagWrapper extends Tag {
var: Identifier = { type: 'Identifier', name: 'raw' };
@ -30,7 +30,7 @@ export default class RawMustacheTagWrapper extends Tag {
const can_use_innerhtml = !in_head && parent_node && !this.prev && !this.next;
if (can_use_innerhtml) {
const insert = content => b`${parent_node}.innerHTML = ${content};`[0];
const insert = (content: Node) => b`${parent_node}.innerHTML = ${content};`[0];
const { init } = this.rename_this_method(
block,

@ -9,7 +9,7 @@ import add_to_set from '../../utils/add_to_set';
import get_slot_data from '../../utils/get_slot_data';
import { is_reserved_keyword } from '../../utils/reserved_keywords';
import is_dynamic from './shared/is_dynamic';
import { Identifier, ObjectExpression } from 'estree';
import { Identifier, ObjectExpression, Node } from 'estree';
import create_debugging_comment from './shared/create_debugging_comment';
export default class SlotWrapper extends Wrapper {
@ -75,9 +75,9 @@ export default class SlotWrapper extends Wrapper {
block = this.slot_block;
}
let get_slot_changes_fn;
let get_slot_spread_changes_fn;
let get_slot_context_fn;
let get_slot_changes_fn: Identifier | 'null';
let get_slot_spread_changes_fn: Identifier | undefined;
let get_slot_context_fn: Identifier | 'null';
if (this.node.values.size > 0) {
get_slot_changes_fn = renderer.component.get_unique_name(`get_${sanitize(slot_name)}_slot_changes`);
@ -176,7 +176,7 @@ export default class SlotWrapper extends Wrapper {
].filter(Boolean);
const all_dirty_condition = all_dirty_conditions.length ? all_dirty_conditions.reduce((condition1, condition2) => x`${condition1} || ${condition2}`) : null;
let slot_update;
let slot_update: Node[];
if (all_dirty_condition) {
const dirty = x`${all_dirty_condition} ? @get_all_dirty_from_scope(${renderer.reference('$$scope')}) : @get_slot_changes(${slot_definition}, ${renderer.reference('$$scope')}, #dirty, ${get_slot_changes_fn})`;

@ -45,7 +45,7 @@ export default class WindowWrapper extends Wrapper {
const { renderer } = this;
const { component } = renderer;
const events = {};
const events: Record<string, Array<{ name: string; value: string }>> = {};
const bindings: Record<string, string> = {};
add_actions(block, '@_window', this.node.actions);

@ -1,7 +1,7 @@
import { b, x } from 'code-red';
import Block from '../../Block';
import Action from '../../../nodes/Action';
import { Expression } from 'estree';
import { Expression, Node } from 'estree';
import is_contextual from '../../../nodes/shared/is_contextual';
export default function add_actions(
@ -16,8 +16,8 @@ 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;
let snippet;
let dependencies;
let snippet: Node | undefined;
let dependencies: string[] | undefined;
if (expression) {
snippet = expression.manipulate(block);

@ -15,7 +15,7 @@ export default function create_debugging_comment(
while (source[c - 1] === '{') c -= 1;
}
let d;
let d: number;
if (node.type === 'InlineComponent' || node.type === 'Element' || node.type === 'SlotTemplate') {
if (node.children.length) {

@ -2,7 +2,7 @@ import Let from '../../../nodes/Let';
import { x, p } from 'code-red';
import Block from '../../Block';
import TemplateScope from '../../../nodes/shared/TemplateScope';
import { BinaryExpression } from 'estree';
import { BinaryExpression, Identifier } from 'estree';
export function get_slot_definition(block: Block, scope: TemplateScope, lets: Let[]) {
if (lets.length === 0) return { block, scope };
@ -21,7 +21,7 @@ export function get_slot_definition(block: Block, scope: TemplateScope, lets: Le
const value_map = new Map();
lets.forEach(l => {
let value;
let value: Identifier;
if (l.names.length > 1) {
// more than one, probably destructuring
const unique_name = block.get_unique_name(l.names.join('_')).name;
@ -86,7 +86,7 @@ export function get_slot_definition(block: Block, scope: TemplateScope, lets: Le
elements[g] = grouped[g]
? grouped[g]
.map(({ name, n }) => x`${name} ? ${1 << n} : 0`)
.reduce((lhs, rhs) => x`${lhs} | ${rhs}`)
.reduce((lhs: ReturnType<typeof x>, rhs: ReturnType<typeof x>) => x`${lhs} | ${rhs}`)
: x`0`;
}

@ -1,3 +1,5 @@
export function is_head(node) {
return node && node.type === 'MemberExpression' && node.object.name === '@_document' && node.property.name === 'head';
import { Node } from 'estree';
export function is_head(node: Node) {
return node && node.type === 'MemberExpression' && node.object['name'] === '@_document' && node.property['name'] === 'head';
}

Loading…
Cancel
Save