-
Notifications
You must be signed in to change notification settings - Fork 0
Add no-placeholder-comments rule #36
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
Open
arnavsurve
wants to merge
1
commit into
main
Choose a base branch
from
rule/no-placeholder-comments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import type { File } from '@babel/types'; | ||
| import type { LintResult } from '../types'; | ||
|
|
||
| const RULE_NAME = 'no-placeholder-comments'; | ||
|
|
||
| const PLACEHOLDER_PATTERNS = [ | ||
| /\bTODO\b/i, | ||
| /\bFIXME\b/i, | ||
| /\bHACK\b/i, | ||
| /\bXXX\b/i, | ||
| /\badd\s+your\b/i, | ||
| /\bimplement\s+(this|here|me)\b/i, | ||
| /\bplaceholder\b/i, | ||
| /\breplace\s+(this|me|with)\b/i, | ||
| /\bfill\s+in\b/i, | ||
| /\byour\s+(code|logic|implementation)\s+here\b/i, | ||
| /\bchange\s+this\b/i, | ||
| /\binsert\s+(here|your)\b/i, | ||
| ]; | ||
|
|
||
| export function noPlaceholderComments(ast: File, code: string): LintResult[] { | ||
| const results: LintResult[] = []; | ||
| const lines = code.split('\n'); | ||
|
|
||
| for (let i = 0; i < lines.length; i++) { | ||
| const line = lines[i]; | ||
| // Match single-line comments (//) and multi-line comment content | ||
| const commentMatch = | ||
| line.match(/\/\/(.*)$/) || line.match(/\/\*(.*)/) || line.match(/^\s*\*\s?(.*)/); | ||
| if (!commentMatch) continue; | ||
|
|
||
| const commentText = commentMatch[1]; | ||
| if (!commentText) continue; | ||
|
|
||
| for (const pattern of PLACEHOLDER_PATTERNS) { | ||
| if (pattern.test(commentText)) { | ||
| results.push({ | ||
| rule: RULE_NAME, | ||
| message: `Remove placeholder comment: "${commentText.trim()}"`, | ||
| line: i + 1, | ||
| column: 0, | ||
| severity: 'warning', | ||
| }); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return results; | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { lintJsxCode } from '../src'; | ||
|
|
||
| const config = { rules: ['no-placeholder-comments'] }; | ||
|
|
||
| describe('no-placeholder-comments rule', () => { | ||
| it('should detect TODO comments', () => { | ||
| const code = `// TODO: implement this later`; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(1); | ||
| expect(results[0].rule).toBe('no-placeholder-comments'); | ||
| }); | ||
|
|
||
| it('should detect FIXME comments', () => { | ||
| const code = `// FIXME: broken logic`; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('should detect "add your code here" comments', () => { | ||
| const code = `// Add your logic here`; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('should detect "implement this" comments', () => { | ||
| const code = `// implement this`; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('should detect placeholder comments', () => { | ||
| const code = `// placeholder for auth logic`; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('should detect "replace this" comments', () => { | ||
| const code = `// replace this with real implementation`; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('should detect "fill in" comments', () => { | ||
| const code = `// fill in the details`; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('should not flag regular comments', () => { | ||
| const code = `// This component handles user authentication`; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should not flag code without comments', () => { | ||
| const code = `const x = 5;`; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should detect comments in multi-line blocks', () => { | ||
| const code = `/* TODO: fix this */`; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(1); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do you need to handle JSX comments too?