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

Commit 7b6c470

Browse files
incendialdkrutskikh
authored andcommitted
fix: fix rule and metric excludes for monorepos (#439)
* fix: fix rule and metric excludes for monorepos * chore: rename excludeRootFolder
1 parent 7968440 commit 7b6c470

File tree

8 files changed

+37
-24
lines changed

8 files changed

+37
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 4.2.0-dev.4
44

5+
* Fix rule and metrics excludes for monorepos.
56
* Revert: Changed the supported `analyzer` version to `^2.1.0`.
67
* Revert: Changed the supported `analyzer` version to `^2.0.0`.
78
* Revert: Changed the supported `analyzer_plugin` version to `^0.7.0`.

lib/src/analyzer_plugin/analyzer_plugin.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,17 @@ class MetricsAnalyzerPlugin extends ServerPlugin {
239239
LintAnalysisConfig? _createConfig(AnalysisDriver driver, String rootPath) {
240240
final file = driver.analysisContext?.contextRoot.optionsFile;
241241
if (file != null && file.exists) {
242-
final options = AnalysisOptions(yamlMapToDartMap(
243-
AnalysisOptionsProvider(driver.sourceFactory).getOptionsFromFile(file),
244-
));
242+
final options = AnalysisOptions(
243+
file.path,
244+
yamlMapToDartMap(
245+
AnalysisOptionsProvider(driver.sourceFactory)
246+
.getOptionsFromFile(file),
247+
),
248+
);
245249
final config = ConfigBuilder.getLintConfigFromOptions(options);
246250
final lintConfig = ConfigBuilder.getLintAnalysisConfig(
247251
config,
248-
rootPath,
252+
options.folderPath ?? rootPath,
249253
classMetrics: const [],
250254
functionMetrics: [
251255
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
@@ -85,10 +85,14 @@ class LintAnalyzer {
8585
final analysisOptions = await analysisOptionsFromContext(context) ??
8686
await analysisOptionsFromFilePath(rootFolder);
8787

88+
final excludesRootFolder = analysisOptions.folderPath ?? rootFolder;
89+
8890
final contextConfig =
8991
ConfigBuilder.getLintConfigFromOptions(analysisOptions).merge(config);
90-
final lintAnalysisConfig =
91-
ConfigBuilder.getLintAnalysisConfig(contextConfig, rootFolder);
92+
final lintAnalysisConfig = ConfigBuilder.getLintAnalysisConfig(
93+
contextConfig,
94+
excludesRootFolder,
95+
);
9296

9397
final contextFolders = folders
9498
.where((path) => normalize(join(rootFolder, path))
@@ -152,7 +156,6 @@ class LintAnalyzer {
152156
internalResult,
153157
config,
154158
filePath,
155-
rootFolder,
156159
);
157160

158161
if (!isExcluded(filePath, config.metricsExcludes)) {
@@ -222,16 +225,14 @@ class LintAnalyzer {
222225
InternalResolvedUnitResult source,
223226
LintAnalysisConfig config,
224227
String filePath,
225-
String? rootFolder,
226228
) =>
227229
config.codeRules
228230
.where((rule) =>
229231
!ignores.isSuppressed(rule.id) &&
230-
(rootFolder == null ||
231-
!isExcluded(
232-
filePath,
233-
prepareExcludes(rule.excludes, rootFolder),
234-
)))
232+
!isExcluded(
233+
filePath,
234+
prepareExcludes(rule.excludes, config.excludesRootFolder),
235+
))
235236
.expand(
236237
(rule) =>
237238
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)