Skip to content
Merged
103 changes: 103 additions & 0 deletions fixtures/init/astro-site/eslint.preset.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,109 @@
}
}
},
{
"files": [
"**/*.astro"
],
"plugins": [],
"parserOptions": {
"projectService": false,
"project": "./tsconfig.json",
"tsconfigRootDir": "<project>",
"extraFileExtensions": [
".astro"
]
}
},
{
"files": [
"**/*.astro/*.ts",
"**/*.astro/*.js"
],
"plugins": [],
"rules": {
"@typescript-eslint/await-thenable": "off",
"@typescript-eslint/consistent-return": "off",
"@typescript-eslint/consistent-type-exports": "off",
"@typescript-eslint/dot-notation": "off",
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/no-array-delete": "off",
"@typescript-eslint/no-base-to-string": "off",
"@typescript-eslint/no-confusing-void-expression": "off",
"@typescript-eslint/no-deprecated": "off",
"@typescript-eslint/no-duplicate-type-constituents": "off",
"@typescript-eslint/no-floating-promises": "off",
"@typescript-eslint/no-for-in-array": "off",
"@typescript-eslint/no-generated-empty-object-type": "off",
"@typescript-eslint/no-implied-eval": "off",
"@typescript-eslint/no-meaningless-void-operator": "off",
"@typescript-eslint/no-misused-promises": "off",
"@typescript-eslint/no-misused-spread": "off",
"@typescript-eslint/no-mixed-enums": "off",
"@typescript-eslint/no-redundant-type-constituents": "off",
"@typescript-eslint/no-unnecessary-boolean-literal-compare": "off",
"@typescript-eslint/no-unnecessary-condition": "off",
"@typescript-eslint/no-unnecessary-qualifier": "off",
"@typescript-eslint/no-unnecessary-template-expression": "off",
"@typescript-eslint/no-unnecessary-type-arguments": "off",
"@typescript-eslint/no-unnecessary-type-assertion": "off",
"@typescript-eslint/no-unnecessary-type-conversion": "off",
"@typescript-eslint/no-unnecessary-type-parameters": "off",
"@typescript-eslint/no-unsafe-argument": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-class-instance-assignment": "off",
"@typescript-eslint/no-unsafe-enum-assignment": "off",
"@typescript-eslint/no-unsafe-enum-comparison": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/no-unsafe-type-assertion": "off",
"@typescript-eslint/no-unsafe-unary-minus": "off",
"@typescript-eslint/no-useless-default-assignment": "off",
"@typescript-eslint/non-nullable-type-assertion-style": "off",
"@typescript-eslint/only-throw-error": "off",
"@typescript-eslint/prefer-destructuring": "off",
"@typescript-eslint/prefer-find": "off",
"@typescript-eslint/prefer-includes": "off",
"@typescript-eslint/prefer-nullish-coalescing": "off",
"@typescript-eslint/prefer-optional-chain": "off",
"@typescript-eslint/prefer-promise-reject-errors": "off",
"@typescript-eslint/prefer-readonly": "off",
"@typescript-eslint/prefer-readonly-parameter-types": "off",
"@typescript-eslint/prefer-reduce-type-parameter": "off",
"@typescript-eslint/prefer-regexp-exec": "off",
"@typescript-eslint/prefer-return-this-type": "off",
"@typescript-eslint/prefer-string-starts-ends-with": "off",
"@typescript-eslint/promise-function-async": "off",
"@typescript-eslint/related-getter-setter-pairs": "off",
"@typescript-eslint/require-array-sort-compare": "off",
"@typescript-eslint/require-await": "off",
"@typescript-eslint/restrict-plus-operands": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/return-await": "off",
"@typescript-eslint/strict-boolean-expressions": "off",
"@typescript-eslint/strict-void-return": "off",
"@typescript-eslint/switch-exhaustiveness-check": "off",
"@typescript-eslint/unbound-method": "off",
"@typescript-eslint/use-unknown-in-catch-callback-variable": "off"
},
"parserOptions": {
"program": null,
"project": false,
"projectService": false
}
},
{
"files": [
"**/*.astro/*.ts",
"**/*.astro/*.js"
],
"plugins": [],
"parserOptions": {
"projectService": false,
"project": null
}
},
{
"plugins": [
"webtops"
Expand Down
40 changes: 40 additions & 0 deletions packages/eslint-plugin-webtops/src/ack.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, expect, it } from "vitest";
import { ackReason } from "./ack.ts";

describe("ackReason", () => {
it("gives the reason, trimmed", () => {
expect(ackReason(" webtops-ack W-28: the cart must work at once ", "W-28")).toBe(
"the cart must work at once",
);
});

it("takes a reason over several lines", () => {
expect(ackReason("\n webtops-ack W-28: the cart\n must work\n", "W-28")).toBe(
"the cart\n must work",
);
});

it("needs no space after the colon, and takes more than one before the law", () => {
expect(ackReason("webtops-ack W-28:reason", "W-28")).toBe("reason");
expect(ackReason("webtops-ack W-28: reason", "W-28")).toBe("reason");
});

it.each([
["no reason", "webtops-ack W-28:"],
["a blank reason", "webtops-ack W-28: "],
["another law", "webtops-ack W-30: reason"],
["a longer law number", "webtops-ack W-280: reason"],
["no colon", "webtops-ack W-28 reason"],
["no space before the law", "webtops-ackW-28: reason"],
["text before it", "see webtops-ack W-28: reason"],
["another marker", "ack W-28: reason"],
["a plain comment", "the cart"],
])("is nothing for %s", (_name, text) => {
expect(ackReason(text, "W-28")).toBeUndefined();
});

it("is nothing for a law that is not the one asked for", () => {
expect(ackReason("webtops-ack W-28: reason", "W-2")).toBeUndefined();
expect(ackReason("webtops-ack W-28: reason", "W-30")).toBeUndefined();
});
});
10 changes: 10 additions & 0 deletions packages/eslint-plugin-webtops/src/ack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* The acknowledgement a soft law (severity A) accepts written in the source: `webtops-ack W-NN: reason`,
* with a reason. The ledger form lives in `.web-tops/` and is read by the laws layer (milestone M3, G8):
* this is the one place the plugin reads an acknowledgement, so G8 widens it here.
*/
/** The reason a comment's text gives for acknowledging the law, or undefined when it does not acknowledge it. */
export function ackReason(text: string, law: string): string | undefined {
const match = /^\s*webtops-ack\s+(W-\d+):\s*(\S[\s\S]*?)\s*$/u.exec(text);
return match?.[1] === law ? match[2] : undefined;
}
8 changes: 5 additions & 3 deletions packages/eslint-plugin-webtops/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ function need<T>(value: T | undefined): T {
return value;
}

const RULES = ["complete-tailwind-classes", "no-unsanitized-html", "public-env-only"];
const JSX_RULES = ["complete-tailwind-classes", "no-unsanitized-html", "public-env-only"];
const ASTRO_RULES = ["astro-assets-image", "astro-client-load-ack", "astro-frontmatter-size"];
const RULES = [...ASTRO_RULES, ...JSX_RULES];

describe("G6a: eslint-plugin-webtops", () => {
it("names itself and holds the three rules of W-23, W-36 and W-37", () => {
it("names itself and holds the rules of W-23, W-28, W-30, W-31, W-36 and W-37", () => {
expect(plugin.meta).toEqual({ name: "eslint-plugin-webtops" });
expect(Object.keys(plugin.rules ?? {}).sort()).toEqual(RULES);
});
Expand Down Expand Up @@ -63,7 +65,7 @@ describe("G6a: eslint-plugin-webtops", () => {
].join("\n");
const [result] = await eslint.lintText(code, { filePath: "a.jsx" });
expect(result?.messages.map((message) => message.ruleId).sort()).toEqual(
RULES.map((name) => `webtops/${name}`),
JSX_RULES.map((name) => `webtops/${name}`),
);
});

Expand Down
8 changes: 7 additions & 1 deletion packages/eslint-plugin-webtops/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import type { ESLint } from "eslint";
import astroAssetsImage from "./rules/astro-assets-image.ts";
import astroClientLoadAck from "./rules/astro-client-load-ack.ts";
import astroFrontmatterSize from "./rules/astro-frontmatter-size.ts";
import completeTailwindClasses from "./rules/complete-tailwind-classes.ts";
import noUnsanitizedHtml from "./rules/no-unsanitized-html.ts";
import publicEnvOnly from "./rules/public-env-only.ts";

/** The rules behind Web-TOPS laws W-23, W-36 and W-37, under the `webtops` namespace. */
/** The rules behind Web-TOPS laws W-23, W-28, W-30, W-31, W-36 and W-37, under the `webtops` namespace. */
const plugin: ESLint.Plugin = {
get meta() {
return { name: "eslint-plugin-webtops" };
},
get rules() {
return {
"astro-assets-image": astroAssetsImage,
"astro-client-load-ack": astroClientLoadAck,
"astro-frontmatter-size": astroFrontmatterSize,
"complete-tailwind-classes": completeTailwindClasses,
"no-unsanitized-html": noUnsanitizedHtml,
"public-env-only": publicEnvOnly,
Expand Down
143 changes: 143 additions & 0 deletions packages/eslint-plugin-webtops/src/rules/astro-assets-image.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { RuleTester } from "eslint";
import type { Linter } from "eslint";
import astro from "eslint-plugin-astro";
import { describe, expect, it } from "vitest";
import rule from "./astro-assets-image.ts";

RuleTester.describe = describe;
RuleTester.it = it;

/** The language options eslint-plugin-astro gives `.astro` files: the Astro parser, with TypeScript in the frontmatter. */
function astroOptions(): Linter.LanguageOptions {
const entry = astro.configs["flat/base"].find((config) => config.name === "astro/base");
if (entry?.languageOptions === undefined) throw new Error("no astro/base config");
return entry.languageOptions;
}

const tester = new RuleTester({ languageOptions: astroOptions() });

const IMPORT = 'import { Image, Picture } from "astro:assets";';

const page = (template: string, imports = IMPORT): { code: string; filename: string } => ({
code: `---\n${imports}\nimport hero from "../hero.png";\n---\n${template}\n`,
filename: "page.astro",
});

const raw = [{ messageId: "raw" as const }];
const lacking = (name: string) => [{ messageId: "dimensions" as const, data: { name } }];

tester.run("astro-assets-image", rule, {
valid: [
{
name: "an Image with dimensions",
...page('<Image src={hero} width={800} height={600} alt="" />'),
},
{
name: "a Picture with dimensions",
...page('<Picture src={hero} width="800" height="600" alt="" />'),
},
{
name: "an Image whose size Astro reads from the file",
...page('<Image src="https://a.test/a.png" inferSize alt="" />'),
},
{
name: "an Image with a spread that may carry the dimensions",
...page("<Image {...props} />"),
},
{
name: "a spread beside other attributes",
...page('<Image src={hero} {...props} alt="" />'),
},
{
name: "a named Image from another module",
...page('<Image src="a" />', 'import { Image } from "../components";'),
},
{
name: "a member element is not read as an Image",
...page('<assets.Image src="a" />', 'import * as assets from "astro:assets";'),
},
{
name: "an aliased import",
...page(
'<Img src={hero} width={1} height={2} alt="" />',
'import { Image as Img } from "astro:assets";',
),
},
{
name: "an Image from another module is not the astro:assets one",
...page('<Image src="a" />', 'import Image from "../Image.astro";'),
},
{
name: "an Image that is not imported",
...page('<Image src="a" />', ""),
},
{
name: "another member of astro:assets",
...page('<Foo src="a" />', 'import { getImage as Foo } from "astro:assets";'),
},
{ name: "an ordinary element", ...page("<div><picture /><p>text</p></div>") },
{
name: "a default import named Image",
...page('<Image src="a" />', 'import Image from "astro:assets";'),
},
],
invalid: [
{ name: "an img", ...page('<img src="a.png" alt="" />'), errors: raw },
{
name: "an img with width and height",
...page('<img src="a.png" width="1" height="2" />'),
errors: raw,
},
{ name: "an img with an expression source", ...page("<img src={hero.src} />"), errors: raw },
{
name: "two img elements, in a parent",
...page('<div><img src="a" /><img src="b" /></div>'),
errors: [...raw, ...raw],
},
{
name: "an Image with no dimensions",
...page('<Image src={hero} alt="" />'),
errors: lacking("Image"),
},
{
name: "an Image with a width only",
...page('<Image src={hero} width={1} alt="" />'),
errors: lacking("Image"),
},
{
name: "an Image with a height only",
...page('<Image src={hero} height={1} alt="" />'),
errors: lacking("Image"),
},
{
name: "a Picture with no dimensions",
...page('<Picture src={hero} alt="" />'),
errors: lacking("Picture"),
},
{
name: "an aliased Image with no dimensions, named by its tag",
...page('<Img src={hero} alt="" />', 'import { Image as Img } from "astro:assets";'),
errors: lacking("Img"),
},
{
name: "an Image imported with a second one",
...page("<Picture src={hero} />", 'import { Picture, Image } from "astro:assets";'),
errors: lacking("Picture"),
},
],
});

describe("W-30's rule", () => {
it("says which law it judges", () => {
expect(rule.meta).toEqual({
type: "suggestion",
docs: { description: "Images go through astro:assets with dimensions (W-30)" },
schema: [],
messages: {
raw: "An <img> is not optimized. Use <Image> or <Picture> from astro:assets, with width and height.",
dimensions:
"<{{ name }}> needs width and height (or inferSize), so the layout does not shift.",
},
});
});
});
Loading
Loading