-
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
temp
commit 178e0fca711b01ca5aea93974f74b9d540a15055
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 |
---|---|---|
@@ -1,18 +1,7 @@ | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
import 'package:drift_dev/src/lints/drift_errors.dart'; | ||
import 'package:drift_dev/src/lints/unawaited_futures_in_transaction.dart'; | ||
|
||
import 'src/lints/column_builder_on_table.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(); | ||
} | ||
|
||
class _DriftLinter extends PluginBase { | ||
@override | ||
List<LintRule> getLintRules(CustomLintConfigs configs) => [ | ||
ColumnBuilderOnTable(), | ||
UnawaitedFuturesInTransaction(), | ||
DriftBuildErrors() | ||
]; | ||
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 was deleted.
Oops, something went wrong.
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,14 @@ | ||
import 'package:custom_lint_builder/custom_lint_builder.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(), | ||
]; | ||
} |
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,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; | ||
} |
70 changes: 0 additions & 70 deletions
70
drift_dev/lib/src/lints/unawaited_futures_in_transaction.dart
This file was deleted.
Oops, something went wrong.
196 changes: 196 additions & 0 deletions
196
drift_dev/lib/src/lints/unawaited_futures_in_transaction_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,196 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:analyzer/dart/element/type.dart'; | ||
import 'package:analyzer/error/error.dart' hide LintCode; | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
/// A lint rule that reports unawaited futures if an additional check returns true. | ||
class _GenericUnawaitedFutureRule extends DartLintRule { | ||
_GenericUnawaitedFutureRule( | ||
{required super.code, required this.additionalCheck}); | ||
|
||
/// An unwaited future will only be reported if the [additionalCheck] returns true. | ||
final bool Function(AstNode) additionalCheck; | ||
|
||
@override | ||
void run(CustomLintResolver resolver, ErrorReporter reporter, | ||
CustomLintContext context) { | ||
context.registry.addExpressionStatement((node) { | ||
node.accept( | ||
_Visitor(this, reporter, code, additionalCheck: additionalCheck)); | ||
}); | ||
context.registry.addCascadeExpression((node) { | ||
node.accept( | ||
_Visitor(this, reporter, code, additionalCheck: additionalCheck)); | ||
}); | ||
context.registry.addInterpolationExpression((node) { | ||
node.accept( | ||
_Visitor(this, reporter, code, additionalCheck: additionalCheck)); | ||
}); | ||
} | ||
} | ||
|
||
final _databaseConnectionUserChecker = | ||
TypeChecker.fromName('DatabaseConnectionUser', packageName: 'drift'); | ||
final _migrationStrategyChecker = | ||
TypeChecker.fromName('MigrationStrategy', packageName: 'drift'); | ||
|
||
/// A lint which reports unawaited futures in a transaction. | ||
final unawaitedFuturesInTransaction = _GenericUnawaitedFutureRule( | ||
code: LintCode( | ||
name: 'unawaited_futures_in_transaction', | ||
problemMessage: | ||
'All futures in a transaction should be awaited to ensure that all operations are completed before the transaction is closed.', | ||
errorSeverity: ErrorSeverity.ERROR, | ||
), | ||
additionalCheck: (node) { | ||
return node.thisOrAncestorMatching( | ||
(method) { | ||
if (method is! MethodInvocation) return false; | ||
final methodElement = method.methodName.staticElement; | ||
if (methodElement is! MethodElement || | ||
methodElement.name != 'transaction') { | ||
return false; | ||
} | ||
// ignore: deprecated_member_use | ||
final enclosingElement = methodElement.enclosingElement; | ||
if (enclosingElement is! ClassElement || | ||
!_databaseConnectionUserChecker.isExactly(enclosingElement)) { | ||
return false; | ||
} | ||
return true; | ||
}, | ||
) != | ||
null; | ||
}); | ||
|
||
/// A lint which reports unawaited futures in a migration. | ||
final unawaitedFuturesInMigration = _GenericUnawaitedFutureRule( | ||
code: LintCode( | ||
name: 'unawaited_futures_in_migration', | ||
problemMessage: | ||
'All futures in a migrations should be awaited to ensure that all operations are completed before the other opperations are performed.', | ||
errorSeverity: ErrorSeverity.ERROR, | ||
), | ||
additionalCheck: (node) { | ||
return node.thisOrAncestorMatching((node) => | ||
(node is InstanceCreationExpression && | ||
node.staticType != null && | ||
_migrationStrategyChecker.isExactlyType(node.staticType!))) != | ||
null; | ||
}); | ||
|
||
// Additional code copied from the original unawaited_futures.dart lint | ||
// Copyright (c) 2016, 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. | ||
// Source: https://github.com/dart-lang/sdk/blob/main/pkg/linter/lib/src/rules/unawaited_futures.dart | ||
|
||
/// A visitor which will report any future that is not awaited. | ||
/// If an [additionalCheck] is provided, it will be used to check if the future should be reported | ||
class _Visitor extends SimpleAstVisitor<void> { | ||
final LintRule rule; | ||
final ErrorReporter reporter; | ||
final LintCode code; | ||
final bool Function(AstNode node) additionalCheck; | ||
|
||
_Visitor(this.rule, this.reporter, this.code, | ||
{required this.additionalCheck}); | ||
|
||
@override | ||
void visitCascadeExpression(CascadeExpression node) { | ||
var sections = node.cascadeSections; | ||
for (var i = 0; i < sections.length; i++) { | ||
_visit(sections[i]); | ||
} | ||
} | ||
|
||
@override | ||
void visitExpressionStatement(ExpressionStatement node) { | ||
var expr = node.expression; | ||
if (expr is AssignmentExpression) return; | ||
|
||
var type = expr.staticType; | ||
if (type == null) { | ||
return; | ||
} | ||
if (type.implementsInterface('Future', 'dart.async')) { | ||
// Ignore a couple of special known cases. | ||
if (_isFutureDelayedInstanceCreationWithComputation(expr) || | ||
_isMapPutIfAbsentInvocation(expr)) { | ||
return; | ||
} | ||
|
||
if (_isEnclosedInAsyncFunctionBody(node) && additionalCheck(node)) { | ||
// Future expression statement that isn't awaited in an async function: | ||
// while this is legal, it's a very frequent sign of an error. | ||
|
||
reporter.atNode(node, code); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
void visitInterpolationExpression(InterpolationExpression node) { | ||
_visit(node.expression); | ||
} | ||
|
||
bool _isEnclosedInAsyncFunctionBody(AstNode node) { | ||
var enclosingFunctionBody = node.thisOrAncestorOfType<FunctionBody>(); | ||
return enclosingFunctionBody?.isAsynchronous ?? false; | ||
} | ||
|
||
/// Detects `Future.delayed(duration, [computation])` creations with a | ||
/// computation. | ||
bool _isFutureDelayedInstanceCreationWithComputation(Expression expr) => | ||
expr is InstanceCreationExpression && | ||
(expr.staticType?.isDartAsyncFuture ?? false) && | ||
expr.constructorName.name?.name == 'delayed' && | ||
expr.argumentList.arguments.length == 2; | ||
|
||
bool _isMapClass(Element? e) => | ||
e is ClassElement && e.name == 'Map' && e.library.name == 'dart.core'; | ||
|
||
/// Detects Map.putIfAbsent invocations. | ||
bool _isMapPutIfAbsentInvocation(Expression expr) => | ||
expr is MethodInvocation && | ||
expr.methodName.name == 'putIfAbsent' && | ||
// ignore: deprecated_member_use | ||
_isMapClass(expr.methodName.staticElement?.enclosingElement); | ||
|
||
void _visit(Expression expr) { | ||
if ((expr.staticType?.isDartAsyncFuture ?? false) && | ||
_isEnclosedInAsyncFunctionBody(expr) && | ||
expr is! AssignmentExpression && | ||
additionalCheck(expr)) { | ||
reporter.atNode(expr, code); | ||
} | ||
} | ||
} | ||
|
||
/// The above code snippet depends on some extensions which are copied from | ||
/// https://github.com/dart-lang/sdk/blob/main/pkg/linter/lib/src/extensions.dart | ||
/// The extensions are copied here below: | ||
extension on DartType? { | ||
bool implementsInterface(String interface, String library) { | ||
var self = this; | ||
if (self is! InterfaceType) { | ||
return false; | ||
} | ||
bool predicate(InterfaceType i) => i.isSameAs(interface, library); | ||
var element = self.element; | ||
return predicate(self) || | ||
!element.isSynthetic && element.allSupertypes.any(predicate); | ||
} | ||
|
||
/// Returns whether `this` is the same element as [interface], declared in | ||
/// [library]. | ||
bool isSameAs(String? interface, String? library) { | ||
var self = this; | ||
return self is InterfaceType && | ||
self.element.name == interface && | ||
self.element.library.name == library; | ||
} | ||
} |
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,20 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:test/test.dart'; | ||
import 'package:path/path.dart' as p; | ||
|
||
void main() { | ||
// Test the linter_test.dart file | ||
test('linter', () async { | ||
final workingDir = p.join(p.current, 'test/lint/test_pkg'); | ||
expect( | ||
await Process.run('dart', ['pub', 'get'], workingDirectory: workingDir) | ||
.then((v) => v.exitCode), | ||
0); | ||
expect( | ||
await Process.run('custom_lint', ['--fatal-infos', '--fatal-warnings'], | ||
workingDirectory: workingDir) | ||
.then((v) => v.exitCode), | ||
0); | ||
}); | ||
} |
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,11 @@ | ||
include: package:lints/recommended.yaml | ||
|
||
analyzer: | ||
plugins: | ||
- custom_lint | ||
language: | ||
strict-casts: true | ||
|
||
custom_lint: | ||
debug: true | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'package:drift/drift.dart'; | ||
|
||
part 'db.g.dart'; | ||
|
||
class Users extends Table { | ||
late final id = integer().autoIncrement()(); | ||
late final name = text()(); | ||
// expect_lint: drift_build_errors | ||
late final age = integer(); | ||
} | ||
|
||
class BrokenTable extends Table { | ||
// expect_lint: drift_build_errors | ||
IntColumn get unknownRef => integer().customConstraint('CHECK foo > 10')(); | ||
} | ||
|
||
@DriftDatabase(tables: [Users]) | ||
class TestDatabase extends _$TestDatabase { | ||
TestDatabase(super.e); | ||
|
||
@override | ||
int get schemaVersion => 1; | ||
|
||
a() async { | ||
transaction( | ||
() async { | ||
// expect_lint: unawaited_futures_in_transaction | ||
into(users).insert(UsersCompanion.insert(name: 'name')); | ||
}, | ||
); | ||
} | ||
} |
Oops, something went wrong.
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,24 @@ | ||
name: test_pkg | ||
description: A starting point for Dart libraries or applications. | ||
version: 1.0.0 | ||
dickermoshe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
environment: | ||
sdk: ^3.5.3 | ||
|
||
dependencies: | ||
drift: | ||
build_runner: ^2.4.13 | ||
|
||
dev_dependencies: | ||
lints: ^4.0.0 | ||
test: ^1.24.0 | ||
drift_dev: | ||
custom_lint: ^0.6.7 | ||
|
||
dependency_overrides: | ||
drift: | ||
path: ../../../../drift | ||
drift_dev: | ||
path: ../../../../drift_dev | ||
sqlparser: | ||
path: ../../../../sqlparser |
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
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.