Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #210 from MattisBrizard/beta
Browse files Browse the repository at this point in the history
Added an ignore_for_file option to ignore linter rules for generated files (Fix #110)
  • Loading branch information
comigor authored Sep 3, 2020
2 parents 802c239 + f0d9a56 commit 509c041
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 6.12.1-beta.1
- Added `ignore_for_file` option to ignore linter rules on generated files.

## 6.11.1-beta.1
- improved canonical types handling

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Each `SchemaMap` is configured this way:
| `queries_glob` | | Glob that selects all query files to be used with this schema. |
| `naming_scheme` | `pathedWithTypes` | The naming scheme to be used on generated classes names. `pathedWithTypes` is the default for retrocompatibility, where the names of previous types are used as prefix of the next class. This can generate duplication on certain schemas. With `pathedWithFields`, the names of previous fields are used as prefix of the next class and with `simple`, only the actual GraphQL class nameis considered. |
| `type_name_field` | `__typename` | The name of the field used to differentiatiate interfaces and union types (commonly `__typename` or `__resolveType`). Note that `__typename` field are not added automatically to the query. If you want interface/union type resolution, you need to manually add it there. |
| `ignore_for_file` | `[]` | The linter rules to ignore for artemis generated files. |

See [examples](./example) for more information and configuration options.

Expand Down
6 changes: 5 additions & 1 deletion lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ ${e}
if (onBuild != null) {
onBuild(libDefinition);
}
writeLibraryDefinitionToBuffer(buffer, libDefinition);
writeLibraryDefinitionToBuffer(
buffer,
options.ignoreForFile,
libDefinition,
);

await buildStep.writeAsString(outputFileId, buffer.toString());

Expand Down
13 changes: 11 additions & 2 deletions lib/generator/print_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,17 @@ String specToString(Spec spec) {
/// Generate Dart code typings from a query or mutation and its response from
/// a [QueryDefinition] into a buffer.
void writeLibraryDefinitionToBuffer(
StringBuffer buffer, LibraryDefinition definition) {
buffer.writeln('// GENERATED CODE - DO NOT MODIFY BY HAND\n');
StringBuffer buffer,
List<String> ignoreForFile,
LibraryDefinition definition,
) {
buffer.writeln('// GENERATED CODE - DO NOT MODIFY BY HAND');
if (ignoreForFile != null && ignoreForFile.isNotEmpty) {
buffer.writeln(
'// ignore_for_file: ${Set<String>.from(ignoreForFile).join(', ')}',
);
}
buffer.write('\n');
buffer.write(specToString(generateLibrarySpec(definition)));
}

Expand Down
6 changes: 6 additions & 0 deletions lib/schema/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ class GeneratorOptions {
@JsonKey(defaultValue: [])
final List<SchemaMap> schemaMapping;

/// A list of linter rules to ignore
/// in generated files.
@JsonKey(defaultValue: [])
final List<String> ignoreForFile;

/// Instantiate generator options.
GeneratorOptions({
this.generateHelpers = true,
this.scalarMapping = const [],
this.fragmentsGlob,
this.schemaMapping = const [],
this.ignoreForFile = const [],
});

/// Build options from a JSON map.
Expand Down
5 changes: 5 additions & 0 deletions lib/schema/options.g2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ GeneratorOptions _$GeneratorOptionsFromJson(Map json) {
)))
?.toList() ??
[],
ignoreForFile: (json['ignore_for_file'] as List)
?.map((e) => e == null ? null : e as String)
?.toList() ??
[],
);
}

Expand All @@ -37,6 +41,7 @@ Map<String, dynamic> _$GeneratorOptionsToJson(GeneratorOptions instance) =>
'scalar_mapping': instance.scalarMapping,
'fragments_glob': instance.fragmentsGlob,
'schema_mapping': instance.schemaMapping,
'ignore_for_file': instance.ignoreForFile,
};

DartType _$DartTypeFromJson(Map<String, dynamic> json) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: artemis
version: 6.11.1-beta.1
version: 6.12.1-beta.1

description: Build dart types from GraphQL schemas and queries (using Introspection Query).
homepage: https://github.com/comigor/artemis
Expand Down
66 changes: 60 additions & 6 deletions test/generator/print_helpers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ class AClass with EquatableMixin {
test('It should generated an empty file by default.', () {
final buffer = StringBuffer();
final definition = LibraryDefinition(basename: r'test_query.graphql');

writeLibraryDefinitionToBuffer(buffer, definition);
final ignoreForFile = <String>[];
writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition);

expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND
Expand All @@ -445,8 +445,9 @@ part 'test_query.graphql.g.dart';
final buffer = StringBuffer();
final definition = LibraryDefinition(
basename: r'test_query.graphql', customImports: ['some_file.dart']);
final ignoreForFile = <String>[];

writeLibraryDefinitionToBuffer(buffer, definition);
writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition);

expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND
Expand All @@ -471,8 +472,9 @@ part 'test_query.graphql.g.dart';
)
],
);
final ignoreForFile = <String>[];

writeLibraryDefinitionToBuffer(buffer, definition);
writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition);

expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND
Expand Down Expand Up @@ -522,8 +524,9 @@ class TestQueryQuery extends GraphQLQuery<TestQuery, JsonSerializable> {
],
),
]);
final ignoreForFile = <String>[];

writeLibraryDefinitionToBuffer(buffer, definition);
writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition);

expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND
Expand Down Expand Up @@ -669,8 +672,9 @@ class TestQueryArguments extends JsonSerializable with EquatableMixin {
],
),
]);
final ignoreForFile = <String>[];

writeLibraryDefinitionToBuffer(buffer, definition);
writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition);

expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND
Expand All @@ -697,4 +701,54 @@ enum SomeEnum {
''');
});
});

test('Should not add ignore_for_file when ignoreForFile is null', () {
final buffer = StringBuffer();
final definition = LibraryDefinition(basename: r'test_query.graphql');
final List<String> ignoreForFile = null;

writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition);

expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND
import 'package:json_annotation/json_annotation.dart';
import 'package:equatable/equatable.dart';
import 'package:gql/ast.dart';
part 'test_query.graphql.g.dart';
''');
});

test('Should not add ignore_for_file when ignoreForFile is empty', () {
final buffer = StringBuffer();
final definition = LibraryDefinition(basename: r'test_query.graphql');
final ignoreForFile = <String>[];

writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition);

expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND
import 'package:json_annotation/json_annotation.dart';
import 'package:equatable/equatable.dart';
import 'package:gql/ast.dart';
part 'test_query.graphql.g.dart';
''');
});

test('Should add // ignore_for_file: ... when ignoreForFile is not empty',
() {
final buffer = StringBuffer();
final definition = LibraryDefinition(basename: r'test_query.graphql');
final ignoreForFile = <String>['my_rule_1', 'my_rule_2'];

writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition);

expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: my_rule_1, my_rule_2
import 'package:json_annotation/json_annotation.dart';
import 'package:equatable/equatable.dart';
import 'package:gql/ast.dart';
part 'test_query.graphql.g.dart';
''');
});
}

0 comments on commit 509c041

Please sign in to comment.