Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Hixie opened this issue May 24, 2016 · 11 comments
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. customer-flutter devexp-linter Issues with the analyzer's support for the linter package linter-lint-request P4 type-enhancement A request for a change that isn't a bug

Comments

@Hixie
Copy link
Contributor

Hixie commented May 24, 2016

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;
}
@pq pq added type-enhancement A request for a change that isn't a bug linter-lint-request customer-flutter labels May 25, 2016
@zoechi
Copy link
Contributor

zoechi commented May 25, 2016

Shouldn't this be handled by dartfmt?

@pq
Copy link
Member

pq commented May 25, 2016

Shouldn't this be handled by dartfmt?

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

@pq
Copy link
Member

pq commented Jun 29, 2016

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?

@bwilkerson
Copy link
Member

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

@Hixie
Copy link
Contributor Author

Hixie commented Jun 29, 2016

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 -.

pq referenced this issue in dart-archive/linter Jul 27, 2016
* Grinder support (`rule:rule_name` and `docs:location`) for rule stub and doc generation (respectively).
* Fix to allow leading underscores in `non_constant_identifier_names`.
* New `valid_regexps` lint (#277).
* New `whitespace_around_ops` lint (#249).
* Fix to `overridden_fields` to flag overridden static fields (#274).
* New `list_remove_unrelated_type` to detect passing a non-`T` value to `List.remove()`` (#271).
* New `empty_catches` lint to catch empty catch blocks (#43).
* Fixed `close_sinks` false positive (#268).
* Added `linter` support for `--strong` to allow for running linter in strong mode.

BUG=
[email protected]

Review URL: https://codereview.chromium.org//2182183004 .
@pq
Copy link
Member

pq commented Jul 28, 2016

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. 👍

@srawlins
Copy link
Member

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...

@Hixie
Copy link
Contributor Author

Hixie commented May 16, 2018

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

@zoechi
Copy link
Contributor

zoechi commented May 16, 2018

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.

@Hixie
Copy link
Contributor Author

Hixie commented May 16, 2018

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 dart-lang/dart_style#530, dart-lang/dart_style#531, dart-lang/dart_style#528, dart-lang/dart_style#525, or my personal favourite, dart-lang/dart_style#452)

@zoechi
Copy link
Contributor

zoechi commented May 16, 2018

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

@srawlins srawlins added the P4 label Oct 11, 2022
@devoncarew devoncarew added devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. labels Nov 18, 2024
@devoncarew devoncarew transferred this issue from dart-archive/linter Nov 18, 2024
@bwilkerson bwilkerson added area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. and removed legacy-area-analyzer Use area-devexp instead. labels Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. customer-flutter devexp-linter Issues with the analyzer's support for the linter package linter-lint-request P4 type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants