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([]);
});
});