Dart lints (#6808)

* Dart - add analysis options with the recommended lints

* Dart - generate code without linter issues

* Dart - remove linter issues in example and test code

* Dart - remove linter issues in lib code

* Dart - update generate code in /tests/
This commit is contained in:
Ivan Dlugos
2021-08-30 21:54:59 +02:00
committed by GitHub
parent 4b9123baff
commit e3c76a5cde
20 changed files with 560 additions and 581 deletions

View File

@@ -22,7 +22,7 @@ const int _sizeofFloat64 = 8;
///
/// This callback is used by other struct's `finish` methods to write the nested
/// struct's fields inline.
typedef void StructBuilder();
typedef StructBuilder = void Function();
/// Buffer with data and some context about it.
class BufferContext {
@@ -143,7 +143,7 @@ class Builder {
/// true, will cause [writeString] to pool strings in the buffer so that
/// identical strings will always use the same offset in tables.
Builder({
this.initialSize: 1024,
this.initialSize = 1024,
bool internStrings = false,
Allocator allocator = const DefaultAllocator(),
this.deduplicateTables = true,
@@ -155,8 +155,7 @@ class Builder {
}
}
/// Calculate the finished buffer size (aligned).
@pragma('vm:prefer-inline')
/// Calculate the finished buffer size (aligned).@pragma('vm:prefer-inline')
int size() => _tail + ((-_tail) & (_maxAlign - 1));
/// Add the [field] with the given boolean [value]. The field is not added if
@@ -750,7 +749,9 @@ class Builder {
/// Zero-pads the buffer, which may be required for some struct layouts.
@pragma('vm:prefer-inline')
void pad(int howManyBytes) {
for (int i = 0; i < howManyBytes; i++) putUint8(0);
for (int i = 0; i < howManyBytes; i++) {
putUint8(0);
}
}
/// Prepare for writing the given `count` of scalars of the given `size`.
@@ -875,7 +876,7 @@ class Float64ListReader extends Reader<List<double>> {
@override
@pragma('vm:prefer-inline')
List<double> read(BufferContext bc, int offset) =>
new _FbFloat64List(bc, bc.derefObject(offset));
_FbFloat64List(bc, bc.derefObject(offset));
}
class Float32ListReader extends Reader<List<double>> {
@@ -888,7 +889,7 @@ class Float32ListReader extends Reader<List<double>> {
@override
@pragma('vm:prefer-inline')
List<double> read(BufferContext bc, int offset) =>
new _FbFloat32List(bc, bc.derefObject(offset));
_FbFloat32List(bc, bc.derefObject(offset));
}
class Float64Reader extends Reader<double> {
@@ -1034,6 +1035,7 @@ abstract class Reader<T> {
/// The reader of string values.
class StringReader extends Reader<String> {
final bool asciiOptimization;
const StringReader({this.asciiOptimization = false}) : super();
@override
@@ -1071,8 +1073,9 @@ abstract class StructReader<T> extends Reader<T> {
/// Return the object at `offset`.
T createObject(BufferContext bc, int offset);
T read(BufferContext bp, int offset) {
return createObject(bp, offset);
@override
T read(BufferContext bc, int offset) {
return createObject(bc, offset);
}
}
@@ -1088,9 +1091,9 @@ abstract class TableReader<T> extends Reader<T> {
T createObject(BufferContext bc, int offset);
@override
T read(BufferContext bp, int offset) {
int objectOffset = bp.derefObject(offset);
return createObject(bp, objectOffset);
T read(BufferContext bc, int offset) {
int objectOffset = bc.derefObject(offset);
return createObject(bc, objectOffset);
}
}
@@ -1249,12 +1252,11 @@ abstract class _FbList<E> extends Object with ListMixin<E> implements List<E> {
int get length => _length ??= bc._getUint32(offset);
@override
void set length(int i) =>
throw new StateError('Attempt to modify immutable list');
set length(int i) => throw StateError('Attempt to modify immutable list');
@override
void operator []=(int i, E e) =>
throw new StateError('Attempt to modify immutable list');
throw StateError('Attempt to modify immutable list');
}
/// List backed by 32-bit unsigned integers.
@@ -1416,7 +1418,7 @@ class DefaultAllocator extends Allocator {
ByteData allocate(int size) => ByteData(size);
@override
void deallocate(ByteData _) {
void deallocate(ByteData data) {
// nothing to do, it's garbage-collected
}
}

View File

@@ -5,22 +5,22 @@ import 'types.dart';
/// The main builder class for creation of a FlexBuffer.
class Builder {
ByteData _buffer;
final ByteData _buffer;
List<_StackValue> _stack = [];
List<_StackPointer> _stackPointers = [];
int _offset = 0;
bool _finished = false;
Map<String, _StackValue> _stringCache = {};
Map<String, _StackValue> _keyCache = {};
Map<_KeysHash, _StackValue> _keyVectorCache = {};
Map<int, _StackValue> _indirectIntCache = {};
Map<double, _StackValue> _indirectDoubleCache = {};
final Map<String, _StackValue> _stringCache = {};
final Map<String, _StackValue> _keyCache = {};
final Map<_KeysHash, _StackValue> _keyVectorCache = {};
final Map<int, _StackValue> _indirectIntCache = {};
final Map<double, _StackValue> _indirectDoubleCache = {};
/// Instantiate the builder if you intent to gradually build up the buffer by calling
/// add... methods and calling [finish] to receive the the resulting byte array.
///
/// The default size of internal buffer is set to 2048. Provide a different value in order to avoid buffer copies.
Builder({int size = 2048}) : _buffer = ByteData(size) {}
Builder({int size = 2048}) : _buffer = ByteData(size);
/// Use this method in order to turn an object into a FlexBuffer directly.
///
@@ -70,25 +70,25 @@ class Builder {
/// Specifically useful when building up a vector where values can be null.
void addNull() {
_integrityCheckOnValueAddition();
_stack.add(_StackValue.WithNull());
_stack.add(_StackValue.withNull());
}
/// Adds a string value.
void addInt(int value) {
_integrityCheckOnValueAddition();
_stack.add(_StackValue.WithInt(value));
_stack.add(_StackValue.withInt(value));
}
/// Adds a bool value.
void addBool(bool value) {
_integrityCheckOnValueAddition();
_stack.add(_StackValue.WithBool(value));
_stack.add(_StackValue.withBool(value));
}
/// Adds a double value.
void addDouble(double value) {
_integrityCheckOnValueAddition();
_stack.add(_StackValue.WithDouble(value));
_stack.add(_StackValue.withDouble(value));
}
/// Adds a string value.
@@ -108,7 +108,7 @@ class Builder {
_pushBuffer(utf8String);
_offset = newOffset;
final stackValue =
_StackValue.WithOffset(stringOffset, ValueType.String, bitWidth);
_StackValue.withOffset(stringOffset, ValueType.String, bitWidth);
_stack.add(stackValue);
_stringCache[value] = stackValue;
}
@@ -129,7 +129,7 @@ class Builder {
_pushBuffer(utf8String);
_offset = newOffset;
final stackValue =
_StackValue.WithOffset(keyOffset, ValueType.Key, BitWidth.width8);
_StackValue.withOffset(keyOffset, ValueType.Key, BitWidth.width8);
_stack.add(stackValue);
_keyCache[value] = stackValue;
}
@@ -148,7 +148,7 @@ class Builder {
_pushBuffer(value.asUint8List());
_offset = newOffset;
final stackValue =
_StackValue.WithOffset(blobOffset, ValueType.Blob, bitWidth);
_StackValue.withOffset(blobOffset, ValueType.Blob, bitWidth);
_stack.add(stackValue);
}
@@ -164,12 +164,12 @@ class Builder {
_stack.add(_indirectIntCache[value]!);
return;
}
final stackValue = _StackValue.WithInt(value);
final stackValue = _StackValue.withInt(value);
final byteWidth = _align(stackValue.width);
final newOffset = _newOffset(byteWidth);
final valueOffset = _offset;
_pushBuffer(stackValue.asU8List(stackValue.width));
final stackOffset = _StackValue.WithOffset(
final stackOffset = _StackValue.withOffset(
valueOffset, ValueType.IndirectInt, stackValue.width);
_stack.add(stackOffset);
_offset = newOffset;
@@ -189,12 +189,12 @@ class Builder {
_stack.add(_indirectDoubleCache[value]!);
return;
}
final stackValue = _StackValue.WithDouble(value);
final stackValue = _StackValue.withDouble(value);
final byteWidth = _align(stackValue.width);
final newOffset = _newOffset(byteWidth);
final valueOffset = _offset;
_pushBuffer(stackValue.asU8List(stackValue.width));
final stackOffset = _StackValue.WithOffset(
final stackOffset = _StackValue.withOffset(
valueOffset, ValueType.IndirectFloat, stackValue.width);
_stack.add(stackOffset);
_offset = newOffset;
@@ -346,14 +346,14 @@ class Builder {
}
}
if (keys != null) {
return _StackValue.WithOffset(vecOffset, ValueType.Map, bitWidth);
return _StackValue.withOffset(vecOffset, ValueType.Map, bitWidth);
}
if (typed) {
final vType =
ValueTypeUtils.toTypedVector(vectorType, fix ? vecLength : 0);
return _StackValue.WithOffset(vecOffset, vType, bitWidth);
return _StackValue.withOffset(vecOffset, vType, bitWidth);
}
return _StackValue.WithOffset(vecOffset, ValueType.Vector, bitWidth);
return _StackValue.withOffset(vecOffset, ValueType.Vector, bitWidth);
}
void _endVector(_StackPointer pointer) {
@@ -405,7 +405,7 @@ class Builder {
offsets.add(_stack[i].offset!);
}
final keysHash = _KeysHash(offsets);
var keysStackValue;
_StackValue? keysStackValue;
if (_keyVectorCache.containsKey(keysHash)) {
keysStackValue = _keyVectorCache[keysHash];
} else {
@@ -424,7 +424,7 @@ class Builder {
'Stack values are not keys $v1 | $v2. Check if you combined [addKey] with add... method calls properly.');
}
var c1, c2;
late int c1, c2;
var index = 0;
do {
c1 = _buffer.getUint8(v1.offset! + index);
@@ -520,32 +520,32 @@ class Builder {
class _StackValue {
late Object _value;
int? _offset;
ValueType _type;
BitWidth _width;
final ValueType _type;
final BitWidth _width;
_StackValue.WithNull()
_StackValue.withNull()
: _type = ValueType.Null,
_width = BitWidth.width8 {}
_width = BitWidth.width8;
_StackValue.WithInt(int value)
_StackValue.withInt(int value)
: _type = ValueType.Int,
_width = BitWidthUtil.width(value),
_value = value {}
_value = value;
_StackValue.WithBool(bool value)
_StackValue.withBool(bool value)
: _type = ValueType.Bool,
_width = BitWidth.width8,
_value = value {}
_value = value;
_StackValue.WithDouble(double value)
_StackValue.withDouble(double value)
: _type = ValueType.Float,
_width = BitWidthUtil.width(value),
_value = value {}
_value = value;
_StackValue.WithOffset(int value, ValueType type, BitWidth width)
_StackValue.withOffset(int value, ValueType type, BitWidth width)
: _offset = value,
_type = type,
_width = width {}
_width = width;
BitWidth storedWidth({BitWidth width = BitWidth.width8}) {
return ValueTypeUtils.isInline(_type)

View File

@@ -19,7 +19,7 @@ class Reference {
this._buffer, this._offset, this._parentWidth, int packedType, this._path,
[int? byteWidth, ValueType? valueType])
: _byteWidth = byteWidth ?? 1 << (packedType & 3),
_valueType = valueType ?? ValueTypeUtils.fromInt(packedType >> 2) {}
_valueType = valueType ?? ValueTypeUtils.fromInt(packedType >> 2);
/// Use this method to access the root value of a FlexBuffer.
static Reference fromBuffer(ByteBuffer buffer) {
@@ -218,13 +218,13 @@ class Reference {
_length = 0;
} else if (_valueType == ValueType.String) {
final indirect = _indirect;
var size_byte_width = _byteWidth;
var size = _readUInt(indirect - size_byte_width,
BitWidthUtil.fromByteWidth(size_byte_width));
var sizeByteWidth = _byteWidth;
var size = _readUInt(indirect - sizeByteWidth,
BitWidthUtil.fromByteWidth(sizeByteWidth));
while (_buffer.getInt8(indirect + size) != 0) {
size_byte_width <<= 1;
size = _readUInt(indirect - size_byte_width,
BitWidthUtil.fromByteWidth(size_byte_width));
sizeByteWidth <<= 1;
size = _readUInt(indirect - sizeByteWidth,
BitWidthUtil.fromByteWidth(sizeByteWidth));
}
_length = size;
} else if (_valueType == ValueType.Key) {
@@ -374,8 +374,8 @@ class Reference {
}
int _diffKeys(
List<int> input, int index, int indirect_offset, int byteWidth) {
final keyOffset = indirect_offset + index * byteWidth;
List<int> input, int index, int indirectOffset, int byteWidth) {
final keyOffset = indirectOffset + index * byteWidth;
final keyIndirectOffset =
keyOffset - _readUInt(keyOffset, BitWidthUtil.fromByteWidth(byteWidth));
for (var i = 0; i < input.length; i++) {

View File

@@ -47,7 +47,7 @@ class BitWidthUtil {
if (value == 8) {
return BitWidth.width64;
}
throw Exception('Unexpected value ${value}');
throw Exception('Unexpected value $value');
}
static int paddingSize(int bufSize, int scalarSize) {