Skip to content

Conversation

@tt-a1i
Copy link
Contributor

@tt-a1i tt-a1i commented Dec 15, 2025

Summary

Implements the eslint/no-sequences rule that disallows the use of the comma operator.

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand. However, this frequently obscures side effects, and its use is often an accident.

Features

  • Allows comma operator in for loop init and update expressions (standard JS pattern)
  • allowInParentheses option (default: true) allows sequences wrapped in parentheses
  • For grammar positions requiring parentheses (if, while, do-while, switch, with), double parentheses are needed to indicate intentionality

Examples

Incorrect:

foo = doSomething(), val;
0, eval("doSomething();");
do {} while ((doSomething(), !!test)); // needs double parens

Correct:

foo = (doSomething(), val);
(0, eval)("doSomething();");
do {} while (((doSomething(), !!test))); // double parens
for (i = 0, j = 10; i < j; i++, j--) {} // for loop allowed

Closes #481 (partially - adds no-sequences)

@tt-a1i tt-a1i requested a review from camc314 as a code owner December 15, 2025 06:12
@github-actions github-actions bot added A-linter Area - Linter C-enhancement Category - New feature or request labels Dec 15, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 15, 2025

CodSpeed Performance Report

Merging #16872 will not alter performance

Comparing tt-a1i:feat/linter-no-sequences (3691618) with main (bb86e0e)

Summary

✅ 4 untouched
⏩ 41 skipped1

Footnotes

  1. 41 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@camc314 camc314 self-assigned this Dec 15, 2025
Copy link
Contributor

@camc314 camc314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

tt-a1i and others added 8 commits December 15, 2025 09:37
This rule disallows the use of the comma operator, except in:
- For loop initializers and update expressions
- Expressions wrapped in parentheses (when allowInParentheses is true)

For grammar positions that require parentheses (if, while, do-while,
switch, with), double parentheses are needed to indicate intentionality.

Options:
- allowInParentheses (default: true): allows comma operator when wrapped
  in parentheses
Follow oxc conventions by using DefaultRuleConfig for cleaner configuration parsing.
- Fix for loop detection to skip ParenthesizedExpression nodes
- Add ArrowFunctionExpression body to double-parens requirement
- Add WithStatement test coverage
- Fix for ((a,b);;) with allowInParentheses: false (ESLint compatibility)
…ibility

The grammar-required parentheses (e.g., `if (...)`) don't appear in the AST
as ParenthesizedExpression nodes. So for if/while/do/switch/with, paren_depth
of 1 already indicates an extra layer of parentheses showing intentional use.

Only arrow function bodies need paren_depth >= 2 because the first layer
is syntactically required (`() => (a, b)` vs `() => a, b` which would be
parsed as `(() => a), b`).

Test cases now match ESLint official documentation examples:
- `if ((doSomething(), !!test))` - PASS (1 extra paren)
- `do {} while ((doSomething(), !!test))` - PASS (1 extra paren)
- `() => (a, b)` - FAIL (needs double parens)
- `() => ((a, b))` - PASS (2 parens for arrow body)
Sync documentation examples with actual implementation:
- Conditions only need single extra parentheses
- Arrow function body needs double parentheses
@camc314 camc314 force-pushed the feat/linter-no-sequences branch from 4464284 to 3691618 Compare December 15, 2025 09:37
@camc314 camc314 merged commit 5a95f62 into oxc-project:main Dec 15, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-linter Area - Linter C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linter Product Plan and Progress

2 participants