Skip to content

Disallow comma operator with side-effect-free left operands #10814

@RyanCavanaugh

Description

@RyanCavanaugh

It's easy to write code like this (#10802)

// This code does not do what it appears to!
let arr = [];
switch(arr.length) {
  case 0, 1:
    return 'zero or one';
  default:
    return 'more than one';
}

Or this

let x = Math.pow((3, 5)); // x = NaN, wat?

Or this:

let a = [(3 + 4), ((1 + 1, 8) * 4)]; // oops

We should disallow left comma operands when they are side-effect free. An expression is side-effect free (SEF) if it is:

  • A literal
  • An identifier
  • A unary operator whose operand is SEF
  • A binary operator, other than the assignment operators, whose operands are both SEF
  • A ternary operator whose operands are all SEF
  • A parenthesized expression which is SEF
  • A function expression

Metadata

Metadata

Assignees

Labels

Breaking ChangeWould introduce errors in existing codeFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions