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

feat: add print-config option to analyze command #1057

Merged
merged 4 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* feat: support proxy calls for check-unused-l10n.
* feat: **Breaking change** cleanup public API.
* chore: restrict `analyzer` version to `>=5.1.0 <5.3.0`.
* feat: add `print-config` option to all commands.

## 4.21.2

Expand Down
1 change: 1 addition & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Future<void> main() async {
'newline-before-return': {'severity': 'info'},
},
antiPatterns: {'long-method': {}},
shouldPrintConfig: false,
);

const analyzer = LintAnalyzer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ abstract class Pattern {
Map<ScopedClassDeclaration, Report> classMetrics,
Map<ScopedFunctionDeclaration, Report> functionMetrics,
);

Map<String, Object?> toJson() => {
'id': id,
'severity': severity.toString(),
'excludes': excludes.toList(),
'dependentMetricIds': dependentMetricIds.toList(),
};
}
15 changes: 14 additions & 1 deletion lib/src/analyzers/lint_analyzer/lint_analysis_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class LintAnalysisConfig {
final Iterable<Glob> rulesExcludes;
final Iterable<Pattern> antiPatterns;
final Iterable<Metric> classesMetrics;
final Iterable<Metric> fileMetrics;
final Iterable<Metric> methodsMetrics;
final Iterable<Metric> fileMetrics;
final Iterable<Glob> metricsExcludes;
final Map<String, Object> metricsConfig;
final String excludesRootFolder;
Expand All @@ -30,4 +30,17 @@ class LintAnalysisConfig {
this.metricsConfig,
this.excludesRootFolder,
);

Map<String, Object?> toJson() => {
'rules': codeRules.map((rule) => rule.toJson()).toList(),
'rules-excludes': rulesExcludes.map((glob) => glob.pattern).toList(),
'anti-patterns':
antiPatterns.map((pattern) => pattern.toJson()).toList(),
'metrics-config': metricsConfig,
'class-metrics': classesMetrics.map((metric) => metric.id).toList(),
'method-metrics': methodsMetrics.map((metric) => metric.id).toList(),
'file-metrics': fileMetrics.map((metric) => metric.id).toList(),
'metrics-excludes':
metricsExcludes.map((glob) => glob.pattern).toList(),
};
}
4 changes: 4 additions & 0 deletions lib/src/analyzers/lint_analyzer/lint_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class LintAnalyzer {
final lintAnalysisConfig =
_getAnalysisConfig(context, rootFolder, config);

if (config.shouldPrintConfig) {
_logger?.printConfig(lintAnalysisConfig.toJson());
}

final filePaths = getFilePaths(
folders,
context,
Expand Down
5 changes: 5 additions & 0 deletions lib/src/analyzers/lint_analyzer/lint_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class LintConfig {
final Map<String, Map<String, Object>> rules;
final Iterable<String> excludeForRulesPatterns;
final Map<String, Map<String, Object>> antiPatterns;
final bool shouldPrintConfig;

const LintConfig({
required this.excludePatterns,
Expand All @@ -19,6 +20,7 @@ class LintConfig {
required this.rules,
required this.excludeForRulesPatterns,
required this.antiPatterns,
required this.shouldPrintConfig,
});

/// Creates the config from analysis [options].
Expand All @@ -32,10 +34,12 @@ class LintConfig {
.readIterableOfString(['rules-exclude'], packageRelated: true),
antiPatterns:
options.readMapOfMap(['anti-patterns'], packageRelated: true),
shouldPrintConfig: false,
);

/// Creates the config from cli [arguments].
factory LintConfig.fromArgs(ParsedArguments arguments) => LintConfig(
shouldPrintConfig: arguments.shouldPrintConfig,
excludePatterns:
arguments.excludePath.isNotEmpty ? [arguments.excludePath] : [],
excludeForMetricsPatterns: const [],
Expand All @@ -54,6 +58,7 @@ class LintConfig {
/// Config coming from [overrides] has a higher priority
/// and overrides conflicting entries.
LintConfig merge(LintConfig overrides) => LintConfig(
shouldPrintConfig: shouldPrintConfig || overrides.shouldPrintConfig,
excludePatterns: {...excludePatterns, ...overrides.excludePatterns},
excludeForMetricsPatterns: {
...excludeForMetricsPatterns,
Expand Down
6 changes: 6 additions & 0 deletions lib/src/analyzers/lint_analyzer/rules/models/rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ abstract class Rule {

/// Returns [Iterable] with [Issue]'s detected while check the passed [source]
Iterable<Issue> check(InternalResolvedUnitResult source);

Map<String, Object?> toJson() => {
'id': id,
'severity': severity.toString(),
'excludes': excludes.toList(),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class AvoidBannedImportsRule extends CommonRule {
excludes: readExcludes(config),
);

@override
Map<String, Object?> toJson() {
final json = super.toJson();
json[_entriesLabel] = _entries.map((entry) => entry.toJson()).toList();

return json;
}

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final activeEntries = _entries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ class _AvoidBannedImportsConfigEntry {
required this.deny,
required this.message,
});

Map<String, Object?> toJson() => {
'paths': paths.map((exp) => exp.pattern).toList(),
'deny': deny.map((exp) => exp.pattern).toList(),
'message': message,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class AvoidNestedConditionalExpressionsRule extends CommonRule {
excludes: readExcludes(config),
);

@override
Map<String, Object?> toJson() {
final json = super.toJson();
json[_ConfigParser._acceptableLevelConfig] = _acceptableLevel;

return json;
}

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor = _Visitor(_acceptableLevel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ class AvoidReturningWidgetsRule extends FlutterRule {
excludes: readExcludes(config),
);

@override
Map<String, Object?> toJson() {
final json = super.toJson();
json[_ConfigParser._ignoredNamesConfig] = _ignoredNames.toList();
json[_ConfigParser._ignoredAnnotationsConfig] =
_ignoredAnnotations.toList();

return json;
}

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor = _Visitor(_ignoredNames, _ignoredAnnotations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class BanNameRule extends CommonRule {
excludes: readExcludes(config),
);

@override
Map<String, Object?> toJson() {
final json = super.toJson();
json[_entriesLabel] = _entries.map((entry) => entry.toJson()).toList();

return json;
}

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor = _Visitor(_entries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ class _BanNameConfigEntry {
final String description;

_BanNameConfigEntry({required this.ident, required this.description});

Map<String, Object?> toJson() => {
'identifier': ident,
'description': description,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class FormatCommentRule extends CommonRule {
excludes: readExcludes(config),
);

@override
Map<String, Object?> toJson() {
final json = super.toJson();
json[_ConfigParser._ignoredPatternsConfig] =
_ignoredPatterns.map((exp) => exp.pattern).toList();

return json;
}

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor = _Visitor(_ignoredPatterns)..checkComments(source.unit.root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,37 @@ class _ConfigParser {
'build-method',
];

static const _orderConfig = 'order';
static const _widgetsOrderConfig = 'widgets-order';
static const _alphabetizeConfig = 'alphabetize';
static const _alphabetizeByTypeConfig = 'alphabetize-by-type';

static final _regExp = RegExp(
'(overridden-|protected-)?(private-|public-)?(static-)?(late-)?'
'(var-|final-|const-)?(nullable-)?(named-)?(factory-)?',
);

static List<_MemberGroup> parseOrder(Map<String, Object> config) {
final order = config['order'] is Iterable
? List<String>.from(config['order'] as Iterable)
final order = config[_orderConfig] is Iterable
? List<String>.from(config[_orderConfig] as Iterable)
: _defaultOrderList;

return order.map(_parseGroup).whereNotNull().toList();
}

static List<_MemberGroup> parseWidgetsOrder(Map<String, Object> config) {
final widgetsOrder = config['widgets-order'] is Iterable
? List<String>.from(config['widgets-order'] as Iterable)
final widgetsOrder = config[_widgetsOrderConfig] is Iterable
? List<String>.from(config[_widgetsOrderConfig] as Iterable)
: _defaultWidgetsOrderList;

return widgetsOrder.map(_parseGroup).whereNotNull().toList();
}

static bool parseAlphabetize(Map<String, Object> config) =>
(config['alphabetize'] as bool?) ?? false;
(config[_alphabetizeConfig] as bool?) ?? false;

static bool parseAlphabetizeByType(Map<String, Object> config) =>
(config['alphabetize-by-type'] as bool?) ?? false;
(config[_alphabetizeByTypeConfig] as bool?) ?? false;

// ignore: long-method
static _MemberGroup? _parseGroup(String group) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ class MemberOrderingRule extends CommonRule {
excludes: readExcludes(config),
);

@override
Map<String, Object?> toJson() {
final json = super.toJson();
json[_ConfigParser._orderConfig] =
_groupsOrder.map((group) => group.rawRepresentation).toList();
json[_ConfigParser._widgetsOrderConfig] =
_widgetsGroupsOrder.map((group) => group.rawRepresentation).toList();
json[_ConfigParser._alphabetizeConfig] = _alphabetize;
json[_ConfigParser._alphabetizeByTypeConfig] = _alphabetizeByType;

return json;
}

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor = _Visitor(_groupsOrder, _widgetsGroupsOrder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import '../../models/common_rule.dart';
import '../../rule_utils.dart';

part 'config_parser.dart';

part 'visitor.dart';

class MissingTestAssertionRule extends CommonRule {
Expand All @@ -33,6 +32,15 @@ class MissingTestAssertionRule extends CommonRule {
hasExcludes(config) ? readExcludes(config) : ['lib/**', 'bin/**'],
);

@override
Map<String, Object?> toJson() {
final json = super.toJson();
json[_ConfigParser._includeAssertionsConfig] = _includeAssertions.toList();
json[_ConfigParser._includeMethodsConfig] = _includeMethods.toList();

return json;
}

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor = _Visitor(_includeAssertions, _includeMethods);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class NoEqualArgumentsRule extends CommonRule {
excludes: readExcludes(config),
);

@override
Map<String, Object?> toJson() {
final json = super.toJson();
json[_ConfigParser._ignoredParametersConfig] = _ignoredParameters.toList();

return json;
}

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor = _Visitor(_ignoredParameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ class NoMagicNumberRule extends CommonRule {
excludes: readExcludes(config),
);

@override
Map<String, Object?> toJson() {
final json = super.toJson();
json[_ConfigParser._allowedConfigName] = _allowedMagicNumbers.toList();

return json;
}

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor = _Visitor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ class PreferCorrectIdentifierLengthRule extends CommonRule {
excludes: readExcludes(config),
);

@override
Map<String, Object?> toJson() {
final json = super.toJson();
json[_minIdentifierLengthLabel] = _validator.minLength;
json[_maxIdentifierLengthLabel] = _validator.maxLength;
json[_exceptionsLabel] = _validator.exceptions;

return json;
}

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor = _Visitor(_validator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ class PreferCorrectTestFileNameRule extends CommonRule {
hasExcludes(config) ? readExcludes(config) : ['lib/**', 'bin/**'],
);

@override
Map<String, Object?> toJson() {
final json = super.toJson();
json[_ConfigParser._namePatternConfig] = _fileNamePattern;

return json;
}

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor = _Visitor(source.path, _fileNamePattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ class PreferCorrectTypeNameRule extends CommonRule {
excludes: readExcludes(config),
);

@override
Map<String, Object?> toJson() {
final json = super.toJson();
json[_minTypeLengthLabel] = _validator.minLength;
json[_maxTypeLengthLabel] = _validator.maxLength;
json[_excludeLabel] = _validator.exceptions;

return json;
}

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor = _Visitor(_validator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ class PreferExtractingCallbacksRule extends FlutterRule {
excludes: readExcludes(config),
);

@override
Map<String, Object?> toJson() {
final json = super.toJson();
json[_ConfigParser._allowedLineCountConfig] = _allowedLineCount;
json[_ConfigParser._ignoredArgumentsConfig] = _ignoredArguments;

return json;
}

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor =
Expand Down
Loading