Skip to main content
← Blog
Guide3 min read

Cursor Rules for Code Quality: A Copyable .mdc File and a Gate That Enforces It

A copyable .cursor/rules file for code quality, and an afterFileEdit hook that scans every file Cursor's agent edits.

This post gives you a copyable Cursor rule for code quality and a hook that checks every edit Cursor’s agent makes. With no quality rules in context, the agent writes the most common version of the code, which is where AI slop comes from.

Use an always-on project rule

Project rules live in .cursor/rules/ as .mdc files. Each has frontmatter that controls when it is attached:

  • alwaysApply: true attaches it to every agent request.
  • globs attaches it when matching files are in play, for example src/**/*.ts.
  • description lets the agent decide to pull it in when relevant.

A quality rule should apply to every change, so use alwaysApply, and keep it short, because an always-on rule spends context on every request.

A copyable code quality rule

Save as .cursor/rules/code-quality.mdc:

---
description: Code quality standard for every change
alwaysApply: true
---

# Code quality

## Scope
- Change only what the task needs. No drive-by refactors or renames.
- Before adding a helper, search for an existing one.
- Delete code you replaced. No leftover helpers, flags, or branches
from an earlier attempt.

## Errors
- Handle an error or propagate it. Never catch and ignore.
- No catch blocks that only log and continue unless the caller
genuinely does not care, and say why in a comment.

## Types
- No "as any", "as unknown as", or non-null assertions to silence
the compiler. Fix the type.

## Comments
- Comment only why, a non-obvious constraint, or a public API contract.
- Never narrate what the code does or describe the edit.
- No commented-out code.

## Size
- Keep functions under about 80 lines and files under about 400.
Split by responsibility, not by line count.

## Done means
- Run "npx aislop scan --changes" and fix what it reports
before saying the task is complete.

The “Done means” section matters most: it gives the agent a check it can run itself. Tune the size limits, which come from what we see in agent-written code, to your codebase. For a tool-neutral version that also works in AGENTS.md, see the AI coding standards rules file.

Check every edit with a Cursor hook

A rule is guidance; the hook is the enforcement. In a long session the rule competes with the task and the open files, so the agent will still sometimes swallow an error to make a test pass or leave an earlier attempt’s helper behind (see AGENTS.md is not enough).

npx aislop hook install --cursor

This adds an afterFileEdit hook to ~/.cursor/hooks.json. aislop then scans every file the agent edits and hands back the score and findings with file and line, so the agent can fix them before it moves on.

  • --project scopes the install to this repository and adds .cursor/rules/aislop.mdc, so the rule and the check ship together in git.
  • --dry-run previews the changes; npx aislop hook status shows what is installed.

Details and the feedback format: agent hooks docs.

Clean up what is already there

npx aislop scan
npx aislop fix
npx aislop fix --cursor

Scan for a baseline, run aislop fix for the safe mechanical fixes, then aislop fix --cursor to open Cursor’s agent with the remaining findings as its prompt. Add the same check to CI with the two-minute quality gate so changes made without the hook are checked before merge.

The same setup for other agents: Claude Code and Codex.

Frequently asked questions

Where do Cursor rules go?

In .cursor/rules/ as .mdc files, with frontmatter that sets when each rule applies: always, for matching file globs, or when the agent judges it relevant. Some projects still use the older single .cursorrules file, but .cursor/rules is the current format.

What should a Cursor rule for code quality include?

Concrete, checkable instructions: handle or propagate errors, no type escape hatches, no narrative comments, no dead code, small functions, and changes scoped to the task. Short rules with examples beat long style essays.

Do Cursor rules guarantee the agent follows them?

No. Rules shape the model's output but do not enforce it, so pair them with a check on every edit and in CI.

How do I make Cursor check code quality on every edit?

Run npx aislop hook install --cursor, which adds an afterFileEdit hook to ~/.cursor/hooks.json so aislop scans every file the agent edits. With --project it also writes .cursor/rules/aislop.mdc to the repository.

Can Cursor fix aislop findings automatically?

Yes. aislop fix applies the mechanical fixes, and aislop fix --cursor opens Cursor's agent with every remaining finding, file and line as the prompt.

All posts