Establishing Consistent Naming Conventions in JavaScript

Explore best practices for naming variables, functions, and classes to improve code readability and maintainability.

0 likes
6 views

Rule Content

{
  "title": "Establishing Consistent Naming Conventions in JavaScript",
  "description": "Enforce best practices for naming variables, functions, and classes to improve code readability and maintainability.",
  "category": "JavaScript Cursor Rules",
  "rules": [
    {
      "pattern": "const [A-Z_]+ =",
      "message": "Constants should be named using UPPER_SNAKE_CASE (e.g., MAX_ATTEMPTS).",
      "severity": "error"
    },
    {
      "pattern": "function [a-z][a-zA-Z0-9]*\\(",
      "message": "Function names should be in camelCase and start with a lowercase letter (e.g., calculateTotal).",
      "severity": "error"
    },
    {
      "pattern": "class [A-Z][a-zA-Z0-9]*",
      "message": "Class names should be in PascalCase and start with an uppercase letter (e.g., UserProfile).",
      "severity": "error"
    },
    {
      "pattern": "let [a-z][a-zA-Z0-9]* =",
      "message": "Variable names should be in camelCase and start with a lowercase letter (e.g., userAge).",
      "severity": "error"
    },
    {
      "pattern": "const [a-z][a-zA-Z0-9]* =",
      "message": "Variable names should be in camelCase and start with a lowercase letter (e.g., userAge).",
      "severity": "error"
    },
    {
      "pattern": "let [a-z][a-zA-Z0-9]*s = \\[",
      "message": "Array variable names should be plural to indicate multiple items (e.g., users).",
      "severity": "warning"
    },
    {
      "pattern": "const [a-z][a-zA-Z0-9]*s = \\[",
      "message": "Array variable names should be plural to indicate multiple items (e.g., users).",
      "severity": "warning"
    },
    {
      "pattern": "let is[A-Z][a-zA-Z0-9]* =",
      "message": "Boolean variables should start with 'is', 'has', or 'should' (e.g., isActive).",
      "severity": "warning"
    },
    {
      "pattern": "const is[A-Z][a-zA-Z0-9]* =",
      "message": "Boolean variables should start with 'is', 'has', or 'should' (e.g., isActive).",
      "severity": "warning"
    },
    {
      "pattern": "function [a-z][a-zA-Z0-9]*Async\\(",
      "message": "Asynchronous functions should have an 'Async' suffix (e.g., fetchDataAsync).",
      "severity": "warning"
    }
  ]
}