dart-lang / tools

This repository is home to tooling related Dart packages.
BSD 3-Clause "New" or "Revised" License
30 stars 24 forks source link

Allow omitting parentheses for Expression.asA(Expression other) #1140

Open Robbendebiene opened 1 year ago

Robbendebiene commented 1 year ago

Expression.asA(Expression other) always wraps the code into parentheses. While this is a good default it is not always necessary. The problem for me is that the generated code triggers the linter rule unnecessary_parenthesis in our CI. Unfortunately dart_style auto fixes does not support the removal of unnecessary parenthesis.

My suggestion would be to have an optional parameter. the following:

https://github.com/dart-lang/code_builder/blob/006790d8f3245124cc322bdf50376536ba28d85c/lib/src/specs/expression.dart#L68-L73

becomes

  Expression asA(Expression other, { bool parenthesized = true }) {
     final exp = BinaryExpression._( expression, other,  'as'));
     return parenthesized
        ? ParenthesizedExpression._(exp)
        : exp
  }
natebosch commented 1 year ago

We typically recommend avoiding that type of style lint for generated code. As mentioned in dart-lang/tools#828 readability is not a goal for the generated output from this package. This is a relatively easy change though, and most of the complexity stays in the calling code deciding what value to pass.

@srawlins do you have any opinions on adding this option?

srawlins commented 1 year ago

Yeah I think that supporting a parenthesized parameter everywhere (it's not just asA right?) would add a fair amount of complexity.

I recommend using // ignore_for_file: type=lint for generated files. See https://dart.dev/tools/analysis#suppressing-rules-for-a-file.