Skip to content

Commit

Permalink
Prepare prefer_int_literals rule and fix for digit separators
Browse files Browse the repository at this point in the history
Work towards #56188

Cq-Include-Trybots: luci.dart.try:flutter-analyze-try,analyzer-win-release-try,pkg-win-release-try
Change-Id: I3a6ed7cb8b61a63c0b2742f408e42e4ae09d732a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/375741
Commit-Queue: Samuel Rawlins <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
srawlins authored and Commit Queue committed Jul 15, 2024
1 parent 23fb2df commit 63f47bb
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,25 @@ class ConvertToIntLiteral extends ResolvedCorrectionProducer {
try {
intValue = literal.value.truncate();
} catch (e) {
// Double cannot be converted to int
// A double that cannot be converted to an int.
return;
}

if (intValue == null || intValue != literal.value) {
if (intValue != literal.value) {
return;
}

await builder.addDartFileEdit(file, (builder) {
if (!literal.literal.lexeme.contains('e')) {
var indexOfDecimal = literal.literal.lexeme.indexOf('.');
if (indexOfDecimal > 0) {
// Preserve digit separators by just truncating the existing lexeme.
builder.addDeletion(SourceRange(literal.offset + indexOfDecimal,
literal.length - indexOfDecimal));
return;
}
}

builder.addReplacement(SourceRange(literal.offset, literal.length),
(builder) {
builder.write('$intValue');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ const double myDouble = 42;
''');
}

Future<void> test_decimal_negative() async {
await resolveTestCode('''
const double myDouble = -/*caret*/42.0;
''');
await assertHasAssist('''
const double myDouble = -42;
''');
}

Future<void> test_decimal_noAssistWithLint() async {
createAnalysisOptionsFile(lints: [LintNames.prefer_int_literals]);
verifyNoTestUnitErrors = false;
Expand All @@ -38,6 +47,24 @@ const double myDouble = /*caret*/42.0;
await assertNoAssist();
}

Future<void> test_decimal_noDigitsLeftOfDecimal() async {
await resolveTestCode('''
const double myDouble = /*caret*/.0;
''');
await assertHasAssist('''
const double myDouble = 0;
''');
}

Future<void> test_decimalWithSeparators() async {
await resolveTestCode('''
const double myDouble = /*caret*/4_200.000_000;
''');
await assertHasAssist('''
const double myDouble = 4_200;
''');
}

Future<void> test_notDouble() async {
await resolveTestCode('''
const double myDouble = /*caret*/42;
Expand All @@ -47,10 +74,19 @@ const double myDouble = /*caret*/42;

Future<void> test_scientific() async {
await resolveTestCode('''
const double myDouble = /*caret*/4.2e1;
const double myDouble = /*caret*/4.2e3;
''');
await assertHasAssist('''
const double myDouble = 42;
const double myDouble = 4200;
''');
}

Future<void> test_scientificWithSeparators() async {
await resolveTestCode('''
const double myDouble = /*caret*/4_00.200_000e3;
''');
await assertHasAssist('''
const double myDouble = 400200;
''');
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/linter/test/rule_test_support.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:analyzer/src/test_utilities/find_element.dart';
import 'package:analyzer/src/test_utilities/find_node.dart';
import 'package:analyzer/src/test_utilities/mock_sdk.dart';
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
import 'package:analyzer_utilities/test/experiments/experiments.dart';
import 'package:analyzer_utilities/test/mock_packages/mock_packages.dart';
import 'package:collection/collection.dart';
import 'package:linter/src/analyzer.dart';
Expand Down Expand Up @@ -177,7 +178,7 @@ class PubPackageResolutionTest extends _ContextResolutionTest {

bool get dumpAstOnFailures => true;

List<String> get experiments => ['macros', 'wildcard-variables'];
List<String> get experiments => experimentsForTests;

List<String> get lintRules => _lintRules;

Expand Down
46 changes: 34 additions & 12 deletions pkg/linter/test/rules/prefer_int_literals_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,23 +167,15 @@ double a = 7.576e2;
''');
}

test_explicitlyTypedDouble_integerWithExponent() async {
await assertDiagnostics(r'''
double a = 7.0e2;
''', [
lint(11, 5),
]);
}

test_explicitlyTypeDynamic_integer() async {
test_explicitTypeDouble_decimal() async {
await assertNoDiagnostics(r'''
dynamic a = 8.0;
double a = 7.3;
''');
}

test_explicitTypeDouble_decimal() async {
test_explicitTypeDouble_decimalWithSeparators() async {
await assertNoDiagnostics(r'''
double a = 7.3;
double a = 1_234.567_8;
''');
}

Expand All @@ -203,6 +195,36 @@ double a = -8.0;
]);
}

test_explicitTypeDouble_integerWithExponent() async {
await assertDiagnostics(r'''
double a = 7.0e2;
''', [
lint(11, 5),
]);
}

test_explicitTypeDouble_integerWithExponentAndSeparators() async {
await assertDiagnostics(r'''
double a = 7_000.0e2;
''', [
lint(11, 9),
]);
}

test_explicitTypeDouble_integerWithSeparators() async {
await assertDiagnostics(r'''
double a = 8_000.000_0;
''', [
lint(11, 11),
]);
}

test_explicitTypeDynamic_integer() async {
await assertNoDiagnostics(r'''
dynamic a = 8.0;
''');
}

test_explicitTypeObject_integer() async {
await assertNoDiagnostics(r'''
Object a = 8.0;
Expand Down

0 comments on commit 63f47bb

Please sign in to comment.