diff --git a/src/compiler/compile/nodes/EventHandler.ts b/src/compiler/compile/nodes/EventHandler.ts index 5872e7e3c5..95ce741f2a 100644 --- a/src/compiler/compile/nodes/EventHandler.ts +++ b/src/compiler/compile/nodes/EventHandler.ts @@ -35,7 +35,7 @@ function is_valid_any_alias_name(alias: string) { if (alias === '*') { return true; } - let idx = alias.indexOf('*'); + const idx = alias.indexOf('*'); if (idx < 0) return false; if (idx !== alias.lastIndexOf('*')) { return false; diff --git a/src/compiler/compile/nodes/InlineComponent.ts b/src/compiler/compile/nodes/InlineComponent.ts index 5949b1c3fc..7c60943720 100644 --- a/src/compiler/compile/nodes/InlineComponent.ts +++ b/src/compiler/compile/nodes/InlineComponent.ts @@ -98,7 +98,7 @@ export default class InlineComponent extends Node { this.scope = scope; } - this.handlers.forEach(h => h.validate()) + this.handlers.forEach(h => h.validate()); const children = []; for (let i = info.children.length - 1; i >= 0; i--) { diff --git a/src/compiler/compile/render_dom/Block.ts b/src/compiler/compile/render_dom/Block.ts index c7b975d0d1..b89dc5a98b 100644 --- a/src/compiler/compile/render_dom/Block.ts +++ b/src/compiler/compile/render_dom/Block.ts @@ -1,10 +1,9 @@ import Renderer, { BindingGroup } from './Renderer'; import Wrapper from './wrappers/shared/Wrapper'; import { b, x } from 'code-red'; -import { Node, Identifier, ArrayPattern } from 'estree'; +import { Node, Identifier, ArrayPattern, Expression } from 'estree'; import { is_head } from './wrappers/shared/is_head'; import { regex_double_quotes } from '../../utils/patterns'; -import { Expression } from 'estree'; export interface Bindings { object: Identifier; @@ -60,7 +59,7 @@ export default class Block { destroy: Array; }; - event_updaters: ({condition:Expression, index:number})[] = []; + event_updaters: Array<{condition:Expression, index:number}> = []; event_listeners: Node[] = []; maintain_context: boolean; diff --git a/src/compiler/compile/render_dom/wrappers/Element/EventHandler.ts b/src/compiler/compile/render_dom/wrappers/Element/EventHandler.ts index cd313e4c1a..7ec77c9357 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/EventHandler.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/EventHandler.ts @@ -37,7 +37,7 @@ export default class EventHandlerWrapper { const snippet = this.get_snippet(block); - let wrappers = []; + const wrappers = []; if (this.node.modifiers.has('trusted')) wrappers.push(x`@trusted`); if (this.node.modifiers.has('self')) wrappers.push(x`@self`); if (this.node.modifiers.has('stopImmediatePropagation')) wrappers.push(x`@stop_immediate_propagation`); diff --git a/src/runtime/internal/dev.ts b/src/runtime/internal/dev.ts index fd0090b3d2..39657c6116 100644 --- a/src/runtime/internal/dev.ts +++ b/src/runtime/internal/dev.ts @@ -74,7 +74,7 @@ export function listen_dev(node: Node, event: string, handler: EventListenerOrEv }; } -export function bubble_dev(component: SvelteComponent, listen_func: Function, node: EventTarget|SvelteComponent, type: string, typeName: string = type): Function { +export function bubble_dev(component: SvelteComponent, listen_func: Function, node: EventTarget | SvelteComponent, type: string, typeName: string = type): Function { dispatch_dev('SvelteComponentAddEventBubble', { component, listen_func, node, type, typeName }); const dispose = bubble(component, listen_func, node, type, typeName); return () => { diff --git a/src/runtime/internal/dom.ts b/src/runtime/internal/dom.ts index 152c88bfb6..8a391c3c05 100644 --- a/src/runtime/internal/dom.ts +++ b/src/runtime/internal/dom.ts @@ -264,7 +264,7 @@ export function wrap_handler(handler: EventListenerOrEventListenerObject, wrappe return result; } -export function listen(node: EventTarget, event: string, handler: EventListenerOrEventListenerObject|null|undefined|false, options?: boolean | AddEventListenerOptions | EventListenerOptions, wrappers?: Function[]) { +export function listen(node: EventTarget, event: string, handler: EventListenerOrEventListenerObject | null | undefined | false, options?: boolean | AddEventListenerOptions | EventListenerOptions, wrappers?: Function[]) { if (handler) { const h = wrap_handler(handler, wrappers); node.addEventListener(event, h, options); @@ -273,12 +273,10 @@ export function listen(node: EventTarget, event: string, handler: EventListenerO return noop; } -export function listen_and_update(get_handler: ()=>EventListenerOrEventListenerObject|null|undefined|false, factory: (handler:EventListenerOrEventListenerObject|null|undefined|false) => Function) { +export function listen_and_update(get_handler: ()=>EventListenerOrEventListenerObject | null | undefined | false, factory: (handler:EventListenerOrEventListenerObject | null | undefined | false) => Function) { let handler = get_handler(); let dispose_handle: Function = factory(handler); - const dispose = () => { - dispose_handle(); - } + const dispose = () => dispose_handle(); // update : dispose.p = () => { const new_handler = get_handler(); @@ -287,7 +285,7 @@ export function listen_and_update(get_handler: ()=>EventListenerOrEventListenerO handler = new_handler; dispose_handle = factory(handler); } - } + }; return dispose; } diff --git a/src/runtime/internal/lifecycle.ts b/src/runtime/internal/lifecycle.ts index 83aef3e0e4..4cb3aaaa30 100644 --- a/src/runtime/internal/lifecycle.ts +++ b/src/runtime/internal/lifecycle.ts @@ -155,7 +155,7 @@ function start_bubble(type: string, bubble: Bubble, callback: Callback) { function start_bubbles(comp : SvelteComponent, bubble: Bubble) { for (const type of Object.keys(comp.$$.callbacks)) { - comp.$$.callbacks[type].forEach( callback => { start_bubble(type, bubble, callback); }) + comp.$$.callbacks[type].forEach( callback => start_bubble(type, bubble, callback)); } } @@ -170,7 +170,7 @@ export function restart_all_callback(comp : SvelteComponent) { function start_callback(comp : SvelteComponent, type: string, callback: Callback) { for (const bubbles of [ comp.$$.bubbles[type], comp.$$.bubbles['*'] ]) { if (bubbles) { - for(const bubble of bubbles) { + for (const bubble of bubbles) { start_bubble(type, bubble, callback); } } @@ -186,8 +186,8 @@ export function add_callback(comp : SvelteComponent, type: string, f: EventListe const callback: Callback = {f, o}; if (o && (o as any)?.once === true) { - callback.f = function () { - const r = f.apply(this, arguments); + callback.f = function(this: any, ...args) { + const r = f.call(this, ...args); remove_callback(comp, type, callback); return r; }; @@ -236,15 +236,20 @@ function add_bubble(comp: SvelteComponent, type: string, f: CallbackFactory): Fu for (const dispose of bubble.r.values()) { dispose(); } - } + }; } +/** + * Schedules a callback to run when an handler is added to the component + * + * TODO : Docs + */ export function onEventListener(type: string, fn: CallbackFactory) { add_bubble(get_current_component(), type, fn); } -export function bubble(component: SvelteComponent, listen_func: Function, node: EventTarget|SvelteComponent, type: string, typeName: string = type): Function { +export function bubble(component: SvelteComponent, listen_func: Function, node: EventTarget | SvelteComponent, type: string, typeName: string = type): Function { if (type === '*') { return add_bubble(component, type, (callback, options, eventType) => { let typeToListen: string = null; @@ -253,12 +258,12 @@ export function bubble(component: SvelteComponent, listen_func: Function, node: } else if (typeName.startsWith('*')) { const len = typeName.length; if (eventType.endsWith(typeName.substring(1))) { - typeToListen = eventType.substring(0, eventType.length - (len-1)); + typeToListen = eventType.substring(0, eventType.length - (len - 1)); } } else if (typeName.endsWith('*')) { const len = typeName.length; - if (eventType.startsWith(typeName.substring(0,len-1))) { - typeToListen = eventType.substring(len-1); + if (eventType.startsWith(typeName.substring(0,len - 1))) { + typeToListen = eventType.substring(len - 1); } } if (typeToListen) { @@ -271,9 +276,9 @@ export function bubble(component: SvelteComponent, listen_func: Function, node: }); } -export function listen_comp(comp: SvelteComponent, event: string, handler: EventListenerOrEventListenerObject|null|undefined|false, options?: boolean | AddEventListenerOptions | EventListenerOptions, wrappers?: Function[]) { +export function listen_comp(comp: SvelteComponent, event: string, handler: EventListenerOrEventListenerObject | null | undefined | false, options?: boolean | AddEventListenerOptions | EventListenerOptions, wrappers?: Function[]) { if (handler) { return comp.$on(event, wrap_handler(handler, wrappers), options); } return noop; -} \ No newline at end of file +} diff --git a/src/runtime/internal/types.ts b/src/runtime/internal/types.ts index 4da02d1bf3..82e75dc81e 100644 --- a/src/runtime/internal/types.ts +++ b/src/runtime/internal/types.ts @@ -25,7 +25,7 @@ export interface Callback { o?: boolean | AddEventListenerOptions | EventListenerOptions; } -export type CallbackFactory = (callback: EventListener, options: boolean | AddEventListenerOptions | EventListenerOptions | undefined, type: string) => Function|void; +export type CallbackFactory = (callback: EventListener, options: boolean | AddEventListenerOptions | EventListenerOptions | undefined, type: string) => Function | undefined; export interface Bubble { f: CallbackFactory;