-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Port ESLint core rule no-constructor-return #76
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
Conversation
## Summary This PR ports the ESLint core rule `no-constructor-return` to rslint, disallowing return statements with values in class constructors. Follow-up task of PR #75. ## Implementation Details - ✅ Created new rule in `internal/rules/no_constructor_return/` - ✅ Detects return statements with values inside class constructors - ✅ Allows bare `return;` statements (without values) for flow control - ✅ Properly handles nested functions - returns in nested functions are ignored - ✅ Registered in global rule registry ## Rule Behavior The rule prevents return statements with values in constructors, which typically indicate mistakes or misunderstandings about how JavaScript constructors work. ### Invalid Patterns ```javascript // Returning values in constructors class C { constructor() { return '' } } class C { constructor() { return 1 } } class C { constructor() { return {} } } class C { constructor() { return this } } // Conditional returns with values class C { constructor(a) { if (!a) { return '' } } } // Multiple returns class C { constructor(x) { if (x) return 'yes'; return 'no'; } } ``` ### Valid Patterns ```javascript // No return statement class C { constructor() { this.value = 1 } } // Bare return for flow control class C { constructor(a) { if (!a) { return } else { a() } } } class C { constructor() { return } } // Nested functions can return values class C { constructor() { function fn() { return true } } } class C { constructor() { this.fn = () => { return true } } } // Methods can return values class C { method() { return '' } } class C { get value() { return '' } } ``` ## Test Coverage - ✅ Ported all test cases from ESLint's test suite - ✅ **21 valid test cases** covering various scenarios - ✅ **12 invalid test cases** with expected error detection - ✅ All tests passing - ✅ Tests include: - Regular functions and arrow functions (allowed) - Classes with and without constructors - Bare return statements (allowed) - Nested functions in constructors - TypeScript-specific cases (parameter properties) - Multiple return statements - Various return value types ## References - ESLint Rule: https://eslint.org/docs/latest/rules/no-constructor-return - ESLint Source: https://github.com/eslint/eslint/blob/main/lib/rules/no-constructor-return.js - ESLint Tests: https://github.com/eslint/eslint/blob/main/tests/lib/rules/no-constructor-return.js - Related PR #75: #75 ## Files Changed - `internal/config/config.go` - Added rule registration (2 lines) - `internal/rules/no_constructor_return/no_constructor_return.go` - Complete rule implementation (~80 lines) - `internal/rules/no_constructor_return/no_constructor_return_test.go` - Comprehensive test suite (~200 lines) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
|
🤖 This pull request has been linked to DevBird Task #1137 View the task details and manage the automated development workflow in DevBird. Learn more about DevBird here or the announcement blog post here. |
📋 DevBird Task PromptObjectivePort the ESLint core rule Documentation & Resources
Rule DescriptionDisallows return statements in constructors. Implementation Requirements
Test Requirements
Success Criteria
This comment was automatically added by DevBird. You can disable this feature in DevBird Settings. |
Code Review for PR #76: Port ESLint core rule no-constructor-returnThank you for this well-structured PR! I've reviewed the implementation and overall it looks solid. Here's my detailed feedback: ✅ Strengths
🔍 Code Quality ObservationsMinor: Consistency with Codebase PatternsLooking at other rules like
Current code: switch current.Kind {
case ast.KindFunctionDeclaration,
ast.KindFunctionExpression,
ast.KindArrowFunction,
ast.KindMethodDeclaration:
return nil
}Suggestion: Double-check the AST types to ensure all function boundaries are covered. Based on the test cases, this appears correct, but it's worth verifying against the typescript-go shim. Potential Edge Case
Looking at your test suite, you don't have a test for class expressions. Consider adding: // Valid: nested class with its own constructor
{Code: `class Outer { method() { class Inner { constructor() { return 1 } } } }`},
// Invalid: class expression with return value
{Code: `const C = class { constructor() { return 1 } }`, Errors: [...]},🎯 Performance ConsiderationsThe implementation is efficient:
🔒 Security ConcernsNo security issues identified. The rule is read-only and doesn't execute or modify code. 📊 Test Coverage RecommendationsYour test coverage is excellent. The only additions I'd suggest:
🐛 Potential BugsNo bugs found! The implementation correctly:
💡 Best Practices Suggestions
📝 DocumentationThe PR description is excellent. However, consider:
✅ Approval RecommendationThis PR is approved with minor suggestions. The implementation is:
The suggestions above are optional improvements that could be addressed in this PR or as follow-ups. 🔧 Action Items (Optional)
Great work on this implementation! 🎉 |
Add the variable name 'kumiko' used in test cases to the spell checker dictionary to fix CI lint checks. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
PR Review: no-constructor-return Rule ImplementationThank you for this well-structured PR! The implementation of the Overall Assessment✅ LGTM with minor suggestions - This is a high-quality implementation that:
Code Quality & Best PracticesStrengths ✨
Suggestions for Improvement 💡1. Code Documentation (Minor)The rule lacks package-level documentation. Consider adding: // Package no_constructor_return implements the no-constructor-return ESLint rule.
// This rule disallows returning values from constructors in JavaScript/TypeScript classes.
//
// In JavaScript, returning a value from a constructor can lead to unexpected behavior:
// - Returning primitive values is ignored
// - Returning objects replaces the newly created instance
// This typically indicates a mistake or misunderstanding of constructor semantics.
package no_constructor_returnLocation: 2. Function Documentation (Minor)While the function names are descriptive, adding comments would improve maintainability: // findEnclosingConstructor walks up the AST from the given node to find the nearest
// enclosing constructor method. Returns nil if the node is not inside a constructor
// or if a function boundary is encountered (nested functions are not considered
// part of the constructor scope for this rule).
func findEnclosingConstructor(node *ast.Node) *ast.Node {Location: Potential Bugs & Issues✅ No Critical Issues FoundThe implementation correctly handles all the tricky cases:
Nitpick: Function Boundary CheckThe function boundary check is thorough but could be more explicit. Consider documenting why each function type stops the search: // Stop at function boundaries - returns in nested functions don't apply to outer constructor
switch current.Kind {
case ast.KindFunctionDeclaration, // function foo() {}
ast.KindFunctionExpression, // const fn = function() {}
ast.KindArrowFunction, // const fn = () => {}
ast.KindMethodDeclaration: // method() {}
// We've hit a nested function boundary, stop searching
return nil
}Location: Performance Considerations✅ Efficient Implementation
Potential Optimization (Optional)Currently, the rule checks every return statement and walks up the tree. For files with many return statements, you could consider:
Security Concerns✅ No Security IssuesThe rule is purely static analysis with no:
Test Coverage✅ Excellent CoverageValid Cases (21 tests):
Invalid Cases (12 tests):
Additional Test Suggestions (Optional)Consider adding edge cases for:
However, the current test suite is already comprehensive and covers the primary use cases from the ESLint rule. Alignment with Repository Standards✅ Follows Conventions
Comparison with ESLint SourceI verified against the ESLint implementation: ✅ Accurate Port
Differences (By Design)The Go implementation uses AST node traversal instead of ESLint's scope analysis, which is appropriate for the RSLint architecture. Final RecommendationsRequired ActionsNone - The PR is ready to merge as-is. Suggested Improvements (Optional)
Commits
SummaryThis is a well-crafted PR that successfully ports the ESLint
Great work! 🎉 Reviewed by: Claude Code |
## Summary This PR ports the TypeScript-ESLint rule `ban-ts-comment` to rslint, which bans @ts-<directive> comments or requires descriptions after directive. Follow-up task of PR #76. ## Implementation Details - ✅ Created new rule in `internal/plugins/typescript/rules/ban_ts_comment/` - ✅ Detects @ts-expect-error, @ts-ignore, @ts-nocheck, and @ts-check directives - ✅ Supports all rule options (configurable per directive) - ✅ Special handling for @ts-ignore (suggests @ts-expect-error instead) - ✅ Supports minimum description length validation - ✅ Supports custom description format validation (regex patterns) - ✅ Properly handles Unicode characters in descriptions (grapheme counting) - ✅ Registered in TypeScript plugin registry ## Rule Behavior The rule prevents TypeScript directive comments that suppress type-checking, or requires them to have descriptive explanations. ### Supported Directives - @ts-expect-error - Suppresses next line error - @ts-ignore - Suppresses next line error (suggests @ts-expect-error instead) - @ts-nocheck - Disables type checking for entire file - @ts-check - Enables type checking for JavaScript files ### Invalid Patterns ```typescript // Bare directive (no description) // @ts-expect-error const a: string = 123; // Description too short (default minimum: 3 characters) // @ts-expect-error: ab const b: string = 123; // Using @ts-ignore (suggests @ts-expect-error) // @ts-ignore const c: string = 123; ``` ### Valid Patterns ```typescript // With description (when allow-with-description is set) // @ts-expect-error: Type mismatch that will be fixed in next release const a: string = 123; // Custom format validation // @ts-expect-error: TS2345 because incompatible types const b: string = 123; // Disabled directive // @ts-check (default: not banned) ``` ## Configuration Options Each directive accepts: - `boolean true` - Completely ban the directive - `"allow-with-description"` - Allow with description - `{ descriptionFormat: "regex" }` - Require description matching pattern - `minimumDescriptionLength` - Minimum character count (default: 3) ## Test Coverage - ✅ Ported all test cases from TypeScript-ESLint repository - ✅ **60 valid test cases** covering various scenarios - ✅ **25 invalid test cases** with expected error detection - ✅ Tests include: - All directive types (expect-error, ignore, nocheck, check) - Configuration variations (disabled, allow-with-description, format) - Single-line and multi-line comments - Triple-slash comments - Minimum description length validation - Custom format validation (regex patterns) - Unicode/emoji descriptions - JSDoc-style comments ## References - Rule documentation: https://typescript-eslint.io/rules/ban-ts-comment/ - TypeScript-ESLint source: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/rules/ban-ts-comment.ts - TypeScript-ESLint tests: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts - Related PR #76: #76 ## Files Changed - `internal/config/config.go` - Added import and rule registration (2 lines) - `internal/plugins/typescript/rules/ban_ts_comment/ban_ts_comment.go` - Complete rule implementation (~320 lines) - `internal/plugins/typescript/rules/ban_ts_comment/ban_ts_comment_test.go` - Comprehensive test suite (~217 lines) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
## Summary This PR ports the TypeScript-ESLint rule `ban-ts-comment` to rslint, which bans @ts-<directive> comments or requires descriptions after directive. Follow-up task of PR #76. ## Implementation Details - ✅ Created new rule in `internal/plugins/typescript/rules/ban_ts_comment/` - ✅ Detects @ts-expect-error, @ts-ignore, @ts-nocheck, and @ts-check directives - ✅ Supports all rule options (configurable per directive) - ✅ Special handling for @ts-ignore (suggests @ts-expect-error instead) - ✅ Supports minimum description length validation - ✅ Supports custom description format validation (regex patterns) - ✅ Properly handles Unicode characters in descriptions (grapheme counting) - ✅ Registered in TypeScript plugin registry ## Rule Behavior The rule prevents TypeScript directive comments that suppress type-checking, or requires them to have descriptive explanations. ### Supported Directives - @ts-expect-error - Suppresses next line error - @ts-ignore - Suppresses next line error (suggests @ts-expect-error instead) - @ts-nocheck - Disables type checking for entire file - @ts-check - Enables type checking for JavaScript files ### Invalid Patterns \`\`\`typescript // Bare directive (no description) // @ts-expect-error const a: string = 123; // Description too short (default minimum: 3 characters) // @ts-expect-error: ab const b: string = 123; // Using @ts-ignore (suggests @ts-expect-error) // @ts-ignore const c: string = 123; \`\`\` ### Valid Patterns \`\`\`typescript // With description (when allow-with-description is set) // @ts-expect-error: Type mismatch that will be fixed in next release const a: string = 123; // Custom format validation // @ts-expect-error: TS2345 because incompatible types const b: string = 123; // Disabled directive // @ts-check (default: not banned) \`\`\` ## Configuration Options Each directive accepts: - \`boolean true\` - Completely ban the directive - \`"allow-with-description"\` - Allow with description - \`{ descriptionFormat: "regex" }\` - Require description matching pattern - \`minimumDescriptionLength\` - Minimum character count (default: 3) ## Test Coverage - ✅ Ported all test cases from TypeScript-ESLint repository - ✅ **60 valid test cases** covering various scenarios - ✅ **25 invalid test cases** with expected error detection - ✅ Tests include: - All directive types (expect-error, ignore, nocheck, check) - Configuration variations (disabled, allow-with-description, format) - Single-line and multi-line comments - Triple-slash comments - Minimum description length validation - Custom format validation (regex patterns) - Unicode/emoji descriptions - JSDoc-style comments ## Test Plan - [ ] CI tests pass - [ ] Manual testing with example code - [ ] Integration with existing linter setup ## References - Rule documentation: https://typescript-eslint.io/rules/ban-ts-comment/ - TypeScript-ESLint source: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/rules/ban-ts-comment.ts - TypeScript-ESLint tests: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/tests/rules/ban-ts-comment.test.ts - Related PR #76: #76 ## Files Changed - \`internal/config/config.go\` - Added import and rule registration (2 lines) - \`internal/plugins/typescript/rules/ban_ts_comment/ban_ts_comment.go\` - Complete rule implementation (~320 lines) - \`internal/plugins/typescript/rules/ban_ts_comment/ban_ts_comment_test.go\` - Comprehensive test suite (~217 lines) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
Summary
This PR ports the ESLint core rule
no-constructor-returnto rslint, disallowing return statements with values in class constructors.Follow-up task of PR #75.
Implementation Details
internal/rules/no_constructor_return/return;statements (without values) for flow controlRule Behavior
The rule prevents return statements with values in constructors, which typically indicate mistakes or misunderstandings about how JavaScript constructors work.
Invalid Patterns
Valid Patterns
Test Coverage
Test Plan
References
Files Changed
internal/config/config.go- Added rule registration (2 lines)internal/rules/no_constructor_return/no_constructor_return.go- Complete rule implementation (~80 lines)internal/rules/no_constructor_return/no_constructor_return_test.go- Comprehensive test suite (~200 lines)🤖 Generated with Claude Code