-
Notifications
You must be signed in to change notification settings - Fork 10.4k
chore(eslint): add no-this-in-static-method rule to prevent context loss #22410
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
chore(eslint): add no-this-in-static-method rule to prevent context loss #22410
Conversation
…ss in static methods Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 2 Skipped Deployments
|
✅ No security or compliance issues detected. Reviewed everything up to 615e4d4. Security Overview
Detected Code Changes
Reply to this PR with |
…irect calls Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>
…is.method' Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>
…ction args, and returns Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>
Working well! |
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.
cubic found 1 issue across 3 files. Review it in cubic.dev
React with 👍 or 👎 to teach cubic. Tag @cubic-dev-ai
to give specific feedback.
Graphite Automations"Add consumer team as reviewer" took an action on this PR • (07/11/25)1 reviewer was added to this PR based on Keith Williams's automation. "Add ready-for-e2e label" took an action on this PR • (07/18/25)1 label was added to this PR based on Keith Williams's automation. |
WalkthroughA new ESLint rule, Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
packages/eslint-plugin/src/rules/no-this-in-static-method.tsOops! Something went wrong! :( ESLint: 8.57.1 ESLint couldn't find the plugin "eslint-plugin-playwright". (The package "eslint-plugin-playwright" was not found when loaded as a Node module from the directory "".) It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:
The plugin "eslint-plugin-playwright" was referenced from the config file in ".eslintrc.js". If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team. packages/eslint-plugin/src/rules/index.tsOops! Something went wrong! :( ESLint: 8.57.1 ESLint couldn't find the plugin "eslint-plugin-playwright". (The package "eslint-plugin-playwright" was not found when loaded as a Node module from the directory "".) It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:
The plugin "eslint-plugin-playwright" was referenced from the config file in ".eslintrc.js". If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team. packages/eslint-plugin/src/configs/recommended.tsOops! Something went wrong! :( ESLint: 8.57.1 ESLint couldn't find the plugin "eslint-plugin-playwright". (The package "eslint-plugin-playwright" was not found when loaded as a Node module from the directory "".) It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:
The plugin "eslint-plugin-playwright" was referenced from the config file in ".eslintrc.js". If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team. ✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
♻️ Duplicate comments (1)
packages/eslint-plugin/src/rules/no-this-in-static-method.ts (1)
8-28
: Fix nested static methods tracking issue.The current implementation uses a single boolean flag to track static method context, which fails when static methods are nested (e.g., a static method containing a nested class with its own static method). This creates false negatives where the rule stops detecting issues in the outer method after exiting the inner one.
Use a counter or stack-based approach instead:
- let currentMethodIsStatic = false; + let staticMethodDepth = 0; let currentClassName = ""; + const classNameStack: string[] = []; return { MethodDefinition(node) { if (node.static && node.key.type === "Identifier") { - currentMethodIsStatic = true; + staticMethodDepth++; if (node.parent?.type === "ClassBody" && node.parent.parent?.type === "ClassDeclaration") { const classNode = node.parent.parent as TSESTree.ClassDeclaration; if (classNode.id?.name) { - currentClassName = classNode.id.name; + classNameStack.push(classNode.id.name); + currentClassName = classNode.id.name; } } } }, "MethodDefinition:exit"(node: TSESTree.MethodDefinition) { if (node.static) { - currentMethodIsStatic = false; - currentClassName = ""; + staticMethodDepth--; + if (staticMethodDepth === 0) { + currentClassName = ""; + classNameStack.length = 0; + } else { + classNameStack.pop(); + currentClassName = classNameStack[classNameStack.length - 1] || ""; + } } }, MemberExpression(node) { if ( - currentMethodIsStatic && + staticMethodDepth > 0 &&
🧹 Nitpick comments (2)
packages/eslint-plugin/src/rules/no-this-in-static-method.ts (2)
37-61
: Consider edge cases in function argument detection.The
isFunctionArgument
logic excludes array methods but might miss other scenarios where context loss occurs. Consider these edge cases:
- Method chaining:
items.map(this.method).filter(this.otherMethod)
- Nested function calls:
Promise.resolve(this.method)
- Event handlers:
element.addEventListener('click', this.method)
Consider expanding the detection logic or documenting known limitations:
const isFunctionArgument = parent?.type === "CallExpression" && parent.arguments.includes(node) && !( parent.callee.type === "MemberExpression" && parent.callee.property.type === "Identifier" && - ["map", "filter", "forEach", "reduce", "find", "some", "every"].includes( + ["map", "filter", "forEach", "reduce", "find", "some", "every", "then", "catch", "finally"].includes( parent.callee.property.name ) );
80-85
: Enhance auto-fix safety checks.The auto-fix implementation should validate that the replacement is safe and doesn't introduce naming conflicts.
Add safety checks for the auto-fix:
fix(fixer) { - if (currentClassName && node.property.type === "Identifier") { + if (currentClassName && node.property.type === "Identifier") { + // Ensure the class name is valid and doesn't conflict + if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(currentClassName)) { + return null; + } return fixer.replaceText(node, `${currentClassName}.${node.property.name}`); } return null; },
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
packages/eslint-plugin/src/configs/recommended.ts
(1 hunks)packages/eslint-plugin/src/rules/index.ts
(1 hunks)packages/eslint-plugin/src/rules/no-this-in-static-method.ts
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Install dependencies / Yarn install & cache
- GitHub Check: Security Check
🔇 Additional comments (3)
packages/eslint-plugin/src/configs/recommended.ts (1)
11-11
: LGTM! Rule correctly added to recommended configuration.The new rule is properly integrated into the recommended configuration with appropriate error severity.
packages/eslint-plugin/src/rules/index.ts (1)
11-11
: LGTM! Rule correctly exported in the rules index.The new rule export follows the established pattern and uses consistent naming.
packages/eslint-plugin/src/rules/no-this-in-static-method.ts (1)
92-107
: LGTM! Rule metadata is well-structured.The rule metadata correctly defines the rule as a problem type with error recommendation, includes clear messaging, and properly declares fixable capability.
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.
Working well for me - lets double check with @calcom/foundation
E2E results are ready! |
…oss (#22410) Co-authored-by: eunjae@cal.com <hey@eunjae.dev> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Keith Williams <keithwillcode@gmail.com> Co-authored-by: Omar López <zomars@me.com>
ESLint Rule: Detect
this.method
Context Loss in Static MethodsSummary
Adds a new ESLint rule
@calcom/eslint/no-this-in-static-method
that detects whenthis.method
references in static methods will lose their context binding. This prevents runtime errors like the one found inRoleOutputMapper.ts
wherethis.toDomainPermission
was passed to.map()
causing the method to lose itsthis
context.The rule detects 6 different patterns where context loss occurs:
items.map(this.method)
const fn = this.method
{ prop: this.method }
[this.method]
someFunc(this.method)
return this.method
The rule allows direct method calls (
this.method()
) since these preserve context, and includes auto-fix functionality that replacesthis.methodName
withClassName.methodName
.Examples of what gets flagged:
Examples of what's allowed:
Review & Testing Checklist for Human
Risk Level: 🟡 Yellow - Complex AST logic with auto-fix functionality
this.methodName
withClassName.methodName
and doesn't break codeRecommended Test Plan:
yarn lint
on the entire codebase to see what the rule catchesyarn lint --fix
on a test file with violationsDiagram
Notes
RoleOutputMapper.ts
andinput-schedules.service.ts
that this rule will catchthis.method
usageSession Details: