Defining and Enforcing Code Style Guides
Establish a comprehensive style guide for JavaScript projects to ensure team-wide adherence to practices and conventions.
0 likes
18 views
Rule Content
{ "title": "Defining and Enforcing Code Style Guides", "description": "Establish a comprehensive style guide for JavaScript projects to ensure team-wide adherence to practices and conventions.", "category": "JavaScript Cursor Rules", "rules": [ { "name": "Use 'Strict Mode'", "description": "Enable 'strict mode' at the beginning of your scripts or functions to enforce secure coding practices and catch common errors.", "pattern": "^(?!.*['\"]use strict['\"]).*", "replacement": "'use strict';\n$0", "message": "Consider adding 'use strict'; at the beginning of your script to enforce secure coding practices." }, { "name": "Use 'const' and 'let' for Variable Declarations", "description": "Use 'const' for variables that do not change and 'let' for variables that are reassigned. Avoid using 'var'.", "pattern": "\\bvar\\b", "replacement": "let", "message": "Replace 'var' with 'let' or 'const' to adhere to modern JavaScript standards." }, { "name": "Leverage Arrow Functions", "description": "Prefer arrow functions for callbacks and inline functions to provide a more concise syntax and inherit the 'this' context from their parent scope.", "pattern": "function\\s*\\(([^)]*)\\)\\s*{", "replacement": "($1) => {", "message": "Consider using arrow functions for a more concise syntax and proper 'this' context." }, { "name": "Organize Code with ES Modules", "description": "Use 'import' and 'export' statements to organize code into ES Modules, ensuring proper encapsulation and modularity.", "pattern": "module\\.exports\\s*=\\s*", "replacement": "export default ", "message": "Use ES6 'export' statements instead of 'module.exports' for better modularity." }, { "name": "Use Template Literals for String Interpolation", "description": "Use template literals instead of string concatenation for cleaner and more readable code when handling strings with variables or expressions.", "pattern": "'([^']*)'\\s*\\+\\s*([^\\s]+)\\s*\\+\\s*'([^']*)'", "replacement": "`$1${$2}$3`", "message": "Replace string concatenation with template literals for improved readability." }, { "name": "Simplify Code with Destructuring Assignment", "description": "Use destructuring to simplify the extraction of values from objects and arrays, making your code cleaner and more concise.", "pattern": "const\\s+([^=]+)\\s*=\\s*([^;]+);\\s*const\\s+([^=]+)\\s*=\\s*\\2\\['([^']+)']", "replacement": "const { $4: $3 } = $2;", "message": "Use destructuring assignment to simplify code when extracting values from objects." }, { "name": "Use Spread Operator for Immutable Data Manipulation", "description": "Use the spread operator to make copies of arrays or objects and manipulate data without mutating the original values.", "pattern": "const\\s+([^=]+)\\s*=\\s*Object\\.assign\\({},\\s*([^;]+)\\);", "replacement": "const $1 = { ...$2 };", "message": "Use the spread operator instead of 'Object.assign' for copying objects." }, { "name": "Use Default Parameters in Functions", "description": "Use default parameters to specify default values for function parameters, making functions more robust and easier to use.", "pattern": "function\\s+([^\\(]+)\\(([^\\)]+)\\)\\s*{", "replacement": "function $1($2 = defaultValue) {", "message": "Consider using default parameters to provide default values for function arguments." }, { "name": "Prefer 'async/await' for Asynchronous Code", "description": "Use 'async/await' syntax for handling asynchronous operations to improve readability and maintainability.", "pattern": "\\.then\\(function\\s*\\(([^)]*)\\)\\s*{", "replacement": "async function($1) {", "message": "Replace '.then' with 'async/await' for better readability of asynchronous code." }, { "name": "Handle Errors Gracefully with Try/Catch", "description": "Wrap API calls and other asynchronous operations in try/catch blocks to handle errors gracefully and prevent crashes.", "pattern": "async function\\s+([^\\(]+)\\(([^\\)]*)\\)\\s*{", "replacement": "async function $1($2) {\n try {", "message": "Wrap asynchronous function bodies in try/catch blocks to handle errors gracefully." }, { "name": "Maintain Consistent Code Formatting", "description": "Follow a consistent code formatting style (indentation, spacing, etc.) and use tools like Prettier to automate this.", "pattern": "\\t", "replacement": " ", "message": "Replace tabs with spaces to maintain consistent code formatting." }, { "name": "Avoid Global Variables", "description": "Avoid using global variables to prevent unexpected behavior and make your code easier to maintain.", "pattern": "window\\.([^\\s]+)\\s*=", "replacement": "const $1 =", "message": "Avoid using global variables; declare variables locally within functions or modules." }, { "name": "Utilize Linters and Code Formatters", "description": "Use linters and code formatters like ESLint and Prettier to enforce coding standards and catch common errors.", "pattern": ".*", "replacement": "", "message": "Ensure that linters and code formatters are configured and used to maintain code quality." }, { "name": "Prefer Strict Type Checking with '===' and '!=='", "description": "Use '===' and '!==' for comparisons to ensure both value and type are equal, avoiding unexpected behavior caused by type coercion.", "pattern": "==", "replacement": "===", "message": "Replace '==' with '===' to enforce strict type checking." }, { "name": "Implement Performance Optimizations for Large-Scale Applications", "description": "Optimize code performance by minimizing resource usage, avoiding memory leaks, and handling asynchronous operations efficiently.", "pattern": ".*", "replacement": "", "message": "Review code for potential performance optimizations, especially in large-scale applications." } ] }