Agent Guidelines

This document contains my coding guidelines and best practices for software projects. It is meant to be general purpose and should be a starting point. If any guidelines here conflict with a project's specific guidelines, then the project's specific guidelines should take precedence.


Baseline Principles

Security

Write secure code.

Javascript

Coding conventions


Frontend Development Guidelines

Project Structure

head component example

<!-- frontend/_components/page-head.webc -->
<head webc:is="fw-head" webc:nokeep>
  <meta charset="UTF-8" />
  <title @text="title"></title>
  <link rel="icon" href="/logo.svg" />
  <link rel="apple-touch-icon" href="/logo-180.png" />
  <meta
    name="viewport"
    content="width=device-width, initial-scale=1, viewport-fit=cover"
  />
  <meta name="description" :content="description" />
  <link rel="manifest" href="/app.webmanifest" />

  <!-- Control the behavior of search engine crawling and indexing -->
  <!-- All Search Engines -->
  <meta name="robots" content="index,follow" />

  <!-- Helps prevent duplicate content issues -->
  <link rel="canonical" :href="canonical" />

  <!-- Facebook Open Graph meta -->
  <meta property="og:url" :content="canonical" />
  <meta property="og:type" content="website" />
  <meta property="og:title" :content="title" />
  <meta property="og:image" :content="og_image" />
  <meta property="og:description" :content="description" />
  <meta property="og:site_name" :content="title" />
  <meta property="og:locale" content="en_US" />

  <script
    src="https://cdn.usefathom.com/script.js"
    defer
    :data-site="fathom_site_id"
    webc:keep
  ></script>

  <script type="module">
    navigator.serviceWorker.register("/sw.js");
  </script>
</head>
// frontend/_data/site.js
const url = "https://app-url-here.com";

export default {
  title: "My App",
  description: "My App description here",
  url,
  canonicalUrl: `${url}/`,
  copyrightName: "My App",
  isProdEnv: process.env.ELEVENTY_ENV === "production",
  fathom_site_id: "my-fathom-site-id-here",
  currentYear: new Date().getFullYear(),
  build_time: new Date().toISOString(),
};
<!-- frontend/includes/layout.webc -->
<html lang="en">
  <head>
    <meta
      webc:is="page-head"
      :title="page.title || site.title"
      :description="page.description || site.description"
      :fathom_site_id="site.fathom_site_id"
      :canonical="site.url + page.url"
    />

    <!-- inline the webc component styles -->
    <style @raw="getBundle('css')" webc:keep></style>
    <!-- inline the webc component js -->
    <script type="module" @raw="getBundle('js')" webc:keep></script>

    <!-- other stuff -->
  </head>
</html>

Hosting

We will use cloudflare workers for hosting. The frontend will be static assets served from cloudflare workers, and the backend will be serverless functions served from cloudflare workers.

Web development

Dependencies

Data Flow Guidelines


Typescript Best Practices

General

/** @import { User } from '../../models/User' */

Lit components

static properties = {
	page_name: { type: String },
    stuff: {type: Array<string>},
    some_state {type: String, state: true}
};

/** @type {string} */
page_name = 'new/edit';
/** @type {string[]} */
stuff = [];
/** @type {string} */
some_state = 'initial';
export class MyThing extends LitElement {
  // ...
}

if (!customElements.get("my-thing")) {
  customElements.define("my-thing", MyThing);
}

Private Class Members

Async/Await Patterns

Type Assertions and Transformations


Testing Guidelines

UI Testing

Non-UI Testing

This applies to both non-UI client-side code and backend code. This includes unit/functional tests and scenario-based tests.

Example

If there is a file sum.js with the following code:

/**
 * @param {number} a
 * @param {number} b
 * @returns {number}
 */
export function sum(a, b) {
  return a + b;
}

Then we want our test next to in the same directory, named sum.test.js with the following code:

import { test } from "node:test";
import assert from "node:assert";
import { sum } from "./sum.js";

test("sum", () => {
  assert.strictEqual(sum(1, 2), 3);
});

Data Dependencies

Test Fixtures

Best Practices


Code Comments Guidelines

General

When to Add Comments

When NOT to Add Comments

Comment Quality Standards


Code Style

Style & Naming Conventions

Function Structure


Eleventy Project Guidelines

Only apply these guidelines to projects that are built with Eleventy.

Webc quirks

Example of accessing props in the webc:setup script

<script webc:setup>
  // @ts-check

  /** @typedef Props
   * @prop {string} name The name of the setting from settings.js
   */

  /**
   * @param {Props} props
   */
  function _(props) {
    // @ts-ignore - settings is available at build time
    const setting = settings[props.name];
    if (!setting) {
      throw new Error(`Setting "${props.name}" not found in settings.js`);
    }

    return {
      storage_key: setting.storage_key,
      label: setting.label,
      default_value: setting.default_value,
    };
  }
</script>

<fw-setting-toggle
  :data-storage-key="_(this).storage_key"
  :data-default-value="_(this).default_value"
  :data-label="_(this).label"
>
  <label>
    <input
      type="checkbox"
      :data-storage-key="_(this).storage_key"
      :checked="_(this).default_value"
    />
    <span id="label" @text="_(this).label"></span>
  </label>
</fw-setting-toggle>

Pull Request Guidelines

Title

Commits