Cotton code
90 lines · 4.0 KB · TypeScriptRaw
/** * Shared ESLint rules for the repo's surviving TypeScript. * * ONE consumer: `apps/cotton/eslint.config.js`, over the Mac package's * `scripts/` and `__tests__/` — plain Node TypeScript that builds around the * Swift app. It takes `restrictedSyntaxRules`, `typeSafetyRules`, * `deprecationRules` and `designSystemAllowlist`. * * Every selector below has to be able to FIRE on TypeScript that exists in * this tree. A rule whose pattern belongs to a runtime the repo no longer * carries is not a dormant safety net — it is a sentence that reads as policy * and can never be violated. Add one only with a call site it could catch. * * Rules are staged: *   - `error` rules: code already satisfies them; keep them enforced. *   - `warn` rules: code has violations; warning keeps them visible while *     migration happens. Graduate to `error` once count hits zero. */// Selectors enforced across the linted TypeScript.const RESTRICTED_SYNTAX = [  // Canonical method verbs. `get` (not fetch/load/list/find), `update` (not  // patch/edit/save), `delete` (not remove) — one verb per meaning, so a  // reader never has to learn which of three names a class chose.  {    selector:      "MethodDefinition[key.name=/^(fetch|load|list|find|patch|edit|save|remove)[A-Z]/]",    message:      "Forbidden method verb. Use the canonical one: get (not fetch/load/list/find), update (not patch/edit/save), delete (not remove).",  },  {    selector:      "PropertyDefinition[key.name=/^(fetch|load|list|find|patch|edit|save|remove)[A-Z]/]",    message:      "Forbidden method verb. Use the canonical one: get (not fetch/load/list/find), update (not patch/edit/save), delete (not remove).",  },  // Colour has ONE source. The Mac's icon forms are derived from the  // constellation-C master and its palette; the apps' colours are generated  // into Swift by `cotton-gen` (`crates/cotton-core/src/design/`). A hex or  // rgba literal in a build script is a second source that will drift from it.  {    selector: "Literal[value=/^#[0-9A-Fa-f]{3,8}$/]",    message:      "Hex colour literals are not allowed here — colour has one source (the constellation-C master for the icon forms, `cotton-gen`'s design module for the apps). If this is deliberate, add `// eslint-disable-next-line no-restricted-syntax -- <reason>`.",  },  {    selector: "Literal[value=/^rgba?\\(/]",    message:      "rgb()/rgba() literals are not allowed here — colour has one source (the constellation-C master for the icon forms, `cotton-gen`'s design module for the apps). If this is deliberate, add `// eslint-disable-next-line no-restricted-syntax -- <reason>`.",  },];// Files allowed to carry raw colour literals: a fixture may need one, which is// the decision every package in the repo has already made. Patterns are// relative to each ESLint invocation's cwd, and `**/foo` matches at any depth.const DESIGN_SYSTEM_ALLOWLIST = [  {    files: ["**/__tests__/**"],    rules: {      "no-restricted-syntax": "off",    },  },];module.exports = {  // The restricted-syntax set. Spread into a consuming flat config's `rules`.  restrictedSyntaxRules: {    "no-restricted-syntax": ["error", ...RESTRICTED_SYNTAX],  },  // Spread AFTER the rules above so the file-pattern override wins.  designSystemAllowlist: DESIGN_SYSTEM_ALLOWLIST,  // `any` is banned. Kept as its own export because the rule needs the  // `@typescript-eslint` plugin registered in the consuming config.  //  // A genuinely unavoidable escape (a third-party callback signature, a  // structural cast through an unmodelled shape) takes a targeted  // `// eslint-disable-next-line @typescript-eslint/no-explicit-any -- <reason>`  // so each one is a reviewed decision instead of a habit.  typeSafetyRules: {    "@typescript-eslint/no-explicit-any": "error",  },  // Flags use of `@deprecated` TS exports. Type-aware: the consuming config  // needs `parserOptions.projectService: true` (or `project`).  deprecationRules: {    "@typescript-eslint/no-deprecated": "error",  },};