fix(eslint): stop layout-sensitive-apis rule flagging object literals - #1932
Draft
hoebbelsB wants to merge 1 commit into
Draft
fix(eslint): stop layout-sensitive-apis rule flagging object literals#1932hoebbelsB wants to merge 1 commit into
hoebbelsB wants to merge 1 commit into
Conversation
The rule's Property selector matched any object property named after a layout-sensitive DOM API, including plain object literals (e.g. Angular CDK's ConnectionPositionPair with offsetX/offsetY) that never touch the DOM. Narrow the selector to `ObjectPattern > Property` so only destructuring reads are flagged, matching the MemberExpression branch's intent of catching actual element reads/writes. Object creation (ObjectExpression) is now exempt. Closes #1603
|
View your CI Pipeline Execution ↗ for commit 4add4cc
💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗ ☁️ Nx Cloud last updated this comment at |
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.
Problem
The prefer-no-layout-sensitive-apis rule flags any object property whose name matches a layout-sensitive DOM API (offsetX, offsetY, scrollTop, clientWidth, etc.), even when the object is a plain object literal that never touches the DOM. Reported in #1603: adding Angular CDK's ConnectionPositionPair literal (which legitimately has offsetX/offsetY fields) triggers a hard lint error under both shipped presets, where the rule is 'error', not 'warn'.
Root cause
The rule used the selector MemberExpression[property.name=apisRegex], Property[key.name=apisRegex]. The Property half matches any Property AST node with a matching key name, regardless of whether it's inside an ObjectPattern (a destructuring read) or an ObjectExpression (an object literal being created). Only the former is an actual DOM-layout read.
Fix
Narrowed the selector to only match Property nodes that are direct children of an ObjectPattern. The MemberExpression branch and handler body are unchanged. Pure selector narrowing - no switch to AssignmentExpression (would regress existing destructuring true positives) and no type-aware linting (rejected in the issue thread for lint-performance and API-surface reasons).
Accepted trade-off: the rule can no longer catch element writes funneled through an object literal, e.g. Object.assign(el, { scrollTop: 0 }). Deliberate cost of staying syntactic.
Changes
Verification
npx nx test eslint-plugin (twice, once with --skip-nx-cache): 12 suites / 68 tests passing (baseline on main was 12/58 - 10 new tests, no regressions). Targeted run: 1 suite / 21 tests passing. This worktree had no node_modules installed at all initially, causing a spurious all-suite failure unrelated to the change; confirmed via git stash that main fails identically without the fix, then ran yarn install --immutable to get a real signal.
Open questions
Closes #1603
Open questions