Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ linter:
- empty_catches
- empty_constructor_bodies
- empty_statements
- eol_at_end_of_file
- exhaustive_cases
- file_names
- flutter_style_todos
Expand Down
5 changes: 4 additions & 1 deletion lib/src/formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ class SimpleFormatter implements ReportFormatter {
var tableWidth = max(_summaryLength, longest + largestCountGuess);
var pad = tableWidth - longest;
var line = ''.padLeft(tableWidth, '-');
out..writeln(line)..writeln('Counts')..writeln(line);
out
..writeln(line)
..writeln('Counts')
..writeln(line);
for (var code in codes) {
out
..write(code.padRight(longest))
Expand Down
2 changes: 2 additions & 0 deletions lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import 'rules/do_not_use_environment.dart';
import 'rules/empty_catches.dart';
import 'rules/empty_constructor_bodies.dart';
import 'rules/empty_statements.dart';
import 'rules/eol_at_end_of_file.dart';
import 'rules/exhaustive_cases.dart';
import 'rules/file_names.dart';
import 'rules/flutter_style_todos.dart';
Expand Down Expand Up @@ -266,6 +267,7 @@ void registerLintRules({bool inTestMode = false}) {
..register(EmptyCatches())
..register(EmptyConstructorBodies())
..register(EmptyStatements())
..register(EolAtEndOfFile())
..register(ExhaustiveCases())
..register(FileNames())
..register(FlutterStyleTodos())
Expand Down
66 changes: 66 additions & 0 deletions lib/src/rules/eol_at_end_of_file.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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/visitor.dart';

import '../analyzer.dart';

const _desc = r'Put a single newline at end of file.';

const _details = r'''

**DO** put a single newline at the end of non-empty files.

**BAD:**
```dart
a {
}
```

**GOOD:**
```dart
b {
}
<-- newline
''';

class EolAtEndOfFile extends LintRule implements NodeLintRule {
EolAtEndOfFile()
: super(
name: 'eol_at_end_of_file',
description: _desc,
details: _details,
group: Group.style);

@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
var visitor = _Visitor(this, context);
registry.addCompilationUnit(this, visitor);
}
}

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

_Visitor(this.rule, this.context);

@override
void visitCompilationUnit(CompilationUnit node) {
var content = context.currentUnit.content;
if (content.isNotEmpty &&
(!content.endsWithNewline || content.endsWithMultipleNewlines)) {
rule.reportLintForOffset(content.trimRight().length, 1);
}
}
}

extension on String {
bool get endsWithNewline => newline.any(endsWith);
static const newline = ['\n', '\r'];
bool get endsWithMultipleNewlines => multipleNewlines.any(endsWith);
static const multipleNewlines = ['\n\n', '\r\r', '\r\n\r\n'];
}
4 changes: 3 additions & 1 deletion lib/src/rules/unnecessary_getters_setters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ class _Visitor extends SimpleAstVisitor<void> {
isSimpleGetter(getter) &&
getterElement.metadata.isEmpty &&
setterElement.metadata.isEmpty) {
rule..reportLint(getter.name)..reportLint(setter.name);
rule
..reportLint(getter.name)
..reportLint(setter.name);
}
}
}
34 changes: 34 additions & 0 deletions test/integration/eol_at_end_of_file.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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/src/lint/io.dart';
import 'package:analyzer/src/lint/linter.dart';
import 'package:linter/src/analyzer.dart';
import 'package:linter/src/cli.dart' as cli;
import 'package:test/test.dart';

import '../mocks.dart';
import '../test_constants.dart';

void main() {
group('eol_at_end_of_file', () {
var currentOut = outSink;
var collectingOut = CollectingSink();
setUp(() => outSink = collectingOut);
tearDown(() {
collectingOut.buffer.clear();
outSink = currentOut;
});
test('eol at end of file', () async {
await cli.runLinter([
'$integrationTestDir/eol_at_end_of_file',
'--rules=eol_at_end_of_file',
], LinterOptions());
expect(
collectingOut.trim(), contains('5 files analyzed, 3 issues found'));
expect(collectingOut.trim(),
contains('Put a single newline at end of file'));
});
});
}
2 changes: 2 additions & 0 deletions test_data/integration/eol_at_end_of_file/lintconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rules:
- eol_at_end_of_file
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
void multipleComments() {
}
// This is just a placeholder.
// This file should be replaced with useful code.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
void multipleNewlines() {
}

2 changes: 2 additions & 0 deletions test_data/integration/eol_at_end_of_file/src/newline.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
void newline() {
}
2 changes: 2 additions & 0 deletions test_data/integration/eol_at_end_of_file/src/noNewline.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
void noNewline() {
}
8 changes: 8 additions & 0 deletions test_data/rules/eol_at_end_of_file.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// 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.

// test w/ `dart test -N eol_at_end_of_file`

bar() {
} //LINT
2 changes: 1 addition & 1 deletion tool/doc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ linter:
.where((r) => r.maturity != Maturity.deprecated)
.map((r) => r.name)
.toList()
..sort();
..sort();
for (var rule in sortedRules) {
sb.write(' - $rule\n');
}
Expand Down