This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 258
/
unused_l10n_analyzer.dart
256 lines (216 loc) · 7.69 KB
/
unused_l10n_analyzer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import 'dart:io';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
// ignore: implementation_imports
import 'package:analyzer/src/dart/element/element.dart';
import 'package:path/path.dart';
import 'package:source_span/source_span.dart';
import '../../config_builder/config_builder.dart';
import '../../config_builder/models/analysis_options.dart';
import '../../reporters/models/reporter.dart';
import '../../utils/analyzer_utils.dart';
import '../../utils/file_utils.dart';
import 'models/unused_l10n_file_report.dart';
import 'models/unused_l10n_issue.dart';
import 'reporters/reporter_factory.dart';
import 'reporters/unused_l10n_report_params.dart';
import 'unused_l10n_config.dart';
import 'unused_l10n_visitor.dart';
/// The analyzer responsible for collecting unused localization reports.
class UnusedL10nAnalyzer {
const UnusedL10nAnalyzer();
/// Returns a reporter for the given [name]. Use the reporter
/// to convert analysis reports to console, JSON or other supported format.
Reporter<UnusedL10nFileReport, void, UnusedL10NReportParams>? getReporter({
required String name,
required IOSink output,
}) =>
reporter(
name: name,
output: output,
);
/// Returns a list of unused localization reports
/// for analyzing all files in the given [folders].
/// The analysis is configured with the [config].
Future<Iterable<UnusedL10nFileReport>> runCliAnalysis(
Iterable<String> folders,
String rootFolder,
UnusedL10nConfig config, {
String? sdkPath,
}) async {
final collection =
createAnalysisContextCollection(folders, rootFolder, sdkPath);
final localizationUsages = <ClassElement, Set<String>>{};
for (final context in collection.contexts) {
final analysisOptions = analysisOptionsFromContext(context) ??
analysisOptionsFromFilePath(rootFolder, context);
final contextConfig =
ConfigBuilder.getUnusedL10nConfigFromOption(analysisOptions)
.merge(config);
final unusedLocalizationAnalysisConfig =
ConfigBuilder.getUnusedL10nConfig(contextConfig, rootFolder);
final contextFolders = folders
.where((path) => normalize(join(rootFolder, path))
.startsWith(context.contextRoot.root.path))
.toList();
final filePaths = extractDartFilesFromFolders(
contextFolders,
rootFolder,
unusedLocalizationAnalysisConfig.globalExcludes,
);
final analyzedFiles =
filePaths.intersection(context.contextRoot.analyzedFiles().toSet());
for (final filePath in analyzedFiles) {
final unit = await context.currentSession.getResolvedUnit(filePath);
_analyzeFile(unit, unusedLocalizationAnalysisConfig.classPattern)
.forEach((classElement, usages) {
localizationUsages.update(
classElement,
(value) => value..addAll(usages),
ifAbsent: () => usages,
);
});
}
final notAnalyzedFiles = filePaths.difference(analyzedFiles);
for (final filePath in notAnalyzedFiles) {
if (unusedLocalizationAnalysisConfig.analyzerExcludedPatterns
.any((pattern) => pattern.matches(filePath))) {
final unit = await resolveFile2(path: filePath);
_analyzeFile(unit, unusedLocalizationAnalysisConfig.classPattern)
.forEach((classElement, usages) {
localizationUsages.update(
classElement,
(value) => value..addAll(usages),
ifAbsent: () => usages,
);
});
}
}
}
return _checkUnusedL10n(localizationUsages, rootFolder);
}
Map<ClassElement, Set<String>> _analyzeFile(
SomeResolvedUnitResult unit,
RegExp classPattern,
) {
if (unit is ResolvedUnitResult) {
final visitor = UnusedL10nVisitor(classPattern);
unit.unit.visitChildren(visitor);
return visitor.invocations;
}
return {};
}
Iterable<UnusedL10nFileReport> _checkUnusedL10n(
Map<ClassElement, Iterable<String>> localizationUsages,
String rootFolder,
) {
final unusedLocalizationIssues = <UnusedL10nFileReport>[];
localizationUsages.forEach((classElement, usages) {
final report = _getUnusedReports(
classElement,
usages,
rootFolder,
);
if (report != null) {
unusedLocalizationIssues.add(report);
}
final hasCustomSupertype = classElement.allSupertypes.length > 1;
if (hasCustomSupertype) {
final supertype = classElement.supertype;
if (supertype is InterfaceType) {
final report = _getUnusedReports(
supertype.element,
usages,
rootFolder,
overriddenClassName: classElement.name,
);
if (report != null) {
unusedLocalizationIssues.add(report);
}
}
}
});
return unusedLocalizationIssues;
}
UnusedL10nFileReport? _getUnusedReports(
ClassElement classElement,
Iterable<String> usages,
String rootFolder, {
String? overriddenClassName,
}) {
final unit = classElement.thisOrAncestorOfType<CompilationUnitElement>();
if (unit == null) {
return null;
}
final lineInfo = unit.lineInfo;
// ignore: unnecessary_null_comparison
if (lineInfo == null) {
return null;
}
final accessorSourceSpans = _getUnusedAccessors(classElement, usages, unit);
final methodSourceSpans = _getUnusedMethods(classElement, usages, unit);
final filePath = unit.source.toString();
final relativePath = relative(filePath, from: rootFolder);
final issues = [
...accessorSourceSpans,
...methodSourceSpans,
];
if (issues.isNotEmpty) {
return UnusedL10nFileReport(
path: filePath,
relativePath: relativePath,
className: overriddenClassName ?? classElement.name,
issues: issues,
);
}
return null;
}
Iterable<UnusedL10nIssue> _getUnusedAccessors(
ClassElement classElement,
Iterable<String> usages,
CompilationUnitElement unit,
) {
final unusedAccessors = classElement.accessors
.where((field) => !field.isPrivate && !usages.contains(field.name))
.map((field) => field.isSynthetic ? field.nonSynthetic : field);
return unusedAccessors
.map((accessor) => _createL10nIssue(accessor as ElementImpl, unit))
.toList();
}
Iterable<UnusedL10nIssue> _getUnusedMethods(
ClassElement classElement,
Iterable<String> usages,
CompilationUnitElement unit,
) {
final unusedMethods = classElement.methods
.where((method) => !method.isPrivate && !usages.contains(method.name))
.map((method) => method.isSynthetic ? method.nonSynthetic : method);
return unusedMethods
.map((method) => _createL10nIssue(method as ElementImpl, unit))
.toList();
}
UnusedL10nIssue _createL10nIssue(
ElementImpl element,
CompilationUnitElement unit,
) {
final offset = element.codeOffset!;
// ignore: unnecessary_non_null_assertion
final lineInfo = unit.lineInfo!;
final offsetLocation = lineInfo.getLocation(offset);
final sourceUrl = element.source!.uri;
final name = element is MethodElement
? '${element.displayName}(${(element as MethodElement).parameters.join(', ')})'
: element.displayName;
return UnusedL10nIssue(
memberName: name,
location: SourceLocation(
offset,
sourceUrl: sourceUrl,
line: offsetLocation.lineNumber,
column: offsetLocation.columnNumber,
),
);
}
}