这是indexloc提供的服务,不要输入任何密码
Skip to content

Fix or ignore linter issues in generated code #18

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

Merged
merged 2 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/tonik/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<a href="https://pub.dev/packages/tonik"><img src="https://img.shields.io/pub/v/tonik?logo=dart" alt="pub verion"></a>
<a href="https://pub.dev/packages/tonik"><img src="https://img.shields.io/pub/likes/tonik?logo=dart" alt="pub likes"></a>
<a href="https://github.com/t-unit/tonik"><img src="https://img.shields.io/github/stars/t-unit/tonik?logo=github" alt="stars on github"></a>
<a href="https://github.com/t-unit/tonik"><img src="https://github.com/t-unit/tonik/actions/workflows/test.yml/badge.svg?branch=main" alt="tests"></a>
<a href="https://github.com/t-unit/tonik"><img src="https://github.com/t-unit/tonik/actions/workflows/test.yml/badge.svg?branch=main" alt="tests"></a>
<a href="https://pub.dev/packages/very_good_analysis"><img src="https://img.shields.io/badge/style-very_good_analysis-B22C89.svg"></a>
</p>


Expand Down
35 changes: 35 additions & 0 deletions packages/tonik_generate/lib/src/analysis_options_generator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'dart:io';
import 'package:path/path.dart' as path;

void generateAnalysisOptions({
required String outputDirectory,
required String package,
}) {
final packageDir = path.join(outputDirectory, package);
final analysisOptionsFile = File(
path.join(packageDir, 'analysis_options.yaml'),
);

if (!analysisOptionsFile.parent.existsSync()) {
analysisOptionsFile.parent.createSync(recursive: true);
}

const content = '''
include: package:lints/recommended.yaml

analyzer:
errors:
lines_longer_than_80_chars: ignore
unnecessary_raw_strings: ignore
unnecessary_brace_in_string_interps: ignore
no_leading_underscores_for_local_identifiers: ignore
cascade_invocations: ignore
deprecated_member_use_from_same_package: ignore
no_leading_underscores_for_library_prefixes: ignore
unused_import: ignore
prefer_is_empty: ignore
unnecessary_nullable_for_final_variable_declarations: ignore
''';

analysisOptionsFile.writeAsStringSync(content);
}
6 changes: 6 additions & 0 deletions packages/tonik_generate/lib/src/generator.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:tonik_core/tonik_core.dart';
import 'package:tonik_generate/src/analysis_options_generator.dart';
import 'package:tonik_generate/src/api_client/api_client_file_generator.dart';
import 'package:tonik_generate/src/api_client/api_client_generator.dart';
import 'package:tonik_generate/src/library_generator.dart';
Expand Down Expand Up @@ -132,6 +133,11 @@ class Generator {
package: package,
);

generateAnalysisOptions(
outputDirectory: outputDirectory,
package: package,
);

modelGenerator.writeFiles(
apiDocument: apiDocument,
outputDirectory: outputDirectory,
Expand Down
25 changes: 25 additions & 0 deletions packages/tonik_generate/lib/src/model/class_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ class ClassGenerator {

Constructor _buildFromSimpleConstructor(String className, ClassModel model) {
final normalizedProperties = normalizeProperties(model.properties.toList());

// If there are no properties, just return the constructor call
if (normalizedProperties.isEmpty) {
return Constructor(
(b) =>
b
..factory = true
..name = 'fromSimple'
..requiredParameters.add(
Parameter(
(b) =>
b
..name = 'value'
..type = refer('String?', 'dart:core'),
),
)
..body = Code('return $className();'),
);
}

final propertyAssignments = <MapEntry<String, Expression>>[];
for (var i = 0; i < normalizedProperties.length; i++) {
final prop = normalizedProperties[i];
Expand Down Expand Up @@ -258,6 +278,11 @@ class ClassGenerator {
Code _buildFromJsonBody(String className, ClassModel model) {
final normalizedProperties = normalizeProperties(model.properties.toList());

// If there are no properties, just return the constructor call
if (normalizedProperties.isEmpty) {
return Block.of([Code('return $className();')]);
}

final codes = <Code>[
Code("final map = json.decodeMap(context: '$className');"),
];
Expand Down
1 change: 1 addition & 0 deletions packages/tonik_generate/lib/src/pubspec_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies:
big_decimal: ^0.5.0
collection: ^1.17.0
dio: ^5.8.0+1
lints: ^6.0.0
meta: ^1.16.0
tonik_util: ^0.0.5
''';
Expand Down
11 changes: 1 addition & 10 deletions packages/tonik_generate/lib/src/util/format_with_header.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import 'package:dart_style/dart_style.dart';

extension FormatWithHeader on DartFormatter {
static const _ignores = [
'lines_longer_than_80_chars',
'unnecessary_raw_strings',
'unnecessary_brace_in_string_interps',
'no_leading_underscores_for_local_identifiers',
'cascade_invocations',
'prefer_is_empty',
];

String formatWithHeader(String code) {
return format('''
// Generated code - do not modify by hand
${_ignores.map((i) => '// ignore_for_file: $i').join('\n')}

$code''');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -597,5 +597,43 @@ void main() {
contains(collapseWhitespace(expectedMethod)),
);
});

test('generates fromJson method for class without properties', () {
final model = ClassModel(
context: context,
name: 'EmptyClass',
properties: const [],
);

const expectedMethod = '''
factory EmptyClass.fromJson(Object? json) {
return EmptyClass();
}''';

final generatedClass = generator.generateClass(model);
expect(
collapseWhitespace(format(generatedClass.accept(emitter).toString())),
contains(collapseWhitespace(expectedMethod)),
);
});

test('generates fromSimple method for class without properties', () {
final model = ClassModel(
context: context,
name: 'EmptyClass',
properties: const [],
);

const expectedMethod = '''
factory EmptyClass.fromSimple(String? value) {
return EmptyClass();
}''';

final generatedClass = generator.generateClass(model);
expect(
collapseWhitespace(format(generatedClass.accept(emitter).toString())),
contains(collapseWhitespace(expectedMethod)),
);
});
});
}