This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
IncompatibleWith validation #1911
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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,124 @@ | ||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:analyzer/dart/analysis/analysis_context_collection.dart'; | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:analyzer/file_system/physical_file_system.dart'; | ||
|
||
import '../analyzer.dart'; | ||
import '../rules.dart'; | ||
import '../utils.dart'; | ||
|
||
const String _INCOMPATIBLE_WITH_CLASS_NAME = 'IncompatibleWith'; | ||
const String _LINT_RULE_CLASS_NAME = 'LintRule'; | ||
const String _LINTER_META_LIB_NAME = 'linter_meta'; | ||
|
||
bool _isIncompatibleWithAnnotation(Element element) => | ||
element is ConstructorElement && | ||
element.enclosingElement.name == _INCOMPATIBLE_WITH_CLASS_NAME && | ||
element.library?.name == _LINTER_META_LIB_NAME; | ||
|
||
class LintCache { | ||
List<LintRuleDetails> details = <LintRuleDetails>[]; | ||
|
||
bool _initialized = false; | ||
|
||
LintRuleDetails findDetailsByClassName(String className) { | ||
for (var detail in details) { | ||
if (detail.className == className) { | ||
return detail; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
LintRuleDetails findDetailsById(String id) { | ||
for (var detail in details) { | ||
if (detail.id == id) { | ||
return detail; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
Future<void> init() async { | ||
if (_initialized) { | ||
return; | ||
} | ||
registerLintRules(); | ||
|
||
// Setup details. | ||
for (var lint in Analyzer.facade.registeredRules) { | ||
details.add(LintRuleDetails(lint.runtimeType.toString(), lint.name)); | ||
} | ||
|
||
// Process compatibility annotations. | ||
final rulePath = File('lib/src/rules').absolute.path; | ||
final collection = AnalysisContextCollection( | ||
includedPaths: [rulePath], | ||
resourceProvider: PhysicalResourceProvider.INSTANCE, | ||
); | ||
|
||
final visitor = _LintVisitor(); | ||
for (var context in collection.contexts) { | ||
for (var filePath in context.contextRoot.analyzedFiles()) { | ||
if (isDartFileName(filePath)) { | ||
final result = await context.currentSession.getResolvedUnit(filePath); | ||
result.unit.accept(visitor); | ||
} | ||
} | ||
} | ||
|
||
for (var classElement in visitor.classElements) { | ||
for (var annotation in classElement.metadata) { | ||
if (_isIncompatibleWithAnnotation(annotation.element)) { | ||
final constantValue = annotation.computeConstantValue(); | ||
final ruleObjects = constantValue?.getField('rules')?.toListValue(); | ||
if (ruleObjects != null) { | ||
final lintClassName = classElement.thisType.name; | ||
final ruleDetails = findDetailsByClassName(lintClassName); | ||
for (var ruleObject in ruleObjects) { | ||
final ruleId = ruleObject.toStringValue(); | ||
ruleDetails.incompatibleRules.add(ruleId); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
_initialized = true; | ||
} | ||
} | ||
|
||
class LintRuleDetails { | ||
final String className; | ||
final String id; | ||
final List<String> incompatibleRules = <String>[]; | ||
LintRuleDetails(this.className, this.id); | ||
|
||
@override | ||
String toString() => '$className:$id'; | ||
} | ||
|
||
class _LintVisitor extends RecursiveAstVisitor { | ||
final List<ClassElement> classElements = <ClassElement>[]; | ||
|
||
@override | ||
void visitClassDeclaration(ClassDeclaration node) { | ||
final classElement = node.declaredElement; | ||
if (classElement == null) { | ||
return; | ||
} | ||
|
||
for (var superType in classElement.allSupertypes) { | ||
if (superType.name == _LINT_RULE_CLASS_NAME) { | ||
classElements.add(classElement); | ||
pq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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,31 @@ | ||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:linter/src/analyzer.dart'; | ||
import 'package:linter/src/rules.dart'; | ||
import 'package:linter/src/util/lint_cache.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
final lintCache = LintCache(); | ||
|
||
group('check for incompatible rules:', () { | ||
registerLintRules(); | ||
for (var rule in Analyzer.facade.registeredRules) { | ||
test(rule.name, () async { | ||
await lintCache.init(); | ||
var lintDetail = lintCache.findDetailsById(rule.name); | ||
for (var incompatibleRule in lintDetail.incompatibleRules) { | ||
final ruleDetail = lintCache.findDetailsById(incompatibleRule); | ||
expect(ruleDetail, isNotNull, | ||
reason: | ||
'No rule found for id: $incompatibleRule (check for typo?)'); | ||
expect(ruleDetail.incompatibleRules, contains(lintDetail.id), | ||
reason: | ||
'${ruleDetail.id} should declare ${lintDetail.id} as `@IncompatibleWith` but does not.'); | ||
} | ||
}); | ||
} | ||
}); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, we could analyze and visit the constructor invocations in
lib/src/rules.dart
. That would probably find all of the lint rule class element faster (although it would miss classes that aren't registered).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's discuss refining this together. I have a few other ideas but a whiteboard would make them easier.
I'll land this as a jumping off point.