Skip to content

Commit

Permalink
add limit offset
Browse files Browse the repository at this point in the history
  • Loading branch information
dickermoshe committed Oct 14, 2024
1 parent cc7a197 commit f2da76c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
8 changes: 6 additions & 2 deletions drift_dev/lib/src/lints/custom_lint_plugin.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:drift_dev/src/lints/offset_without_limit.dart';
import 'package:drift_dev/src/lints/drift_backend_errors.dart';
import 'package:drift_dev/src/lints/unawaited_futures_in_transaction.dart';
import 'package:meta/meta.dart';

@internal
class DriftLinter extends PluginBase {
@override
List<LintRule> getLintRules(CustomLintConfigs configs) =>
[UnawaitedFuturesInTransaction(), DriftBuildErrors()];
List<LintRule> getLintRules(CustomLintConfigs configs) => [
UnawaitedFuturesInTransaction(),
DriftBuildErrors(),
OffsetWithoutLimit()
];
}
60 changes: 60 additions & 0 deletions drift_dev/lib/src/lints/offset_without_limit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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/options.dart';
import 'package:logging/logging.dart';

import '../analysis/driver/driver.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 will result in a ',
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;
}
21 changes: 21 additions & 0 deletions drift_dev/test/linter_test/pkg/lib/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Users extends Table {
late final name = text()();
// expect_lint: drift_build_errors
late final age = integer();
// ignore: drift_build_errors
late final age2 = integer()();
}

class BrokenTable extends Table {
Expand All @@ -22,6 +24,17 @@ class TestDatabase extends _$TestDatabase {
int get schemaVersion => 1;

a() async {
// expect_lint: offset_without_limit
managers.users.get(offset: 1);
// expect_lint: offset_without_limit
managers.users.watch(offset: 1);
managers.users.get();
managers.users.get(distinct: true);
managers.users.get(limit: 1);
managers.users.get(limit: 1, distinct: true);
managers.users.get(limit: 1, offset: 1);
managers.users.get(limit: 1, offset: 1, distinct: true);

transaction(
() async {
// expect_lint: unawaited_futures_in_transaction
Expand All @@ -30,3 +43,11 @@ class TestDatabase extends _$TestDatabase {
);
}
}

class A {
void get() {}
}

void a() {
A().get();
}
4 changes: 0 additions & 4 deletions drift_dev/test/linter_test/pkg/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
name: linter_test
description: A starting point for Dart libraries or applications.
version: 1.0.0
# repository: https://github.com/my_org/my_repo

environment:
sdk: ^3.5.3

# Add regular dependencies here.
dependencies:
drift:
build_runner: ^2.4.13
# path: ^1.8.0

dev_dependencies:
lints: ^4.0.0
Expand Down

0 comments on commit f2da76c

Please sign in to comment.