Skip to content

Commit 9bb2d49

Browse files
FMorschelCommit Queue
authored andcommitted
[analyzer] Fixes included diagnostics for analysis_options
This change fixes the offset of some diagnostics so they better align with what they are analysing. Also fixes a false-positive for `included_file_warning`. Fixes: #61147 Change-Id: I3ec7f7a5042fc230410ad3b28453ab60fe6d452e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/448061 Auto-Submit: Felipe Morschel <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 31f7950 commit 9bb2d49

File tree

3 files changed

+105
-14
lines changed

3 files changed

+105
-14
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,18 @@ List<Diagnostic> analyzeAnalysisOptions(
201201
}
202202
}
203203

204-
if (includeNode is YamlScalar) {
205-
validateInclude(includeNode);
206-
} else if (includeNode is YamlList) {
207-
for (var includeValue in includeNode.nodes) {
208-
if (includeValue is YamlScalar) {
209-
validateInclude(includeValue);
210-
}
204+
var includes = switch (includeNode) {
205+
YamlScalar node => [node],
206+
YamlList(:var nodes) => nodes.whereType<YamlScalar>().toList(),
207+
_ => const <YamlScalar>[],
208+
};
209+
210+
for (var includeValue in includes) {
211+
if (sourceIsOptionsForContextRoot) {
212+
initialIncludeSpan = null;
213+
includeChain.clear();
211214
}
215+
validateInclude(includeValue);
212216
}
213217
}
214218

pkg/analyzer/test/src/diagnostics/analysis_options/include_file_not_found_test.dart

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ include: "./analysis_options.yaml"
2424
);
2525
}
2626

27+
Future<void> test_notFound_existent_list_first() async {
28+
newFile('/included1.yaml', '');
29+
await assertErrorsInCode(
30+
'''
31+
include:
32+
- ./analysis_options.yaml
33+
- included1.yaml
34+
''',
35+
[error(AnalysisOptionsWarningCode.recursiveIncludeFile, 13, 23)],
36+
);
37+
}
38+
39+
Future<void> test_notFound_existent_list_second() async {
40+
newFile('/included1.yaml', '');
41+
await assertErrorsInCode(
42+
'''
43+
include:
44+
- included1.yaml
45+
- ./analysis_options.yaml
46+
''',
47+
[error(AnalysisOptionsWarningCode.recursiveIncludeFile, 32, 23)],
48+
);
49+
}
50+
2751
Future<void> test_notFound_existent_notQuoted() async {
2852
await assertErrorsInCode(
2953
'''
@@ -55,7 +79,54 @@ include: "package:pedantic/analysis_options.yaml"
5579
40,
5680
text:
5781
"The include file 'package:pedantic/analysis_options.yaml'"
58-
" in '${convertPath('/analysis_options.yaml')}' can't be found when analyzing '/'.",
82+
" in '${convertPath('/analysis_options.yaml')}' can't be found "
83+
"when analyzing '/'.",
84+
),
85+
],
86+
);
87+
}
88+
89+
Future<void> test_notFound_nonexistent_list_first() async {
90+
newFile('/included1.yaml', '');
91+
await assertErrorsInCode(
92+
'''
93+
# We don't depend on pedantic, but we should consider adding it.
94+
include:
95+
- package:pedantic/analysis_options.yaml
96+
- included1.yaml
97+
''',
98+
[
99+
error(
100+
AnalysisOptionsWarningCode.includeFileNotFound,
101+
78,
102+
38,
103+
text:
104+
"The include file 'package:pedantic/analysis_options.yaml'"
105+
" in '${convertPath('/analysis_options.yaml')}' can't be found "
106+
"when analyzing '/'.",
107+
),
108+
],
109+
);
110+
}
111+
112+
Future<void> test_notFound_nonexistent_list_second() async {
113+
newFile('/included1.yaml', '');
114+
await assertErrorsInCode(
115+
'''
116+
# We don't depend on pedantic, but we should consider adding it.
117+
include:
118+
- included1.yaml
119+
- package:pedantic/analysis_options.yaml
120+
''',
121+
[
122+
error(
123+
AnalysisOptionsWarningCode.includeFileNotFound,
124+
97,
125+
38,
126+
text:
127+
"The include file 'package:pedantic/analysis_options.yaml'"
128+
" in '${convertPath('/analysis_options.yaml')}' can't be found "
129+
"when analyzing '/'.",
59130
),
60131
],
61132
);
@@ -74,7 +145,8 @@ include: package:pedantic/analysis_options.yaml
74145
38,
75146
text:
76147
"The include file 'package:pedantic/analysis_options.yaml'"
77-
" in '${convertPath('/analysis_options.yaml')}' can't be found when analyzing '/'.",
148+
" in '${convertPath('/analysis_options.yaml')}' can't be found "
149+
"when analyzing '/'.",
78150
),
79151
],
80152
);
@@ -93,7 +165,8 @@ include: 'package:pedantic/analysis_options.yaml'
93165
40,
94166
text:
95167
"The include file 'package:pedantic/analysis_options.yaml'"
96-
" in '${convertPath('/analysis_options.yaml')}' can't be found when analyzing '/'.",
168+
" in '${convertPath('/analysis_options.yaml')}' can't be found "
169+
"when analyzing '/'.",
97170
),
98171
],
99172
);

pkg/analyzer/test/src/diagnostics/analysis_options/recursive_include_file_test.dart

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ include: analysis_options.yaml
2727
21,
2828
text:
2929
"The include file 'analysis_options.yaml' "
30-
"in '${convertPath('/analysis_options.yaml')}' includes itself recursively.",
30+
"in '${convertPath('/analysis_options.yaml')}' includes itself "
31+
'recursively.',
3132
),
3233
],
3334
);
@@ -46,12 +47,25 @@ include:
4647
21,
4748
text:
4849
"The include file 'analysis_options.yaml' "
49-
"in '${convertPath('/analysis_options.yaml')}' includes itself recursively.",
50+
"in '${convertPath('/analysis_options.yaml')}' includes itself "
51+
'recursively.',
5052
),
5153
],
5254
);
5355
}
5456

57+
Future<void> test_notRecursive() async {
58+
newFile('/a.yaml', '''
59+
include: b.yaml
60+
''');
61+
newFile('/b.yaml', '');
62+
await assertNoErrorsInCode('''
63+
include:
64+
- a.yaml
65+
- b.yaml
66+
''');
67+
}
68+
5569
Future<void> test_recursive() async {
5670
newFile('/a.yaml', '''
5771
include: b.yaml
@@ -116,8 +130,8 @@ include:
116130
[
117131
error(
118132
AnalysisOptionsWarningCode.recursiveIncludeFile,
119-
13,
120-
10,
133+
28,
134+
6,
121135
text:
122136
"The include file 'analysis_options.yaml' "
123137
"in '${convertPath('/b.yaml')}' includes itself recursively.",

0 commit comments

Comments
 (0)