Skip to main content
RapidDev - Software Development Agency
cursor-tutorial

How to Make Cursor Add Code Comments

Make Cursor add JSDoc comments automatically by creating .cursorrules that require documentation on all exported functions, using Cmd+K to add comments to existing code inline, and leveraging Composer to document entire files at once. Cursor generates accurate JSDoc when it can see the function's TypeScript types and implementation.

What you'll learn

  • How to create .cursorrules that require JSDoc on all exported functions
  • How to use Cmd+K to add comments to existing code inline
  • How to document entire files at once with Composer
  • How to set up auto-attaching rules for documentation standards
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Beginner7 min read10 minCursor Free/Pro, TypeScript/JavaScriptMarch 2026RapidDev Engineering Team
TL;DR

Make Cursor add JSDoc comments automatically by creating .cursorrules that require documentation on all exported functions, using Cmd+K to add comments to existing code inline, and leveraging Composer to document entire files at once. Cursor generates accurate JSDoc when it can see the function's TypeScript types and implementation.

Automating Code Documentation with Cursor

Cursor can generate accurate JSDoc comments that describe parameters, return types, exceptions, and usage examples. The key is giving it clear documentation standards through rules and referencing the actual code with @file so it produces comments that match the implementation. This tutorial covers inline commenting with Cmd+K, bulk documentation with Composer, and rules that make documentation a default behavior.

Prerequisites

  • Cursor installed (Free or Pro)
  • A TypeScript or JavaScript project with exported functions
  • Basic understanding of JSDoc comment syntax

Step-by-step guide

1

Add documentation rules to .cursorrules

Create rules that define your documentation standards. Specify which elements need JSDoc, what tags to include, and the level of detail expected. Cursor follows these rules when generating new code and when adding comments to existing code.

.cursorrules
1# .cursorrules
2
3## Documentation Rules
4- ALL exported functions must have JSDoc comments
5- Include: @param (with type and description), @returns, @throws, @example
6- Internal/private functions: brief single-line comment only
7- React components: document props interface, not the component function
8- Use @see for references to related functions
9- Keep descriptions concise: 1-2 sentences max
10- Do not document self-evident getters/setters
11- Examples should be runnable TypeScript, not pseudocode

Expected result: Cursor includes JSDoc comments on all exported functions in generated code.

2

Add JSDoc to existing functions with Cmd+K

Select an existing function in your editor, press Cmd+K, and ask Cursor to add JSDoc. Cursor reads the function signature, parameter types, and implementation to generate accurate documentation. This is the fastest way to document individual functions.

Cmd+K inline prompt
1// Select a function in your editor, press Cmd+K and type:
2// Add a JSDoc comment to this function with:
3// @param tags for each parameter with type and description
4// @returns with type and description
5// @throws if any errors are thrown
6// @example with a realistic usage example

Pro tip: Select multiple functions at once (or the entire file with Cmd+A) and Cmd+K will add JSDoc to all of them in a single pass.

Expected result: The selected function gets a complete JSDoc comment block above it with all specified tags.

3

Document entire files with Composer

For bulk documentation, use Composer (Cmd+I) to add JSDoc to every exported function in a file. Reference the file with @file and specify your documentation standards. Composer handles multi-function files efficiently.

Cursor Composer prompt
1// Prompt to type in Cursor Composer (Cmd+I):
2// @src/utils/orderCalculator.ts
3// Add JSDoc comments to every exported function in this file.
4// For each function include:
5// - Brief description (1 sentence)
6// - @param for each parameter with type and purpose
7// - @returns with type and what it represents
8// - @throws if the function throws errors
9// - @example with realistic usage
10// Do NOT modify any function implementations — only add comments.

Pro tip: Add 'Do NOT modify any function implementations' to prevent Cursor from changing your code while adding documentation.

Expected result: Every exported function in the file gets complete JSDoc documentation without any code changes.

4

Create an auto-attaching documentation rule

Create a .cursor/rules/documentation.mdc that auto-attaches for all source files. This ensures documentation standards apply whenever Cursor generates or edits code, without needing to mention documentation in every prompt.

.cursor/rules/documentation.mdc
1---
2description: JSDoc documentation standards
3globs: "src/**/*.ts, src/**/*.tsx, lib/**/*.ts"
4alwaysApply: false
5---
6
7- Every exported function MUST have JSDoc with @param, @returns, @example
8- Every exported interface/type MUST have a description comment
9- Every exported class MUST have a class-level JSDoc
10- React components: document the Props interface, not the component
11- Use @see to reference related functions in the same module
12- Keep descriptions under 2 sentences
13- @example code must be valid TypeScript that compiles

