diff --git a/web/eslint.config.js b/web/eslint.config.js index 299de69b3..97f84c35e 100644 --- a/web/eslint.config.js +++ b/web/eslint.config.js @@ -7,6 +7,7 @@ import svelte from "eslint-plugin-svelte"; import { defineConfig, includeIgnoreFile } from "eslint/config"; import globals from "globals"; import ts from "typescript-eslint"; +import { staleAwait } from "./scripts/staleAwait.ts"; const gitignorePath = path.resolve(import.meta.dirname, ".gitignore"); @@ -93,6 +94,11 @@ export default defineConfig( "no-restricted-imports": ["error", { patterns: [fixtures] }] } }, + { + files: ["src/routes/**/*.svelte"], + ignores: ["**/*.stories.svelte"], + rules: { "no-restricted-syntax": ["error", ...staleAwait] } + }, { files: ["src/routes/**"], ignores: ["**/*.stories.svelte", "**/*.test.ts"], diff --git a/web/scripts/staleAwait.test.ts b/web/scripts/staleAwait.test.ts new file mode 100644 index 000000000..d30ab90f2 --- /dev/null +++ b/web/scripts/staleAwait.test.ts @@ -0,0 +1,44 @@ +import { Linter } from "eslint"; +import svelte from "eslint-plugin-svelte"; +import { describe, expect, it } from "vitest"; +import { staleAwait } from "./staleAwait.ts"; + +const linter = new Linter(); +const config = [ + ...svelte.configs.base, + { files: ["**/*.svelte"], rules: { "no-restricted-syntax": ["error" as const, ...staleAwait] } } +]; + +const flagged = (markup: string) => + linter + .verify(`\n${markup}`, config, "+page.svelte") + .map((message) => message.line - 1); + +describe("stale await", () => { + it("flags the issue list before 32879b21f3", () => + expect( + flagged(`{#await data.issues} + +{:then issues} + +{/await}`) + ).toEqual([3])); + + it("flags a then branch that reads page", () => + expect(flagged("{#await data.x then x}{page.url.search}{/await}")).toEqual([1])); + it("flags a pending or catch branch that reads data", () => { + expect(flagged("{#await data.x}{data.q}{:then x}ok{/await}")).toEqual([1]); + expect(flagged("{#await data.x}wait{:then x}ok{:catch e}{data.q}{/await}")).toEqual([1]); + }); + it("flags one block once, however often it reads", () => + expect(flagged("{#await data.x then x}{data.a}{data.b}{/await}")).toHaveLength(1)); + + it("leaves a branch that only reads the settled value", () => + expect(flagged("{#await data.x}wait{:then x}{x.length}{/await}")).toEqual([])); + it("leaves the expression alone, it runs with the navigation", () => + expect(flagged("{#await data.x then x}{x.length}{/await}")).toEqual([])); + it("leaves a property or key that happens to be called data", () => { + expect(flagged("{#await load() then x}{x.data}{/await}")).toEqual([]); + expect(flagged("{#await load() then x}{JSON.stringify({ data: x })}{/await}")).toEqual([]); + }); +}); diff --git a/web/scripts/staleAwait.ts b/web/scripts/staleAwait.ts new file mode 100644 index 000000000..25c7d62fc --- /dev/null +++ b/web/scripts/staleAwait.ts @@ -0,0 +1,9 @@ +// a settled promise runs {:then} in its own batch, which sees the old data and page while a navigation lands +const readsRouteState = + "Identifier[name=/^(data|page)$/]:not(MemberExpression > Identifier.property, Property > Identifier.key)"; + +export const staleAwait = ["Pending", "Then", "Catch"].map((branch) => ({ + selector: `SvelteAwait${branch}Block:has(${readsRouteState})`, + message: + "{#await} branches can run before a navigation lands, seeing the old data and page. await in a instead: {@const value = await ...}." +}));