type cleanup

pull/8569/head
S. Elliott Johnson 3 years ago
parent 68adbc18ae
commit 3d4e58e1f0

@ -0,0 +1,76 @@
/**
* Actions can return an object containing the two properties defined in this interface. Both are optional.
* - update: An action can have a parameter. This method will be called whenever that parameter changes,
* immediately after Svelte has applied updates to the markup. `ActionReturn` and `ActionReturn<never>` both
* mean that the action accepts no parameters, which makes it illegal to set the `update` method.
* - destroy: Method that is called after the element is unmounted
*
* Additionally, you can specify which additional attributes and events the action enables on the applied element.
* This applies to TypeScript typings only and has no effect at runtime.
*
* Example usage:
* ```ts
* interface Attributes {
* newprop?: string;
* 'on:event': (e: CustomEvent<boolean>) => void;
* }
*
* export function myAction(node: HTMLElement, parameter: Parameter): ActionReturn<Parameter, Attributes> {
* // ...
* return {
* update: (updatedParameter) => {...},
* destroy: () => {...}
* };
* }
* ```
*
* Docs: https://svelte.dev/docs#template-syntax-element-directives-use-action
*/
export interface ActionReturn<
Parameter = never,
Attributes extends Record<string, any> = Record<never, any>
> {
update?: [Parameter] extends [never] ? never : (parameter: Parameter) => void;
destroy?: () => void;
/**
* ### DO NOT USE THIS
* This exists solely for type-checking and has no effect at runtime.
* Set this through the `Attributes` generic instead.
*/
$$_attributes?: Attributes;
}
/**
* Actions are functions that are called when an element is created.
* You can use this interface to type such actions.
* The following example defines an action that only works on `<div>` elements
* and optionally accepts a parameter which it has a default value for:
* ```ts
* export const myAction: Action<HTMLDivElement, { someProperty: boolean } | undefined> = (node, param = { someProperty: true }) => {
* // ...
* }
* ```
* `Action<HTMLDivElement>` and `Action<HTMLDiveElement, never>` both signal that the action accepts no parameters.
*
* You can return an object with methods `update` and `destroy` from the function and type which additional attributes and events it has.
* See interface `ActionReturn` for more details.
*
* Docs: https://svelte.dev/docs#template-syntax-element-directives-use-action
*/
export interface Action<
Element = HTMLElement,
Parameter = never,
Attributes extends Record<string, any> = Record<never, any>
> {
<Node extends Element>(
...args: [Parameter] extends [never]
? [node: Node]
: undefined extends Parameter
? [node: Node, parameter?: Parameter]
: [node: Node, parameter: Parameter]
): void | ActionReturn<Parameter, Attributes>;
}
// Implementation notes:
// - undefined extends X instead of X extends undefined makes this work better with both strict and nonstrict mode
// - [X] extends [never] is needed, X extends never would reduce the whole resulting type to never and not to one of the condition outcomes

@ -1,59 +1 @@
export {};
// Implementation notes:
// - undefined extends X instead of X extends undefined makes this work better with both strict and nonstrict mode
// - [X] extends [never] is needed, X extends never would reduce the whole resulting type to never and not to one of the condition outcomes
/**
* Actions can return an object containing the two properties defined in this interface. Both are optional.
* - update: An action can have a parameter. This method will be called whenever that parameter changes,
* immediately after Svelte has applied updates to the markup. `ActionReturn` and `ActionReturn<never>` both
* mean that the action accepts no parameters, which makes it illegal to set the `update` method.
* - destroy: Method that is called after the element is unmounted
*
* Additionally, you can specify which additional attributes and events the action enables on the applied element.
* This applies to TypeScript typings only and has no effect at runtime.
*
* Example usage:
* ```ts
* interface Attributes {
* newprop?: string;
* 'on:event': (e: CustomEvent<boolean>) => void;
* }
*
* export function myAction(node: HTMLElement, parameter: Parameter): ActionReturn<Parameter, Attributes> {
* // ...
* return {
* update: (updatedParameter) => {...},
* destroy: () => {...}
* };
* }
* ```
*
* Docs: https://svelte.dev/docs#template-syntax-element-directives-use-action
* @typedef {Object} ActionReturn
* @property {[Parameter] extends [never] ? never : (parameter: Parameter) => void} [update]
* @property {()=>void} [destroy]
* @property {Attributes} [$$_attributes]
* ### DO NOT USE THIS
* This exists solely for type-checking and has no effect at runtime.
* Set this through the `Attributes` generic instead.
*/
/**
* Actions are functions that are called when an element is created.
* You can use this interface to type such actions.
* The following example defines an action that only works on `<div>` elements
* and optionally accepts a parameter which it has a default value for:
* ```ts
* export const myAction: Action<HTMLDivElement, { someProperty: boolean } | undefined> = (node, param = { someProperty: true }) => {
* // ...
* }
* ```
* `Action<HTMLDivElement>` and `Action<HTMLDiveElement, never>` both signal that the action accepts no parameters.
*
* You can return an object with methods `update` and `destroy` from the function and type which additional attributes and events it has.
* See interface `ActionReturn` for more details.
*
* Docs: https://svelte.dev/docs#template-syntax-element-directives-use-action
* @typedef {Object} Action
*/

