Skip to content

[eslint-plugin-react-hooks] exhaustive-deps leaves sparse array holes when removing an Effect Event from a dependency list #38442

Backlog

Description

@Tortleya

Affected version and impact

I tested [email protected] (current npm latest) with [email protected]. Accepting the “Remove the dependency” suggestion for an Effect Event in a multi-element dependency array leaves a sparse array hole whenever a comma follows the removed identifier (e.g., [onTick, b] -> [, b], [a, onTick, b] -> [a, , b], [a, onTick,] -> [a, ,]), keeping the array's old length and violating ESLint's core no-sparse-arrays rule. Removing a final entry without a trailing comma leaves a stray trailing comma ([a, onTick] -> [a, ]). This happens with the normal editor suggestion, and the same edit is applied automatically when enableDangerousAutofixThisMayCauseInfiniteLoops: true is enabled.

Reproduction

Run this in an empty directory.

npm install [email protected] [email protected]
node <<'JS'
const {Linter} = require('eslint');
const hooks = require('eslint-plugin-react-hooks');

const source = `function Widget({a, b}) {
  const onTick = useEffectEvent(() => {});
  useEffect(() => { console.log(a, b); onTick(); }, [a, onTick, b]);
}`;

const linter = new Linter();
const config = {plugins: {hooks}, rules: {'hooks/exhaustive-deps': 'warn'}};
const [{suggestions}] = linter.verify(source, config);
const {range: [start, end], text} = suggestions[0].fix;
const edited = source.slice(0, start) + text + source.slice(end);
console.log('suggestion:', edited.split('\n')[2].trim());
console.log('diagnostics after edit:', linter.verify(edited, config).length);
console.log('no-sparse-arrays:', linter.verify(edited, {rules: {'no-sparse-arrays': 'error'}}).map(m => m.message));

const autofix = linter.verifyAndFix(source, {
  plugins: {hooks},
  rules: {'hooks/exhaustive-deps': ['warn', {enableDangerousAutofixThisMayCauseInfiniteLoops: true}]},
});
console.log('autofix:   ', autofix.output.split('\n')[2].trim());
JS

Output

suggestion: useEffect(() => { console.log(a, b); onTick(); }, [a, , b]);
diagnostics after edit: 0
no-sparse-arrays: [ 'Unexpected comma in middle of array.' ]
autofix:    useEffect(() => { console.log(a, b); onTick(); }, [a, , b]);

Both the suggestion and autofix leave [a, , b]); (which triggers no-sparse-arrays), and re-linting exhaustive-deps reports zero diagnostics because the dependency-array iteration skips elided elements. I expected [a, b]);. A single-element example misses this because removing its only identifier already produces [].

Cause and possible fix

At edae846a9a17442c00429f2b15375afbfa0376c9, ExhaustiveDeps.ts#L795-L814 calls fixer.removeRange(declaredDependencyNode.range!) at #L809, removing only the identifier's range without removing its adjoining comma. Removing the appropriate adjoining comma would preserve the remaining expressions cleanly; care is needed when two Effect Events are adjacent (e.g., [onTick, onTock]) so separate fixes do not produce overlapping edit ranges. Regression tests covering first, middle, last (with and without trailing comma), adjacent, and commented entries would lock down the token boundaries.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions