Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
import Widget from "./Widget.tsx";
---
<!-- expect: astro/missing-client-only-directive-value -->
<Widget client:only />
6 changes: 6 additions & 0 deletions fixtures/laws/W-49/bad/astro-no-conflict-set-directives.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
const html = "<b>a</b>";
const text = "a";
---
<!-- expect: astro/no-conflict-set-directives -->
<div set:html={html} set:text={text}></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---
<!-- expect: astro/no-deprecated-astro-canonicalurl -->
<link rel="canonical" href={Astro.canonicalURL} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
// expect: astro/no-deprecated-astro-fetchcontent
const posts = Astro.fetchContent("./posts/*.md");
---
<p>{posts.length}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
// expect: astro/no-deprecated-astro-resolve
const src = Astro.resolve("./a.png");
---
<img src={src} alt="a" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
// expect: astro/no-deprecated-getentrybyslug
import { getEntryBySlug } from "astro:content";
const entry = await getEntryBySlug("blog", "a");
---
<p>{entry}</p>
5 changes: 5 additions & 0 deletions fixtures/laws/W-49/bad/astro-no-exports-from-components.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
// expect: astro/no-exports-from-components
export const value = 1;
---
<p>{value}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
// expect: astro/no-prerender-export-outside-pages
export const prerender = true;
---
<p>x</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
---
<p>x</p>
<!-- expect: astro/no-unused-define-vars-in-style -->
<style define:vars={{ color: "red" }}>
p { margin: 0; }
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
import Widget from "./Widget.tsx";
---
<Widget client:only="react" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
const html = "<b>a</b>";
const text = "a";
---
<div set:text={text}></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
---
<link rel="canonical" href={Astro.url} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
const posts = Object.values(import.meta.glob("./posts/*.md"));
---
<p>{posts.length}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
const src = new URL("./a.png", Astro.url).href;
---
<img src={src} alt="a" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
import { getEntry } from "astro:content";
const entry = await getEntry("blog", "a");
---
<p>{entry}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
const value = 1;
---
<p>{value}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
const prerender = true;
void prerender;
---
<p>x</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
---
<p>x</p>
<style define:vars={{ color: "red" }}>
p { color: var(--color); }
</style>
61 changes: 61 additions & 0 deletions src/cli/laws-probe-astro.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { symlinkSync } from "node:fs";
import { join } from "node:path";
import { afterAll, describe, expect, it } from "vitest";
import { REAL_TOOL_TIMEOUT } from "../gate/test-support-timeouts.ts";
import { cleanTemp, put, tempDir } from "../laws/probe/testkit.ts";
import { failing, KIT, messages, probeFrom, probeProcess } from "./test-support-probe.ts";
import type { ProbeReport } from "../laws/probe/types.ts";

afterAll(cleanTemp);

/**
* An astro-site project that is not the kit: the kit's own ESLint preset for the astro variant, read from its
* source, and the kit's tools through a symlink. `off` is a rule its config turns off for `.astro` files.
*/
function astroSite(off?: string): string {
const dir = tempDir();
symlinkSync(join(KIT, "node_modules"), join(dir, "node_modules"));
const turned =
off === undefined ? "[]" : `[{ files: ["**/*.astro"], rules: { "${off}": "off" } }]`;
put(dir, {
"tsconfig.json": '{ "extends": "' + join(KIT, "tsconfig.json") + '" }\n',
"package.json": '{ "type": "module", "dependencies": { "astro": "5.0.0" } }\n',
"eslint.config.js": [
`import { eslintPreset } from "${join(KIT, "src/presets/eslint.js")}";`,
'const base = await eslintPreset({ variant: "astro", tsconfigRootDir: import.meta.dirname, class: "astro-site" });',
`export default [...base, ...${turned}];`,
"",
].join("\n"),
});
return dir;
}

function probe(dir: string, law: string): { code: number; report: ProbeReport } {
return probeFrom(probeProcess(dir, "--class", "astro-site", "--law", law, "--json"));
}