@ -0,0 +1,14 @@
// todo: same as Transition, should it be shared?
export interface AnimationConfig {
delay?: number;
duration?: number;
easing?: (t: number) => number;
css?: (t: number, u: number) => string;
tick?: (t: number, u: number) => void;
}
export interface FlipParams {
delay?: number;
duration?: number | ((len: number) => number);
easing?: (t: number) => number;
}

@ -3,9 +3,9 @@ import { is_function } from '../internal';
/**
* @param {Element} node
* @param {{ from: DOMRect; to: DOMRect }}
* @param {FlipParams} params
* @returns {import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").AnimationConfig}
* @param {{ from: DOMRect; to: DOMRect }} fromTo
* @param {import('.').FlipParams} params
* @returns {import('.').AnimationConfig}
*/
export function flip(node, { from, to }, params = {}) {
const style = getComputedStyle(node);
@ -27,19 +27,3 @@ export function flip(node, { from, to }, params = {}) {
}
};
}
/**
* @typedef {Object} AnimationConfig
* @property {number} [delay]
* @property {number} [duration]
* @property {(t:number)=>number} [easing]
* @property {(t:number,u:number)=>string} [css]
* @property {(t:number,u:number)=>void} [tick]
*/
/**
* @typedef {Object} FlipParams
* @property {number} [delay]
* @property {number|((len:number)=>number)} [duration]
* @property {(t:number)=>number} [easing]
*/

@ -95,7 +95,7 @@ export function init(
) {
const parent_component = current_component;
set_current_component(component);
/** @type {import('.').T$$} */
/** @type {import('./public.d.ts').T$$} */
const $$ = (component.$$ = {
fragment: null,
ctx: [],
@ -352,7 +352,7 @@ function get_custom_element_value(prop, value, props_definition, transform) {
* @internal
*
* Turn a Svelte component into a custom element.
* @param {import('./dev').ComponentType} Component A Svelte component constructor
* @param {import('./public.d.ts').ComponentType} Component A Svelte component constructor
* @param {Record<string, CustomElementPropDefinition>} props_definition The props to observe
* @param {string[]} slots The slots to create
* @param {string[]} accessors Other accessors besides the ones for props the component has

@ -13,7 +13,7 @@ export class ResizeObserverSingleton {
/**
* @param {Element} element
* @param {Listener} listener
* @param {import('./private.d.ts').Listener} listener
* @returns {() => void}
*/
observe(element, listener) {
@ -53,30 +53,3 @@ export class ResizeObserverSingleton {
// Needs to be written like this to pass the tree-shake-test
ResizeObserverSingleton.entries = 'WeakMap' in globals ? new WeakMap() : undefined;
/** @typedef {(entry: ResizeObserverEntry) => any} Listener */
/** @typedef {'border-box' | 'content-box' | 'device-pixel-content-box'} ResizeObserverBoxOptions */
/**
* @typedef {Object} ResizeObserverSize
* @property {number} blockSize
* @property {number} inlineSize
*/
/**
* @typedef {Object} ResizeObserverEntry
* @property {readonly ResizeObserverSize[]} borderBoxSize
* @property {readonly ResizeObserverSize[]} contentBoxSize
* @property {DOMRectReadOnly} contentRect
* @property {readonly ResizeObserverSize[]} devicePixelContentBoxSize
* @property {Element} target
*/
/**
* @typedef {Object} ResizeObserverOptions
* @property {ResizeObserverBoxOptions} [box]
*/
/** @typedef {Object} ResizeObserver */
/** @typedef {Object} ResizeObserverCallback */

@ -5,8 +5,8 @@ import { create_rule, delete_rule } from './style_manager.js';
/**
* @param {Element & ElementCSSInlineStyle} node
* @param {PositionRect} from
* @param {AnimationFn} fn
* @param {import('./private.d.ts').PositionRect} from
* @param {import('./private.d.ts').AnimationFn} fn
* @returns {any}
*/
export function create_animation(node, from, fn, params) {
@ -88,7 +88,7 @@ export function fix_position(node) {
/**
* @param {Element & ElementCSSInlineStyle} node
* @param {PositionRect} a
* @param {import('./private.d.ts').PositionRect} a
* @returns {void}
*/
export function add_transform(node, a) {
@ -99,12 +99,3 @@ export function add_transform(node, a) {
node.style.transform = `${transform} translate(${a.left - b.left}px, ${a.top - b.top}px)`;
}
}
/** @typedef {DOMRect | ClientRect} PositionRect */
/**
* @typedef {(
* node: Element,
* { from, to }: { from: PositionRect; to: PositionRect },
* params: any
* ) => import('../animate').AnimationConfig} AnimationFn
*/

@ -6,13 +6,13 @@ import { get_current_component, set_current_component } from './lifecycle.js';
/**
* @template T
* @param {Promise<T>} promise
* @param {PromiseInfo<T>} info
* @param {import('./private.d.ts').PromiseInfo<T>} info
* @returns {boolean}
*/
export function handle_promise(promise, info) {
const token = (info.token = {});
/**
* @param {import('.').FragmentFactory} type
* @param {import('./public.d.ts').FragmentFactory} type
* @param {0 | 1 | 2} index
* @param {number} [key]
* @param {any} [value]
@ -98,22 +98,3 @@ export function update_await_block_branch(info, ctx, dirty) {
}
info.block.p(child_ctx, dirty);
}
/**
* @typedef {Object} PromiseInfo
* @template T
* @property {null|any} ctx
* @property {{}} token
* @property {boolean} hasCatch
* @property {FragmentFactory} pending
* @property {FragmentFactory} then
* @property {FragmentFactory} catch
* @property {number} value
* @property {number} error
* @property {T} [resolved]
* @property {FragmentFactory|null} current
* @property {Fragment|null} block
* @property {[null|Fragment,null|Fragment,null|Fragment]} blocks
* @property {()=>HTMLElement} mount
* @property {HTMLElement} anchor
*/

@ -1,2 +1,3 @@
/** @type {typeof globalThis} */
export const globals =
typeof window !== 'undefined' ? window : typeof globalThis !== 'undefined' ? globalThis : global;

@ -1,39 +0,0 @@
/**
* INTERNAL, DO NOT USE. Code may change at any time.
*/
export interface Fragment {
key: string | null;
first: null;
/* create */ c: () => void;
/* claim */ l: (nodes: any) => void;
/* hydrate */ h: () => void;
/* mount */ m: (target: HTMLElement, anchor: any) => void;
/* update */ p: (ctx: T$$['ctx'], dirty: T$$['dirty']) => void;
/* measure */ r: () => void;
/* fix */ f: () => void;
/* animate */ a: () => void;
/* intro */ i: (local: any) => void;
/* outro */ o: (local: any) => void;
/* destroy */ d: (detaching: 0 | 1) => void;
}
export type FragmentFactory = (ctx: any) => Fragment;
export interface T$$ {
dirty: number[];
ctx: any[];
bound: any;
update: () => void;
callbacks: any;
after_update: any[];
props: Record<string, 0 | string>;
fragment: null | false | Fragment;
not_equal: any;
before_update: any[];
context: Map<any, any>;
on_mount: any[];
on_destroy: any[];
skip_bound: boolean;
on_disconnect: any[];
root: Element | ShadowRoot;
}

@ -1,18 +0,0 @@
export interface EventDispatcher<EventMap extends Record<string, any>> {
// Implementation notes:
// - undefined extends X instead of X extends undefined makes this work better with both strict and nonstrict mode
// - [X] extends [never] is needed, X extends never would reduce the whole resulting type to never and not to one of the condition outcomes
<Type extends keyof EventMap>(
...args: [EventMap[Type]] extends [never]
? [type: Type, parameter?: null | undefined, options?: DispatchOptions]
: null extends EventMap[Type]
? [type: Type, parameter?: EventMap[Type], options?: DispatchOptions]
: undefined extends EventMap[Type]
? [type: Type, parameter?: EventMap[Type], options?: DispatchOptions]
: [type: Type, parameter: EventMap[Type], options?: DispatchOptions]
): boolean;
}
export interface DispatchOptions {
cancelable?: boolean;
}

@ -92,7 +92,7 @@ export function onDestroy(fn) {
*
* https://svelte.dev/docs#run-time-svelte-createeventdispatcher
* @template {Record<string, any>} EventMap
* @returns {import('./lifecycle').EventDispatcher<EventMap>}
* @returns {import('./public.d.ts').EventDispatcher<EventMap>}
*/
export function createEventDispatcher() {
const component = get_current_component();
@ -175,9 +175,3 @@ export function bubble(component, event) {
callbacks.slice().forEach((fn) => fn.call(this, event));
}
}
/** @typedef {Object} EventDispatcher */
/**
* @typedef {Object} DispatchOptions
* @property {boolean} [cancelable]
*/

@ -27,11 +27,11 @@ export function clear_loops() {
/**
* Creates a new task that runs on each raf frame
* until it returns a falsy value or is aborted
* @param {TaskCallback} callback
* @returns {Task}
* @param {import('./private.d.ts').TaskCallback} callback
* @returns {import('./public.d.ts').Task}
*/
export function loop(callback) {
/** @type {TaskEntry} */
/** @type {import('./private.d.ts').TaskEntry} */
let task;
if (tasks.size === 0) raf(run_tasks);
return {
@ -43,12 +43,3 @@ export function loop(callback) {
}
};
}
/** @typedef {(now: number) => boolean | void} TaskCallback */
/** @typedef {{ c: TaskCallback; f: () => void }} TaskEntry */
/**
* @typedef {Object} Task
* @property {() => void} abort
* @property {Promise<void>} promise
*/

@ -0,0 +1,81 @@
import type { AnimationConfig } from '../animate';
import type { Fragment, FragmentFactory } from './public';
export type AnimationFn = (
node: Element,
{ from, to }: { from: PositionRect; to: PositionRect },
params: any
) => AnimationConfig;
type Listener = (entry: ResizeObserverEntry) => any;
//todo: documentation says it is DOMRect, but in IE it would be ClientRect
export type PositionRect = DOMRect | ClientRect;
export interface PromiseInfo<T> {
ctx: null | any;
// unique object instance as a key to compare different promises
token: {};
hasCatch: boolean;
pending: FragmentFactory;
then: FragmentFactory;
catch: FragmentFactory;
// ctx index for resolved value and rejected error
value: number;
error: number;
// resolved value or rejected error
resolved?: T;
// the current factory function for creating the fragment
current: FragmentFactory | null;
// the current fragment
block: Fragment | null;
// tuple of the pending, then, catch fragment
blocks: [null | Fragment, null | Fragment, null | Fragment];
// DOM elements to mount and anchor on for the {#await} block
mount: () => HTMLElement;
anchor: HTMLElement;
}
// TODO: Remove this
export interface ResizeObserverSize {
readonly blockSize: number;
readonly inlineSize: number;
}
export interface ResizeObserverEntry {
readonly borderBoxSize: readonly ResizeObserverSize[];
readonly contentBoxSize: readonly ResizeObserverSize[];
readonly contentRect: DOMRectReadOnly;
readonly devicePixelContentBoxSize: readonly ResizeObserverSize[];
readonly target: Element;
}
export type ResizeObserverBoxOptions = 'border-box' | 'content-box' | 'device-pixel-content-box';
export interface ResizeObserverOptions {
box?: ResizeObserverBoxOptions;
}
export interface ResizeObserver {
disconnect(): void;
observe(target: Element, options?: ResizeObserverOptions): void;
unobserve(target: Element): void;
}
export interface ResizeObserverCallback {
(entries: ResizeObserverEntry[], observer: ResizeObserver): void;
}
export declare let ResizeObserver: {
prototype: ResizeObserver;
new (callback: ResizeObserverCallback): ResizeObserver;
};
export interface StyleInformation {
stylesheet: CSSStyleSheet;
rules: Record<string, true>;
}
export type TaskCallback = (now: number) => boolean | void;
export type TaskEntry = { c: TaskCallback; f: () => void };

@ -1,19 +1,5 @@
import type { SvelteComponent } from './Component';
export interface SvelteComponentDev<
Props extends Record<string, any> = any,
Events extends Record<string, any> = any,
Slots extends Record<string, any> = any // eslint-disable-line @typescript-eslint/no-unused-vars
> {
$set(props?: Partial<Props>): void;
$on<K extends Extract<keyof Events, string>>(
type: K,
callback: ((e: Events[K]) => void) | null | undefined
): () => void;
$destroy(): void;
[accessor: string]: any;
}
export interface ComponentConstructorOptions<
Props extends Record<string, any> = Record<string, any>
> {
@ -26,12 +12,37 @@ export interface ComponentConstructorOptions<
$$inline?: boolean;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface SvelteComponentTyped<
Props extends Record<string, any> = any,
Events extends Record<string, any> = any,
Slots extends Record<string, any> = any
> extends SvelteComponentDev<Props, Events, Slots> {}
/**
* Convenience type to get the events the given component expects. Example:
* ```html
* <script lang="ts">
* import type { ComponentEvents } from 'svelte';
* import Component from './Component.svelte';
*
* function handleCloseEvent(event: ComponentEvents<Component>['close']) {
* console.log(event.detail);
* }
* </script>
*
* <Component on:close={handleCloseEvent} />
* ```
*/
export type ComponentEvents<Component extends SvelteComponent> =
Component extends SvelteComponentDev<any, infer Events> ? Events : never;
/**
* Convenience type to get the props the given component expects. Example:
* ```html
* <script lang="ts">
* import type { ComponentProps } from 'svelte';
* import Component from './Component.svelte';
*
* const props: ComponentProps<Component> = { foo: 'bar' }; // Errors if these aren't the correct props
* </script>
* ```
*/
export type ComponentProps<Component extends SvelteComponent> =
Component extends SvelteComponentDev<infer Props> ? Props : never;
/**
* Convenience type to get the type of a Svelte component. Useful for example in combination with
@ -61,34 +72,87 @@ export type ComponentType<Component extends SvelteComponentDev = SvelteComponent
element?: typeof HTMLElement;
};
/**
* Convenience type to get the props the given component expects. Example:
* ```html
* <script lang="ts">
* import type { ComponentProps } from 'svelte';
* import Component from './Component.svelte';
*
* const props: ComponentProps<Component> = { foo: 'bar' }; // Errors if these aren't the correct props
* </script>
* ```
*/
export type ComponentProps<Component extends SvelteComponent> =
Component extends SvelteComponentDev<infer Props> ? Props : never;
export interface DispatchOptions {
cancelable?: boolean;
}
export interface EventDispatcher<EventMap extends Record<string, any>> {
// Implementation notes:
// - undefined extends X instead of X extends undefined makes this work better with both strict and nonstrict mode
// - [X] extends [never] is needed, X extends never would reduce the whole resulting type to never and not to one of the condition outcomes
<Type extends keyof EventMap>(
...args: [EventMap[Type]] extends [never]
? [type: Type, parameter?: null | undefined, options?: DispatchOptions]
: null extends EventMap[Type]
? [type: Type, parameter?: EventMap[Type], options?: DispatchOptions]
: undefined extends EventMap[Type]
? [type: Type, parameter?: EventMap[Type], options?: DispatchOptions]
: [type: Type, parameter: EventMap[Type], options?: DispatchOptions]
): boolean;
}
/**
* Convenience type to get the events the given component expects. Example:
* ```html
* <script lang="ts">
* import type { ComponentEvents } from 'svelte';
* import Component from './Component.svelte';
*
* function handleCloseEvent(event: ComponentEvents<Component>['close']) {
* console.log(event.detail);
* }
* </script>
*
* <Component on:close={handleCloseEvent} />
* ```
* INTERNAL, DO NOT USE. Code may change at any time.
*/
export type ComponentEvents<Component extends SvelteComponent> =
Component extends SvelteComponentDev<any, infer Events> ? Events : never;
export interface Fragment {
key: string | null;
first: null;
/* create */ c: () => void;
/* claim */ l: (nodes: any) => void;
/* hydrate */ h: () => void;
/* mount */ m: (target: HTMLElement, anchor: any) => void;
/* update */ p: (ctx: T$$['ctx'], dirty: T$$['dirty']) => void;
/* measure */ r: () => void;
/* fix */ f: () => void;
/* animate */ a: () => void;
/* intro */ i: (local: any) => void;
/* outro */ o: (local: any) => void;
/* destroy */ d: (detaching: 0 | 1) => void;
}
export type FragmentFactory = (ctx: any) => Fragment;
export interface SvelteComponentDev<
Props extends Record<string, any> = any,
Events extends Record<string, any> = any,
Slots extends Record<string, any> = any // eslint-disable-line @typescript-eslint/no-unused-vars
> {
$set(props?: Partial<Props>): void;
$on<K extends Extract<keyof Events, string>>(
type: K,
callback: ((e: Events[K]) => void) | null | undefined
): () => void;
$destroy(): void;
[accessor: string]: any;
}
export interface T$$ {
dirty: number[];
ctx: any[];
bound: any;
update: () => void;
callbacks: any;
after_update: any[];
props: Record<string, 0 | string>;
fragment: null | false | Fragment;
not_equal: any;
before_update: any[];
context: Map<any, any>;
on_mount: any[];
on_destroy: any[];
skip_bound: boolean;
on_disconnect: any[];
root: Element | ShadowRoot;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface SvelteComponentTyped<
Props extends Record<string, any> = any,
Events extends Record<string, any> = any,
Slots extends Record<string, any> = any
> extends SvelteComponentDev<Props, Events, Slots> {}
export interface Task {
abort(): void;
promise: Promise<void>;
}

@ -3,6 +3,7 @@ import { raf } from './environment.js';
// we need to store the information for multiple documents because a Svelte application could also contain iframes
// https://github.com/sveltejs/svelte/issues/3624
/** @type {Map<Document | ShadowRoot, import('./private.d.ts').StyleInformation>} */
const managed_styles = new Map();
let active = 0;
@ -96,9 +97,3 @@ export function clear_rules() {
managed_styles.clear();
});
}
/**
* @typedef {Object} StyleInformation
* @property {CSSStyleSheet} stylesheet
* @property {Record<string,true>} rules
*/

@ -5,7 +5,7 @@ import { create_rule, delete_rule } from './style_manager';
import { custom_event } from './dom';
import { add_render_callback } from './scheduler';
import { TransitionConfig } from '../transition';
import type { Fragment } from '.';
import type { Fragment } from './public';
let promise: Promise<void> | null;
type INTRO = 1;

@ -19,8 +19,10 @@ export function assign(tar, src) {
// Adapted from https://github.com/then/is-promise/blob/master/index.js
// Distributed under MIT License https://github.com/then/is-promise/blob/master/LICENSE
/** @param {any} value
* @returns {boolean}
/**
* @template T
* @param {any} value
* @returns {value is PromiseLike<T>}
*/
export function is_promise(value) {
return (
@ -47,15 +49,17 @@ export function blank_object() {
return Object.create(null);
}
/** @param {Function[]} fns
/**
* @param {Function[]} fns
* @returns {void}
*/
export function run_all(fns) {
fns.forEach(run);
}
/** @param {any} thing
* @returns {boolean}
/**
* @param {any} thing
* @returns {thing is Function}
*/
export function is_function(thing) {
return typeof thing === 'function';

Loading…
Cancel
Save