From d1a06cf97b833609909cd28cd5b9744a43d0e26c Mon Sep 17 00:00:00 2001 From: Jaime Wren Date: Fri, 10 Jul 2020 19:10:07 +0000 Subject: [PATCH] Update dartdev to use the format command object from dart_style. I thought that this landed over a month ago. This needs to be rolled into the Dart SDK 2.9 New output from dart format --help: Idiomatically formats Dart source code. Usage: dart format [options...] -h, --help Print this usage information. Common options: -o, --output Where formatted output should be written. [json] Print code and selection as JSON [none] Discard. [show] Print code to terminal. [write] (default) Overwrite formatted files on disc. --show Which filenames to print. [all] All visited files and directories. [changed] (default) Only the names of files whose formatting is changed. [none] No file names or directories. --summary Summary shown after formatting completes. [line] (default) Single line summary. [none] No summary. [profile] Tracks how long it took for format each file. Non-whitespace fixes (off by default): --fix Apply all style fixes. --fix-doc-comments Use triple slash for documentation comments. --fix-function-typedefs Use new syntax for function type typedefs. --fix-named-default-separator Use "=" as the separator before named parameter default values. --fix-optional-const Remove "const" keyword inside constant context. --fix-optional-new Remove "new" keyword. --fix-single-cascade-statements Remove unnecessary single cascades from expression statements. Other options: -l, --line-length Wrap lines longer than this. (defaults to "80") -i, --indent Spaces of leading indentation. (defaults to "0") --set-exit-if-changed Return exit code 1 if there are any formatting changes. --follow-links Follow links to files and directories. If unset, links will be ignored. --version Show version information. Options when formatting from stdin: --selection Selection to preserve formatted as "start:length". --stdin-name The path name to show when an error occurs. (defaults to "stdin") Run "dart help" to see global options. Change-Id: I18a49abd919672e988296f83c23636b14c29bdeb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/153900 Reviewed-by: Bob Nystrom Commit-Queue: Jaime Wren --- pkg/dartdev/lib/dartdev.dart | 2 +- pkg/dartdev/lib/src/commands/format.dart | 52 ---------------------- pkg/dartdev/test/commands/format_test.dart | 41 ++++------------- 3 files changed, 10 insertions(+), 85 deletions(-) delete mode 100644 pkg/dartdev/lib/src/commands/format.dart diff --git a/pkg/dartdev/lib/dartdev.dart b/pkg/dartdev/lib/dartdev.dart index 3d7bb9c339f5..86494c7bd027 100644 --- a/pkg/dartdev/lib/dartdev.dart +++ b/pkg/dartdev/lib/dartdev.dart @@ -7,6 +7,7 @@ import 'dart:io' as io; import 'package:analyzer/src/dart/analysis/experiments.dart'; import 'package:args/args.dart'; import 'package:args/command_runner.dart'; +import 'package:dart_style/src/cli/format_command.dart'; import 'package:cli_util/cli_logging.dart'; import 'package:nnbd_migration/migration_cli.dart'; import 'package:usage/usage.dart'; @@ -15,7 +16,6 @@ import 'src/analytics.dart'; import 'src/commands/analyze.dart'; import 'src/commands/compile.dart'; import 'src/commands/create.dart'; -import 'src/commands/format.dart'; import 'src/commands/pub.dart'; import 'src/commands/run.dart'; import 'src/commands/test.dart'; diff --git a/pkg/dartdev/lib/src/commands/format.dart b/pkg/dartdev/lib/src/commands/format.dart deleted file mode 100644 index c1dfc60d4b82..000000000000 --- a/pkg/dartdev/lib/src/commands/format.dart +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (c) 2020, 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 'dart:async'; - -import '../core.dart'; -import '../sdk.dart'; - -class FormatCommand extends DartdevCommand { - FormatCommand() : super('format', 'Format Dart source code.') { - // TODO(jwren) When https://github.com/dart-lang/dart_style/issues/889 - // is resolved, have dart_style provide the ArgParser, instead of creating - // one here. - argParser - ..addFlag('dry-run', - abbr: 'n', - help: 'Show which files would be modified but make no changes.') - ..addFlag('set-exit-if-changed', - help: 'Return exit code 1 if there are any formatting changes.') - ..addFlag('machine', - abbr: 'm', help: 'Produce machine-readable JSON output.') - ..addOption('line-length', - abbr: 'l', - help: - 'Wrap lines longer than this length. Defaults to 80 characters.', - defaultsTo: '80'); - } - - @override - FutureOr run() async { - List args = List.from(argResults.arguments); - - // By printing and returning if there are no arguments, this changes the - // default unix-pipe behavior of dartfmt: - if (args.isEmpty) { - printUsage(); - return 0; - } - - // By always adding '--overwrite', the default behavior of dartfmt by - // is changed to have the UX of 'flutter format *'. The flag is not added - // if 'dry-run' has been passed as they are not compatible. - if (!argResults['dry-run']) { - args.add('--overwrite'); - } - - var process = await startProcess(sdk.dartfmt, args); - routeToStdout(process); - return process.exitCode; - } -} diff --git a/pkg/dartdev/test/commands/format_test.dart b/pkg/dartdev/test/commands/format_test.dart index 1b68d981185c..1045907d8b92 100644 --- a/pkg/dartdev/test/commands/format_test.dart +++ b/pkg/dartdev/test/commands/format_test.dart @@ -17,22 +17,14 @@ void format() { tearDown(() => p?.dispose()); - test('implicit --help', () { - p = project(); - var result = p.runSync('format', []); - expect(result.exitCode, 0); - expect(result.stderr, isEmpty); - expect(result.stdout, contains('Format Dart source code.')); - expect(result.stdout, contains('Usage: dart format [arguments]')); - }); - test('--help', () { p = project(); var result = p.runSync('format', ['--help']); expect(result.exitCode, 0); expect(result.stderr, isEmpty); - expect(result.stdout, contains('Format Dart source code.')); - expect(result.stdout, contains('Usage: dart format [arguments]')); + expect(result.stdout, contains('Idiomatically formats Dart source code.')); + expect(result.stdout, + contains('Usage: dart format [options...] ')); }); test('unchanged', () { @@ -40,7 +32,7 @@ void format() { ProcessResult result = p.runSync('format', [p.relativeFilePath]); expect(result.exitCode, 0); expect(result.stderr, isEmpty); - expect(result.stdout, startsWith('Unchanged ${p.relativeFilePath}')); + expect(result.stdout, startsWith('Formatted 1 file (0 changed) in ')); }); test('formatted', () { @@ -48,25 +40,10 @@ void format() { ProcessResult result = p.runSync('format', [p.relativeFilePath]); expect(result.exitCode, 0); expect(result.stderr, isEmpty); - expect(result.stdout, startsWith('Formatted ${p.relativeFilePath}')); - }); - - test('dry-run changes', () { - p = project(mainSrc: 'int get foo => 1;\n'); - ProcessResult result = - p.runSync('format', ['--dry-run', p.relativeFilePath]); - expect(result.exitCode, 0); - expect(result.stderr, isEmpty); - expect(result.stdout, startsWith(p.relativeFilePath)); - }); - - test('dry-run no changes', () { - p = project(mainSrc: 'int get foo => 1;\n'); - ProcessResult result = - p.runSync('format', ['--dry-run', p.relativeFilePath]); - expect(result.exitCode, 0); - expect(result.stderr, isEmpty); - expect(result.stdout, isEmpty); + expect( + result.stdout, + startsWith( + 'Formatted lib/main.dart\nFormatted 1 file (1 changed) in ')); }); test('unknown file', () { @@ -76,6 +53,6 @@ void format() { expect(result.exitCode, 0); expect(result.stderr, startsWith('No file or directory found at "$unknownFilePath".')); - expect(result.stdout, isEmpty); + expect(result.stdout, startsWith('Formatted no files in ')); }); }