This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
Add require_trailing_comma lint rule. #2557
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
56e2a4a
Add require_trailing_comma lint rule.
lauweijie a25cde1
Fix formatting with dartfmt.
lauweijie fccdfce
Address some reviewer comments.
lauweijie 5a6b7cd
Rename require_trailing_comma to require_trailing_commas.
lauweijie a234db4
Add note that the lint rule assumes `dartfmt` has been run over the c…
lauweijie 36551d3
Update note on running dartfmt
lauweijie 0a7a68b
Annotate lint as experimental
lauweijie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/token.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
|
||
import '../analyzer.dart'; | ||
|
||
const _desc = r'Use trailing commas for all function calls and declarations.'; | ||
|
||
const _details = r''' | ||
|
||
**DO** use trailing commas for all function calls and declarations unless the | ||
function call or definition, from the start of the function name up to the | ||
closing parenthesis, fits in a single line. | ||
|
||
**GOOD:** | ||
```dart | ||
void run() { | ||
method( | ||
'does not fit on one line', | ||
'test test test test test test test test test test test', | ||
); | ||
} | ||
``` | ||
|
||
**BAD:** | ||
```dart | ||
void run() { | ||
method('does not fit on one line', | ||
'test test test test test test test test test test test'); | ||
} | ||
``` | ||
|
||
**Exception:** If the final parameter/argument is positional (vs named) and is | ||
either a function literal implemented using curly braces, a literal map, a | ||
literal set or a literal array. This exception only applies if the final | ||
parameter does not fit entirely on one line. | ||
|
||
**Note:** This lint rule assumes `dartfmt` has been run over the code and may | ||
produce false positives until that has happened. | ||
|
||
'''; | ||
|
||
class RequireTrailingCommas extends LintRule implements NodeLintRule { | ||
RequireTrailingCommas() | ||
: super( | ||
name: 'require_trailing_commas', | ||
description: _desc, | ||
details: _details, | ||
group: Group.style, | ||
maturity: Maturity.experimental, | ||
); | ||
|
||
@override | ||
void registerNodeProcessors( | ||
NodeLintRegistry registry, | ||
LinterContext context, | ||
) { | ||
var visitor = _Visitor(this); | ||
registry | ||
..addCompilationUnit(this, visitor) | ||
..addArgumentList(this, visitor) | ||
..addFormalParameterList(this, visitor) | ||
..addAssertStatement(this, visitor) | ||
..addAssertInitializer(this, visitor); | ||
} | ||
} | ||
|
||
class _Visitor extends SimpleAstVisitor<void> { | ||
static const _trailingCommaCode = LintCode( | ||
'require_trailing_commas', | ||
'Missing a required trailing comma.', | ||
); | ||
|
||
final LintRule rule; | ||
|
||
LineInfo? _lineInfo; | ||
|
||
_Visitor(this.rule); | ||
|
||
@override | ||
void visitCompilationUnit(CompilationUnit node) => _lineInfo = node.lineInfo; | ||
|
||
@override | ||
void visitArgumentList(ArgumentList node) { | ||
super.visitArgumentList(node); | ||
if (node.arguments.isEmpty) return; | ||
_checkTrailingComma( | ||
node.leftParenthesis, | ||
node.rightParenthesis, | ||
node.arguments.last, | ||
); | ||
} | ||
|
||
@override | ||
void visitFormalParameterList(FormalParameterList node) { | ||
super.visitFormalParameterList(node); | ||
if (node.parameters.isEmpty) return; | ||
_checkTrailingComma( | ||
node.leftParenthesis, | ||
node.rightParenthesis, | ||
node.parameters.last, | ||
); | ||
} | ||
|
||
@override | ||
void visitAssertStatement(AssertStatement node) { | ||
super.visitAssertStatement(node); | ||
_checkTrailingComma( | ||
node.leftParenthesis, | ||
node.rightParenthesis, | ||
node.message ?? node.condition, | ||
); | ||
} | ||
|
||
@override | ||
void visitAssertInitializer(AssertInitializer node) { | ||
super.visitAssertInitializer(node); | ||
_checkTrailingComma( | ||
node.leftParenthesis, | ||
node.rightParenthesis, | ||
node.message ?? node.condition, | ||
); | ||
} | ||
|
||
void _checkTrailingComma( | ||
Token leftParenthesis, | ||
Token rightParenthesis, | ||
AstNode lastNode, | ||
) { | ||
// Early exit if trailing comma is present. | ||
if (lastNode.endToken.next?.type == TokenType.COMMA) return; | ||
|
||
// No trailing comma is needed if the function call or declaration, up to | ||
// the closing parenthesis, fits on a single line. Ensuring the left and | ||
// right parenthesis are on the same line is sufficient since dartfmt places | ||
// the left parenthesis right after the identifier (on the same line). | ||
if (_isSameLine(leftParenthesis, rightParenthesis)) return; | ||
|
||
// Check the last parameter to determine if there are any exceptions. | ||
if (_shouldAllowTrailingCommaException(lastNode)) return; | ||
|
||
rule.reportLintForToken(rightParenthesis, errorCode: _trailingCommaCode); | ||
} | ||
|
||
bool _isSameLine(Token token1, Token token2) => | ||
_lineInfo!.getLocation(token1.offset).lineNumber == | ||
_lineInfo!.getLocation(token2.offset).lineNumber; | ||
|
||
bool _shouldAllowTrailingCommaException(AstNode lastNode) { | ||
// No exceptions are allowed if the last parameter is named. | ||
if (lastNode is FormalParameter && lastNode.isNamed) return false; | ||
|
||
// No exceptions are allowed if the entire last parameter fits on one line. | ||
if (_isSameLine(lastNode.beginToken, lastNode.endToken)) return false; | ||
|
||
// Exception is allowed if the last parameter is a function literal. | ||
if (lastNode is FunctionExpression && lastNode.body is BlockFunctionBody) { | ||
return true; | ||
} | ||
|
||
// Exception is allowed if the last parameter is a set, map or list literal. | ||
if (lastNode is SetOrMapLiteral || lastNode is ListLiteral) return true; | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.