-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add ESLint complexity rules template to prevent technical debt #293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| # ESLint Complexity Rules | ||
|
|
||
| This directory contains recommended ESLint complexity rules to prevent technical debt accumulation. | ||
|
|
||
| ## Overview | ||
|
|
||
| Code complexity rules help maintain code quality by enforcing limits on: | ||
|
|
||
| - Cyclomatic complexity | ||
| - Function length | ||
| - File length | ||
| - Nesting depth | ||
| - Function parameters | ||
|
|
||
| ## Files | ||
|
|
||
| - `complexity-rules.mjs`: Exportable complexity rules configuration | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Import in ESLint Config (Flat Config) | ||
|
|
||
| ```javascript | ||
| import { complexityRules } from './eslint/complexity-rules.mjs'; | ||
|
|
||
| export default [ | ||
| { | ||
| files: ['**/*.{js,jsx,ts,tsx}'], | ||
| rules: { | ||
| ...complexityRules, | ||
| // Your other rules | ||
| }, | ||
| }, | ||
| ]; | ||
| ``` | ||
|
|
||
| ### Copy Rules Directly | ||
|
|
||
| Copy the rules object from `complexity-rules.mjs` into your existing ESLint configuration. | ||
|
|
||
| ## Rules Reference | ||
|
|
||
| | Rule | Limit | Purpose | | ||
| | ------------------------ | ----- | --------------------------- | | ||
| | `complexity` | 15 | Cyclomatic complexity limit | | ||
| | `max-lines-per-function` | 100 | Function length limit | | ||
| | `max-lines` | 500 | File length limit | | ||
| | `max-depth` | 4 | Nesting depth limit | | ||
| | `max-params` | 5 | Function parameter limit | | ||
|
|
||
| ## Implementation Strategy | ||
|
|
||
| ### Phase 1: Warning Mode (Current) | ||
|
|
||
| Set all rules to `"warn"` to detect issues without breaking builds: | ||
|
|
||
| ```javascript | ||
| "complexity": ["warn", { "max": 15 }] | ||
| ``` | ||
|
|
||
| This allows you to: | ||
|
|
||
| - Identify existing violations | ||
| - Prevent new technical debt | ||
| - Gradually refactor problematic code | ||
|
|
||
| ### Phase 2: Error Mode (Future) | ||
|
|
||
| Once the codebase is compliant, upgrade to `"error"`: | ||
|
|
||
| ```javascript | ||
| "complexity": ["error", { "max": 15 }] | ||
| ``` | ||
|
|
||
| This will: | ||
|
|
||
| - Block CI/CD pipeline on violations | ||
| - Enforce strict compliance | ||
| - Maintain code quality standards | ||
|
|
||
| ## Test File Exceptions | ||
|
|
||
| Consider relaxing rules for test files: | ||
|
|
||
| ```javascript | ||
| { | ||
| files: ['**/*.test.js', '**/*.spec.js', '**/test/**/*.js'], | ||
| rules: { | ||
| 'max-lines-per-function': 'off', | ||
| 'complexity': 'off', | ||
| }, | ||
| } | ||
| ``` | ||
|
|
||
| ## Customization | ||
|
|
||
| Adjust limits based on your project needs: | ||
|
|
||
| ```javascript | ||
| export const complexityRules = { | ||
| complexity: ['warn', { max: 10 }], // Stricter | ||
| 'max-lines-per-function': [ | ||
| 'warn', | ||
| { | ||
| max: 150, // More lenient | ||
| skipBlankLines: true, | ||
| skipComments: true, | ||
| }, | ||
| ], | ||
| }; | ||
| ``` | ||
|
|
||
| ## CI Integration | ||
|
|
||
| Add ESLint complexity checks to your CI pipeline: | ||
|
|
||
| ```yaml | ||
| - name: Run ESLint | ||
| run: npm run lint | ||
| ``` | ||
|
|
||
| ## Source | ||
|
|
||
| These rules were discovered from `Elu-co-jp/management_tools` repository and recommended for organization-wide adoption. | ||
|
|
||
| ## Related Files | ||
|
|
||
| - `/eslint.config.mjs`: Main ESLint configuration for this repository | ||
| - `.github/workflows/templates/unified-ci.yml`: CI workflow template with linting | ||
|
|
||
| ## Benefits | ||
|
|
||
| - **Prevents technical debt**: Catches complex code early | ||
| - **Improves readability**: Enforces consistent code structure | ||
| - **Maintains quality**: Automated enforcement in CI/CD | ||
| - **Gradual adoption**: Warn-first approach allows incremental improvements | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * ESLint Complexity Rules Template | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * These rules help prevent technical debt accumulation by enforcing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * code complexity limits. Use this as a reference for your projects. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Source: Discovered from Elu-co-jp/management_tools | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Implementation Strategy: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * - Phase 1: Set rules to "warn" to detect issues without breaking builds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * - Phase 2: Upgrade to "error" once codebase is compliant | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const complexityRules = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Cyclomatic Complexity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Limits the complexity of functions by counting the number of linearly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * independent paths through the code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Recommended: 15 (warn), stricter: 10 (error) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| complexity: ['warn', { max: 15 }], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Function Length | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Limits the number of lines in a function to maintain readability. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Recommended: 100 lines (warn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Blank lines and comments are not counted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'max-lines-per-function': [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'warn', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max: 100, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| skipBlankLines: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| skipComments: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * File Length | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Limits the number of lines in a file to maintain focus and cohesion. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Recommended: 500 lines (warn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Blank lines and comments are not counted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'max-lines': [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'warn', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max: 500, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| skipBlankLines: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| skipComments: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Nesting Depth | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Limits the depth of nested blocks to improve readability. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Recommended: 4 levels (warn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'max-depth': ['warn', 4], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Function Parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Limits the number of parameters a function can accept. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Recommended: 5 parameters (warn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Consider using an options object for more parameters. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'max-params': ['warn', 5], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Usage Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * import { complexityRules } from './eslint/complexity-rules.mjs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * export default [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * files: ['**\/*.{js,jsx,ts,tsx}'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * rules: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * ...complexityRules, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * // Your other rules | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+74
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the escaped backslash in the glob pattern. Line 81 contains an incorrectly escaped forward slash in the glob pattern. The backslash before the forward slash is unnecessary and will cause the pattern to fail matching files correctly. 🔎 Proposed fix * export default [
* {
- * files: ['**\/*.{js,jsx,ts,tsx}'],
+ * files: ['**/*.{js,jsx,ts,tsx}'],
* rules: {
* ...complexityRules,📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: keito4/config
Length of output: 39
🏁 Script executed:
Repository: keito4/config
Length of output: 1824
🏁 Script executed:
Repository: keito4/config
Length of output: 225
🏁 Script executed:
Repository: keito4/config
Length of output: 4085
🏁 Script executed:
Repository: keito4/config
Length of output: 308
Add
max-nested-callbacksto the Rules Reference table.The
max-nested-callbacksrule is configured ineslint.config.mjs(line 45: limit 3, line 53: limit 5 for test files) but missing from the README's Rules Reference table. Add it for completeness:max-nested-callbacks🤖 Prompt for AI Agents