-
Notifications
You must be signed in to change notification settings - Fork 24
Migrate avoid_late_keyword rule (default behavior) and tests #231
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
Islam-Shaaban-Ibrahim
wants to merge
25
commits into
analysis_server_migration
Choose a base branch
from
migrate/avoid_late_keyword
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 all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
43a6648
Created analysis_options.yaml rules parser
Dariaa14 87abe51
Improved yaml parser and added the analysis_options loader
Dariaa14 4c604a3
Improved rules loader from yaml
Dariaa14 87f5870
Added verification before looking for .yaml's path
Dariaa14 ca9df7c
Fields and getters are now declared before the constructor
Dariaa14 226a748
Added method to get options of a rule by it's name
Dariaa14 08c3e8e
Made suggested changes to file upward finder
Dariaa14 1eee0b2
Removed top-level variable
Dariaa14 0ef7917
Improved name of variable in loadRuleFromContext
Dariaa14 86c3f4d
Updated analysis options to have rules for each configuration file path
Dariaa14 e0490f7
Updated file upward finder to not mix File from dart.io with file fro…
Dariaa14 03a53ba
Added usage example in avoid_global_state_rule
Dariaa14 9f90265
style: move getters and fields before constructor
andrew-bekhiet-solid a8d53e4
style: improve readability
andrew-bekhiet-solid 73514fd
fix: don't parse enabled if the rule has configured options
andrew-bekhiet-solid c6a2453
feat: reload rules from file if newer
andrew-bekhiet-solid 55af03a
test: add AnalysisOptionsLoaderTest
andrew-bekhiet-solid 06c5367
feat(SolidLintRule): add parameter parsing
andrew-bekhiet-solid affc62c
fix: use Map<String, Object?> for raw rule config
andrew-bekhiet-solid 1b10b3d
fix: method name
andrew-bekhiet-solid 4c30ac9
fix: make sure rules options are loaded before getting parameters
andrew-bekhiet-solid 1cb4497
refactor: remove unused AnalysisOptionsLoader from AvoidGlobalStateRule
andrew-bekhiet-solid 0975ba7
refactor: extract duplicate logic
andrew-bekhiet-solid fd624c4
Migrate avoid_late_keyword rule (default behavior) and tests
Islam-Shaaban-Ibrahim 6092317
feat: read parameter by extending SolidLintRule
andrew-bekhiet-solid 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
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
109 changes: 109 additions & 0 deletions
109
lib/src/common/parameter_parser/analysis_options_loader.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,109 @@ | ||
| import 'package:analyzer/analysis_rule/rule_context.dart'; | ||
| import 'package:analyzer/file_system/file_system.dart'; | ||
| import 'package:analyzer/file_system/physical_file_system.dart'; | ||
| import 'package:solid_lints/src/common/parameter_parser/cached_package_rules.dart'; | ||
| import 'package:yaml/yaml.dart'; | ||
|
|
||
| /// Loads and parses analysis options from a Dart project's YAML file. | ||
| class AnalysisOptionsLoader { | ||
| final ResourceProvider _resourceProvider; | ||
| final Map<String, CachedPackageRules> _rulesCache = {}; | ||
|
|
||
| /// Creates an instance of [AnalysisOptionsLoader] | ||
| AnalysisOptionsLoader({ResourceProvider? resourceProvider}) | ||
| : _resourceProvider = | ||
| resourceProvider ?? PhysicalResourceProvider.INSTANCE; | ||
|
|
||
| /// Gets the options for a specific rule by its name. | ||
| Map<String, Object?>? getRuleOptions(RuleContext context, String ruleName) => | ||
| _withNearestAnalysisOptionsFilePathForContext<Map<String, Object?>?>( | ||
| context, | ||
| (path) => _rulesCache[path]?.rules[ruleName], | ||
| ); | ||
|
|
||
| /// Loads lint rules from the analysis options file for all rules | ||
| /// using the provided [RuleContext]. | ||
| void loadRulesOptionsFromContext(RuleContext context) => | ||
| _withNearestAnalysisOptionsFilePathForContext( | ||
| context, | ||
| _loadRulesOptionsIfNewer, | ||
| ); | ||
|
|
||
| T? _withNearestAnalysisOptionsFilePathForContext<T>( | ||
| RuleContext context, | ||
| T Function(String) f, | ||
| ) { | ||
| final packageRootPath = context.package?.root.path; | ||
| if (packageRootPath == null) return null; | ||
|
|
||
| final yamlPath = _findNearestAnalysisOptionsFilePath(packageRootPath); | ||
| if (yamlPath == null) return null; | ||
|
|
||
| return f(yamlPath); | ||
| } | ||
|
|
||
| void _loadRulesOptionsIfNewer(String yamlPath) { | ||
| final analysisOptionsFile = _resourceProvider.getFile(yamlPath); | ||
| final modificationStamp = analysisOptionsFile.modificationStamp; | ||
| final cachedRules = _rulesCache[yamlPath]; | ||
|
|
||
| if (cachedRules?.modificationStamp == modificationStamp) { | ||
| return; | ||
| } | ||
|
|
||
| final rules = _getRules(analysisOptionsFile); | ||
| _rulesCache[yamlPath] = CachedPackageRules( | ||
| modificationStamp: modificationStamp, | ||
| rules: rules, | ||
| ); | ||
| } | ||
|
|
||
| String? _findNearestAnalysisOptionsFilePath(String packageRootPath) { | ||
| final pathContext = _resourceProvider.pathContext; | ||
| String currentDirectoryPath = packageRootPath; | ||
|
|
||
| while (pathContext.dirname(currentDirectoryPath) != currentDirectoryPath) { | ||
| final candidatePath = | ||
| pathContext.join(currentDirectoryPath, 'analysis_options.yaml'); | ||
| final candidateFile = _resourceProvider.getFile(candidatePath); | ||
|
|
||
| if (candidateFile.exists) { | ||
| return candidatePath; | ||
| } | ||
|
|
||
| final parentDir = pathContext.dirname(currentDirectoryPath); | ||
| currentDirectoryPath = parentDir; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| Map<String, Map<String, Object?>> _getRules(File? analysisOptionsFile) { | ||
| if (analysisOptionsFile == null || !analysisOptionsFile.exists) { | ||
| return {}; | ||
| } | ||
|
|
||
| final optionsString = analysisOptionsFile.readAsStringSync(); | ||
| Object? yaml; | ||
| try { | ||
| yaml = loadYaml(optionsString) as Object?; | ||
| } catch (err) { | ||
| return {}; | ||
| } | ||
|
|
||
| if (yaml | ||
| case {'plugins': {'solid_lints': {'diagnostics': final diagnostics?}}} | ||
| when diagnostics is Map) { | ||
| return Map.fromEntries( | ||
| diagnostics.entries.where((e) => e.key is String && e.value is Map).map( | ||
| (e) => MapEntry( | ||
| e.key as String, | ||
| Map<String, Object?>.from(e.value as Map), | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| return {}; | ||
| } | ||
| } |
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,14 @@ | ||
| /// Cached rules for a dart package | ||
| class CachedPackageRules { | ||
| /// The last modification stamp of the analysis options file | ||
| final int modificationStamp; | ||
|
|
||
| /// Cached rules options by rule name for the package | ||
| final Map<String, Map<String, Object?>> rules; | ||
|
|
||
| /// Creates an instance of [CachedPackageRules] | ||
| const CachedPackageRules({ | ||
| required this.modificationStamp, | ||
| required this.rules, | ||
| }); | ||
| } |
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
48 changes: 48 additions & 0 deletions
48
lib/src/lints/avoid_late_keyword/visitors/avoid_late_keyword_visitor.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,48 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
| import 'package:solid_lints/src/lints/avoid_late_keyword/avoid_late_keyword_rule.dart'; | ||
| import 'package:solid_lints/src/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart'; | ||
| import 'package:solid_lints/src/utils/types_utils.dart'; | ||
|
|
||
| /// Visitor for [AvoidLateKeywordRule]. | ||
| class AvoidLateKeywordVisitor extends SimpleAstVisitor<void> { | ||
| final AvoidLateKeywordRule _rule; | ||
|
|
||
| final AvoidLateKeywordParameters _parameters; | ||
|
|
||
| /// Creates an instance of [AvoidLateKeywordVisitor]. | ||
| AvoidLateKeywordVisitor(this._rule, this._parameters); | ||
|
|
||
| @override | ||
| void visitVariableDeclaration(VariableDeclaration node) { | ||
| if (!_shouldReport(node)) return; | ||
|
|
||
| _rule.reportAtNode(node); | ||
| } | ||
|
|
||
| bool _shouldReport(VariableDeclaration node) { | ||
| final isLateDeclaration = node.isLate; | ||
| if (!isLateDeclaration) return false; | ||
|
|
||
| final hasIgnoredType = _hasIgnoredType(node); | ||
| if (hasIgnoredType) return false; | ||
|
|
||
| final allowInitialized = _parameters.allowInitialized; | ||
| if (!allowInitialized) return true; | ||
|
|
||
| final hasInitializer = node.initializer != null; | ||
| return !hasInitializer; | ||
| } | ||
|
|
||
| bool _hasIgnoredType(VariableDeclaration node) { | ||
| final ignoredTypes = _parameters.ignoredTypes.toSet(); | ||
| if (ignoredTypes.isEmpty) return false; | ||
|
|
||
| final variableType = node.declaredFragment?.element.type; | ||
| if (variableType == null) return false; | ||
|
|
||
| return variableType.hasIgnoredType( | ||
| ignoredTypes: ignoredTypes, | ||
| ); | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
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.