Skip to content

Commit

Permalink
new check: no trailing spaces in test source
Browse files Browse the repository at this point in the history
Ensure test source files aren't created w/ trailing spaces. (These are just noise and distract in code reviews.)

Change-Id: I34259521c27a652df085865ab91ee83349591de0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/386826
Reviewed-by: Samuel Rawlins <[email protected]>
Commit-Queue: Phil Quitslund <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Sep 25, 2024
1 parent 39bcea7 commit dcc8c22
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/linter/test/rules/always_specify_types_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class C {
await assertNoDiagnostics(r'''
class S {
S({int? p1, required int p2});
}
}
class C extends S {
C({super.p1, required super.p2});
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/linter/test/rules/annotate_redeclares_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ extension type E(A a) implements A {

test_method_annotated() async {
await assertNoDiagnostics(r'''
import 'package:meta/meta.dart';
import 'package:meta/meta.dart';
class A {
void m() {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class A {
var b = newFile('$testPackageLibPath/b.dart', r'''
part of 'a.dart';
augment class A {
augment class A {
int a = 1;
}
''');
Expand All @@ -52,7 +52,7 @@ class A {}
var b = newFile('$testPackageLibPath/b.dart', r'''
part of 'a.dart';
augment class A {
augment class A {
static int f = 1;
}
''');
Expand All @@ -76,7 +76,7 @@ class A {}
var b = newFile('$testPackageLibPath/b.dart', r'''
part of 'a.dart';
augment class A {
augment class A {
static void m() {}
}
''');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class A {
var b = newFile('$testPackageLibPath/b.dart', r'''
part of 'a.dart';
augment class A {
augment class A {
A.aa() : a = 1;
}
''');
Expand Down Expand Up @@ -105,7 +105,7 @@ augment class A {
newFile('$testPackageLibPath/a.dart', r'''
part 'test.dart';
class A {
class A {
A.aa();
}
''');
Expand All @@ -124,7 +124,7 @@ augment class A {
newFile('$testPackageLibPath/a.dart', r'''
part 'test.dart';
class A {
class A {
const A();
}
''');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ part of 'a.dart';
augment class A {
augment void m(int q) {}
augment void m(int q) {}
augment void m(int q) {}
}
''', [
lint(58, 1), // Only the first augmentation gets linted.
Expand Down
10 changes: 5 additions & 5 deletions pkg/linter/test/rules/overridden_fields_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class A extends O { }
var b = newFile('$testPackageLibPath/b.dart', r'''
part of 'a.dart';
augment class A {
augment class A {
final a = '';
}
''');
Expand All @@ -41,7 +41,7 @@ augment class A {

result = await resolveFile(b.path);
await assertDiagnosticsIn(errors, [
lint(46, 1),
lint(45, 1),
]);
}

Expand All @@ -54,22 +54,22 @@ class O {
}
class A extends O {
@override
@override
final a = '';
}
''');

var b = newFile('$testPackageLibPath/b.dart', r'''
part of 'a.dart';
augment class A {
augment class A {
augment final a = '';
}
''');

result = await resolveFile(a.path);
await assertDiagnosticsIn(errors, [
lint(86, 1),
lint(85, 1),
]);

result = await resolveFile(b.path);
Expand Down
4 changes: 2 additions & 2 deletions pkg/linter/test/rules/prefer_void_to_null_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ part 'test.dart';
class A {
Future<Null>? f;
}
}
''');

await assertNoDiagnostics(r'''
Expand Down Expand Up @@ -55,7 +55,7 @@ part 'test.dart';
class A {
Future<Null>? get v => null;
}
}
''');

await assertNoDiagnostics(r'''
Expand Down
7 changes: 6 additions & 1 deletion pkg/linter/tool/checks/driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'package:path/path.dart' as path;

import '../../test/mocks.dart';
import 'rules/no_solo_tests.dart';
import 'rules/no_trailing_spaces.dart';
import 'rules/visit_registered_nodes.dart';

Future<void> main() async {
Expand All @@ -31,7 +32,11 @@ Future<void> main() async {
}
}

var customChecks = [VisitRegisteredNodes(), NoSoloTests()];
var customChecks = [
VisitRegisteredNodes(),
NoSoloTests(),
NoTrailingSpaces(),
];

Future<List<AnalysisError>> runChecks() async {
var rules =
Expand Down
54 changes: 54 additions & 0 deletions pkg/linter/tool/checks/rules/no_trailing_spaces.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2024, 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/visitor.dart';
import 'package:linter/src/analyzer.dart';

const _desc = r"Don't create string literals with trailing spaces in tests.";

class NoTrailingSpaces extends LintRule {
static const LintCode code = LintCode('no_trailing_spaces', _desc,
correctionMessage: 'Try removing the trailing spaces.',
hasPublishedDocs: true);

NoTrailingSpaces()
: super(
name: 'no_trailing_spaces',
description: _desc,
);

@override
LintCode get lintCode => code;

@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
if (context.definingUnit.unit.inTestDir) {
var visitor = _Visitor(this);
registry.addMethodInvocation(this, visitor);
}
}
}

class _Visitor extends SimpleAstVisitor<void> {
final LintRule rule;

_Visitor(this.rule);

@override
void visitMethodInvocation(MethodInvocation node) {
var arguments = node.argumentList.arguments;
for (var sourceString in arguments) {
if (sourceString is! SimpleStringLiteral) return;

var literal = sourceString.literal;
if (literal is! StringToken) return;

if (literal.lexeme.contains(' \n')) {
rule.reportLintForToken(literal);
}
}
}
}

0 comments on commit dcc8c22

Please sign in to comment.