Skip to content

Commit 2fd4c94

Browse files
author
Ivan Dlugos
committed
add some useful effective-dart linter rules
1 parent 8f97aec commit 2fd4c94

36 files changed

+332
-344
lines changed

objectbox/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Expose `PutMode` - allowing semantics choice between put, update and insert.
1515
* Hide internal classes not intended for general use (e.g. all Model* classes).
1616
* Rename `versionLib()` to `nativeLibraryVersion()`.
17+
* Change `TxMode` enum values to lowercase.
1718
* Remove `flags` from the `Property()` annotation.
1819

1920
## 0.10.0 (2020-12-01)

objectbox/analysis_options.yaml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
include: package:pedantic/analysis_options.yaml
2+
# Effective dart has some things we don't use so let's stick with pedantic for now
3+
# include: package:effective_dart/analysis_options.yaml
24

35
linter:
6+
# Some additional rules from pub.dev analyzer and package:effective_dart
47
rules:
5-
# additional rules used by pub.dev
6-
non_constant_identifier_names: true
8+
- non_constant_identifier_names
9+
- constant_identifier_names
10+
- public_member_api_docs
11+
- prefer_expression_function_bodies
12+
- prefer_typing_uninitialized_variables
13+
- use_function_type_syntax_for_parameters
14+
- directives_ordering
15+
- avoid_positional_boolean_parameters
16+
- avoid_returning_this
17+
- unnecessary_brace_in_string_interps
18+
- unnecessary_lambdas
19+
- prefer_relative_imports
20+
- type_annotate_public_apis
21+
- prefer_initializing_formals
22+
# - lines_longer_than_80_chars
723

