-
Notifications
You must be signed in to change notification settings - Fork 387
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
Linter for drift #3281
Open
dickermoshe
wants to merge
17
commits into
develop
Choose a base branch
from
drift_lint
base: develop
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.
+1,296
−32
Open
Linter for drift #3281
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9ccc51f
chore: update deps
dickermoshe c439a9e
feat: add drift_lint to docs project
dickermoshe e3bc59b
temp
dickermoshe 54e204c
Implement drift backend
simolus3 178e0fc
temp
dickermoshe 569b6cd
temp2
dickermoshe 0ebc163
fix lint
dickermoshe 3a97943
add linter
dickermoshe 343cb10
fix lint
dickermoshe 228c45a
add linter docs
dickermoshe 3cc6cc6
Update drift_dev/test/lint/test_pkg/pubspec.yaml
dickermoshe 45b7499
Merge branch 'drift_lint' of https://github.com/simolus3/drift into d…
dickermoshe 32d476b
Revert "temp"
dickermoshe 536d139
clean up
dickermoshe 11a1c3f
bump deps
dickermoshe 1db8609
update build
dickermoshe 030677b
fix
dickermoshe 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
add linter
commit 3a97943c5a05367f05cea152617005c781483e28
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -37,3 +37,6 @@ docs/docs/*.wasm | |
docs/docs/*.css | ||
docs/docs/examples/** | ||
docs/web/robots.txt | ||
|
||
# Linting | ||
custom_lint.log |
This file contains 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,7 +1,14 @@ | ||
include: package:lints/recommended.yaml | ||
|
||
analyzer: | ||
plugins: | ||
- custom_lint | ||
language: | ||
strict-casts: true | ||
exclude: | ||
- "**/*.g.dart" | ||
|
||
custom_lint: | ||
debug: true | ||
# Optional, will cause custom_lint to log its internal debug information | ||
verbose: true |
This file contains 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 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,7 @@ | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
import 'package:drift_dev/src/lints/custom_lint_plugin.dart'; | ||
|
||
/// This function is automaticly recognized by custom_lint to include this drift_dev package as a linter | ||
PluginBase createPlugin() { | ||
return DriftLinter(); | ||
} |
This file contains 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 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 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 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,18 @@ | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
import 'package:drift_dev/src/lints/drift_backend_error_lint.dart'; | ||
import 'package:drift_dev/src/lints/non_null_insert_with_ignore_lint.dart'; | ||
import 'package:drift_dev/src/lints/offset_without_limit_lint.dart'; | ||
import 'package:drift_dev/src/lints/unawaited_futures_in_transaction_lint.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
@internal | ||
class DriftLinter extends PluginBase { | ||
@override | ||
List<LintRule> getLintRules(CustomLintConfigs configs) => [ | ||
unawaitedFuturesInMigration, | ||
unawaitedFuturesInTransaction, | ||
OffsetWithoutLimit(), | ||
DriftBuildErrors(), | ||
NonNullInsertWithIgnore() | ||
]; | ||
} |
This file contains 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,113 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:analyzer/dart/analysis/results.dart'; | ||
import 'package:analyzer/dart/analysis/session.dart'; | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:analyzer/error/error.dart' hide LintCode; | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
import 'package:drift_dev/src/analysis/backend.dart'; | ||
import 'package:drift_dev/src/analysis/driver/error.dart'; | ||
import 'package:drift_dev/src/analysis/options.dart'; | ||
import 'package:logging/logging.dart'; | ||
|
||
import '../analysis/driver/driver.dart'; | ||
|
||
final columnBuilderChecker = | ||
TypeChecker.fromName('DriftDatabase', packageName: 'drift'); | ||
|
||
class DriftBuildErrors extends DartLintRule { | ||
DriftBuildErrors() : super(code: _errorCode); | ||
|
||
static const _errorCode = LintCode( | ||
name: 'drift_build_errors', | ||
problemMessage: '{0}', | ||
errorSeverity: ErrorSeverity.ERROR, | ||
); | ||
LintCode get _warningCode => LintCode( | ||
name: _errorCode.name, | ||
problemMessage: _errorCode.problemMessage, | ||
errorSeverity: ErrorSeverity.WARNING); | ||
|
||
@override | ||
void run(CustomLintResolver resolver, ErrorReporter reporter, | ||
CustomLintContext context) async { | ||
final unit = await resolver.getResolvedUnitResult(); | ||
final backend = CustomLintBackend(unit.session); | ||
final driver = DriftAnalysisDriver(backend, const DriftOptions.defaults()); | ||
|
||
final file = await driver.fullyAnalyze(unit.uri); | ||
for (final error in file.allErrors) { | ||
if (error.span case final span?) { | ||
// ignore: deprecated_member_use | ||
reporter.reportErrorForSpan( | ||
error.level == DriftAnalysisErrorLevel.warning | ||
? _warningCode | ||
: _errorCode, | ||
span, | ||
[error.message.trim()]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
class CustomLintBackend extends DriftBackend { | ||
@override | ||
final Logger log = Logger('drift_dev.CustomLintBackend'); | ||
final AnalysisSession session; | ||
|
||
CustomLintBackend(this.session); | ||
|
||
@override | ||
bool get canReadDart => true; | ||
|
||
@override | ||
Future<AstNode?> loadElementDeclaration(Element element) async { | ||
final library = element.library; | ||
if (library == null) return null; | ||
|
||
final info = await library.session.getResolvedLibraryByElement(library); | ||
if (info is ResolvedLibraryResult) { | ||
return info.getElementDeclaration(element)?.node; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@override | ||
Future<String> readAsString(Uri uri) async { | ||
final file = session.getFile(uri.path); | ||
|
||
if (file is FileResult) { | ||
return file.content; | ||
} | ||
|
||
throw FileSystemException('Not a file result: $file'); | ||
} | ||
|
||
@override | ||
Future<LibraryElement> readDart(Uri uri) async { | ||
final result = await session.getLibraryByUri(uri.toString()); | ||
if (result is LibraryElementResult) { | ||
return result.element; | ||
} | ||
|
||
throw NotALibraryException(uri); | ||
} | ||
|
||
@override | ||
Future<Expression> resolveExpression( | ||
Uri context, String dartExpression, Iterable<String> imports) { | ||
throw CannotReadExpressionException('Not supported at the moment'); | ||
} | ||
|
||
@override | ||
Future<Element?> resolveTopLevelElement( | ||
Uri context, String reference, Iterable<Uri> imports) { | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
Uri resolveUri(Uri base, String uriString) => base.resolve(uriString); | ||
} |
60 changes: 60 additions & 0 deletions
60
drift_dev/lib/src/lints/non_null_insert_with_ignore_lint.dart
This file contains 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,60 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/error/error.dart' hide LintCode; | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
final managerTypeChecker = | ||
TypeChecker.fromName('BaseTableManager', packageName: 'drift'); | ||
final insertStatementChecker = | ||
TypeChecker.fromName('InsertStatement', packageName: 'drift'); | ||
final insertOrIgnoreChecker = | ||
TypeChecker.fromName('InsertMode', packageName: 'drift'); | ||
|
||
class NonNullInsertWithIgnore extends DartLintRule { | ||
NonNullInsertWithIgnore() : super(code: _code); | ||
|
||
static const _code = LintCode( | ||
name: 'non_null_insert_with_ignore', | ||
problemMessage: | ||
'`insertReturning` and `createReturning` will throw an exception if a row isn\'t actually inserted. Use `createReturningOrNull` or `insertReturningOrNull` if you want to ignore conflicts.', | ||
errorSeverity: ErrorSeverity.WARNING, | ||
); | ||
|
||
@override | ||
void run(CustomLintResolver resolver, ErrorReporter reporter, | ||
CustomLintContext context) async { | ||
context.registry.addMethodInvocation( | ||
(node) { | ||
if (node.argumentList.arguments.isEmpty) return; | ||
switch (node.function) { | ||
case SimpleIdentifier func: | ||
if (func.name == "insertReturning" || | ||
func.name == "createReturning") { | ||
switch (func.parent) { | ||
case MethodInvocation func: | ||
final targetType = func.realTarget?.staticType; | ||
if (targetType != null) { | ||
if (managerTypeChecker.isSuperTypeOf(targetType) || | ||
insertStatementChecker.isExactlyType(targetType)) { | ||
final namedArgs = func.argumentList.arguments | ||
.whereType<NamedExpression>(); | ||
for (final arg in namedArgs) { | ||
if (arg.name.label.name == "mode") { | ||
switch (arg.expression) { | ||
case PrefixedIdentifier mode: | ||
if (mode.identifier.name == "insertOrIgnore") { | ||
print("Found insertOrIgnore"); | ||
reporter.atNode(node, _code); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
); | ||
} | ||
} |
This file contains 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,51 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:analyzer/error/error.dart' hide LintCode; | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
final managerTypeChecker = | ||
TypeChecker.fromName('BaseTableManager', packageName: 'drift'); | ||
|
||
class OffsetWithoutLimit extends DartLintRule { | ||
OffsetWithoutLimit() : super(code: _code); | ||
|
||
static const _code = LintCode( | ||
name: 'offset_without_limit', | ||
problemMessage: 'Using offset without a limit doesnt have any effect.', | ||
errorSeverity: ErrorSeverity.ERROR, | ||
); | ||
|
||
@override | ||
void run(CustomLintResolver resolver, ErrorReporter reporter, | ||
CustomLintContext context) async { | ||
context.registry.addMethodInvocation( | ||
(node) { | ||
if (node.argumentList.arguments.isEmpty) return; | ||
final func = _typeCheck<SimpleIdentifier>(node.function); | ||
|
||
if (func?.name == "get" || func?.name == "watch") { | ||
final target = _typeCheck<PrefixedIdentifier>(node.target); | ||
final managerGetter = | ||
_typeCheck<PropertyAccessorElement>(target?.staticElement); | ||
if (managerGetter != null) { | ||
if (managerTypeChecker.isSuperTypeOf(managerGetter.returnType)) { | ||
final namedArgs = | ||
node.argumentList.arguments.whereType<NamedExpression>(); | ||
if (namedArgs | ||
.every((element) => element.name.label.name != "limit") && | ||
namedArgs | ||
.any((element) => element.name.label.name == "offset")) { | ||
reporter.atNode(node, _code); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
); | ||
} | ||
} | ||
|
||
T? _typeCheck<T>(i) { | ||
return i is T ? i : null; | ||
} |
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.
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.
Not a huge fan of this, is there a way to tell
custom_lint
to use a different import for a package providing linters (my preferred library would belib/integrations/custom_lint.dart
).But if that's not possible so be it, I can file an issue on
custom_lint
and we can leave this as-is until that gets resolved.