Expected result: Documentation rules auto-attach for all TypeScript source files.

Complete working example

src/utils/orderCalculator.ts
1/**
2 * Calculates the total price for an order including all line items.
3 *
4 * @param order - The order containing items with prices and quantities
5 * @returns The total price in cents as an integer
6 * @throws {Error} If any item has a negative price
7 * @example
8 * ```typescript
9 * const total = calculateOrderTotal({
10 * items: [
11 * { name: 'Widget', priceCents: 1000, quantity: 2 },
12 * { name: 'Gadget', priceCents: 2500, quantity: 1 },
13 * ],
14 * });
15 * console.log(total); // 4500
16 * ```
17 */
18export function calculateOrderTotal(order: Order): number {
19 return order.items.reduce((sum, item) => {
20 if (item.priceCents < 0) {
21 throw new Error(`Invalid price for item: ${item.name}`);
22 }
23 return sum + item.priceCents * item.quantity;
24 }, 0);
25}
26
27/**
28 * Applies a discount to a subtotal amount.
29 *
30 * @param subtotalCents - The original amount in cents
31 * @param discount - The discount to apply (percentage or fixed amount)
32 * @returns The discounted amount in cents, minimum 0
33 * @example
34 * ```typescript
35 * const discounted = applyDiscount(10000, { type: 'percent', value: 10 });
36 * console.log(discounted); // 9000
37 * ```
38 * @see calculateOrderTotal
39 */
40export function applyDiscount(
41 subtotalCents: number,
42 discount: Discount
43): number {
44 const reduction =
45 discount.type === 'percent'
46 ? Math.floor(subtotalCents * (Math.min(discount.value, 100) / 100))
47 : discount.value;
48
49 return Math.max(0, subtotalCents - reduction);
50}
51
52/** Order containing line items for price calculation. */
53export interface Order {
54 items: OrderItem[];
55}
56
57/** A single line item in an order. */
58export interface OrderItem {
59 name: string;
60 priceCents: number;
61 quantity: number;
62}
63
64/** Discount that can be applied to an order subtotal. */
65export interface Discount {
66 type: 'percent' | 'fixed';
67 value: number;
68}

Common mistakes when making Cursor Add Code Comments

Why it's a problem: Asking Cursor to add comments without specifying which tags to include

How to avoid: Always specify required tags: @param, @returns, @throws, @example in your prompt or .cursorrules.

Why it's a problem: Not preventing code modifications during documentation

How to avoid: Include 'Do NOT modify any function implementations — only add comments' in your documentation prompt.

Why it's a problem: Documenting every function including trivial getters

How to avoid: Add a rule: 'Do not document self-evident getters, setters, or single-line utility functions.'

Best practices

  • Create .cursorrules requiring JSDoc on all exported functions with specific tag requirements
  • Use Cmd+K for quick inline documentation of individual functions
  • Use Composer for bulk documentation of entire files
  • Require @example tags with runnable TypeScript code for complex functions
  • Document interfaces and types alongside functions for complete API documentation
  • Add 'Do NOT modify implementations' to all documentation prompts
  • Use auto-attaching .cursor/rules/ for consistent documentation across the project

Still stuck?

Copy one of these prompts to get a personalized, step-by-step explanation.

ChatGPT Prompt

Add JSDoc comments to every exported function in this TypeScript file. Include @param, @returns, @throws, and @example tags. Keep descriptions concise. Do not modify any implementations.

Cursor Prompt

@src/utils/orderCalculator.ts Add JSDoc to every exported function and interface. Include @param with types, @returns, @throws, @example with runnable TypeScript. Do NOT modify implementations. Keep descriptions to 1-2 sentences.

Frequently asked questions

Can Cursor add JSDoc to an entire project at once?

Not in a single prompt. Process one file at a time in Composer sessions for best results. You can create a script that lists undocumented files and process them sequentially.

Does Cursor generate accurate @example code?

Yes, when it can see the function's TypeScript types and implementation. Reference the file with @file so Cursor reads the actual signature. Examples may need minor adjustments for import paths.

Should I use JSDoc or TypeScript types for documentation?

Use both. TypeScript types handle parameter and return types. JSDoc adds descriptions, examples, and thrown error documentation that types cannot express. Cursor can generate both together.

How do I prevent Cursor from adding redundant comments?

Add a rule: 'Do not add comments that restate what the code does. Only document why, edge cases, and non-obvious behavior.' This eliminates comments like '// returns the name' above getName().

Can Cursor generate documentation in Python docstring format?

Yes. Specify 'Use Google-style Python docstrings' or 'Use NumPy docstring format' in your .cursorrules. Cursor adapts to any documentation format when instructed.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.