-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add low_readability_numeric_literals lint rule and quick fix (#210
- Loading branch information
Showing
8 changed files
with
219 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
114 changes: 114 additions & 0 deletions
114
packages/nilts/lib/src/lints/low_readability_numeric_literals.dart
This file contains 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,114 @@ | ||
import 'package:analyzer/error/error.dart' as analyzer; | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
import 'package:nilts/src/change_priority.dart'; | ||
|
||
/// A class for `low_readability_numeric_literals` rule. | ||
/// | ||
/// This rule checks numeric literals with 5 or more digits. | ||
/// | ||
/// - Target SDK : >= Flutter 3.27.0 (Dart 3.6.0) | ||
/// - Rule type : Practice | ||
/// - Maturity level : Experimental | ||
/// - Quick fix : ✅ | ||
/// | ||
/// **Consider** using digit separators for numeric literals with 5 or more | ||
/// digits to improve readability. | ||
/// | ||
/// **BAD:** | ||
/// ```dart | ||
/// const int value = 123456; | ||
/// ``` | ||
/// | ||
/// **GOOD:** | ||
/// ```dart | ||
/// const int value = 123_456; | ||
/// ``` | ||
/// | ||
/// See also: | ||
/// | ||
/// - [Digit Separators in Dart 3.6](https://medium.com/dartlang/announcing-dart-3-6-778dd7a80983) | ||
/// - [Built-in types | Dart](https://dart.dev/language/built-in-types#numbers) | ||
class LowReadabilityNumericLiterals extends DartLintRule { | ||
/// Creates a new instance of [LowReadabilityNumericLiterals]. | ||
const LowReadabilityNumericLiterals() : super(code: _code); | ||
|
||
static const _code = LintCode( | ||
name: 'low_readability_numeric_literals', | ||
problemMessage: | ||
'Numeric literals with 5 or more digits should use digit separators ' | ||
'for better readability.', | ||
url: 'https://github.com/ronnnnn/nilts#low_readability_numeric_literals', | ||
); | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ErrorReporter reporter, | ||
CustomLintContext context, | ||
) { | ||
context.registry.addIntegerLiteral((node) { | ||
final value = node.value; | ||
if (value == null) return; | ||
|
||
final literal = node.literal.lexeme; | ||
if (literal.contains('_')) return; | ||
|
||
if (value.abs() >= 10000) { | ||
reporter.atNode(node, _code); | ||
} | ||
}); | ||
} | ||
|
||
@override | ||
List<Fix> getFixes() => [ | ||
_AddDigitSeparators(), | ||
]; | ||
} | ||
|
||
class _AddDigitSeparators extends DartFix { | ||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ChangeReporter reporter, | ||
CustomLintContext context, | ||
analyzer.AnalysisError analysisError, | ||
List<analyzer.AnalysisError> others, | ||
) { | ||
context.registry.addIntegerLiteral((node) { | ||
if (!node.sourceRange.intersects(analysisError.sourceRange)) return; | ||
|
||
final value = node.value; | ||
if (value == null) return; | ||
|
||
final literal = node.literal.lexeme; | ||
if (literal.contains('_')) return; | ||
|
||
reporter | ||
.createChangeBuilder( | ||
message: 'Add digit separators', | ||
priority: ChangePriority.addDigitSeparators, | ||
) | ||
.addDartFileEdit((builder) { | ||
final newLiteral = _addSeparators(literal); | ||
builder.addSimpleReplacement(node.sourceRange, newLiteral); | ||
}); | ||
}); | ||
} | ||
|
||
String _addSeparators(String literal) { | ||
final buffer = StringBuffer(); | ||
var count = 0; | ||
|
||
for (var i = literal.length - 1; i >= 0; i--) { | ||
buffer.write(literal[i]); | ||
count++; | ||
if (count == 3 && i != 0) { | ||
buffer.write('_'); | ||
count = 0; | ||
} | ||
} | ||
|
||
return buffer.toString().split('').reversed.join(); | ||
} | ||
} |
This file contains 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
63 changes: 63 additions & 0 deletions
63
packages/nilts_test/test/lints/low_readability_numeric_literals.dart
This file contains 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,63 @@ | ||
// ignore_for_file: avoid_js_rounded_ints | ||
// ignore_for_file: prefer_const_declarations | ||
// ignore_for_file: prefer_final_locals | ||
// ignore_for_file: unused_element | ||
// ignore_for_file: unused_local_variable | ||
|
||
const _globalConstant1 = 1234; | ||
// expect_lint: low_readability_numeric_literals | ||
const _globalConstant2 = 12345; | ||
|
||
var _globalVariable1 = 1234; | ||
// expect_lint: low_readability_numeric_literals | ||
var _globalVariable2 = 12345; | ||
|
||
final _globalFinal1 = 1234; | ||
// expect_lint: low_readability_numeric_literals | ||
final _globalFinal2 = 12345; | ||
|
||
void main() { | ||
const constant1 = 1234; | ||
// expect_lint: low_readability_numeric_literals | ||
const constant2 = 12345; | ||
const constant3 = 12_345; | ||
// expect_lint: low_readability_numeric_literals | ||
const constant4 = 1234567890; | ||
const constant5 = 1_234_567_890; | ||
// expect_lint: low_readability_numeric_literals | ||
const constant6 = 123456789012345; | ||
const constant7 = 123_456_789_012_345; | ||
|
||
var variable1 = 1234; | ||
// expect_lint: low_readability_numeric_literals | ||
var variable2 = 12345; | ||
var variable3 = 12_345; | ||
// expect_lint: low_readability_numeric_literals | ||
var variable4 = 1234567890; | ||
var variable5 = 1_234_567_890; | ||
// expect_lint: low_readability_numeric_literals | ||
var variable6 = 123456789012345; | ||
var variable7 = 123_456_789_012_345; | ||
|
||
final final1 = 1234; | ||
// expect_lint: low_readability_numeric_literals | ||
final final2 = 12345; | ||
final final3 = 12_345; | ||
// expect_lint: low_readability_numeric_literals | ||
final final4 = 1234567890; | ||
final final5 = 1_234_567_890; | ||
// expect_lint: low_readability_numeric_literals | ||
final final6 = 123456789012345; | ||
final final7 = 123_456_789_012_345; | ||
|
||
const hex1 = 0x1234; | ||
// expect_lint: low_readability_numeric_literals | ||
const hex2 = 0x12345; | ||
const hex3 = 0x12_345; | ||
// expect_lint: low_readability_numeric_literals | ||
const hex4 = 0x1234567890; | ||
const hex5 = 0x1_234_567_890; | ||
// expect_lint: low_readability_numeric_literals | ||
const hex6 = 0x123456789012345; | ||
const hex7 = 0x123_456_789_012_345; | ||
} |
This file contains 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