linter icon indicating copy to clipboard operation
linter copied to clipboard

Linter should suggest having a space on either side of any binary operators, and before any unary operators

Open Hixie opened this issue 9 years ago • 11 comments

This code shows why you might care:

void main() {
  print(5 ~/ 10); // prints the result of integer divining five by ten
  print(5 /~ 10); // prints the result of dividing five by the bitwise-complement of ten
  int x = 10;
  x += 1; // adds one to x
  x =+ 1; // sets x to one
}

With the suggested lint, the following code would be ok:

void main() {
  print(5 ~/ 10);
  print(5 / ~10);
  int x = 10;
  x += 1;
  x = +1;
}

Hixie avatar May 24 '16 20:05 Hixie

Shouldn't this be handled by dartfmt?

zoechi avatar May 25 '16 08:05 zoechi

Shouldn't this be handled by dartfmt?

In general, yes. However, some folks don't use it. (Specifically, flutter.)

pq avatar May 25 '16 13:05 pq

FWIW this bit

x =+ 1; // sets x to one

produces a warning ("expected an identifier"). Maybe it didn't before?

@Hixie : is the intent here to make it more obvious when someone intends a binary op but is getting a unary one? Typing, for example, 5 /~ 10 when they meant 5 ~/ 10?

pq avatar Jun 29 '16 17:06 pq

The code x =+ 1; produces an error because + is not a valid prefix operator.

bwilkerson avatar Jun 29 '16 17:06 bwilkerson

is the intent here to make it more obvious when someone intends a binary op but is getting a unary one? Typing, for example, 5 /~ 10 when they meant 5 ~/ 10?

That was in fact the exact case that led to my filing this bug.

Regarding =+, change all my examples from using + to using -.

Hixie avatar Jun 29 '16 23:06 Hixie

Update: the lint as described in 1fdc9f9 caused enough confusion (namely around the overlapping roles of the formatter and analyzer) that we've decided to back it out pending more consideration. Along those lines, @bwilkerson had some interesting thoughts around focusing on the particular mistakes (e.g., ~/ vs. /~) rather than more generally linting something ensured by the formatter.

Sidenote: this further emphasizes the value in finding a happy place where the formatter plays nice with (in this case Flutter) idiomatic Dart. Luckily, folks are working on just that. 👍

pq avatar Jul 28 '16 15:07 pq

Just my 2c: I think we'd be much more productive to let dartfmt make rules about whitespace, and linter make rules about anything but whitespace. It would be a real waste to get into the whitespace game int the linter...

srawlins avatar May 16 '18 18:05 srawlins

some of us can't use the formatter because it is too opinionated, but still want to avoid bugs like this one.

Hixie avatar May 16 '18 18:05 Hixie

A simplified formatter that doesn't change line-breaks, similar to how it works in IntelliJ with TypeScript would probably provide more value than linter rules.

zoechi avatar May 16 '18 18:05 zoechi

If we had a formatter that didn't mess up expressions like:

  if (offset.x == velocity.x * xFactor + x0 &&
      offset.y == velocity.y           + xy)
    return true;

...then sign me up. Until then, a linter can solve the problem described in the OP in a way a formatter can't.

(see https://github.com/dart-lang/dart_style/issues/530, https://github.com/dart-lang/dart_style/issues/531, https://github.com/dart-lang/dart_style/issues/528, https://github.com/dart-lang/dart_style/issues/525, or my personal favourite, https://github.com/dart-lang/dart_style/issues/452)

Hixie avatar May 16 '18 19:05 Hixie

@Hixie I guess in this case I'm in favor of linter rules as well :D

zoechi avatar May 16 '18 19:05 zoechi