describe("G5 the Astro twins are proven under the astro preset, and each rule is the one its twin proves", () => {
it(
"W-22 and W-49 pass over their .astro and .tsx twins, none skipped",
() => {
const dir = astroSite();
const [w22, w49] = [probe(dir, "W-22"), probe(dir, "W-49")];
expect([w22.code, w49.code]).toEqual([0, 0]);
expect([failing(w22.report), failing(w49.report)]).toEqual([[], []]);
expect([w22.report.skipped, w49.report.skipped]).toEqual([[], []]);
},
REAL_TOOL_TIMEOUT,
);

it(
"a config that turns one Astro rule off fails the law red on that rule's twin, and on no other",
() => {
const result = probe(astroSite("astro/no-deprecated-astro-resolve"), "W-49");
expect(result.code).toBe(1);
expect(messages(result.report)).toEqual([
"W-49/bad/astro-no-deprecated-astro-resolve.astro:3 stayed quiet: astro/no-deprecated-astro-resolve did not fire on its bad twin",
]);
},
REAL_TOOL_TIMEOUT,
);
});
52 changes: 51 additions & 1 deletion src/cli/laws-probe-fake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const laws = (): readonly ProbeLaw[] => [

const passed: ProbeOutcome = {
ok: true,
report: { laws: [{ id: "W-99", verdict: "pass", failures: [], unrelated: [] }] },
report: { laws: [{ id: "W-99", verdict: "pass", failures: [], unrelated: [] }], skipped: [] },
};

const failed: ProbeOutcome = {
Expand All @@ -42,6 +42,7 @@ const failed: ProbeOutcome = {
unrelated: [],
},
],
skipped: [],
},
};

Expand Down Expand Up @@ -83,6 +84,7 @@ describe("G3b the command prints a probe's report and picks its exit code", () =
expect(result.code).toBe(0);
expect(JSON.parse(result.out)).toEqual({
laws: [{ id: "W-99", verdict: "pass", failures: [], unrelated: [] }],
skipped: [],
});
expect(result.out.endsWith("}\n")).toBe(true);
});
Expand Down Expand Up @@ -222,3 +224,51 @@ describe("G3b the command's own problems", () => {
expect(made.fixtures).toBe(join(KIT, "fixtures", "laws"));
});
});

describe("R105 the request says why Astro twins are skipped", () => {
const skipOf = async (
className: string,
files: Record<string, string> = {},
): Promise<unknown> => {
const cwd = tempDir();
put(cwd, files);
const { probe, requests } = given(passed);
await cli(["--class", className, "--law", "W-99"], cwd, { probe, laws });
return first(requests).astroSkip;
};
const MANIFEST = (field: string): Record<string, string> => ({
"package.json": JSON.stringify({ [field]: { astro: "^5.0.0" } }),
});

it("an astro-site lints Astro files, so nothing is skipped", async () => {
expect(await skipOf("astro-site")).toBeUndefined();
});

it("a ui-components and a ts-library project do not lint them, and the reason names the class", async () => {
expect(await skipOf("ui-components")).toBe(
"the ui-components preset does not lint .astro files",
);
expect(await skipOf("ts-library")).toBe("the ts-library preset does not lint .astro files");
});

it("a web-app lints them only when its package.json depends on astro, under any dependency field", async () => {
const why = "a web-app lints .astro files only when the project depends on astro";
expect(await skipOf("web-app")).toBe(why);
expect(await skipOf("web-app", { "package.json": "{}" })).toBe(why);
expect(await skipOf("web-app", { "package.json": "not json" })).toBe(why);
expect(await skipOf("web-app", MANIFEST("dependencies"))).toBeUndefined();
expect(await skipOf("web-app", MANIFEST("devDependencies"))).toBeUndefined();
expect(await skipOf("web-app", MANIFEST("peerDependencies"))).toBeUndefined();
expect(
await skipOf("web-app", { "package.json": JSON.stringify({ dependencies: ["astro"] }) }),
).toBe(why);
});

it("the class is the one web-tops.yaml names when no --class is given", async () => {
const cwd = tempDir();
put(cwd, { "web-tops.yaml": "class: astro-site\n" });
const { probe, requests } = given(passed);
await cli(["--law", "W-99"], cwd, { probe, laws });
expect(first(requests).astroSkip).toBeUndefined();
});
});
24 changes: 20 additions & 4 deletions src/cli/laws-probe-ui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import { REAL_TOOL_TIMEOUT } from "../gate/test-support-timeouts.ts";
import { lawsOfClass } from "../laws/probe/catalog.ts";
import { kitProbeLaws } from "../laws/probe/testkit.ts";
import { cleanTemp, put } from "../laws/probe/testkit.ts";
import { adopter, failing, messages, probeOf, verdicts } from "./test-support-probe.ts";
import {
adopter,
failing,
messages,
probeOf,
probeProcess,
verdicts,
} from "./test-support-probe.ts";

afterAll(cleanTemp);

const ALL = kitProbeLaws().map((law) => law.id);
const WEB_APP = lawsOfClass(kitProbeLaws(), "web-app").map((law) => law.id);
const TS_ONLY = lawsOfClass(kitProbeLaws(), "ts-library").map((law) => law.id);

/** Replaces one line of a config, as an adopter that did not enable a rule would have it. */
Expand All @@ -24,10 +31,19 @@ describe("G3c `laws probe` under a class with JSX and CSS: W-22 and W-26", () =>

it("proves W-22 and W-26 beside the laws every class maps, and every law passes", () => {
expect(ui.code).toBe(0);
expect(ui.report.laws.map((law) => law.id)).toEqual(ALL);
expect(ui.report.laws.map((law) => law.id)).toEqual(WEB_APP);
expect(failing(ui.report)).toEqual([]);
});

it("a law whose twins are all Astro is not probed for a web-app that does not depend on astro, and the exit says why", () => {
const run = probeProcess(adopter(undefined, true), "--class", "web-app", "--law", "W-49");
expect(run.code).toBe(2);
expect(run.err).toBe(
"webtops laws probe: every twin of the given laws was skipped: a web-app lints .astro files only when the project depends on astro\n",
);
expect(run.out).toBe("");
});

it(
"a ts-library probes neither, and a project's web-tops.yaml names its class",
async () => {
Expand All @@ -36,7 +52,7 @@ describe("G3c `laws probe` under a class with JSX and CSS: W-22 and W-26", () =>
expect(plain.report.laws.map((law) => law.id)).toEqual(TS_ONLY);
put(dir, { "web-tops.yaml": "class: web-app\n" });
const named = await probeOf(dir);
expect(named.report.laws.map((law) => law.id)).toEqual(ALL);
expect(named.report.laws.map((law) => law.id)).toEqual(WEB_APP);
},
REAL_TOOL_TIMEOUT,
);
Expand Down
39 changes: 37 additions & 2 deletions src/cli/laws-probe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,47 @@ const absolute = (cwd: string, path: string): string =>
const noLaws = (law: string | undefined): string =>
law === undefined ? "no law to probe" : `no law ${law} to probe`;

/** The one law asked for, or the laws the project's class maps: `--class`, else `web-tops.yaml`, else `ts-library`. */
/** The project's class: `--class`, else `web-tops.yaml`, else `ts-library`. */
const classOf = (io: Io, flags: Flags): string =>
flags.class ?? projectString(io.cwd, "class") ?? defaultClass();

/** The one law asked for, or the laws the project's class maps. */
function lawsFor(io: Io, deps: ProbeDeps, flags: Flags): ProbeLaw[] {
const laws = deps.laws();
if (flags.law !== undefined) {
return laws.filter((law) => law.id === flags.law);
}
return lawsOfClass(laws, flags.class ?? projectString(io.cwd, "class") ?? defaultClass());
return lawsOfClass(laws, classOf(io, flags));
}

/** Whether the project's `package.json` lists `astro` among its dependencies. */
function dependsOnAstro(cwd: string): boolean {
try {
const manifest: unknown = JSON.parse(readFileSync(join(cwd, "package.json"), "utf8"));
return ["dependencies", "devDependencies", "peerDependencies"].some((field) => {
const group = step(manifest, field);
return isRecord(group) && "astro" in group;
});
} catch {
return false;
}
}

/**
* Why `.astro` twins are left out, or undefined when the class's preset lints them: an astro-site always does, a
* web-app only when the project depends on astro, and every other class has no Astro files to lint.
*/
function astroSkip(io: Io, flags: Flags): string | undefined {
const className = classOf(io, flags);
if (className === "astro-site") {
return undefined;
}
if (className === "web-app") {
return dependsOnAstro(io.cwd)
? undefined
: "a web-app lints .astro files only when the project depends on astro";
}
return `the ${className} preset does not lint .astro files`;
}

/** The laws to probe, or what is wrong: the catalog cannot be read, or the flags choose no law. */
Expand Down Expand Up @@ -139,6 +173,7 @@ function requestOf(io: Io, deps: ProbeDeps, flags: Flags, laws: readonly ProbeLa
eslintConfig:
flags.config === undefined ? projectEslintConfig(io.cwd) : absolute(io.cwd, flags.config),
tsconfig: absolute(io.cwd, flags.tsconfig ?? "tsconfig.json"),
astroSkip: astroSkip(io, flags),
};
}

Expand Down
10 changes: 9 additions & 1 deletion src/laws/probe/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const ids = (className: string): string[] =>
lawsOfClass(kitProbeLaws(), className).map((law) => law.id);

describe("G3a the laws the probe proves are the catalog's ticked lint and compiler laws", () => {
it("reads the fourteen laws a lint rule or a flag judges, in order, from laws/W-NN.yaml", () => {
it("reads the fifteen laws a lint rule or a flag judges (judged v0.1 or m3), in order, from laws/W-NN.yaml", () => {
expect(kitProbeLaws().map((law) => law.id)).toEqual([
"W-01",
"W-02",
Expand All @@ -23,6 +23,7 @@ describe("G3a the laws the probe proves are the catalog's ticked lint and compil
"W-26",
"W-34",
"W-35",
"W-49",
]);
});

Expand All @@ -42,6 +43,13 @@ describe("G3a the laws the probe proves are the catalog's ticked lint and compil
expect(ids("web-app")).toContain("W-26");
});

it("W-49 is mapped by the astro-site class alone", () => {
expect(ids("astro-site")).toContain("W-49");
for (const className of ["ts-library", "ui-components", "web-app", "node-service"]) {
expect(ids(className)).not.toContain("W-49");
}
});

it("a class the catalog does not know maps nothing, and a law with no class list is mapped by every class", () => {
expect(ids("cli")).toEqual([]);
expect(lawsOfClass([], "ts-library")).toEqual([]);
Expand Down
4 changes: 3 additions & 1 deletion src/laws/probe/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ describe("G3a every law the probe proves has a bad and a clean twin, marked", ()
it("only a bad twin carries a marker, and each file is an ES module so the twins do not share globals", () => {
const withMarkers = TWINS.filter((twin) => parseMarkers(twin.file, text(twin.file)).length > 0);
expect(withMarkers.every((twin) => twin.kind === "bad")).toBe(true);
const sources = TWINS.filter((twin) => !twin.file.endsWith(".css"));
const sources = TWINS.filter(
(twin) => !twin.file.endsWith(".css") && !twin.file.endsWith(".astro"),
);
const scripts = sources.filter((twin) => !/^(import|export) /mu.test(text(twin.file)));
expect(scripts.map((twin) => twin.file)).toEqual([]);
});
Expand Down
Loading
Loading