Establishing a Modular Code Structure in Node.js

Learn how to break down your Node.js applications into manageable, reusable modules to improve maintainability and collaboration.

0 likes
7 views

Rule Content

{
  "title": "Establishing a Modular Code Structure in Node.js",
  "description": "Learn how to break down your Node.js applications into manageable, reusable modules to improve maintainability and collaboration.",
  "category": "Node.js Cursor Rules",
  "rules": [
    {
      "name": "Single Responsibility Principle",
      "description": "Ensure each module has a single, well-defined responsibility to promote maintainability and scalability.",
      "pattern": "function\\s+\\w+\\s*\\(.*\\)\\s*\\{[^}]*\\}",
      "action": "Review function to ensure it adheres to the Single Responsibility Principle."
    },
    {
      "name": "Consistent Naming Conventions",
      "description": "Use clear and descriptive names for modules, functions, and variables to enhance code readability.",
      "pattern": "\\b(var|let|const|function|class)\\s+\\w+",
      "action": "Check naming conventions for clarity and consistency."
    },
    {
      "name": "Encapsulation of Internal Details",
      "description": "Expose only necessary functions and variables from modules to maintain a clear public API.",
      "pattern": "module\\.exports\\s*=\\s*\\{[^}]*\\}",
      "action": "Ensure only necessary functions and variables are exported."
    },
    {
      "name": "Avoid Global Dependencies",
      "description": "Minimize the use of global variables to prevent tightly-coupled code and improve testability.",
      "pattern": "global\\.",
      "action": "Review usage of global variables and refactor to reduce dependencies."
    },
    {
      "name": "Use ES Modules",
      "description": "Utilize ES modules (import/export) for better syntax and efficient bundling.",
      "pattern": "require\\(.*\\)",
      "action": "Consider refactoring to use ES module syntax (import/export)."
    },
    {
      "name": "Implement Error Handling",
      "description": "Use try-catch blocks or error-first callbacks to handle and propagate errors effectively.",
      "pattern": "try\\s*\\{[^}]*\\}\\s*catch\\s*\\(.*\\)\\s*\\{[^}]*\\}",
      "action": "Ensure proper error handling is implemented."
    },
    {
      "name": "Write Unit Tests",
      "description": "Develop unit tests for modules to ensure correctness and maintain stability during code changes.",
      "pattern": "describe\\(.*\\)\\s*\\{[^}]*\\}",
      "action": "Verify that unit tests are written and cover module functionality."
    },
    {
      "name": "Document Modules",
      "description": "Provide clear documentation for modules, including usage, inputs, outputs, and relevant details.",
      "pattern": "/\\*\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/",
      "action": "Check for comprehensive documentation comments."
    },
    {
      "name": "Follow Code Style Guidelines",
      "description": "Adhere to consistent code style guidelines for indentation, naming conventions, and formatting.",
      "pattern": "\\b(var|let|const|function|class)\\s+\\w+",
      "action": "Ensure code style guidelines are consistently followed."
    },
    {
      "name": "Use Linters and Code Analyzers",
      "description": "Utilize tools like ESLint to enforce coding standards and identify potential issues.",
      "pattern": "eslint\\s*--fix",
      "action": "Verify that linting tools are configured and used."
    }
  ]
}