Lint and format src/runtime/internal/Component.js

pull/8569/head
S. Elliott Johnson 3 years ago
parent 78da97493b
commit 2c591835fe

@ -1,7 +1,20 @@
import { add_render_callback, flush, flush_render_callbacks, schedule_update, dirty_components } from './scheduler'; import {
add_render_callback,
flush,
flush_render_callbacks,
schedule_update,
dirty_components
} from './scheduler';
import { current_component, set_current_component } from './lifecycle'; import { current_component, set_current_component } from './lifecycle';
import { blank_object, is_empty, is_function, run, run_all, noop } from './utils'; import { blank_object, is_empty, is_function, run, run_all, noop } from './utils';
import { children, detach, start_hydrating, end_hydrating, get_custom_elements_slots, insert } from './dom'; import {
children,
detach,
start_hydrating,
end_hydrating,
get_custom_elements_slots,
insert
} from './dom';
import { transition_in } from './transitions'; import { transition_in } from './transitions';
/** @returns {void} */ /** @returns {void} */
export function bind(component, name, callback) { export function bind(component, name, callback) {
@ -31,8 +44,7 @@ export function mount_component(component, target, anchor) {
// the destructured on_destroy may still reference to the old array // the destructured on_destroy may still reference to the old array
if (component.$$.on_destroy) { if (component.$$.on_destroy) {
component.$$.on_destroy.push(...new_on_destroy); component.$$.on_destroy.push(...new_on_destroy);
} } else {
else {
// Edge case - component was destroyed immediately, // Edge case - component was destroyed immediately,
// most likely as a result of a binding initialising // most likely as a result of a binding initialising
run_all(new_on_destroy); run_all(new_on_destroy);
@ -64,7 +76,16 @@ function make_dirty(component, i) {
component.$$.dirty[(i / 31) | 0] |= 1 << i % 31; component.$$.dirty[(i / 31) | 0] |= 1 << i % 31;
} }
/** @returns {void} */ /** @returns {void} */
export function init(component, options, instance, create_fragment, not_equal, props, append_styles, dirty = [-1]) { export function init(
component,
options,
instance,
create_fragment,
not_equal,
props,
append_styles,
dirty = [-1]
) {
const parent_component = current_component; const parent_component = current_component;
set_current_component(component); set_current_component(component);
/** @type {T$$} */ /** @type {T$$} */
@ -95,10 +116,8 @@ export function init(component, options, instance, create_fragment, not_equal, p
? instance(component, options.props || {}, (i, ret, ...rest) => { ? instance(component, options.props || {}, (i, ret, ...rest) => {
const value = rest.length ? rest[0] : ret; const value = rest.length ? rest[0] : ret;
if ($$.ctx && not_equal($$.ctx[i], ($$.ctx[i] = value))) { if ($$.ctx && not_equal($$.ctx[i], ($$.ctx[i] = value))) {
if (!$$.skip_bound && $$.bound[i]) if (!$$.skip_bound && $$.bound[i]) $$.bound[i](value);
$$.bound[i](value); if (ready) make_dirty(component, i);
if (ready)
make_dirty(component, i);
} }
return ret; return ret;
}) })
@ -115,13 +134,11 @@ export function init(component, options, instance, create_fragment, not_equal, p
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
$$.fragment && $$.fragment.l(nodes); $$.fragment && $$.fragment.l(nodes);
nodes.forEach(detach); nodes.forEach(detach);
} } else {
else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
$$.fragment && $$.fragment.c(); $$.fragment && $$.fragment.c();
} }
if (options.intro) if (options.intro) transition_in(component.$$.fragment);
transition_in(component.$$.fragment);
mount_component(component, options.target, options.anchor); mount_component(component, options.target, options.anchor);
end_hydrating(); end_hydrating();
flush(); flush();
@ -211,7 +228,12 @@ if (typeof HTMLElement === 'function') {
// this.$$data takes precedence over this.attributes // this.$$data takes precedence over this.attributes
const name = this.$$get_prop_name(attribute.name); const name = this.$$get_prop_name(attribute.name);
if (!(name in this.$$data)) { if (!(name in this.$$data)) {
this.$$data[name] = get_custom_element_value(name, attribute.value, this.$$props_definition, 'toProp'); this.$$data[name] = get_custom_element_value(
name,
attribute.value,
this.$$props_definition,
'toProp'
);
} }
} }
this.$$component = new this.$$componentCtor({ this.$$component = new this.$$componentCtor({
@ -236,10 +258,14 @@ if (typeof HTMLElement === 'function') {
// We don't need this when working within Svelte code, but for compatibility of people using this outside of Svelte // We don't need this when working within Svelte code, but for compatibility of people using this outside of Svelte
// and setting attributes through setAttribute etc, this is helpful // and setting attributes through setAttribute etc, this is helpful
attributeChangedCallback(attr, _oldValue, newValue) { attributeChangedCallback(attr, _oldValue, newValue) {
if (this.$$reflecting) if (this.$$reflecting) return;
return;
attr = this.$$get_prop_name(attr); attr = this.$$get_prop_name(attr);
this.$$data[attr] = get_custom_element_value(attr, newValue, this.$$props_definition, 'toProp'); this.$$data[attr] = get_custom_element_value(
attr,
newValue,
this.$$props_definition,
'toProp'
);
this.$$component.$set({ [attr]: this.$$data[attr] }); this.$$component.$set({ [attr]: this.$$data[attr] });
} }
disconnectedCallback() { disconnectedCallback() {
@ -253,8 +279,13 @@ if (typeof HTMLElement === 'function') {
}); });
} }
$$get_prop_name(attribute_name) { $$get_prop_name(attribute_name) {
return (Object.keys(this.$$props_definition).find((key) => this.$$props_definition[key].attribute === attribute_name || return (
(!this.$$props_definition[key].attribute && key.toLowerCase() === attribute_name)) || attribute_name); Object.keys(this.$$props_definition).find(
(key) =>
this.$$props_definition[key].attribute === attribute_name ||
(!this.$$props_definition[key].attribute && key.toLowerCase() === attribute_name)
) || attribute_name
);
} }
}; };
} }
@ -269,8 +300,7 @@ function get_custom_element_value(prop, value, props_definition, transform) {
value = type === 'Boolean' && typeof value !== 'boolean' ? value != null : value; value = type === 'Boolean' && typeof value !== 'boolean' ? value != null : value;
if (!transform || !props_definition[prop]) { if (!transform || !props_definition[prop]) {
return value; return value;
} } else if (transform === 'toAttribute') {
else if (transform === 'toAttribute') {
switch (type) { switch (type) {
case 'Object': case 'Object':
case 'Array': case 'Array':
@ -282,8 +312,7 @@ function get_custom_element_value(prop, value, props_definition, transform) {
default: default:
return value; return value;
} }
} } else {
else {
switch (type) { switch (type) {
case 'Object': case 'Object':
case 'Array': case 'Array':
@ -308,14 +337,22 @@ function get_custom_element_value(prop, value, props_definition, transform) {
* @param {boolean} use_shadow_dom Whether to use shadow DOM * @param {boolean} use_shadow_dom Whether to use shadow DOM
* @returns {Class<Class>} A custom element class * @returns {Class<Class>} A custom element class
*/ */
export function create_custom_element(Component, props_definition, slots, accessors, use_shadow_dom) { export function create_custom_element(
Component,
props_definition,
slots,
accessors,
use_shadow_dom
) {
const Class = class extends SvelteElement { const Class = class extends SvelteElement {
constructor() { constructor() {
super(Component, slots, use_shadow_dom); super(Component, slots, use_shadow_dom);
this.$$props_definition = props_definition; this.$$props_definition = props_definition;
} }
static get observedAttributes() { static get observedAttributes() {
return Object.keys(props_definition).map((key) => (props_definition[key].attribute || key).toLowerCase()); return Object.keys(props_definition).map((key) =>
(props_definition[key].attribute || key).toLowerCase()
);
} }
}; };
Object.keys(props_definition).forEach((prop) => { Object.keys(props_definition).forEach((prop) => {
@ -331,11 +368,15 @@ export function create_custom_element(Component, props_definition, slots, access
this.$$component?.$set({ [prop]: value }); this.$$component?.$set({ [prop]: value });
if (props_definition[prop].reflect) { if (props_definition[prop].reflect) {
this.$$reflecting = true; this.$$reflecting = true;
const attribute_value = get_custom_element_value(prop, value, props_definition, 'toAttribute'); const attribute_value = get_custom_element_value(
prop,
value,
props_definition,
'toAttribute'
);
if (attribute_value == null) { if (attribute_value == null) {
this.removeAttribute(prop); this.removeAttribute(prop);
} } else {
else {
this.setAttribute(props_definition[prop].attribute || prop, attribute_value); this.setAttribute(props_definition[prop].attribute || prop, attribute_value);
} }
this.$$reflecting = false; this.$$reflecting = false;
@ -375,8 +416,7 @@ export class SvelteComponent {
callbacks.push(callback); callbacks.push(callback);
return () => { return () => {
const index = callbacks.indexOf(callback); const index = callbacks.indexOf(callback);
if (index !== -1) if (index !== -1) callbacks.splice(index, 1);
callbacks.splice(index, 1);
}; };
} }
/** @returns {void} */ /** @returns {void} */
@ -389,9 +429,6 @@ export class SvelteComponent {
} }
} }
/** @typedef {Object} CustomElementPropDefinition /** @typedef {Object} CustomElementPropDefinition
* @property {string} [attribute] * @property {string} [attribute]
* @property {boolean} [reflect] * @property {boolean} [reflect]

Loading…
Cancel
Save