Skip to content

Commit 33ad46e

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: validate top-level analysis options sections are maps
Also tidy some comments and the "enable experiments" validator name. Change-Id: I67121937331ffd4cc119d81d3bd993ff80e43fc6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/443400 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent c428b05 commit 33ad46e

File tree

2 files changed

+68
-38
lines changed

2 files changed

+68
-38
lines changed

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

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ class AnalyzerOptionsValidator extends _CompositeValidator {
375375
_AnalyzerTopLevelOptionsValidator(),
376376
_StrongModeOptionValueValidator(),
377377
_ErrorFilterOptionValidator(),
378-
_EnabledExperimentsValidator(),
378+
_EnableExperimentsValidator(),
379379
_LanguageOptionValidator(),
380380
_OptionalChecksValueValidator(),
381381
_CannotIgnoreOptionValidator(),
@@ -524,30 +524,24 @@ class _CodeStyleOptionsValidator extends OptionsValidator {
524524
}
525525

526526
void _validateFormat(DiagnosticReporter reporter, YamlNode format) {
527-
if (format is YamlMap) {
527+
if (format is! YamlScalar) {
528528
reporter.atSourceSpan(
529529
format.span,
530530
AnalysisOptionsWarningCode.INVALID_SECTION_FORMAT,
531531
arguments: [AnalysisOptionsFile.format],
532532
);
533-
} else if (format is YamlScalar) {
534-
var formatValue = toBool(format.valueOrThrow);
535-
if (formatValue == null) {
536-
reporter.atSourceSpan(
537-
format.span,
538-
AnalysisOptionsWarningCode.UNSUPPORTED_VALUE,
539-
arguments: [
540-
AnalysisOptionsFile.format,
541-
format.valueOrThrow,
542-
AnalysisOptionsFile._trueOrFalseProposal,
543-
],
544-
);
545-
}
546-
} else if (format is YamlList) {
533+
return;
534+
}
535+
var formatValue = toBool(format.valueOrThrow);
536+
if (formatValue == null) {
547537
reporter.atSourceSpan(
548538
format.span,
549-
AnalysisOptionsWarningCode.INVALID_SECTION_FORMAT,
550-
arguments: [AnalysisOptionsFile.format],
539+
AnalysisOptionsWarningCode.UNSUPPORTED_VALUE,
540+
arguments: [
541+
AnalysisOptionsFile.format,
542+
format.valueOrThrow,
543+
AnalysisOptionsFile._trueOrFalseProposal,
544+
],
551545
);
552546
}
553547
}
@@ -567,8 +561,8 @@ class _CompositeValidator extends OptionsValidator {
567561
}
568562
}
569563

570-
/// Validates `analyzer` enabled experiments configuration options.
571-
class _EnabledExperimentsValidator extends OptionsValidator {
564+
/// Validates the `analyzer` `enable-experiments` configuration options.
565+
class _EnableExperimentsValidator extends OptionsValidator {
572566
@override
573567
void validate(DiagnosticReporter reporter, YamlMap options) {
574568
var analyzer = options.valueAt(AnalysisOptionsFile.analyzer);
@@ -1170,8 +1164,9 @@ class _StrongModeOptionValueValidator extends OptionsValidator {
11701164
/// Validates top-level options. For example,
11711165
///
11721166
/// ```yaml
1173-
/// plugin:
1174-
/// top-level-option: true
1167+
/// analyzer:
1168+
/// exclude:
1169+
/// - lib/generated/
11751170
/// ```
11761171
class _TopLevelOptionValidator extends OptionsValidator {
11771172
final String pluginName;
@@ -1190,21 +1185,28 @@ class _TopLevelOptionValidator extends OptionsValidator {
11901185
@override
11911186
void validate(DiagnosticReporter reporter, YamlMap options) {
11921187
var node = options.valueAt(pluginName);
1193-
if (node is YamlMap) {
1194-
node.nodes.forEach((k, v) {
1195-
if (k is YamlScalar) {
1196-
if (!supportedOptions.contains(k.value)) {
1197-
reporter.atSourceSpan(
1198-
k.span,
1199-
_warningCode,
1200-
arguments: [pluginName, k.valueOrThrow, _valueProposal],
1201-
);
1202-
}
1203-
}
1204-
// TODO(pq): consider an error if the node is not a Scalar.
1205-
});
1188+
if (node == null) {
1189+
return;
1190+
}
1191+
if (node is! YamlMap) {
1192+
reporter.atSourceSpan(
1193+
node.span,
1194+
AnalysisOptionsWarningCode.INVALID_SECTION_FORMAT,
1195+
arguments: [AnalysisOptionsFile.cannotIgnore],
1196+
);
1197+
return;
12061198
}
1207-
// TODO(srawlins): Report non-Map with
1208-
// AnalysisOptionsWarningCode.INVALID_SECTION_FORMAT.
1199+
node.nodes.forEach((k, v) {
1200+
if (k is YamlScalar) {
1201+
if (!supportedOptions.contains(k.value)) {
1202+
reporter.atSourceSpan(
1203+
k.span,
1204+
_warningCode,
1205+
arguments: [pluginName, k.valueOrThrow, _valueProposal],
1206+
);
1207+
}
1208+
}
1209+
// TODO(pq): consider an error if the node is not a Scalar.
1210+
});
12091211
}
12101212
}

pkg/analyzer/test/src/options/options_file_validator_test.dart

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ analyzer:
154154
);
155155
}
156156

157-
test_analyzer_enableExperiment_notAList() {
157+
test_analyzer_enableExperiment_mapValue() {
158158
validate(
159159
'''
160160
analyzer:
@@ -165,6 +165,16 @@ analyzer:
165165
);
166166
}
167167

168+
test_analyzer_enableExperiment_scalarValue() {
169+
validate(
170+
'''
171+
analyzer:
172+
enable-experiment: 7
173+
''',
174+
[AnalysisOptionsWarningCode.INVALID_SECTION_FORMAT],
175+
);
176+
}
177+
168178
test_analyzer_error_code_supported() {
169179
validate('''
170180
analyzer:
@@ -308,6 +318,15 @@ analyzer:
308318
''', []);
309319
}
310320

321+
test_analyzer_scalarValue() {
322+
validate(
323+
'''
324+
analyzer: 7
325+
''',
326+
[AnalysisOptionsWarningCode.INVALID_SECTION_FORMAT],
327+
);
328+
}
329+
311330
test_analyzer_supported_exclude() {
312331
validate('''
313332
analyzer:
@@ -380,6 +399,15 @@ code-style:
380399
''', []);
381400
}
382401

402+
test_codeStyle_nonMap() {
403+
validate(
404+
'''
405+
code-style: 7
406+
''',
407+
[AnalysisOptionsWarningCode.INVALID_SECTION_FORMAT],
408+
);
409+
}
410+
383411
test_codeStyle_unsupported_list() {
384412
validate(
385413
'''

0 commit comments

Comments
 (0)