Skip to content
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

chore: validate promise handling #11842

Merged
merged 10 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import lube from 'eslint-plugin-lube';
export default [
...svelte_config,
{
languageOptions: {
parserOptions: {
project: true
}
},
plugins: {
lube
},
rules: {
'@typescript-eslint/await-thenable': 'error',
'@typescript-eslint/no-floating-promises': 'error',
'@typescript-eslint/no-misused-promises': 'error',
'@typescript-eslint/require-await': 'error',
'no-console': 'error',
'lube/svelte-naming-convention': ['error', { fixSameNames: true }],
// eslint isn't that well-versed with JSDoc to know that `foo: /** @type{..} */ (foo)` isn't a violation of this rule, so turn it off
Expand Down Expand Up @@ -43,7 +52,6 @@ export default [
'documentation',
// contains a fork of the REPL which doesn't adhere to eslint rules
'sites/svelte-5-preview/**',
'playgrounds/demo/src/**',
'tmp/**',
// wasn't checked previously, reenable at some point
'sites/svelte.dev/**'
Expand Down
19 changes: 12 additions & 7 deletions packages/svelte/src/internal/client/dom/elements/custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,18 @@ if (typeof HTMLElement === 'function') {
disconnectedCallback() {
this.$$cn = false;
// In a microtask, because this could be a move within the DOM
Promise.resolve().then(() => {
if (!this.$$cn && this.$$c) {
this.$$c.$destroy();
destroy_effect(this.$$me);
this.$$c = undefined;
}
});
Promise.resolve()
.then(() => {
if (!this.$$cn && this.$$c) {
this.$$c.$destroy();
destroy_effect(this.$$me);
this.$$c = undefined;
}
})
.catch((err) => {
// eslint-disable-next-line no-console
console.error(err);
});
}

/**
Expand Down
19 changes: 12 additions & 7 deletions packages/svelte/src/internal/client/dom/elements/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,19 @@ export function add_form_reset_listener() {
(evt) => {
// Needs to happen one tick later or else the dom properties of the form
// elements have not updated to their reset values yet
Promise.resolve().then(() => {
if (!evt.defaultPrevented) {
for (const e of /**@type {HTMLFormElement} */ (evt.target).elements) {
// @ts-expect-error
e.__on_r?.();
Promise.resolve()
.then(() => {
if (!evt.defaultPrevented) {
for (const e of /**@type {HTMLFormElement} */ (evt.target).elements) {
// @ts-expect-error
e.__on_r?.();
}
}
}
});
})
.catch((err) => {
// eslint-disable-next-line no-console
console.error(err);
});
},
// In the capture phase to guarantee we get noticed of it (no possiblity of stopPropagation)
{ capture: true }
Expand Down
7 changes: 6 additions & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,12 @@ export function schedule_effect(signal) {
} else if (current_scheduler_mode === FLUSH_YIELD) {
if (!is_yield_task_queued) {
is_yield_task_queued = true;
yield_tick().then(process_deferred);
yield_tick()
.then(process_deferred)
.catch((err) => {
// eslint-disable-next-line no-console
console.error(err);
});
}
}

Expand Down
11 changes: 8 additions & 3 deletions packages/svelte/src/motion/spring.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,14 @@ export function spring(value, opts = {}) {
});
}
return new Promise((fulfil) => {
/** @type {import('../internal/client/types').Task} */ (task).promise.then(() => {
if (token === current_token) fulfil();
});
/** @type {import('../internal/client/types').Task} */ (task).promise
.then(() => {
if (token === current_token) fulfil();
})
.catch((err) => {
// eslint-disable-next-line no-console
console.error(err);
});
});
}
/** @type {import('./public.js').Spring<T>} */
Expand Down
1 change: 0 additions & 1 deletion playgrounds/demo/.prettierignore

This file was deleted.

4 changes: 1 addition & 3 deletions playgrounds/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
"ssr": "node ./server.js",
"build": "vite build --outDir dist/client && vite build --outDir dist/server --ssr src/entry-server.ts",
"prod": "npm run build && node dist",
"preview": "vite preview",
"format": "prettier --check --write .",
"lint": "prettier --check . && eslint"
"preview": "vite preview"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^3.1.0",
Expand Down
4 changes: 3 additions & 1 deletion playgrounds/sandbox/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ function mkdirp(dir) {
const svelte_modules = glob('**/*.svelte', { cwd: `${cwd}/input` });
const js_modules = glob('**/*.js', { cwd: `${cwd}/input` });

for (const generate of ['client', 'server']) {
/** @type {Array<'client'|'server'>} */
const compile_types = ['client', 'server'];
for (const generate of compile_types) {
console.error(`\n--- generating ${generate} ---\n`);
for (const file of svelte_modules) {
const input = `${cwd}/input/${file}`;
Expand Down
2 changes: 1 addition & 1 deletion playgrounds/sandbox/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"allowJs": true,
"checkJs": true
},
"include": ["./input"]
"include": ["./run.js", "./input"]
}
Loading