-
Notifications
You must be signed in to change notification settings - Fork 24
fix: support package-level analysis options and rule suppression #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
solid-illiaaihistov
wants to merge
4
commits into
solid-software:analysis_server_migration
Choose a base branch
from
solid-illiaaihistov:317-restore-package-default-options
base: analysis_server_migration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ed46119
fix: support package-level analysis options and rule suppression
dbdc1d4
refactor: improve analysis options parsing robustness with type stand…
930b5bc
refactor: implement file-change detection for package configs and sup…
bc38e85
fix: improve robustness of configuration parsing and URI resolution t…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,4 @@ | ||
| analyzer: | ||
| plugins: | ||
| - custom_lint | ||
| exclude: | ||
| # General generated files | ||
| - "**/*.g.dart" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /// Shared constants for the solid_lints package. | ||
| const kPluginName = 'solid_lints'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
lib/src/common/parameter_parser/analysis_options_parser.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import 'package:analyzer/file_system/file_system.dart'; | ||
| import 'package:solid_lints/src/common/constants.dart'; | ||
| import 'package:solid_lints/src/common/parameter_parser/package_config_resolver.dart'; | ||
| import 'package:solid_lints/src/common/parameter_parser/rules_data.dart'; | ||
| import 'package:yaml/yaml.dart'; | ||
|
|
||
| /// Parser for analysis_options.yaml files to extract RulesData. | ||
| class AnalysisOptionsParser { | ||
| final ResourceProvider _resourceProvider; | ||
| final PackageConfigResolver _packageConfigResolver; | ||
|
|
||
| /// Creates a new instance of [AnalysisOptionsParser]. | ||
| AnalysisOptionsParser(this._resourceProvider, this._packageConfigResolver); | ||
|
|
||
| /// Parses the given [analysisOptionsFile] and resolves its imports | ||
| /// to return [RulesData]. | ||
| RulesData parse(File? analysisOptionsFile) { | ||
| return _parseWithSeen(analysisOptionsFile, {}); | ||
| } | ||
|
|
||
| RulesData _parseWithSeen(File? analysisOptionsFile, Set<String> seenPaths) { | ||
| if (analysisOptionsFile == null || !analysisOptionsFile.exists) { | ||
| return const RulesData.empty(); | ||
| } | ||
|
|
||
| final path = analysisOptionsFile.path; | ||
| if (seenPaths.contains(path)) { | ||
| return const RulesData.empty(); | ||
| } | ||
| seenPaths.add(path); | ||
|
|
||
| final yaml = _parseYaml(analysisOptionsFile); | ||
| if (yaml == null) { | ||
| return const RulesData.empty(); | ||
| } | ||
|
|
||
| final mergedRules = <String, Map<String, Object?>>{}; | ||
| final disabledRules = <String>{}; | ||
|
|
||
| _resolveAndMergeIncludes( | ||
| analysisOptionsFile, | ||
| yaml, | ||
| seenPaths, | ||
| mergedRules, | ||
| disabledRules, | ||
| ); | ||
| _parseRuleOptions(yaml, mergedRules, disabledRules); | ||
| _parseSuppressedErrors(yaml, mergedRules, disabledRules); | ||
|
|
||
| return RulesData(rules: mergedRules, disabledRules: disabledRules); | ||
| } | ||
|
solid-illiaaihistov marked this conversation as resolved.
|
||
|
|
||
| Map<dynamic, dynamic>? _parseYaml(File file) { | ||
| try { | ||
| final optionsString = file.readAsStringSync(); | ||
| final parsed = loadYaml(optionsString); | ||
| return parsed is Map ? parsed : null; | ||
| } catch (_) { | ||
| return null; | ||
| } | ||
| } | ||
|
solid-illiaaihistov marked this conversation as resolved.
Outdated
|
||
|
|
||
| void _resolveAndMergeIncludes( | ||
| File baseFile, | ||
| Map<dynamic, dynamic> yaml, | ||
| Set<String> seenPaths, | ||
| Map<String, Map<String, Object?>> mergedRules, | ||
| Set<String> disabledRules, | ||
| ) { | ||
| final includeOption = yaml['include']; | ||
| if (includeOption is! String) return; | ||
|
|
||
| final includedFile = _resolveIncludedFile(baseFile, includeOption); | ||
| if (includedFile == null) return; | ||
|
|
||
| final includedData = _parseWithSeen(includedFile, seenPaths); | ||
| mergedRules.addAll(includedData.rules); | ||
| disabledRules.addAll(includedData.disabledRules); | ||
| } | ||
|
solid-illiaaihistov marked this conversation as resolved.
|
||
|
|
||
| File? _resolveIncludedFile(File baseFile, String includePath) { | ||
| final pathContext = _resourceProvider.pathContext; | ||
| if (includePath.startsWith('package:')) { | ||
| final resolvedPath = _packageConfigResolver.resolvePackageUri( | ||
| baseFile.path, | ||
| includePath, | ||
| ); | ||
| if (resolvedPath != null) { | ||
| return _resourceProvider.getFile(resolvedPath); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| final baseDir = pathContext.dirname(baseFile.path); | ||
| final resolvedPath = pathContext.join(baseDir, includePath); | ||
| return _resourceProvider.getFile(resolvedPath); | ||
| } | ||
|
|
||
| void _parseRuleOptions( | ||
| Map<dynamic, dynamic> yaml, | ||
| Map<String, Map<String, Object?>> mergedRules, | ||
| Set<String> disabledRules, | ||
| ) { | ||
| final rawDiagnostics = _extractDiagnostics(yaml); | ||
| if (rawDiagnostics is! Map) return; | ||
|
|
||
| for (final entry in rawDiagnostics.entries) { | ||
| final key = entry.key; | ||
| if (key is! String) continue; | ||
|
|
||
| final ruleName = key; | ||
| final value = entry.value; | ||
|
|
||
| if (value is Map) { | ||
| final existingOptions = mergedRules[ruleName] ?? {}; | ||
| mergedRules[ruleName] = <String, Object?>{ | ||
| ...existingOptions, | ||
| for (final optionEntry in value.entries) | ||
| if (optionEntry.key is String) | ||
| optionEntry.key as String: optionEntry.value, | ||
| }; | ||
| disabledRules.remove(ruleName); | ||
| } else if (value is bool) { | ||
| if (value) { | ||
| disabledRules.remove(ruleName); | ||
| } else { | ||
| mergedRules.remove(ruleName); | ||
| disabledRules.add(ruleName); | ||
| } | ||
| } | ||
|
solid-illiaaihistov marked this conversation as resolved.
Outdated
solid-illiaaihistov marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| Object? _extractDiagnostics(Map<dynamic, dynamic> yaml) { | ||
| final pluginConfig = yaml[kPluginName]; | ||
| if (pluginConfig is Map) { | ||
| return pluginConfig['diagnostics']; | ||
| } | ||
|
|
||
| final pluginsConfig = yaml['plugins']; | ||
| if (pluginsConfig is Map) { | ||
| final pluginSubConfig = pluginsConfig[kPluginName]; | ||
| if (pluginSubConfig is Map) { | ||
| return pluginSubConfig['diagnostics']; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /// Parses rule suppression configured under `analyzer: errors:`. | ||
| /// | ||
| /// By default, when a user excludes/ignores a rule using the IDE | ||
| /// quick fix or standard settings, the Dart analysis server appends | ||
| /// the following to `analysis_options.yaml`: | ||
| /// | ||
| /// ```yaml | ||
| /// analyzer: | ||
| /// errors: | ||
| /// solid_lints/rule_name: ignore | ||
| /// ``` | ||
| /// | ||
| /// To respect this standard mechanism and prevent the plugin rules | ||
| /// from running, we parse this section and add suppressed rules | ||
| /// to [disabledRules]. | ||
| void _parseSuppressedErrors( | ||
| Map<dynamic, dynamic> yaml, | ||
| Map<String, Map<String, Object?>> mergedRules, | ||
| Set<String> disabledRules, | ||
| ) { | ||
| final analyzer = yaml['analyzer']; | ||
| if (analyzer is! Map) return; | ||
|
|
||
| final errors = analyzer['errors']; | ||
| if (errors is! Map) return; | ||
|
|
||
| const pluginPrefix = '$kPluginName/'; | ||
|
|
||
| for (final entry in errors.entries) { | ||
| final key = entry.key; | ||
| if (key is! String) continue; | ||
|
|
||
| final errorValue = entry.value; | ||
| final ruleName = key.startsWith(pluginPrefix) | ||
| ? key.substring(pluginPrefix.length) | ||
| : key; | ||
|
|
||
| if (errorValue == 'ignore') { | ||
| mergedRules.remove(ruleName); | ||
| disabledRules.add(ruleName); | ||
| } else { | ||
| disabledRules.remove(ruleName); | ||
| } | ||
|
solid-illiaaihistov marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
solid-illiaaihistov marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.