Skip to content

Commit f22f843

Browse files
authored
Fix lints in package:ffigen (#1328)
1 parent 2a0d097 commit f22f843

File tree

77 files changed

+691
-681
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+691
-681
lines changed

pkgs/ffigen/analysis_options.yaml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ analyzer:
1919
language:
2020
strict-casts: true
2121
strict-inference: true
22-
strict-raw-types: false #TODO(https://github.com/dart-lang/native/issues/1281): Fix.
2322

2423
linter:
2524
rules:
@@ -28,18 +27,3 @@ linter:
2827
prefer_final_locals: true
2928
prefer_final_in_for_each: true
3029
use_super_parameters: true
31-
always_declare_return_types: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix
32-
avoid_dynamic_calls: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix
33-
comment_references: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix
34-
constant_identifier_names: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix
35-
library_annotations: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix
36-
lines_longer_than_80_chars: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix
37-
omit_local_variable_types: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix.
38-
only_throw_errors: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix
39-
prefer_asserts_in_initializer_lists: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix
40-
prefer_const_constructors: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix.
41-
prefer_relative_imports: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix
42-
prefer_single_quotes: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix.
43-
type_annotate_public_apis: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix
44-
unnecessary_parenthesis: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix
45-
unreachable_from_main: false # TODO(https://github.com/dart-lang/native/issues/1281): Fix

pkgs/ffigen/example/libclang-example/custom_import.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import 'dart:ffi';
66

77
/// Represents a native unsigned pointer-sized integer in C.
88
///
9-
/// [UintPtr] is not constructible in the Dart code and serves purely as marker in
10-
/// type signatures.
9+
/// [UintPtr] is not constructible in the Dart code and serves purely as marker
10+
/// in type signatures.
1111
@AbiSpecificIntegerMapping({
1212
Abi.androidArm: Uint32(),
1313
Abi.androidArm64: Uint64(),
@@ -34,8 +34,8 @@ final class UintPtr extends AbiSpecificInteger {
3434

3535
/// `unsigned long` in C.
3636
///
37-
/// [UnsignedLong] is not constructible in the Dart code and serves purely as marker in
38-
/// type signatures.
37+
/// [UnsignedLong] is not constructible in the Dart code and serves purely as
38+
/// marker in type signatures.
3939
@AbiSpecificIntegerMapping({
4040
Abi.androidArm: Uint32(),
4141
Abi.androidArm64: Uint64(),

pkgs/ffigen/example/shared_bindings/generate.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void main() {
2626
final res = runFfigenForConfig(sdkPath, configPath);
2727
print(res.stdout.toString());
2828
if (res.exitCode != 0) {
29-
throw Exception("Some error occurred: ${res.stderr.toString()}");
29+
throw Exception('Some error occurred: ${res.stderr.toString()}');
3030
}
3131
}
3232
}

pkgs/ffigen/lib/src/code_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// Generates FFI bindings for a given [Library].
5+
/// Generates FFI bindings for a given Library.
66
library code_generator;
77

88
export 'code_generator/binding.dart';

pkgs/ffigen/lib/src/code_generator/binding.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract class Binding {
3535
/// Converts a Binding to its actual string representation.
3636
///
3737
/// Note: This does not print the typedef dependencies.
38-
/// Must call [getTypedefDependencies] first.
38+
/// Must call getTypedefDependencies first.
3939
BindingString toBindingString(Writer w);
4040

4141
/// Returns the Objective C bindings, if any.

pkgs/ffigen/lib/src/code_generator/compound.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:ffigen/src/code_generator.dart';
5+
import '../code_generator.dart';
66

77
import 'binding_string.dart';
88
import 'utils.dart';

pkgs/ffigen/lib/src/code_generator/enum_class.dart

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ class EnumClass extends BindingType {
8282
/// See [scanForDuplicates] and [writeUniqueMembers].
8383
final Set<EnumConstant> uniqueMembers = {};
8484

85-
/// Returns a string to declare the enum member and any documentation it may have had.
85+
/// Returns a string to declare the enum member and any documentation it may
86+
/// have had.
8687
String formatValue(EnumConstant ec) {
8788
final buffer = StringBuffer();
8889
final enumValueName = namer.makeUnique(ec.name);
@@ -100,8 +101,9 @@ class EnumClass extends BindingType {
100101
///
101102
/// Since all enum values in Dart are distinct, these duplicates do not
102103
/// get their own values in Dart. Rather, they are aliases of the original
103-
/// value. For example, if a native enum has 2 constants with a value of 10, only
104-
/// one enum value will be generated in Dart, and the other will be set equal to it.
104+
/// value. For example, if a native enum has 2 constants with a value of 10,
105+
/// only one enum value will be generated in Dart, and the other will be set
106+
/// equal to it.
105107
void scanForDuplicates() {
106108
uniqueMembers.clear();
107109
uniqueToDuplicates.clear();
@@ -126,13 +128,14 @@ class EnumClass extends BindingType {
126128
///
127129
/// Eg, C: `apple = 1`, Dart: `apple(1)`
128130
void writeUniqueMembers(StringBuffer s) {
129-
s.writeAll(uniqueMembers.map(formatValue), ",\n");
131+
s.writeAll(uniqueMembers.map(formatValue), ',\n');
130132
if (uniqueMembers.isNotEmpty) s.write(';\n');
131133
}
132134

133135
/// Writes alias declarations for all members with duplicate values.
134136
///
135-
/// Eg, C: `banana = 10, yellow_fruit = 10`. Dart: `static const yellow_fruit = banana`.
137+
/// Eg, C: `banana = 10, yellow_fruit = 10`.
138+
/// Dart: `static const yellow_fruit = banana`.
136139
void writeDuplicateMembers(StringBuffer s) {
137140
if (duplicateToOriginal.isEmpty) return;
138141
for (final entry in duplicateToOriginal.entries) {
@@ -143,48 +146,49 @@ class EnumClass extends BindingType {
143146
final originalName = enumNames[original]!;
144147
enumNames[duplicate] = duplicateName;
145148
if (duplicate.dartDoc != null) {
146-
s.write("$depth/// ");
147-
s.writeAll(duplicate.dartDoc!.split("\n"), "\n$depth/// ");
148-
s.write("\n");
149+
s.write('$depth/// ');
150+
s.writeAll(duplicate.dartDoc!.split('\n'), '\n$depth/// ');
151+
s.write('\n');
149152
}
150-
s.write("${depth}static const $duplicateName = $originalName;\n");
153+
s.write('${depth}static const $duplicateName = $originalName;\n');
151154
}
152155
}
153156

154157
/// Writes the constructor for the enum.
155158
///
156159
/// Always accepts an integer value to match the native value.
157160
void writeConstructor(StringBuffer s) {
158-
s.write("${depth}final int value;\n");
159-
s.write("${depth}const $name(this.value);\n");
161+
s.write('${depth}final int value;\n');
162+
s.write('${depth}const $name(this.value);\n');
160163
}
161164

162165
/// Overrides [Enum.toString] so all aliases are included, if any.
163166
///
164-
/// If a native enum has two members with the same value, they are functionally
165-
/// identical, and should be represented as such. This method overrides [toString]
166-
/// to include all duplicate members in the same message.
167+
/// If a native enum has two members with the same value, they are
168+
/// functionally identical, and should be represented as such. This method
169+
/// overrides [toString] to include all duplicate members in the same message.
167170
void writeToStringOverride(StringBuffer s) {
168171
if (duplicateToOriginal.isEmpty) return;
169-
s.write("$depth@override\n");
170-
s.write("${depth}String toString() {\n");
172+
s.write('$depth@override\n');
173+
s.write('${depth}String toString() {\n');
171174
for (final entry in uniqueToDuplicates.entries) {
172-
// [!] All enum values were given a name when their declarations were generated
175+
// [!] All enum values were given a name when their declarations were
176+
// generated.
173177
final unique = entry.key;
174178
final originalName = enumNames[unique]!;
175179
final duplicates = entry.value;
176180
if (duplicates.isEmpty) continue;
177181
final allDuplicates = [
178182
for (final duplicate in [unique] + duplicates)
179-
"$name.${enumNames[duplicate]!}",
180-
].join(", ");
183+
'$name.${enumNames[duplicate]!}',
184+
].join(', ');
181185
s.write(
182186
'$depth$depth'
183187
'if (this == $originalName) return "$allDuplicates";\n',
184188
);
185189
}
186-
s.write("${depth * 2}return super.toString();\n");
187-
s.write("$depth}");
190+
s.write('${depth * 2}return super.toString();\n');
191+
s.write('$depth}');
188192
}
189193

190194
/// Writes the DartDoc string for this enum.
@@ -194,23 +198,24 @@ class EnumClass extends BindingType {
194198
}
195199
}
196200

197-
/// Writes a sealed class when no members exist, because Dart enums cannot be empty.
201+
/// Writes a sealed class when no members exist, because Dart enums cannot be
202+
/// empty.
198203
void writeEmptyEnum(StringBuffer s) {
199-
s.write("sealed class $name { }\n");
204+
s.write('sealed class $name { }\n');
200205
}
201206

202207
/// Writes a static function that maps integers to enum values.
203208
void writeFromValue(StringBuffer s) {
204-
s.write("${depth}static $name fromValue(int value) => switch (value) {\n");
209+
s.write('${depth}static $name fromValue(int value) => switch (value) {\n');
205210
for (final member in uniqueMembers) {
206211
final memberName = enumNames[member]!;
207-
s.write("$depth$depth${member.value} => $memberName,\n");
212+
s.write('$depth$depth${member.value} => $memberName,\n');
208213
}
209214
s.write(
210215
'$depth${depth}_ => '
211216
'throw ArgumentError("Unknown value for $name: \$value"),\n',
212217
);
213-
s.write("$depth};\n");
218+
s.write('$depth};\n');
214219
}
215220

216221
bool get _isBuiltIn =>
@@ -220,7 +225,7 @@ class EnumClass extends BindingType {
220225
BindingString toBindingString(Writer w) {
221226
final s = StringBuffer();
222227
if (_isBuiltIn) {
223-
return BindingString(type: BindingStringType.enum_, string: '');
228+
return const BindingString(type: BindingStringType.enum_, string: '');
224229
}
225230
scanForDuplicates();
226231

@@ -230,13 +235,13 @@ class EnumClass extends BindingType {
230235
} else {
231236
s.write('enum $name {\n');
232237
writeUniqueMembers(s);
233-
s.write("\n");
238+
s.write('\n');
234239
writeDuplicateMembers(s);
235-
s.write("\n");
240+
s.write('\n');
236241
writeConstructor(s);
237-
s.write("\n");
242+
s.write('\n');
238243
writeFromValue(s);
239-
s.write("\n");
244+
s.write('\n');
240245
writeToStringOverride(s);
241246
s.write('}\n\n');
242247
}
@@ -284,7 +289,7 @@ class EnumClass extends BindingType {
284289
String value, {
285290
required bool objCRetain,
286291
}) =>
287-
"$value.value";
292+
'$value.value';
288293

289294
@override
290295
String convertFfiDartTypeToDartType(
@@ -293,7 +298,7 @@ class EnumClass extends BindingType {
293298
required bool objCRetain,
294299
String? objCEnclosingClass,
295300
}) =>
296-
"${getDartType(w)}.fromValue($value)";
301+
'${getDartType(w)}.fromValue($value)';
297302
}
298303

299304
/// Represents a single value in an enum.

pkgs/ffigen/lib/src/code_generator/func.dart

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:ffigen/src/code_generator.dart';
6-
import 'package:ffigen/src/config_provider/config_types.dart';
5+
import '../code_generator.dart';
6+
import '../config_provider/config_types.dart';
77

88
import 'binding_string.dart';
99
import 'utils.dart';
@@ -17,7 +17,8 @@ import 'writer.dart';
1717
/// int sum(int a, int b);
1818
/// ```
1919
///
20-
/// The generated Dart code for this function (without `FfiNative`) is as follows.
20+
/// The generated Dart code for this function (without `FfiNative`) is as
21+
/// follows.
2122
///
2223
/// ```dart
2324
/// int sum(int a, int b) {
@@ -126,10 +127,11 @@ class Func extends LookUpBinding {
126127
.map((p) => '${p.type.getDartType(w)} ${p.name},\n')
127128
.join('');
128129

129-
final argString = functionType.dartTypeParameters
130-
.map((p) =>
131-
'${p.type.convertDartTypeToFfiDartType(w, p.name, objCRetain: false)},\n')
132-
.join('');
130+
final argString = functionType.dartTypeParameters.map((p) {
131+
final type =
132+
p.type.convertDartTypeToFfiDartType(w, p.name, objCRetain: false);
133+
return '$type,\n';
134+
}).join('');
133135
funcImplCall = functionType.returnType.convertFfiDartTypeToDartType(
134136
w,
135137
'$funcVarName($argString)',
@@ -166,8 +168,8 @@ $dartReturnType $enclosingFuncName($dartArgDeclString) => $funcImplCall;
166168
if (exposeSymbolAddress) {
167169
// Add to SymbolAddress in writer.
168170
w.symbolAddressWriter.addNativeSymbol(
169-
type:
170-
'${w.ffiLibraryPrefix}.Pointer<${w.ffiLibraryPrefix}.NativeFunction<$cType>>',
171+
type: '${w.ffiLibraryPrefix}.Pointer<'
172+
'${w.ffiLibraryPrefix}.NativeFunction<$cType>>',
171173
name: name,
172174
);
173175
}
@@ -186,8 +188,8 @@ $dartReturnType $enclosingFuncName($dartArgDeclString) {
186188
if (exposeSymbolAddress) {
187189
// Add to SymbolAddress in writer.
188190
w.symbolAddressWriter.addSymbol(
189-
type:
190-
'${w.ffiLibraryPrefix}.Pointer<${w.ffiLibraryPrefix}.NativeFunction<$cType>>',
191+
type: '${w.ffiLibraryPrefix}.Pointer<'
192+
'${w.ffiLibraryPrefix}.NativeFunction<$cType>>',
191193
name: name,
192194
ptrName: funcPointerName,
193195
);

pkgs/ffigen/lib/src/code_generator/func_type.dart

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:ffigen/src/code_generator.dart';
6-
import 'package:ffigen/src/code_generator/utils.dart';
5+
import '../code_generator.dart';
6+
import 'utils.dart';
77

88
import 'writer.dart';
99

@@ -30,11 +30,11 @@ class FunctionType extends Type {
3030
String? varArgPack;
3131
if (varArgWrapper != null && varArgParameters.isNotEmpty) {
3232
final varArgPackBuf = StringBuffer();
33-
varArgPackBuf.write("$varArgWrapper<(");
34-
varArgPackBuf.write((varArgParameters).map<String>((p) {
33+
varArgPackBuf.write('$varArgWrapper<(');
34+
varArgPackBuf.write(varArgParameters.map<String>((p) {
3535
return '${typeToString(p.type)} ${writeArgumentNames ? p.name : ""}';
3636
}).join(', '));
37-
varArgPackBuf.write(",)>");
37+
varArgPackBuf.write(',)>');
3838
varArgPack = varArgPackBuf.toString();
3939
}
4040

@@ -108,7 +108,7 @@ class FunctionType extends Type {
108108
return;
109109
}
110110
final paramNamer = UniqueNamer({});
111-
for (int i = 0; i < parameters.length; i++) {
111+
for (var i = 0; i < parameters.length; i++) {
112112
final finalName = paramNamer.makeUnique(names[i]);
113113
parameters[i] = Parameter(
114114
type: parameters[i].type,
@@ -124,9 +124,7 @@ class NativeFunc extends Type {
124124
// Either a FunctionType or a Typealias of a FunctionType.
125125
final Type _type;
126126

127-
NativeFunc(this._type) {
128-
assert(_type is FunctionType || _type is Typealias);
129-
}
127+
NativeFunc(this._type) : assert(_type is FunctionType || _type is Typealias);
130128

131129
FunctionType get type {
132130
if (_type is Typealias) {

pkgs/ffigen/lib/src/code_generator/global.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,21 @@ class Global extends LookUpBinding {
7676
final pointerName =
7777
w.wrapperLevelUniqueNamer.makeUnique('_$globalVarName');
7878

79-
s.write(
80-
"late final ${w.ffiLibraryPrefix}.Pointer<$cType> $pointerName = ${w.lookupFuncIdentifier}<$cType>('$originalName');\n\n");
79+
s.write('late final ${w.ffiLibraryPrefix}.Pointer<$cType> $pointerName = '
80+
"${w.lookupFuncIdentifier}<$cType>('$originalName');\n\n");
8181
final baseTypealiasType = type.typealiasType;
8282
if (baseTypealiasType is Compound) {
8383
if (baseTypealiasType.isOpaque) {
84-
s.write(
85-
'${w.ffiLibraryPrefix}.Pointer<$cType> get $globalVarName => $pointerName;\n\n');
84+
s.write('${w.ffiLibraryPrefix}.Pointer<$cType> get $globalVarName =>'
85+
' $pointerName;\n\n');
8686
} else {
8787
s.write('$dartType get $globalVarName => $pointerName.ref;\n\n');
8888
}
8989
} else {
9090
s.write('$dartType get $globalVarName => $pointerName.value;\n\n');
9191
if (!constant) {
92-
s.write(
93-
'set $globalVarName($dartType value) => $pointerName.value = value;\n\n');
92+
s.write('set $globalVarName($dartType value) =>'
93+
'$pointerName.value = value;\n\n');
9494
}
9595
}
9696

0 commit comments

Comments
 (0)