[Eslint Plugin] Fix enforce-update-with-where false positive when .from() precedes .where()#5786
Open
LeSingh1 wants to merge 1 commit into
Open
Conversation
…om() precedes .where() The enforce-update-with-where rule tracked the previously visited MemberExpression to decide whether .set() was followed by .where(). ESLint visits MemberExpression nodes from the outermost call inward, so for db.update().set().from(...).where(...) the .from MemberExpression was the most recently visited node by the time the rule reached .set, causing a false positive. The rule now walks up the AST from the .set() MemberExpression along the surrounding chain of CallExpression/MemberExpression nodes and reports only when .where is not present anywhere later in that chain. Closes 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.
Closes #5612.
The enforce-update-with-where rule reports an update without where when .from() appears before .where() in the chain, for example:
The rule decides whether to report on the .set() MemberExpression by checking a module-level lastNodeName variable that is updated each time a MemberExpression is visited. ESLint visits MemberExpression nodes from the outermost call inward, so for the chain above the visit order is .where, then .from, then .set, then .update. By the time .set is visited, lastNodeName is "from" rather than "where", so the rule fires even though .where is present in the same chain.
The fix replaces the visit-order heuristic with a small AST walk that follows the surrounding CallExpression and MemberExpression nodes upward from the .set() MemberExpression, and reports only when no later chained call uses the name where. Behavior for chains without a trailing .from() is unchanged.
Tests:
Added two valid cases to tests/update.test.ts covering .from() before .where(), with and without a trailing .returning(). Both fail on main and pass with the fix. All 113 existing rule tests still pass.