Add "clang-tidy --fix" flag to automatically apply fixes (flutter/engine#29654)

This commit is contained in:
Jenn Magder
2021-11-10 15:13:01 -08:00
committed by GitHub
parent 3b7f04b18c
commit 4d76095361
4 changed files with 29 additions and 5 deletions

View File

@@ -48,6 +48,7 @@ class ClangTidy {
required io.Directory repoPath, required io.Directory repoPath,
String checksArg = '', String checksArg = '',
bool lintAll = false, bool lintAll = false,
bool fix = false,
StringSink? outSink, StringSink? outSink,
StringSink? errSink, StringSink? errSink,
}) : }) :
@@ -56,6 +57,7 @@ class ClangTidy {
repoPath: repoPath, repoPath: repoPath,
checksArg: checksArg, checksArg: checksArg,
lintAll: lintAll, lintAll: lintAll,
fix: fix,
errSink: errSink, errSink: errSink,
), ),
_outSink = outSink ?? io.stdout, _outSink = outSink ?? io.stdout,
@@ -207,7 +209,7 @@ class ClangTidy {
break; break;
case LintAction.lint: case LintAction.lint:
_outSink.writeln('🔶 linting $relativePath'); _outSink.writeln('🔶 linting $relativePath');
jobs.add(command.createLintJob(checks)); jobs.add(command.createLintJob(checks, options.fix));
break; break;
case LintAction.skipThirdParty: case LintAction.skipThirdParty:
_outSink.writeln('🔷 ignoring $relativePath (third_party)'); _outSink.writeln('🔷 ignoring $relativePath (third_party)');

View File

@@ -129,11 +129,13 @@ class Command {
} }
/// The job for the process runner for the lint needed for this command. /// The job for the process runner for the lint needed for this command.
WorkerJob createLintJob(String? checks) { WorkerJob createLintJob(String? checks, bool fix) {
final List<String> args = <String>[ final List<String> args = <String>[
filePath, filePath,
if (checks != null) if (checks != null)
checks, checks,
if (fix)
'--fix',
'--', '--',
]; ];
args.addAll(tidyArgs.split(' ')); args.addAll(tidyArgs.split(' '));

View File

@@ -17,6 +17,7 @@ class Options {
this.verbose = false, this.verbose = false,
this.checksArg = '', this.checksArg = '',
this.lintAll = false, this.lintAll = false,
this.fix = false,
this.errorMessage, this.errorMessage,
StringSink? errSink, StringSink? errSink,
}) : checks = checksArg.isNotEmpty ? '--checks=$checksArg' : null, }) : checks = checksArg.isNotEmpty ? '--checks=$checksArg' : null,
@@ -58,6 +59,7 @@ class Options {
checksArg: options.wasParsed('checks') ? options['checks'] as String : '', checksArg: options.wasParsed('checks') ? options['checks'] as String : '',
lintAll: io.Platform.environment['FLUTTER_LINT_ALL'] != null || lintAll: io.Platform.environment['FLUTTER_LINT_ALL'] != null ||
options['lint-all'] as bool, options['lint-all'] as bool,
fix: options['fix'] as bool,
errSink: errSink, errSink: errSink,
); );
} }
@@ -91,6 +93,11 @@ class Options {
help: 'lint all of the sources, regardless of FLUTTER_NOLINT.', help: 'lint all of the sources, regardless of FLUTTER_NOLINT.',
defaultsTo: false, defaultsTo: false,
) )
..addFlag(
'fix',
help: 'Apply suggested fixes.',
defaultsTo: false,
)
..addFlag( ..addFlag(
'verbose', 'verbose',
help: 'Print verbose output.', help: 'Print verbose output.',
@@ -134,6 +141,9 @@ class Options {
/// Whether all files should be linted. /// Whether all files should be linted.
final bool lintAll; final bool lintAll;
/// Whether checks should apply available fix-ups to the working copy.
final bool fix;
/// If there was a problem with the command line arguments, this string /// If there was a problem with the command line arguments, this string
/// contains the error message. /// contains the error message.
final String? errorMessage; final String? errorMessage;
@@ -146,7 +156,7 @@ class Options {
_errSink.writeln(message); _errSink.writeln(message);
} }
_errSink.writeln( _errSink.writeln(
'Usage: bin/main.dart [--help] [--lint-all] [--verbose] [--diff-branch]', 'Usage: bin/main.dart [--help] [--lint-all] [--fix] [--verbose] [--diff-branch]',
); );
_errSink.writeln(_argParser.usage); _errSink.writeln(_argParser.usage);
} }

View File

@@ -204,14 +204,24 @@ Future<int> main(List<String> args) async {
expect(commands, isNotEmpty); expect(commands, isNotEmpty);
final Command command = commands.first; final Command command = commands.first;
expect(command.tidyPath, contains('clang/bin/clang-tidy')); expect(command.tidyPath, contains('clang/bin/clang-tidy'));
final WorkerJob job = command.createLintJob(null); final WorkerJob jobNoFix = command.createLintJob(null, false);
expect(job.command, <String>[ expect(jobNoFix.command, <String>[
'../../buildtools/mac-x64/clang/bin/clang-tidy', '../../buildtools/mac-x64/clang/bin/clang-tidy',
filePath, filePath,
'--', '--',
'', '',
filePath, filePath,
]); ]);
final WorkerJob jobWithFix = command.createLintJob(null, true);
expect(jobWithFix.command, <String>[
'../../buildtools/mac-x64/clang/bin/clang-tidy',
filePath,
'--fix',
'--',
'',
filePath,
]);
}); });
test('Command getLintAction flags third_party files', () async { test('Command getLintAction flags third_party files', () async {