-
-
Save ezequieltejada/442ae9abd60e25983722ff104ece623b to your computer and use it in GitHub Desktop.
ESLint 200-Line Max File Size Rule for Better AI Coding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 🧠 My game-changing ESLint rule that makes AI coding 10x better: | |
* - Enforces 200-line max file size | |
* - Counts only actual code (ignores comments) | |
* - Gives helpful refactoring suggestions | |
* - Works perfectly with Cursor AI's "Fix in Chat" | |
* | |
* Custom ESLint rule to limit file size to 200 lines | |
* @type {import("eslint").Rule.RuleModule} | |
*/ | |
export const maxFileLines = { | |
meta: { | |
type: 'suggestion', | |
docs: { | |
description: 'Enforce maximum file line count of 200', | |
category: 'Best Practices', | |
recommended: true, | |
}, | |
schema: [], // no options | |
messages: { | |
tooManyLines: | |
'File has too many lines ({{count}}). Maximum allowed is 200 lines. Please split this file into smaller, more focused modules with single responsibilities. Consider extracting utility functions, separating components, or moving complex logic into dedicated files.', | |
}, | |
}, | |
create(context) { | |
return { | |
Program(node) { | |
const sourceCode = context.sourceCode() | |
const lines = sourceCode.lines || sourceCode.text.split(/\r?\n/) | |
// Count non-empty, non-comment lines | |
let codeLineCount = 0 | |
const comments = sourceCode.getAllComments() | |
const commentLines = new Set() | |
// Mark all comment lines | |
comments.forEach(comment => { | |
const startLine = comment.loc.start.line | |
const endLine = comment.loc.end.line | |
for (let i = startLine; i <= endLine; i++) { | |
commentLines.add(i) | |
} | |
}) | |
// Count actual code lines | |
for (let i = 0; i < lines.length; i++) { | |
const lineNumber = i + 1 | |
const line = lines[i].trim() | |
if (line.length > 0 && !commentLines.has(lineNumber)) { | |
codeLineCount++ | |
} | |
} | |
if (codeLineCount > 200) { | |
context.report({ | |
node, | |
messageId: 'tooManyLines', | |
data: { | |
count: codeLineCount, | |
}, | |
}) | |
} | |
}, | |
} | |
}, | |
} | |
/** | |
* Plugin object with our custom rules | |
*/ | |
export const customRules = { | |
rules: { | |
'max-file-lines': maxFileLines, | |
}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import js from '@eslint/js' | |
import { customRules } from './custom-rules.js' | |
/** | |
* A minimal shared ESLint configuration with custom rules. | |
* | |
* @type {import("eslint").Linter.Config[]} | |
*/ | |
export const config = [ | |
js.configs.recommended, | |
{ | |
plugins: { | |
'custom-rules': customRules, | |
}, | |
rules: { | |
'custom-rules/max-file-lines': 'warn', | |
}, | |
}, | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment