Skip to content

fix(eslint-plugin): enforce-update-with-where false positive when .from() precedes .where()#5629

Open
Yanhu007 wants to merge 1 commit into
drizzle-team:mainfrom
Yanhu007:fix/eslint-update-where-from-chain
Open

fix(eslint-plugin): enforce-update-with-where false positive when .from() precedes .where()#5629
Yanhu007 wants to merge 1 commit into
drizzle-team:mainfrom
Yanhu007:fix/eslint-update-where-from-chain

Conversation

@Yanhu007
Copy link
Copy Markdown

Summary

Fixes #5612

The enforce-update-with-where rule incorrectly flags update chains where .from() appears before .where():

// ❌ Incorrectly flagged as missing .where()
await tx
  .update(table)
  .set({ ... })
  .from(sql\`(VALUES ...) AS v(...)\`)
  .where(eq(table.id, v.id));

Root Cause

The rule used a module-level lastNodeName variable 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 — so lastNodeName is 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 lastNodeName approach with a chainContainsWhere() function that walks upward through the AST from the .set() node, following the CallExpression → MemberExpression chain pattern, to check if .where() exists anywhere in the method chain.

Tests

Added valid cases for .from().where() chains.

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: enforce-update-with-where false positive when .from() precedes .where() in chain

1 participant