824
# exclude standalone packages:
925
analyzer:
1026
exclude:
1127
- example/**
1228
- generator/**
1329
- benchmark/**
30+
- lib/src/bindings/objectbox-c.dart
31+
- lib/flatbuffers/** # Not really our code
1432
- test/objectbox.g.dart # TODO remove exception after #168 is implemented
1533
strong-mode:
1634
implicit-casts: false

objectbox/lib/integration_test.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
library integration_test;
22

3-
import 'package:objectbox/internal.dart';
4-
import './src/bindings/helpers.dart';
53
import './src/bindings/bindings.dart';
4+
import './src/bindings/helpers.dart';
5+
import 'internal.dart';
6+
7+
// ignore_for_file: public_member_api_docs
68

79
// Todo: maybe make this a standalone package
810

911
/// Implements simple integration tests for platform compatibility.
1012
/// It's functions are designed to be callable from flutter apps - to test on the target platform.
1113
class IntegrationTest {
12-
static const int64_max = 9223372036854775807;
14+
static const int64Max = 9223372036854775807;
1315

1416
static void int64() {
15-
assert('9223372036854775807' == '$int64_max');
17+
assert('9223372036854775807' == '$int64Max');
1618
}
1719

1820
static void model() {
1921
// create a model with a single entity and a single property
2022
final modelInfo = ModelInfo();
2123
final property =
22-
ModelProperty(IdUid(1, int64_max - 1), 'id', OBXPropertyType.Long);
23-
final entity = ModelEntity(IdUid(1, int64_max), 'entity', modelInfo);
24+
ModelProperty(IdUid(1, int64Max - 1), 'id', OBXPropertyType.Long);
25+
final entity = ModelEntity(IdUid(1, int64Max), 'entity', modelInfo);
2426
property.entity = entity;
2527
entity.properties.add(property);
2628
entity.lastPropertyId = property.id;

objectbox/lib/internal.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
/// This library serves as an entrypoint for generated code and objectbox tools.
22
/// Don't import into your own code, use 'objectbox.dart' instead.
3+
library objectbox_internal;
4+
35
export 'src/model.dart';
46
export 'src/modelinfo/index.dart';
57
export 'src/query/query.dart';
6-
export 'src/relations/to_one.dart';
8+
export 'src/relations/info.dart';
79
export 'src/relations/to_many.dart';
10+
export 'src/relations/to_one.dart';

objectbox/lib/objectbox.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ library objectbox;
88
export 'src/annotations.dart';
99
export 'src/box.dart' show Box;
1010
export 'src/common.dart';
11+
export 'src/observable.dart';
1112
export 'src/query/query.dart'
1213
show
1314
Query,
@@ -18,10 +19,8 @@ export 'src/query/query.dart'
1819
IntegerPropertyQuery,
1920
DoublePropertyQuery,
2021
StringPropertyQuery;
21-
export 'src/relations/info.dart';
22-
export 'src/relations/to_one.dart' show ToOne;
2322
export 'src/relations/to_many.dart' show ToMany;
23+
export 'src/relations/to_one.dart' show ToOne;
2424
export 'src/store.dart' show Store;
2525
export 'src/sync.dart';
2626
export 'src/transaction.dart' show TxMode;
27-
export 'src/observable.dart';

objectbox/lib/src/bindings/bindings.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import 'objectbox-c.dart';
66
// let files importing bindings.dart also get all the OBX_* types
77
export 'objectbox-c.dart';
88

9+
// ignore_for_file: public_member_api_docs
10+
911
ObjectBoxC loadObjectBoxLib() {
1012
DynamicLibrary /*?*/ lib;
1113
var libName = 'objectbox';
@@ -24,9 +26,10 @@ ObjectBoxC loadObjectBoxLib() {
2426
lib = DynamicLibrary.open('/usr/local/lib/' + libName);
2527
}
2628
} else if (Platform.isIOS) {
27-
// this works in combination with `'OTHER_LDFLAGS' => '-framework ObjectBox'` in objectbox_flutter_libs.podspec
29+
// this works in combination with 'OTHER_LDFLAGS' => '-framework ObjectBox'
30+
// in objectbox_flutter_libs.podspec
2831
lib = DynamicLibrary.process();
29-
// alternatively, if `DynamicLibrary.process()` wasn't faster (it should be though...)
32+
// alternatively, if `DynamicLibrary.process()` wasn't faster (it should be)
3033
// libName = 'ObjectBox.framework/ObjectBox';
3134
} else if (Platform.isAndroid) {
3235
libName = 'lib' + libName + '-jni.so';

objectbox/lib/src/bindings/data_visitor.dart

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
import 'dart:ffi';
2+
23
import 'package:ffi/ffi.dart' show allocate, free;
34

5+
import '../modelinfo/entity_definition.dart';
46
import '../store.dart';
57
import 'bindings.dart';
6-
import '../modelinfo/entity_definition.dart';
8+
9+
// ignore_for_file: public_member_api_docs
710

811
/// This file implements C call forwarding using a trampoline approach.
912
///
10-
/// When you want to pass a dart callback to a C function you cannot use lambdas and instead the callback must be
11-
/// a static function, otherwise `Pointer.fromFunction()` called with your function won't compile.
12-
/// Since static functions don't have any state, you must either rely on a global state or use a "userData" pointer
13-
/// pass-through functionality provided by a C function.
13+
/// When you want to pass a dart callback to a C function you cannot use lambdas
14+
/// and instead the callback must be a static function, otherwise
15+
/// [Pointer.fromFunction()] called with your function won't compile.
16+
/// Since static functions don't have any state, you must either rely on a
17+
/// global state or use a "userData" pointer pass-through functionality provided
18+
/// by a C function.
1419
///
15-
/// The DataVisitor class tries to alleviate the burden of managing this and instead allows using lambdas from
16-
/// user-code, internally mapping the C calls to the appropriate lambda.
20+
/// The DataVisitor class tries to alleviate the burden of managing this and
21+
/// instead allows using lambdas from user-code, internally mapping the C calls
22+
/// to the appropriate lambda.
1723
///
1824
/// Sample usage:
1925
/// final results = <T>[];
2026
/// final visitor = DataVisitor((Pointer<Uint8> dataPtr, int length) {
2127
/// final bytes = dataPtr.asTypedList(length);
2228
/// results.add(_fbManager.unmarshal(bytes));
23-
/// return true; // return value usually indicates to the C function whether it should continue.
29+
/// return true; // return value usually indicates whether to continue.
2430
/// });
2531
///
26-
/// final err = bindings.obx_query_visit(_cQuery, visitor.fn, visitor.userData, offset, limit);
27-
/// visitor.close(); // make sure to close the visitor, unregistering the callback it from the forwarder
32+
/// final err = bindings.obx_query_visit(_cQuery, visitor.fn,
33+
/// visitor.userData, offset, limit);
34+
/// visitor.close(); // make sure to close the visitor
2835
/// checkObx(err);
2936
3037
int _lastId = 0;
3138
final _callbacks = <int, bool Function(Pointer<Uint8> dataPtr, int length)>{};
3239

33-
// called from C, forwards calls to the actual callback registered at the given ID
40+
// Called from C, forwards to the actual callback registered at the given ID.
3441
int _forwarder(Pointer<Void> callbackId, Pointer<Void> dataPtr, int size) {
3542
if (callbackId == null || callbackId.address == 0) {
3643
throw Exception(

objectbox/lib/src/bindings/flatbuffers.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import 'dart:ffi';
2+
import 'dart:io' show Platform;
13
import 'dart:typed_data';
24

35
import 'package:ffi/ffi.dart' as f;
4-
import 'dart:ffi';
5-
import 'dart:io' show Platform;
66

77
import '../../flatbuffers/flat_buffers.dart' as fb;
88

9+
// ignore_for_file: public_member_api_docs
10+
911
class BuilderWithCBuffer {
1012
final _allocator = _Allocator();
1113
final int _initialSize;
@@ -40,7 +42,7 @@ class BuilderWithCBuffer {
4042
} else {
4143
_allocator._allocs.keys
4244
.toList(growable: false)
43-
.forEach((data) => _allocator.deallocate(data));
45+
.forEach(_allocator.deallocate);
4446
}
4547
}
4648
}

objectbox/lib/src/bindings/helpers.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import 'dart:typed_data';
33

44
import 'package:ffi/ffi.dart';
55

6-
import '../store.dart';
7-
import 'bindings.dart';
86
import '../annotations.dart';
97
import '../common.dart';
108
import '../modelinfo/entity_definition.dart';
9+
import '../store.dart';
10+
import 'bindings.dart';
11+
12+
// ignore_for_file: public_member_api_docs
1113

1214
void checkObx(int code) {
1315
if (code != OBX_SUCCESS) {
@@ -86,7 +88,7 @@ String obxPropertyTypeToString(int type) {
8688
return 'stringVector';
8789
}
8890

89-
throw Exception('Invalid OBXPropertyType: ${type}');
91+
throw Exception('Invalid OBXPropertyType: $type');
9092
}
9193

9294
int propertyTypeToOBXPropertyType(PropertyType type) {
@@ -108,7 +110,7 @@ int propertyTypeToOBXPropertyType(PropertyType type) {
108110
case PropertyType.byteVector:
109111
return OBXPropertyType.ByteVector;
110112
}
111-
throw Exception('Invalid PropertyType: ${type}');
113+
throw Exception('Invalid PropertyType: $type');
112114
}
113115

114116
class CursorHelper<T> {
@@ -124,7 +126,8 @@ class CursorHelper<T> {
124126

125127
bool _closed = false;
126128

127-
CursorHelper(this._store, Pointer<OBX_txn> txn, this._entity, bool isWrite)
129+
CursorHelper(this._store, Pointer<OBX_txn> txn, this._entity,
130+
{/*required*/ bool isWrite})
128131
: ptr = checkObxPtr(
129132
C.cursor(txn, _entity.model.id.id), 'failed to create cursor') {
130133
if (!isWrite) {

objectbox/lib/src/bindings/structs.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import 'dart:ffi';
22
import 'dart:typed_data' show Uint8List;
3+
34
import 'package:ffi/ffi.dart' show allocate, free, Utf8;
5+
46
import '../common.dart';
57
import 'bindings.dart';
68

7-
// Disable some linter rules for this file
9+
// ignore_for_file: public_member_api_docs
810
// ignore_for_file: camel_case_types
911

1012
/// Execute the given function, managing the resources consistently
@@ -48,15 +50,17 @@ class OBX_bytes_wrapper {
4850
Pointer<Void> get ptr => _cBytes.ref.data;
4951

5052
/// Returns a pointer to OBX_bytes with copy of the passed data.
51-
/// Warning: this creates two unmanaged pointers which must be freed manually: OBX_bytes.freeManaged(result).
53+
/// Warning: this creates two unmanaged pointers which must be freed manually:
54+
/// [freeManaged()].
5255
/// ObjectBox requires object data to be aligned to the length of 4.
5356
OBX_bytes_wrapper.managedCopyOf(Uint8List data, {/*required*/ bool align})
5457
: _cBytes = allocate<OBX_bytes>() {
5558
final bytes = _cBytes.ref;
5659

5760
bytes.size = align ? ((data.length + 3.0) ~/ 4.0) * 4 : data.length;
5861

59-
// NOTE: currently there's no way to get access to the underlying memory of Uint8List to avoid a copy.
62+
// NOTE: currently there's no way to get access to the underlying memory of
63+
// Uint8List to avoid a copy.
6064
// See https://github.com/dart-lang/ffi/issues/27
6165
// if (data.length == bytes.length) {
6266
// bytes._dataPtr = data.some-way-to-get-the-underlying-memory-pointer

0 commit comments

Comments
 (0)