Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit d01d3cf

Browse files
authored
fix: fix rule and metric excludes for monorepos (#439)
* fix: fix rule and metric excludes for monorepos * chore: rename excludeRootFolder
1 parent ae0af1c commit d01d3cf

File tree

8 files changed

+40
-24
lines changed

8 files changed

+40
-24
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Fix rule and metrics excludes for monorepos.
6+
37
## 4.2.0
48

59
* Add static code diagnostics `avoid-ignoring-return-values`, `prefer-match-file-name`, `prefer-single-widget-per-file`.

lib/src/analyzer_plugin/analyzer_plugin.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,17 @@ class MetricsAnalyzerPlugin extends ServerPlugin {
238238
LintAnalysisConfig? _createConfig(AnalysisDriver driver, String rootPath) {
239239
final file = driver.analysisContext?.contextRoot.optionsFile;
240240
if (file != null && file.exists) {
241-
final options = AnalysisOptions(yamlMapToDartMap(
242-
AnalysisOptionsProvider(driver.sourceFactory).getOptionsFromFile(file),
243-
));
241+
final options = AnalysisOptions(
242+
file.path,
243+
yamlMapToDartMap(
244+
AnalysisOptionsProvider(driver.sourceFactory)
245+
.getOptionsFromFile(file),
246+
),
247+
);
244248
final config = ConfigBuilder.getLintConfigFromOptions(options);
245249
final lintConfig = ConfigBuilder.getLintAnalysisConfig(
246250
config,
247-
rootPath,
251+
options.folderPath ?? rootPath,
248252
classMetrics: const [],
249253
functionMetrics: [
250254
CyclomaticComplexityMetric(config: config.metrics),

lib/src/analyzers/lint_analyzer/lint_analysis_config.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class LintAnalysisConfig {
1616
final Iterable<Metric> methodsMetrics;
1717
final Iterable<Glob> metricsExcludes;
1818
final Map<String, Object> metricsConfig;
19+
final String excludesRootFolder;
1920

2021
const LintAnalysisConfig(
2122
this.globalExcludes,
@@ -25,5 +26,6 @@ class LintAnalysisConfig {
2526
this.methodsMetrics,
2627
this.metricsExcludes,
2728
this.metricsConfig,
29+
this.excludesRootFolder,
2830
);
2931
}

lib/src/analyzers/lint_analyzer/lint_analyzer.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,14 @@ class LintAnalyzer {
8080
final analysisOptions = await analysisOptionsFromContext(context) ??
8181
await analysisOptionsFromFilePath(rootFolder);
8282

83+
final excludesRootFolder = analysisOptions.folderPath ?? rootFolder;
84+
8385
final contextConfig =
8486
ConfigBuilder.getLintConfigFromOptions(analysisOptions).merge(config);
85-
final lintAnalysisConfig =
86-
ConfigBuilder.getLintAnalysisConfig(contextConfig, rootFolder);
87+
final lintAnalysisConfig = ConfigBuilder.getLintAnalysisConfig(
88+
contextConfig,
89+
excludesRootFolder,
90+
);
8791

8892
final contextFolders = folders
8993
.where((path) => normalize(join(rootFolder, path))
@@ -142,7 +146,6 @@ class LintAnalyzer {
142146
internalResult,
143147
config,
144148
filePath,
145-
rootFolder,
146149
);
147150

148151
if (!isExcluded(filePath, config.metricsExcludes)) {
@@ -212,16 +215,14 @@ class LintAnalyzer {
212215
InternalResolvedUnitResult source,
213216
LintAnalysisConfig config,
214217
String filePath,
215-
String? rootFolder,
216218
) =>
217219
config.codeRules
218220
.where((rule) =>
219221
!ignores.isSuppressed(rule.id) &&
220-
(rootFolder == null ||
221-
!isExcluded(
222-
filePath,
223-
prepareExcludes(rule.excludes, rootFolder),
224-
)))
222+
!isExcluded(
223+
filePath,
224+
prepareExcludes(rule.excludes, config.excludesRootFolder),
225+
))
225226
.expand(
226227
(rule) =>
227228
rule.check(source).where((issue) => !ignores.isSuppressedAt(

lib/src/config_builder/config_builder.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ class ConfigBuilder {
2020

2121
static LintAnalysisConfig getLintAnalysisConfig(
2222
LintConfig config,
23-
String rootPath, {
23+
String excludesRootFolder, {
2424
Iterable<Metric<num>>? classMetrics,
2525
Iterable<Metric<num>>? functionMetrics,
2626
}) =>
2727
LintAnalysisConfig(
28-
prepareExcludes(config.excludePatterns, rootPath),
28+
prepareExcludes(config.excludePatterns, excludesRootFolder),
2929
getRulesById(config.rules),
3030
getPatternsById(config.antiPatterns),
3131
classMetrics ??
@@ -38,8 +38,9 @@ class ConfigBuilder {
3838
config: config.metrics,
3939
measuredType: EntityType.methodEntity,
4040
),
41-
prepareExcludes(config.excludeForMetricsPatterns, rootPath),
41+
prepareExcludes(config.excludeForMetricsPatterns, excludesRootFolder),
4242
config.metrics,
43+
excludesRootFolder,
4344
);
4445

4546
static UnusedFilesConfig getUnusedFilesConfigFromArgs(

lib/src/config_builder/models/analysis_options.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ const _analysisOptionsFileName = 'analysis_options.yaml';
1616
class AnalysisOptions {
1717
final Map<String, Object> options;
1818

19-
const AnalysisOptions(this.options);
19+
final String? _path;
20+
21+
const AnalysisOptions(this._path, this.options);
22+
23+
String? get folderPath => _path?.split('/$_analysisOptionsFileName').first;
2024

2125
Iterable<String> readIterableOfString(Iterable<String> pathSegments) {
2226
Object? data = options;
@@ -107,8 +111,8 @@ Future<AnalysisOptions> analysisOptionsFromFilePath(String path) {
107111

108112
Future<AnalysisOptions> analysisOptionsFromFile(File? options) async =>
109113
options != null && options.existsSync()
110-
? AnalysisOptions(await _loadConfigFromYamlFile(options))
111-
: const AnalysisOptions({});
114+
? AnalysisOptions(options.path, await _loadConfigFromYamlFile(options))
115+
: const AnalysisOptions(null, {});
112116

113117
Future<Map<String, Object>> _loadConfigFromYamlFile(File options) async {
114118
try {

test/analyzers/lint_analyzer/lint_config_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'package:dart_code_metrics/src/analyzers/lint_analyzer/lint_config.dart';
33
import 'package:dart_code_metrics/src/config_builder/models/analysis_options.dart';
44
import 'package:test/test.dart';
55

6-
const _options = AnalysisOptions({
6+
const _options = AnalysisOptions('path', {
77
'include': 'package:pedantic/analysis_options.yaml',
88
'analyzer': {
99
'exclude': ['test/resources/**'],
@@ -91,7 +91,7 @@ void main() {
9191
group('fromAnalysisOptions constructs instance from passed', () {
9292
test('empty options', () {
9393
final config =
94-
LintConfig.fromAnalysisOptions(const AnalysisOptions({}));
94+
LintConfig.fromAnalysisOptions(const AnalysisOptions(null, {}));
9595

9696
expect(config.excludePatterns, isEmpty);
9797
expect(config.excludeForMetricsPatterns, isEmpty);

test/config_builder/models/analysis_options_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void main() {
102102

103103
group('AnalysisOptions', () {
104104
test('readIterableOfString returns iterables with data or not', () {
105-
const options = AnalysisOptions(_options);
105+
const options = AnalysisOptions(null, _options);
106106

107107
expect(options.readIterableOfString([]), isEmpty);
108108
expect(options.readIterableOfString(['key']), isEmpty);
@@ -121,7 +121,7 @@ void main() {
121121
});
122122

123123
test('readMap returns map with data or not', () {
124-
const options = AnalysisOptions(_options);
124+
const options = AnalysisOptions(null, _options);
125125

126126
expect(options.readMap([]), equals(_options));
127127
expect(options.readMap(['include']), isEmpty);
@@ -140,7 +140,7 @@ void main() {
140140
});
141141

142142
test('readMapOfMap returns map with data or not', () async {
143-
const options = AnalysisOptions({
143+
const options = AnalysisOptions(null, {
144144
'dart_code_metrics': {
145145
'metrics': {'metric-id1': 10},
146146
'metrics-exclude': ['documentation/**'],

0 commit comments

Comments
 (0)