fix(eslint-plugin): enforce-update-with-where false positive when .from() precedes .where()#5629
Open
Yanhu007 wants to merge 1 commit into
Open
Conversation
…om() precedes .where()
The rule used a global lastNodeName variable to track whether .where()
appeared before .set() in the AST traversal order. Since .set() is
visited before .from().where() in depth-first traversal, the rule
incorrectly reported a violation for:
db.update(table).set({...}).from(sql).where(eq(...))
Replace the lastNodeName approach with chainContainsWhere() that walks
upward through the AST from the .set() MemberExpression, following
the CallExpression → MemberExpression chain pattern, to check if
.where() exists anywhere later in the chain.
Fixes drizzle-team#5612
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #5612
The
enforce-update-with-whererule incorrectly flags update chains where.from()appears before.where():Root Cause
The rule used a module-level
lastNodeNamevariable to track whether.where()appeared before.set()in AST traversal order. Since ESLint visits nodes depth-first, the.set()MemberExpression is visited before the outer.from().where()chain — solastNodeNameis never'where'when the rule checks.This approach also has a secondary issue: the global variable leaks state across unrelated expressions in the same file.
Fix
Replace the
lastNodeNameapproach with achainContainsWhere()function that walks upward through the AST from the.set()node, following theCallExpression → MemberExpressionchain pattern, to check if.where()exists anywhere in the method chain.Tests
Added valid cases for
.from().where()chains.