From 0e8a14149058bdb8b88d14771afb0f0c42cea62d Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 30 May 2025 22:45:59 +0200 Subject: [PATCH] Add custom eslint rule: no-placeholder-comments --- eslint-rules/README.md | 49 +++++++++++++++++++++++++ eslint-rules/index.js | 7 ++++ eslint-rules/no-placeholder-comments.js | 45 +++++++++++++++++++++++ eslint.config.js | 3 ++ 4 files changed, 104 insertions(+) create mode 100644 eslint-rules/README.md create mode 100644 eslint-rules/index.js create mode 100644 eslint-rules/no-placeholder-comments.js diff --git a/eslint-rules/README.md b/eslint-rules/README.md new file mode 100644 index 0000000..7c882dd --- /dev/null +++ b/eslint-rules/README.md @@ -0,0 +1,49 @@ +# Custom ESLint Rules + +This directory contains custom ESLint rules for the project. + +## no-placeholder-comments + +This rule detects and flags comments that start with "// In a real" (case-insensitive). These comments typically indicate placeholder implementations that should be replaced with real code. + +### Examples + +❌ **Bad** - These will trigger the rule: +```javascript +// In a real application, this would connect to a database +const data = []; + +// in a real world scenario, this would be different +const config = {}; + +/* In a real implementation, we would handle errors */ +const handleError = () => {}; +``` + +✅ **Good** - These are fine: +```javascript +// This is a regular comment +const data = []; + +// TODO: Implement database connection +const config = {}; + +// Note: In a real application, consider using a database +const handleError = () => {}; +``` + +### Configuration + +The rule is configured in `eslint.config.js` as: +```javascript +"custom/no-placeholder-comments": "error" +``` + +You can change the severity level to: +- `"off"` - Disable the rule +- `"warn"` - Show as warning +- `"error"` - Show as error (current setting) + +### Purpose + +This rule helps ensure that placeholder comments used during development are replaced with actual implementations before code is committed or deployed to production. \ No newline at end of file diff --git a/eslint-rules/index.js b/eslint-rules/index.js new file mode 100644 index 0000000..7569a01 --- /dev/null +++ b/eslint-rules/index.js @@ -0,0 +1,7 @@ +import noPlaceholderComments from './no-placeholder-comments.js'; + +export default { + rules: { + 'no-placeholder-comments': noPlaceholderComments, + }, +}; \ No newline at end of file diff --git a/eslint-rules/no-placeholder-comments.js b/eslint-rules/no-placeholder-comments.js new file mode 100644 index 0000000..b2f7b95 --- /dev/null +++ b/eslint-rules/no-placeholder-comments.js @@ -0,0 +1,45 @@ +/** + * Custom ESLint rule to detect placeholder comments starting with "// In a real" + * These comments indicate incomplete implementations that should be replaced with real code. + */ + +export default { + meta: { + type: "problem", + docs: { + description: "Disallow placeholder comments starting with '// In a real'", + category: "Best Practices", + recommended: true, + }, + fixable: null, + schema: [], + messages: { + placeholderComment: "Placeholder comment detected: '{{comment}}'. This should be replaced with a real implementation.", + }, + }, + + create(context) { + const sourceCode = context.getSourceCode(); + + return { + Program() { + const comments = sourceCode.getAllComments(); + + comments.forEach((comment) => { + const commentText = comment.value.trim(); + + // Check if comment starts with "In a real" (case-insensitive) + if (commentText.toLowerCase().startsWith("in a real")) { + context.report({ + node: comment, + messageId: "placeholderComment", + data: { + comment: `// ${commentText}`, + }, + }); + } + }); + }, + }; + }, +}; \ No newline at end of file diff --git a/eslint.config.js b/eslint.config.js index e67846f..0434c3a 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -3,6 +3,7 @@ import globals from "globals"; import reactHooks from "eslint-plugin-react-hooks"; import reactRefresh from "eslint-plugin-react-refresh"; import tseslint from "typescript-eslint"; +import customRules from "./eslint-rules/index.js"; export default tseslint.config( { ignores: ["dist"] }, @@ -16,6 +17,7 @@ export default tseslint.config( plugins: { "react-hooks": reactHooks, "react-refresh": reactRefresh, + "custom": customRules, }, rules: { ...reactHooks.configs.recommended.rules, @@ -24,6 +26,7 @@ export default tseslint.config( { allowConstantExport: true }, ], "@typescript-eslint/no-unused-vars": "off", + "custom/no-placeholder-comments": "error", }, } );