JavaScript Cursor Rules: ES6+ Development Best Practices

Cursor rules for JavaScript development enforcing ES6+ standards, React best practices, and clean code principles with AI assistance for production-ready code.

January 16, 2024by PromptGenius Team
javascriptcursor-rulesai-codingbest-practiceses6

Overview

Professional cursor rules for JavaScript development that enforce modern standards and best practices. These rules help AI assistants generate clean, efficient, and maintainable JavaScript code with proper context and documentation.

Note:

Enforces ES6+ standards, React 18+ patterns, and context-aware code generation for production-ready development.

Rules Configuration

---
description: Enforces best practices for JavaScript development, focusing on context-aware code generation, modern patterns, and maintainable architecture. Provides comprehensive guidelines for writing clean, efficient, and secure JavaScript code with proper context.
globs: **/*.{js,jsx,ts,tsx}
---
# JavaScript Best Practices

You are an expert in JavaScript, Node.js, React, Vue, and related web technologies.
You understand modern JavaScript development practices, architectural patterns, and the importance of providing complete context in code generation.

### Context-Aware Code Generation
- Always provide complete module context including imports and exports
- Include relevant configuration files (package.json) when generating projects
- Generate complete function signatures with proper parameters and JSDoc
- Include comprehensive documentation explaining the purpose, parameters, and return values
- Provide context about the module's role in the larger system architecture

### Code Style and Structure
- Follow clean code principles with meaningful names and proper documentation
- Structure code in logical modules following domain-driven design principles
- Implement proper separation of concerns (components, services, utilities)
- Use modern JavaScript features (ES6+, async/await, optional chaining) appropriately
- Maintain consistent code formatting using Prettier or similar tools

### Framework Best Practices
- Use React 18+ features and best practices
- Implement proper state management (React Context, Redux)
- Configure proper routing with React Router
- Use proper component composition and hooks
- Implement proper error boundaries
- Configure proper testing setup (Jest, React Testing Library)

### Testing and Quality
- Write comprehensive unit tests with proper test context
- Include integration tests for critical paths
- Use proper mocking strategies with Jest
- Implement E2E tests with Cypress or Playwright
- Include performance tests for critical components
- Maintain high test coverage for core business logic

### Security and Performance
- Implement proper input validation and sanitization
- Use secure authentication and token management
- Configure proper CORS and CSRF protection
- Implement rate limiting and request validation
- Use proper caching strategies
- Optimize bundle size and loading performance

### API Design
- Follow RESTful principles with proper HTTP methods
- Use proper status codes and error responses
- Implement proper versioning strategies
- Document APIs using OpenAPI/Swagger
- Include proper request/response validation
- Implement proper pagination and filtering

### State Management
- Use proper state management patterns
- Implement proper data fetching strategies
- Use proper caching mechanisms
- Handle loading and error states properly
- Implement proper optimistic updates
- Use proper state persistence when needed

### Build and Deployment
- Use proper bundlers (Webpack, Vite)
- Implement proper CI/CD pipelines
- Use Docker for containerization
- Configure proper environment variables
- Implement proper logging and monitoring
- Use proper deployment strategies

Key Features

📦

Complete Code Context

Full module imports, exports, and JSDoc blocks included automatically

🚀

Modern ES6+ Features

Async/await, destructuring, arrow functions, and optional chaining

Testing Built-In

Automatic generation of Jest tests with proper mocking strategies

🔒

Security First

Input validation, CSRF/CORS protection, and secure token management

⚛️

React 18+ Ready

Hooks, context, suspense, and concurrent rendering patterns

🏗️

Clean Architecture

Proper separation of concerns and component composition patterns

Installation

1

Choose Your IDE

Select the appropriate file path based on your development environment.

2

Create the Rules File

Create the cursor rules file in your project:

Create file: .cursor/rules/javascript.mdc

3

Add the Rules Configuration

Copy the rules configuration above into your newly created file.

4

Start Coding

Your AI assistant will now follow JavaScript best practices automatically.

Use Cases

React Applications

Full-stack React applications with proper component architecture and state management

API Development

Node.js REST APIs with proper validation, error handling, and testing

Full-Stack Development

End-to-end applications with frontend and backend JavaScript integration

Utility Libraries

Reusable JavaScript packages with comprehensive documentation and tests

Standards Reference

StandardDescriptionEnforcement
ES6+Modern JavaScript syntax and featuresRequired in all code
JSDocComprehensive documentation blocksRequired for functions and classes
TestingJest/Vitest test coverageAuto-generated with code
SecurityInput validation and sanitizationBuilt into patterns
ReactComponent best practices (18+)Enforced via structure
ArchitectureClean separation of concernsEnforced via modules

Note:

Combine these rules with ESLint, Prettier, and TypeScript for maximum code quality enforcement.

Best Practices

Code Organization

  • Components handle UI logic and presentation
  • Services contain business logic
  • Utilities provide reusable helper functions
  • Custom hooks encapsulate component logic

Type Safety

  • Use TypeScript for large projects or teams
  • Add JSDoc types for JavaScript-only projects
  • Document all function parameters
  • Declare return types for all functions

Testing Strategy

  • Unit tests for business logic and utilities
  • Integration tests for component interactions
  • E2E tests for critical user workflows
  • Mock external dependencies appropriately

React Patterns

  • Use functional components with hooks
  • Implement proper error boundaries
  • Optimize with React.memo when appropriate
  • Handle loading and error states explicitly

Note:

These rules work with vanilla JavaScript, Node.js, and React projects. Adjust framework-specific sections as needed for your technology stack.

See Also

Explore other cursor rules for complementary languages and frameworks:

Examples

/**
 * UserService handles user-related operations.
 * Provides methods for user management and authentication.
 */
class UserService {
  constructor(apiClient, cache) {
    this.apiClient = apiClient;
    this.cache = cache;
  }

  /**
   * Finds a user by their email address.
   *
   * @param {string} email - The email address to search for
   * @returns {Promise<Object|null>} Promise containing the user if found
   * @throws {Error} if the request fails
   */
  async findUserByEmail(email) {
    try {
      const cachedUser = await this.cache.get(`user:${email}`);
      if (cachedUser) {
        return JSON.parse(cachedUser);
      }

      const user = await this.apiClient.get(`/users?email=${email}`);
      await this.cache.set(`user:${email}`, JSON.stringify(user));
      return user;
    } catch (error) {
      throw new Error('Failed to find user by email: ' + error.message);
    }
  }
}

/**
 * Tests for UserService functionality.
 */
describe('UserService', () => {
  let service;
  let apiClient;
  let cache;

  beforeEach(() => {
    apiClient = {
      get: jest.fn(),
    };

    cache = {
      get: jest.fn(),
      set: jest.fn(),
    };

    service = new UserService(apiClient, cache);
  });

  it('should find user by email when user exists', async () => {
    // Given
    const email = '[email protected]';
    const user = { id: 1, email };
    apiClient.get.mockResolvedValue(user);

    // When
    const result = await service.findUserByEmail(email);

    // Then
    expect(result).toEqual(user);
    expect(apiClient.get).toHaveBeenCalledWith(`/users?email=${email}`);
  });

  it('should return null when user not found', async () => {
    // Given
    const email = '[email protected]';
    apiClient.get.mockResolvedValue(null);

    // When
    const result = await service.findUserByEmail(email);

    // Then
    expect(result).toBeNull();
    expect(apiClient.get).toHaveBeenCalledWith(`/users?email=${email}`);
  });
});