Open modulovalue opened 10 months ago
Seems like the easiest fix is to change the spec. We generally allow trailing commas on places like this.
Right, I don't think it's reasonable to introduce a breaking change in order to remove support for trailing commas in in particular situation where it is actually already implemented.
One thing, though, does dart format
know about these trailing commas? I would have expected the following to have more line breaks, but it is actually formatted as shown here:
void main() {
for (int i; i < 10; print("foo"), ++i, print("bar"),) {
break;
}
}
@munificent, what do you think about trailing commas in <forLoopParts>
?
Wow, I'm a little surprised the formatter doesn't end up inadvertently dropping the trailing comma on the floor.
Filed this to track how we want to handle it: https://github.com/dart-lang/dart_style/issues/1354
As far as the language is concerned, it's hard to say. The language doesn't seem to have any consistent principle for where trailing commas are allowed:
I guess for loop updaters are most similar to enum values where there is a closing delimiter, but the stuff inside the delimiter is more than just the comma-separated list of things? In an enum, there may be ;
followed by members. In a for loop, there are the preceding for parts clauses.
For enums, I'd say there is a trailing delimiter, which is either }
or ;
.
I agree that the leading and trailing delimiters are not necessarily matched, and for loop increments are like that too, started by ;
and ended by )
.
Inside that, for-loop increments are more like function arguments, they are comma separated completely general expressions.
Currently implements
also has a recognizable end, terminated by either {
or ;
(mixin application classes). I'd avoid exploiting that, since it's really just an accident that nothing else can come after.
I hope this issue isn't viewed as some evidence that there's "people who depend on this feature". I've discovered this one by accident (it seems that @lrhn discovered it first: https://github.com/dart-lang/language/issues/2430#issuecomment-1225458685!) and I don't plan on ever using it. I wouldn't mind a breaking change that removed this feature to prevent any potential formatter issues.
@munificent wrote
As far as the language is concerned, it's hard to say. The language doesn't seem to have any consistent principle for where trailing commas are allowed:
- Has closing delimiter and trailing comma is allowed: collection literal, argument list, parameter list, assert statement arguments.
- Has closing delimiter but disallows trailing comma: type parameter list, type argument list.
Consider:
Notice the trailing comma, which, according to the spec, doesn't appear to be valid Dart syntax. However, that program is accepted as a valid dart program by the implementation.
The analyzer and parsers based on Dart.g produce the following parse trees:
Notice how the ANTLR based parser rejects that program, but the analyzer does not.