Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
feat: add delete-files flag (#632)
Browse files Browse the repository at this point in the history
* feat: add  flag

* test: update tests

* chore: review fixes

* chore: review fixes

* tests: add some test cases

* tests: add some test cases

Co-authored-by: Dmitry Krutskikh <[email protected]>
  • Loading branch information
incendial and dkrutskikh authored Jan 9, 2022
1 parent 09038f6 commit 0de44b7
Show file tree
Hide file tree
Showing 31 changed files with 198 additions and 95 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* chore: restrict `analyzer_plugin` version to `>=0.8.0 <0.10.0`.
* feat: support extensions and static getters for check-unused-l10n.
* feat: improve `prefer-correct-type-name` rule.
* feat: add `delete-files` flag to `check-unused-files` command.
* build: activate check_unused_files on CI.

## 4.8.1
Expand Down
2 changes: 1 addition & 1 deletion lib/src/analyzers/lint_analyzer/lint_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LintAnalyzer {

/// Returns a reporter for the given [name]. Use the reporter
/// to convert analysis reports to console, JSON or other supported format.
Reporter<FileReport, Object>? getReporter({
Reporter<FileReport, Object, void>? getReporter({
required String name,
required IOSink output,
required String reportFolder,
Expand Down
10 changes: 7 additions & 3 deletions lib/src/analyzers/lint_analyzer/reporters/reporter_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import 'reporters_list/github/lint_github_reporter.dart';
import 'reporters_list/html/lint_html_reporter.dart';
import 'reporters_list/json/lint_json_reporter.dart';

final _implementedReports = <String,
Reporter<FileReport, Object> Function(IOSink output, String reportFolder)>{
final _implementedReports = <
String,
Reporter<FileReport, Object, void> Function(
IOSink output,
String reportFolder,
)>{
ConsoleReporter.id: (output, _) => LintConsoleReporter(output),
ConsoleReporter.verboseId: (output, _) =>
LintConsoleReporter(output, reportAll: true),
Expand All @@ -26,7 +30,7 @@ final _implementedReports = <String,
LintCodeClimateReporter(output, gitlabCompatible: true),
};

Reporter<FileReport, Object>? reporter({
Reporter<FileReport, Object, void>? reporter({
required String name,
required IOSink output,
required String reportFolder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import 'models/code_climate_issue_severity.dart';
///
/// Use it to create reports in Code Climate format.
class LintCodeClimateReporter extends CodeClimateReporter<LintFileReport,
SummaryLintReportRecord<Object>> {
SummaryLintReportRecord<Object>, void> {
LintCodeClimateReporter(IOSink output, {bool gitlabCompatible = false})
: super(
output,
Expand All @@ -30,6 +30,7 @@ class LintCodeClimateReporter extends CodeClimateReporter<LintFileReport,
Future<void> report(
Iterable<LintFileReport> records, {
Iterable<SummaryLintReportRecord<Object>> summary = const [],
void additionalParams,
}) async {
if (records.isEmpty) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import 'lint_console_reporter_helper.dart';
/// Lint console reporter.
///
/// Use it to create reports in console format.
class LintConsoleReporter
extends ConsoleReporter<LintFileReport, SummaryLintReportRecord<Object>> {
class LintConsoleReporter extends ConsoleReporter<LintFileReport,
SummaryLintReportRecord<Object>, void> {
/// If true will report info about all files even if they're not above warning threshold
final bool reportAll;

Expand All @@ -26,6 +26,7 @@ class LintConsoleReporter
Future<void> report(
Iterable<LintFileReport> records, {
Iterable<SummaryLintReportRecord<Object>> summary = const [],
void additionalParams,
}) async {
var hasReportData = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ const _deprecationMessage =
///
/// **Note:** this reporter is deprecated and should not be used.
/// Consider using Dart Code Metrics GitHub Action instead.
class LintGitHubReporter
extends GitHubReporter<LintFileReport, SummaryLintReportRecord<Object>> {
class LintGitHubReporter extends GitHubReporter<LintFileReport,
SummaryLintReportRecord<Object>, void> {
const LintGitHubReporter(IOSink output) : super(output);

@override
Future<void> report(
Iterable<LintFileReport> records, {
Iterable<SummaryLintReportRecord<Object>> summary = const [],
void additionalParams,
}) async {
if (records.isEmpty) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ const _designIssues = 'Design issues';
/// Lint HTML reporter.
///
/// Use it to create reports in HTML format.
class LintHtmlReporter
extends HtmlReporter<LintFileReport, SummaryLintReportRecord<Object>> {
class LintHtmlReporter extends HtmlReporter<LintFileReport,
SummaryLintReportRecord<Object>, void> {
LintHtmlReporter(String reportFolder) : super(reportFolder);

@override
Future<void> report(
Iterable<LintFileReport> records, {
Iterable<SummaryLintReportRecord<Object>> summary = const [],
void additionalParams,
}) async {
await super.report(records);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import '../../../models/summary_lint_report_record.dart';
///
/// Use it to create reports in JSON format.
@immutable
class LintJsonReporter
extends JsonReporter<LintFileReport, SummaryLintReportRecord<Object>> {
class LintJsonReporter extends JsonReporter<LintFileReport,
SummaryLintReportRecord<Object>, void> {
const LintJsonReporter(IOSink output) : super(output, 2);

@override
Future<void> report(
Iterable<LintFileReport> records, {
Iterable<SummaryLintReportRecord<Object>> summary = const [],
void additionalParams,
}) async {
if (records.isEmpty) {
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/analyzers/lint_analyzer/rules/rules_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ final _implementedRules = <String, Rule Function(Map<String, Object>)>{
AvoidUnnecessaryTypeCastsRule.ruleId: (config) =>
AvoidUnnecessaryTypeCastsRule(config),
AvoidUnrelatedTypeAssertionsRule.ruleId: (config) =>
AvoidUnnecessaryTypeAssertionsRule(config),
AvoidUnrelatedTypeAssertionsRule(config),
AvoidUnusedParametersRule.ruleId: (config) =>
AvoidUnusedParametersRule(config),
AvoidWrappingInPaddingRule.ruleId: (config) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// Represents additional reporter params
class ReportParams {
final bool deleteUnusedFiles;

const ReportParams({this.deleteUnusedFiles = false});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import '../../../reporters/models/console_reporter.dart';
import '../../../reporters/models/json_reporter.dart';
import '../../../reporters/models/reporter.dart';
import '../models/unused_files_file_report.dart';
import 'report_params.dart';
import 'reporters_list/console/unused_files_console_reporter.dart';
import 'reporters_list/json/unused_files_json_reporter.dart';

final _implementedReports =
<String, Reporter<UnusedFilesFileReport, void> Function(IOSink output)>{
final _implementedReports = <
String,
Reporter<UnusedFilesFileReport, void, ReportParams> Function(
IOSink output,
)>{
ConsoleReporter.id: (output) => UnusedFilesConsoleReporter(output),
JsonReporter.id: (output) => UnusedFilesJsonReporter(output),
};

Reporter<UnusedFilesFileReport, void>? reporter({
Reporter<UnusedFilesFileReport, void, ReportParams>? reporter({
required String name,
required IOSink output,
}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import 'dart:io';

import '../../../../../reporters/models/console_reporter.dart';
import '../../../models/unused_files_file_report.dart';
import '../../report_params.dart';

/// Unused files console reporter.
///
/// Use it to create reports in console format.
class UnusedFilesConsoleReporter
extends ConsoleReporter<UnusedFilesFileReport, void> {
extends ConsoleReporter<UnusedFilesFileReport, void, ReportParams> {
UnusedFilesConsoleReporter(IOSink output) : super(output);

@override
Future<void> report(
Iterable<UnusedFilesFileReport> records, {
Iterable<void> summary = const [],
ReportParams? additionalParams,
}) async {
if (records.isEmpty) {
output.writeln('${okPen('✔')} no unused files found!');
Expand All @@ -26,14 +28,18 @@ class UnusedFilesConsoleReporter

for (final analysisRecord in sortedRecords) {
output.writeln(
'${warnigPen('⚠')} unused file: ${analysisRecord.relativePath}',
'${warningPen('⚠')} unused file: ${analysisRecord.relativePath}',
);
}

final filesCount = alarmPen(sortedRecords.length);

final outputRecord = (additionalParams?.deleteUnusedFiles ?? false)
? '${okPen('✔')} $filesCount files were successfully deleted'
: '${alarmPen('✖')} total unused files - $filesCount';

output
..writeln('')
..writeln(
'${alarmPen('✖')} total unused files - ${alarmPen(sortedRecords.length)}',
);
..writeln(outputRecord);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import 'package:meta/meta.dart';

import '../../../../../reporters/models/json_reporter.dart';
import '../../../models/unused_files_file_report.dart';
import '../../report_params.dart';

/// Unused files JSON reporter.
///
/// Use it to create reports in JSON format.
@immutable
class UnusedFilesJsonReporter
extends JsonReporter<UnusedFilesFileReport, void> {
extends JsonReporter<UnusedFilesFileReport, void, ReportParams> {
const UnusedFilesJsonReporter(IOSink output) : super(output, 2);

@override
Future<void> report(
Iterable<UnusedFilesFileReport> records, {
Iterable<void> summary = const [],
ReportParams? additionalParams,
}) async {
if (records.isEmpty) {
return;
Expand All @@ -27,6 +29,7 @@ class UnusedFilesJsonReporter
'formatVersion': formatVersion,
'timestamp': getTimestamp(),
'unusedFiles': records.map(_analysisRecordToJson).toList(),
'automaticallyDeleted': additionalParams?.deleteUnusedFiles ?? false,
});

output.write(encodedReport);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import '../../reporters/models/reporter.dart';
import '../../utils/analyzer_utils.dart';
import '../../utils/file_utils.dart';
import 'models/unused_files_file_report.dart';
import 'reporters/report_params.dart';
import 'reporters/reporter_factory.dart';
import 'unused_files_config.dart';
import 'unused_files_visitor.dart';
Expand All @@ -20,7 +21,7 @@ class UnusedFilesAnalyzer {

/// Returns a reporter for the given [name]. Use the reporter
/// to convert analysis reports to console, JSON or other supported format.
Reporter<UnusedFilesFileReport, void>? getReporter({
Reporter<UnusedFilesFileReport, void, ReportParams>? getReporter({
required String name,
required IOSink output,
}) =>
Expand Down Expand Up @@ -95,6 +96,12 @@ class UnusedFilesAnalyzer {
}).toSet();
}

void deleteAllUnusedFiles(Iterable<UnusedFilesFileReport> reports) {
for (final report in reports) {
File(report.path).deleteSync();
}
}

Iterable<String> _analyzeFile(String filePath, SomeResolvedUnitResult unit) {
if (unit is ResolvedUnitResult) {
final visitor = UnusedFilesVisitor(filePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import '../models/unused_l10n_file_report.dart';
import 'reporters_list/console/unused_l10n_console_reporter.dart';
import 'reporters_list/json/unused_l10n_json_reporter.dart';

final _implementedReports =
<String, Reporter<UnusedL10nFileReport, void> Function(IOSink output)>{
final _implementedReports = <String,
Reporter<UnusedL10nFileReport, void, void> Function(IOSink output)>{
ConsoleReporter.id: (output) => UnusedL10nConsoleReporter(output),
JsonReporter.id: (output) => UnusedL10nJsonReporter(output),
};

Reporter<UnusedL10nFileReport, void>? reporter({
Reporter<UnusedL10nFileReport, void, void>? reporter({
required String name,
required IOSink output,
}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import '../../../models/unused_l10n_file_report.dart';
///
/// Use it to create reports in console format.
class UnusedL10nConsoleReporter
extends ConsoleReporter<UnusedL10nFileReport, void> {
extends ConsoleReporter<UnusedL10nFileReport, void, void> {
UnusedL10nConsoleReporter(IOSink output) : super(output);

@override
Future<void> report(
Iterable<UnusedL10nFileReport> records, {
Iterable<void> summary = const [],
void additionalParams,
}) async {
if (records.isEmpty) {
output.writeln('${okPen('✔')} no unused localization found!');
Expand All @@ -38,7 +39,7 @@ class UnusedL10nConsoleReporter
final pathOffset = offset.padRight(5);

output
..writeln('$offset ${warnigPen('⚠')} unused ${issue.memberName}')
..writeln('$offset ${warningPen('⚠')} unused ${issue.memberName}')
..writeln('$pathOffset at $path:$line:$column');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import '../../../models/unused_l10n_issue.dart';
///
/// Use it to create reports in JSON format.
@immutable
class UnusedL10nJsonReporter extends JsonReporter<UnusedL10nFileReport, void> {
class UnusedL10nJsonReporter
extends JsonReporter<UnusedL10nFileReport, void, void> {
const UnusedL10nJsonReporter(IOSink output) : super(output, 2);

@override
Future<void> report(
Iterable<UnusedL10nFileReport> records, {
Iterable<void> summary = const [],
void additionalParams,
}) async {
if (records.isEmpty) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class UnusedL10nAnalyzer {

/// Returns a reporter for the given [name]. Use the reporter
/// to convert analysis reports to console, JSON or other supported format.
Reporter<UnusedL10nFileReport, void>? getReporter({
Reporter<UnusedL10nFileReport, void, void>? getReporter({
required String name,
required IOSink output,
}) =>
Expand Down
Loading

0 comments on commit 0de44b7

Please sign in to comment.