Skip to content

Commit c5a53ed

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Simplify CLI; make AnalysisOptions nonPackageLanguageVersion a getter
The CLI was the only reason this field was mutable; dropping dead support in the CLI means it can instead be a getter. Change-Id: Iaaf3fd2a1cda9b568fb1a743827eb07fa8975319 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395164 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 9ec68ec commit c5a53ed

File tree

5 files changed

+29
-94
lines changed

5 files changed

+29
-94
lines changed

pkg/analyzer/lib/src/dart/analysis/analysis_options.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,6 @@ class AnalysisOptionsImpl implements AnalysisOptions {
309309

310310
ExperimentStatus _contextFeatures;
311311

312-
/// The language version to use for libraries that are not in a package.
313-
///
314-
/// If a library is in a package, this language version is *not* used,
315-
/// even if the package does not specify the language version.
316-
Version nonPackageLanguageVersion = ExperimentStatus.currentVersion;
317-
318312
/// The set of features to use for libraries that are not in a package.
319313
///
320314
/// If a library is in a package, this feature set is *not* used, even if the
@@ -486,6 +480,12 @@ class AnalysisOptionsImpl implements AnalysisOptions {
486480
nonPackageFeatureSet = featureSet;
487481
}
488482

483+
/// The language version to use for libraries that are not in a package.
484+
///
485+
/// If a library is in a package, this language version is *not* used,
486+
/// even if the package does not specify the language version.
487+
Version get nonPackageLanguageVersion => ExperimentStatus.currentVersion;
488+
489489
Uint32List get signature {
490490
if (_signature == null) {
491491
ApiSignature buffer = ApiSignature();

pkg/analyzer_cli/lib/src/driver.dart

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,8 @@ class Driver implements CommandLineStarter {
441441
return analyzer.analyze(formatter);
442442
}
443443

444-
bool _shouldBeFatal(ErrorSeverity severity, CommandLineOptions options) {
445-
if (severity == ErrorSeverity.ERROR) {
446-
return true;
447-
} else {
448-
return false;
449-
}
450-
}
444+
bool _shouldBeFatal(ErrorSeverity severity, CommandLineOptions options) =>
445+
severity == ErrorSeverity.ERROR;
451446

452447
void _verifyAnalysisOptionsFileExists(CommandLineOptions options) {
453448
var path = options.defaultAnalysisOptionsPath;
@@ -470,10 +465,8 @@ class Driver implements CommandLineStarter {
470465
newOptions.defaultPackagesPath == previous.defaultPackagesPath &&
471466
_equalMaps(newOptions.declaredVariables, previous.declaredVariables) &&
472467
newOptions.log == previous.log &&
473-
newOptions.defaultLanguageVersion == previous.defaultLanguageVersion &&
474468
newOptions.disableCacheFlushing == previous.disableCacheFlushing &&
475-
_equalLists(
476-
newOptions.enabledExperiments!, previous.enabledExperiments!);
469+
_equalLists(newOptions.enabledExperiments, previous.enabledExperiments);
477470
}
478471

479472
/// Perform a deep comparison of two string lists.

pkg/analyzer_cli/lib/src/options.dart

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ import 'package:analyzer/src/util/sdk.dart';
1313
import 'package:analyzer_cli/src/ansi.dart' as ansi;
1414
import 'package:analyzer_cli/src/driver.dart';
1515
import 'package:args/args.dart';
16-
import 'package:pub_semver/pub_semver.dart';
1716

1817
const _analysisOptionsFileOption = 'options';
1918
const _binaryName = 'dartanalyzer';
20-
const _defaultLanguageVersionOption = 'default-language-version';
2119
const _defineVariableOption = 'D';
2220
const _enableExperimentOption = 'enable-experiment';
2321
const _enableInitializingFormalAccessFlag = 'initializing-formal-access';
@@ -30,10 +28,6 @@ const _sdkPathOption = 'dart-sdk';
3028
/// *Visible for testing.*
3129
ExitHandler exitHandler = io.exit;
3230

33-
T cast<T>(dynamic value) => value as T;
34-
35-
T? castNullable<T>(dynamic value) => value as T?;
36-
3731
/// Print the given [message] to stderr and exit with the given [exitCode].
3832
void printAndFail(String message, {int exitCode = 15}) {
3933
errorSink.writeln(message);
@@ -110,30 +104,28 @@ class CommandLineOptions {
110104
ResourceProvider resourceProvider,
111105
ArgResults args,
112106
) : _argResults = args,
113-
dartSdkPath = castNullable(args[_sdkPathOption]),
114-
disableCacheFlushing = cast(args['disable-cache-flushing']),
115-
displayVersion = cast(args['version']),
116-
ignoreUnrecognizedFlags = cast(args[_ignoreUnrecognizedFlagsFlag]),
117-
log = cast(args['log']),
107+
dartSdkPath = args.option(_sdkPathOption),
108+
disableCacheFlushing = args.flag('disable-cache-flushing'),
109+
displayVersion = args.flag('version'),
110+
ignoreUnrecognizedFlags = args.flag(_ignoreUnrecognizedFlagsFlag),
111+
log = args.flag('log'),
118112
jsonFormat = args['format'] == 'json',
119113
machineFormat = args['format'] == 'machine',
120-
perfReport = castNullable(args['x-perf-report']),
121-
batchMode = cast(args['batch']),
114+
perfReport = args.option('x-perf-report'),
115+
batchMode = args.flag('batch'),
122116
sourceFiles = args.rest,
123-
trainSnapshot = cast(args['train-snapshot']),
124-
verbose = cast(args['verbose']),
125-
color = cast(args['color']) {
117+
trainSnapshot = args.flag('train-snapshot'),
118+
verbose = args.flag('verbose'),
119+
color = args.flag('color') {
126120
//
127121
// File locations.
128122
//
129123
defaultAnalysisOptionsPath = _absoluteNormalizedPath(
130124
resourceProvider,
131-
castNullable(args[_analysisOptionsFileOption]),
132-
);
133-
defaultPackagesPath = _absoluteNormalizedPath(
134-
resourceProvider,
135-
castNullable(args[_packagesOption]),
125+
args.option(_analysisOptionsFileOption),
136126
);
127+
defaultPackagesPath =
128+
_absoluteNormalizedPath(resourceProvider, args.option(_packagesOption));
137129

138130
//
139131
// Declared variables.
@@ -158,15 +150,9 @@ class CommandLineOptions {
158150
}
159151
}
160152

161-
/// The default language version for files that are not in a package.
162-
/// (Or null if no default language version to force.)
163-
String? get defaultLanguageVersion {
164-
return castNullable(_argResults[_defaultLanguageVersionOption]);
165-
}
166-
167153
/// A list of the names of the experiments that are to be enabled.
168-
List<String>? get enabledExperiments {
169-
return castNullable(_argResults[_enableExperimentOption]);
154+
List<String> get enabledExperiments {
155+
return _argResults.multiOption(_enableExperimentOption);
170156
}
171157

172158
/// Update the [analysisOptions] with flags that the user specified
@@ -175,16 +161,6 @@ class CommandLineOptions {
175161
/// flags that the user specified as command line options more important,
176162
/// so override the corresponding options.
177163
void updateAnalysisOptions(AnalysisOptionsImpl analysisOptions) {
178-
var defaultLanguageVersion = this.defaultLanguageVersion;
179-
if (defaultLanguageVersion != null) {
180-
var nonPackageLanguageVersion =
181-
Version.parse('$defaultLanguageVersion.0');
182-
analysisOptions.nonPackageLanguageVersion = nonPackageLanguageVersion;
183-
analysisOptions.nonPackageFeatureSet = FeatureSet.latestLanguageVersion()
184-
.restrictToVersion(nonPackageLanguageVersion);
185-
}
186-
187-
var enabledExperiments = this.enabledExperiments!;
188164
if (enabledExperiments.isNotEmpty) {
189165
analysisOptions.contextFeatures = FeatureSet.fromEnableFlags2(
190166
sdkLanguageVersion: ExperimentStatus.currentVersion,
@@ -406,10 +382,6 @@ class CommandLineOptions {
406382
defaultsTo: false,
407383
negatable: false,
408384
hide: hide)
409-
..addOption(_defaultLanguageVersionOption,
410-
help: 'The default language version when it is not specified via '
411-
'other ways (internal, tests only).',
412-
hide: false)
413385
..addFlag('disable-cache-flushing', defaultsTo: false, hide: hide)
414386
..addOption('x-perf-report',
415387
help: 'Writes a performance report to the given file (experimental).',
@@ -449,21 +421,21 @@ class CommandLineOptions {
449421
var results = parser.parse(args);
450422

451423
// Help requests.
452-
if (cast(results['help'])) {
424+
if (results.flag('help')) {
453425
_showUsage(parser, fromHelp: true);
454426
exitHandler(0);
455427
return null; // Only reachable in testing.
456428
}
457429

458430
// Batch mode and input files.
459-
if (cast(results['batch'])) {
431+
if (results.flag('batch')) {
460432
if (results.rest.isNotEmpty) {
461433
errorSink.writeln('No source files expected in the batch mode.');
462434
_showUsage(parser);
463435
exitHandler(15);
464436
return null; // Only reachable in testing.
465437
}
466-
} else if (cast(results['version'])) {
438+
} else if (results.flag('version')) {
467439
outSink.writeln('$_binaryName version ${_getVersion()}');
468440
exitHandler(0);
469441
return null; // Only reachable in testing.
@@ -476,8 +448,7 @@ class CommandLineOptions {
476448
}
477449

478450
if (results.wasParsed(_enableExperimentOption)) {
479-
var names =
480-
(results[_enableExperimentOption] as List).cast<String>().toList();
451+
var names = results.multiOption(_enableExperimentOption);
481452
var errorFound = false;
482453
for (var validationResult in validateFlags(names)) {
483454
if (validationResult.isError) {

pkg/analyzer_cli/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ dependencies:
1414
linter: any
1515
meta: any
1616
path: any
17-
pub_semver: any
1817
yaml: any
1918

2019
# Use 'any' constraints here; we get our versions from the DEPS file.
2120
dev_dependencies:
2221
lints: any
22+
pub_semver: any
2323
test_reflective_loader: any
2424
test: any

pkg/analyzer_cli/test/options_test.dart

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
1414
import 'package:analyzer_cli/src/driver.dart';
1515
import 'package:analyzer_cli/src/options.dart';
1616
import 'package:args/args.dart';
17-
import 'package:pub_semver/pub_semver.dart';
1817
import 'package:test/test.dart';
1918
import 'package:test_reflective_loader/test_reflective_loader.dart';
2019

@@ -288,34 +287,6 @@ class ArgumentsTest with ResourceProviderMixin {
288287
);
289288
}
290289

291-
void test_updateAnalysisOptions_defaultLanguageVersion() {
292-
_applyAnalysisOptions(
293-
['a.dart'],
294-
(_) {},
295-
(analysisOptions) {
296-
expect(
297-
analysisOptions.nonPackageLanguageVersion,
298-
ExperimentStatus.currentVersion,
299-
);
300-
var featureSet = analysisOptions.nonPackageFeatureSet;
301-
expect(featureSet.isEnabled(Feature.non_nullable), isTrue);
302-
},
303-
);
304-
305-
_applyAnalysisOptions(
306-
['--default-language-version=2.7', 'a.dart'],
307-
(_) {},
308-
(analysisOptions) {
309-
expect(
310-
analysisOptions.nonPackageLanguageVersion,
311-
Version.parse('2.7.0'),
312-
);
313-
var featureSet = analysisOptions.nonPackageFeatureSet;
314-
expect(featureSet.isEnabled(Feature.non_nullable), isFalse);
315-
},
316-
);
317-
}
318-
319290
void test_updateAnalysisOptions_enableExperiment() {
320291
var feature_a = ExperimentalFeature(
321292
index: 0,

0 commit comments

Comments
 (0)