Skip to content

fix: use arrow functions over bound funcs #12915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gentle-feet-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: use arrow functions instead of bound functions
3 changes: 2 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export default [
rules: {
'@typescript-eslint/await-thenable': 'error',
'@typescript-eslint/no-unused-expressions': 'off',
'@typescript-eslint/require-await': 'error'
'@typescript-eslint/require-await': 'error',
'@typescript-eslint/unbound-method': 'error'
},
ignores: [
'packages/adapter-node/rollup.config.js',
Expand Down
34 changes: 17 additions & 17 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface Adapter {
* This function is called after SvelteKit has built your app.
* @param builder An object provided by SvelteKit that contains methods for adapting the app
*/
adapt(builder: Builder): MaybePromise<void>;
adapt: (builder: Builder) => MaybePromise<void>;
/**
* Checks called during dev and build to determine whether specific features will work in production with this adapter
*/
Expand All @@ -49,7 +49,7 @@ export interface Adapter {
* Creates an `Emulator`, which allows the adapter to influence the environment
* during dev, build and prerendering
*/
emulate?(): MaybePromise<Emulator>;
emulate?: () => MaybePromise<Emulator>;
}

export type LoadProperties<input extends Record<string, any> | void> = input extends void
Expand Down Expand Up @@ -685,7 +685,7 @@ export interface KitConfig {
*/
export type Handle = (input: {
event: RequestEvent;
resolve(event: RequestEvent, opts?: ResolveOptions): MaybePromise<Response>;
resolve: (event: RequestEvent, opts?: ResolveOptions) => MaybePromise<Response>;
}) => MaybePromise<Response>;

/**
Expand Down Expand Up @@ -791,14 +791,14 @@ export interface LoadEvent<
*
* `setHeaders` has no effect when a `load` function runs in the browser.
*/
setHeaders(headers: Record<string, string>): void;
setHeaders: (headers: Record<string, string>) => void;
/**
* `await parent()` returns data from parent `+layout.js` `load` functions.
* Implicitly, a missing `+layout.js` is treated as a `({ data }) => data` function, meaning that it will return and forward data from parent `+layout.server.js` files.
*
* Be careful not to introduce accidental waterfalls when using `await parent()`. If for example you only want to merge parent data into the returned output, call it _after_ fetching your other data.
*/
parent(): Promise<ParentData>;
parent: () => Promise<ParentData>;
/**
* This function declares that the `load` function has a _dependency_ on one or more URLs or custom identifiers, which can subsequently be used with [`invalidate()`](https://svelte.dev/docs/kit/$app-navigation#invalidate) to cause `load` to rerun.
*
Expand Down Expand Up @@ -836,7 +836,7 @@ export interface LoadEvent<
* <button on:click={increase}>Increase Count</button>
* ```
*/
depends(...deps: Array<`${string}:${string}`>): void;
depends: (...deps: Array<`${string}:${string}`>) => void;
/**
* Use this function to opt out of dependency tracking for everything that is synchronously called within the callback. Example:
*
Expand All @@ -850,7 +850,7 @@ export interface LoadEvent<
* }
* ```
*/
untrack<T>(fn: () => T): T;
untrack: <T>(fn: () => T) => T;
}

export interface NavigationEvent<
Expand Down Expand Up @@ -1059,7 +1059,7 @@ export interface RequestEvent<
/**
* The client's IP address, set by the adapter.
*/
getClientAddress(): string;
getClientAddress: () => string;
/**
* Contains custom data that was added to the request within the [`server handle hook`](https://svelte.dev/docs/kit/hooks#Server-hooks-handle).
*/
Expand Down Expand Up @@ -1107,7 +1107,7 @@ export interface RequestEvent<
*
* You cannot add a `set-cookie` header with `setHeaders` — use the [`cookies`](https://svelte.dev/docs/kit/@sveltejs-kit#Cookies) API instead.
*/
setHeaders(headers: Record<string, string>): void;
setHeaders: (headers: Record<string, string>) => void;
/**
* The requested URL.
*/
Expand Down Expand Up @@ -1140,20 +1140,20 @@ export interface ResolveOptions {
* but they will always be split at sensible boundaries such as `%sveltekit.head%` or layout/page components.
* @param input the html chunk and the info if this is the last chunk
*/
transformPageChunk?(input: { html: string; done: boolean }): MaybePromise<string | undefined>;
transformPageChunk?: (input: { html: string; done: boolean }) => MaybePromise<string | undefined>;
/**
* Determines which headers should be included in serialized responses when a `load` function loads a resource with `fetch`.
* By default, none will be included.
* @param name header name
* @param value header value
*/
filterSerializedResponseHeaders?(name: string, value: string): boolean;
filterSerializedResponseHeaders?: (name: string, value: string) => boolean;
/**
* Determines what should be added to the `<head>` tag to preload it.
* By default, `js` and `css` files will be preloaded.
* @param input the type of the file and its path
*/
preload?(input: { type: 'font' | 'css' | 'js' | 'asset'; path: string }): boolean;
preload?: (input: { type: 'font' | 'css' | 'js' | 'asset'; path: string }) => boolean;
}

export interface RouteDefinition<Config = any> {
Expand Down Expand Up @@ -1222,7 +1222,7 @@ export interface ServerLoadEvent<
*
* Be careful not to introduce accidental waterfalls when using `await parent()`. If for example you only want to merge parent data into the returned output, call it _after_ fetching your other data.
*/
parent(): Promise<ParentData>;
parent: () => Promise<ParentData>;
/**
* This function declares that the `load` function has a _dependency_ on one or more URLs or custom identifiers, which can subsequently be used with [`invalidate()`](https://svelte.dev/docs/kit/$app-navigation#invalidate) to cause `load` to rerun.
*
Expand Down Expand Up @@ -1260,7 +1260,7 @@ export interface ServerLoadEvent<
* <button on:click={increase}>Increase Count</button>
* ```
*/
depends(...deps: string[]): void;
depends: (...deps: string[]) => void;
/**
* Use this function to opt out of dependency tracking for everything that is synchronously called within the callback. Example:
*
Expand All @@ -1274,7 +1274,7 @@ export interface ServerLoadEvent<
* }
* ```
*/
untrack<T>(fn: () => T): T;
untrack: <T>(fn: () => T) => T;
}

/**
Expand Down Expand Up @@ -1345,7 +1345,7 @@ export type SubmitFunction<
formElement: HTMLFormElement;
controller: AbortController;
submitter: HTMLElement | null;
cancel(): void;
cancel: () => void;
}) => MaybePromise<
| void
| ((opts: {
Expand All @@ -1358,7 +1358,7 @@ export type SubmitFunction<
* @param options Set `reset: false` if you don't want the `<form>` values to be reset after a successful submission.
* @param invalidateAll Set `invalidateAll: false` if you don't want the action to call `invalidateAll` after submission.
*/
update(options?: { reset?: boolean; invalidateAll?: boolean }): Promise<void>;
update: (options?: { reset?: boolean; invalidateAll?: boolean }) => Promise<void>;
}) => void)
>;

Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ export async function dev(vite, vite_config, svelte_config) {
});
}
align_exports();
// eslint-disable-next-line @typescript-eslint/unbound-method -- We'll pass `this` back to it later
const ws_send = vite.ws.send;
/** @param {any} args */
vite.ws.send = function (...args) {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/app/stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const navigating = {
* A readable store whose initial value is `false`. If [`version.pollInterval`](https://svelte.dev/docs/kit/configuration#version) is a non-zero value, SvelteKit will poll for new versions of the app and update the store value to `true` when it detects one. `updated.check()` will force an immediate check, regardless of polling.
*
* On the server, this store can only be subscribed to during component initialization. In the browser, it can be subscribed to at any time.
* @type {import('svelte/store').Readable<boolean> & { check(): Promise<boolean> }}
* @type {import('svelte/store').Readable<boolean> & { check: () => Promise<boolean> }}
*/
export const updated = {
subscribe(fn) {
Expand Down
3 changes: 3 additions & 0 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ if (DEV && BROWSER) {
);
};

// eslint-disable-next-line @typescript-eslint/unbound-method -- We'll pass `history` as `this`
const push_state = history.pushState;
history.pushState = (...args) => {
warn();
return push_state.apply(history, args);
};

// eslint-disable-next-line @typescript-eslint/unbound-method -- We'll pass `history` as `this`
const replace_state = history.replaceState;
history.replaceState = (...args) => {
warn();
Expand Down Expand Up @@ -1374,6 +1376,7 @@ async function navigate({
[STATES_KEY]: state
};

// eslint-disable-next-line @typescript-eslint/unbound-method -- We'll pass `history` as `this`
const fn = replace_state ? history.replaceState : history.pushState;
fn.call(history, entry, '', url);

Expand Down
3 changes: 3 additions & 0 deletions packages/kit/src/runtime/server/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function get_cookies(request, url, trailing_slash) {
return c.value;
}

// eslint-disable-next-line @typescript-eslint/unbound-method -- Fixed in `cookie@1.0.0`'s types
const decoder = opts?.decode || decodeURIComponent;
const req_cookies = parse(header, { decode: decoder });
const cookie = req_cookies[name]; // the decoded string or undefined
Expand Down Expand Up @@ -92,6 +93,7 @@ export function get_cookies(request, url, trailing_slash) {
* @param {import('cookie').CookieParseOptions} opts
*/
getAll(opts) {
// eslint-disable-next-line @typescript-eslint/unbound-method -- Fixed in `cookie@1.0.0`'s types
const decoder = opts?.decode || decodeURIComponent;
const cookies = parse(header, { decode: decoder });

Expand Down Expand Up @@ -161,6 +163,7 @@ export function get_cookies(request, url, trailing_slash) {
if (!domain_matches(destination.hostname, cookie.options.domain)) continue;
if (!path_matches(destination.pathname, cookie.options.path)) continue;

// eslint-disable-next-line @typescript-eslint/unbound-method -- Fixed in `cookie@1.0.0`'s types
const encoder = cookie.options.encode || encodeURIComponent;
combined_cookies[cookie.name] = encoder(cookie.value);
}
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/runtime/server/page/load_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ export function create_universal_fetch(event, state, fetched, csr, resolve_opts)

if (csr) {
// ensure that excluded headers can't be read
// eslint-disable-next-line @typescript-eslint/unbound-method -- We'll pass `response.headers` as `this`
const get = response.headers.get;
response.headers.get = (key) => {
const lower = key.toLowerCase();
Expand Down
22 changes: 11 additions & 11 deletions packages/kit/src/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ export interface ServerModule {
}

export interface ServerInternalModule {
set_assets(path: string): void;
set_building(): void;
set_manifest(manifest: SSRManifest): void;
set_prerendering(): void;
set_private_env(environment: Record<string, string>): void;
set_public_env(environment: Record<string, string>): void;
set_read_implementation(implementation: (path: string) => ReadableStream): void;
set_safe_public_env(environment: Record<string, string>): void;
set_version(version: string): void;
set_fix_stack_trace(fix_stack_trace: (error: unknown) => string): void;
set_assets: (path: string) => void;
set_building: () => void;
set_manifest: (manifest: SSRManifest) => void;
set_prerendering: () => void;
set_private_env: (environment: Record<string, string>) => void;
set_public_env: (environment: Record<string, string>) => void;
set_read_implementation: (implementation: (path: string) => ReadableStream) => void;
set_safe_public_env: (environment: Record<string, string>) => void;
set_version: (version: string) => void;
set_fix_stack_trace: (fix_stack_trace: (error: unknown) => string) => void;
}

export interface Asset {
Expand Down Expand Up @@ -404,7 +404,7 @@ export interface SSRRoute {

export interface SSRState {
fallback?: string;
getClientAddress(): string;
getClientAddress: () => string;
/**
* True if we're currently attempting to render an error page
*/
Expand Down
6 changes: 4 additions & 2 deletions packages/kit/src/types/private.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ export interface AdapterEntry {
* - Fallback pages: `/foo/[c]` is a fallback for `/foo/a-[b]`, and `/[...catchall]` is a fallback for all routes
* - Grouping routes that share a common `config`: `/foo` should be deployed to the edge, `/bar` and `/baz` should be deployed to a serverless function
*/
filter(route: RouteDefinition): boolean;
filter: (route: RouteDefinition) => boolean;

/**
* A function that is invoked once the entry has been created. This is where you
* should write the function to the filesystem and generate redirect manifests.
*/
complete(entry: { generateManifest(opts: { relativePath: string }): string }): MaybePromise<void>;
complete: (entry: {
generateManifest(opts: { relativePath: string }): string;
}) => MaybePromise<void>;
}

// Based on https://github.com/josh-hemphill/csp-typed-directives/blob/latest/src/csp.types.ts
Expand Down
Loading
Loading