diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..e3b0cf7 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,3 @@ +# Every request must be reviewed and accepted by: + +* @crispy-riccardo diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..cddd619 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,24 @@ +--- +name: Bug report +about: Create a report to help us improve +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: + +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..a09db44 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,16 @@ +--- +name: Feature request +about: Suggest an idea for this project +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..6b9372e --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,23 @@ + + +## Description + + + +## Type of Change + + + +- [ ] โœจ New feature (non-breaking change which adds functionality) +- [ ] ๐Ÿ› ๏ธ Bug fix (non-breaking change which fixes an issue) +- [ ] โŒ Breaking change (fix or feature that would cause existing functionality to change) +- [ ] ๐Ÿงน Code refactor +- [ ] โœ… Build configuration change +- [ ] ๐Ÿ“ Documentation +- [ ] ๐Ÿ—‘๏ธ Chore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9001448 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.dart_tool +.packages +pubspec.lock +.DS_Store +.idea diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..99b639e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# 1.0.0 + +- First release diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..c79c81d --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,39 @@ +# Contributing to Crispy Analysis + +๐Ÿ‘๐ŸŽ‰ First off, thanks for taking the time to contribute! ๐ŸŽ‰๐Ÿ‘ + +These are mostly guidelines, not rules. Use your best judgment, +and feel free to propose changes to this document in a pull request. + +## Proposing a Change + +If you intend to change the public API, or make any non-trivial changes +to the implementation, we recommend filing an issue. +This lets us reach an agreement on your proposal before you put significant +effort into it. + +If youโ€™re only fixing a bug, itโ€™s fine to submit a pull request right away +but we still recommend to file an issue detailing what youโ€™re fixing. +This is helpful in case we donโ€™t accept that specific fix but want to keep +track of the issue. + +## Creating a Pull Request + +Before creating a pull request please: + +1. Fork the repository and create your branch from `main`. +1. Install all dependencies (`flutter packages get` or `pub get`). +1. Squash your commits and ensure you have a meaningful commit message. +1. If youโ€™ve fixed a bug or added code that should be tested, add tests! + Pull Requests without 100% test coverage will not be approved. +1. Ensure the test suite passes. +1. If you've changed the public API, make sure to update/add documentation. +1. Format your code (`dartfmt -w .`). +1. Analyze your code (`dartanalyzer --fatal-infos --fatal-warnings .`). +1. Create the Pull Request. +1. Verify that all status checks are passing. + +While the prerequisites above must be satisfied prior to having your +pull request reviewed, the reviewer(s) may ask you to complete additional +design work, tests, or other changes before your pull request can be ultimately +accepted. diff --git a/README.md b/README.md new file mode 100644 index 0000000..5c0327a --- /dev/null +++ b/README.md @@ -0,0 +1,89 @@ +# Crispy Analysis + +[![Crispy Bacon SRL][logo]][crispy_link] + +Maintained by [Riccardo Strazzer][maintainer_link] + +[![style: crispy analysis][badge]][badge_link] + +--- + +This package provides lint rules for Dart and Flutter which are used at [Crispy Bacon SRL][crispy_bacon_link]. For more information, see the [complete list of options][analysis_options_yaml]. + +The example folder contains some example. If you find some cases where some rules are too aggressive please fill a issue and we can discuss about it's removal. + +## Usage + +To use the lints, add as a dev dependency in your `pubspec.yaml`: + +```yaml +crispy_analysis: + git: https://github.com/crispybaconsrl/crispy_analysis +``` + +Then, add an include in `analysis_options.yaml`: + +```yaml +include: package:crispy_analysis/analysis_options.yaml +``` + +This will ensure you always use the latest version of the lints. If you wish to restrict the lint version, specify a version of `analysis_options.yaml` instead: + +```yaml +include: package:crispy_analysis/analysis_options.1.0.0.yaml +``` + +## Suppressing Lints + +There may be cases where specific lint rules are undesirable. Lint rules can be suppressed at the line, file, or project level. + +An example use case for suppressing lint rules at the file level is suppressing the `prefer_const_constructors` in order to achieve 100% code coverage. This is due to the fact that const constructors are executed before the tests are run, resulting in no coverage collection. + +### Line Level + +To suppress a specific lint rule for a specific line of code, use an `ignore` comment directly above the line: + +```dart +// ignore: public_member_api_docs +class A {} +``` + +### File Level + +To suppress a specific lint rule of a specific file, use an `ignore_for_file` comment at the top of the file: + +```dart +// ignore_for_file: public_member_api_docs + +class A {} + +class B {} +``` + +### Project Level + +To suppress a specific lint rule for an entire project, modify `analysis_options.yaml`: + +```yaml +include: package:crispy_analysis/analysis_options.yaml +linter: + rules: + public_member_api_docs: false +``` + +## Badge + +To indicate your project is using `crispy_analysis` โ†’ +[![style: crispy analysis][badge]][badge_link] + +```md +[![style: crispy analysis](https://img.shields.io/badge/style-crispy_analysis-A1022B.svg)](https://github.com/crispybaconsrl/crispy_analysis) +``` + +[analysis_options_yaml]: https://github.com/crispybaconsrl/crispy_analysis/blob/main/lib/analysis_options.1.0.0.yaml +[badge]: https://img.shields.io/badge/style-crispy_analysis-A1022B.svg +[badge_link]: https://github.com/crispybaconsrl/crispy_analysis +[logo]: ./assets/crispybacon_logo.png +[crispy_bacon_link]: https://crispybacon.it +[crispy_link]: https://crispybacon.it +[maintainer_link]: https://github.com/crispy-riccardo diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..a4bfc29 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1 @@ +include: lib/analysis_options.yaml diff --git a/assets/crispybacon_logo.png b/assets/crispybacon_logo.png new file mode 100644 index 0000000..0c412a6 Binary files /dev/null and b/assets/crispybacon_logo.png differ diff --git a/example/analysis_options.yaml b/example/analysis_options.yaml new file mode 100644 index 0000000..021d9ac --- /dev/null +++ b/example/analysis_options.yaml @@ -0,0 +1,5 @@ +include: package:crispy_analysis/analysis_options.yaml + +linter: + rules: + public_member_api_docs: false \ No newline at end of file diff --git a/example/lib/class_example.dart b/example/lib/class_example.dart new file mode 100644 index 0000000..553e039 --- /dev/null +++ b/example/lib/class_example.dart @@ -0,0 +1,35 @@ +abstract class Dog { + String get breed; + + String get color; + + /// avoid_return_types_on_setters + void set color(String color); + + /// avoid_setters_without_getters + set owner(String owner); + + void feed(String food); +} + +class Husky extends Dog { + @override + final String breed = 'Husky'; + + /// annotate_overrides + final String color = 'Black'; + + @override + set color(String color) { + /// avoid_returning_null_for_void + return null; + } + + @override + set owner(String owner) {} + + @override + void feed(String whatFood) { + /// avoid_renaming_method_parameters + } +} diff --git a/example/lib/example.dart b/example/lib/example.dart new file mode 100644 index 0000000..1411032 --- /dev/null +++ b/example/lib/example.dart @@ -0,0 +1,96 @@ +// The following syntax deactivates a lint for the entire file: +// ignore_for_file: avoid_print + +import 'dart:math'; + +import './extensions.dart'; + +void main() { + /// The following line would normally show a lint warning + /// but we can disable the lint rule for this line using the following syntax. + var greeting = 'hello world'; // ignore: prefer_final_locals + + /// The following line would normally show a lint warning + /// but we can disable the lint rule for this file using `ignore_for_file`. + print(greeting); + + final classExample = ClassExample(); + + /// avoid_single_cascade_in_expression_statements + classExample..methodExample(test2: ''); + + /// avoid_init_to_null + /// prefer_const_declarations + final String? initToNull = null; + print(initToNull); + + /// always_declare_return_types + var _example = simpleMethod(); + String example2 = simpleMethodForcedType(); + + classExample.methodExample(test2: 'test'); + + print(_example.capitalize); + print(example2.capitalize); + + /// avoid_bool_literals_in_conditional_expressions + final bool test = Random().nextBool() ? true : false; + + if (test) { + print('true'); + } else { + print('false'); + } + + /// avoid_escaping_inner_quotes + print('It\'s not fun'); + print("It's not fun"); + + /// avoid_function_literals_in_foreach_calls + /// unnecessary_lambdas + [1, 2, 3, 4, 5, 6].forEach((element) => print(element)); + + /// avoid_multiple_declarations_per_line + String? a, b, c; + print([a, b, c]); + + /// avoid_redundant_argument_values + changeActive2(isActive: true); + changeActive2(isActive: false); +} + +/// always_declare_return_types +simpleMethod() { + return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'; +} + +String simpleMethodForcedType() { + return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'; +} + +/// avoid_positional_boolean_parameters +void changeActive(bool isActive) { + // do something +} + +void changeActive2({bool isActive = false}) { + // do something +} + +/// avoid_private_typedef_functions +typedef void _PrivateTypeDef(); + +class ClassExample { + final String firstVariable; + final String secondVariable; + + ClassExample({ + this.firstVariable = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', + this.secondVariable = '', + }); + + /// always_put_required_named_parameters_first + void methodExample({String? test, required String test2}) { + print('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. $test $test2'); + } +} diff --git a/example/lib/extensions.dart b/example/lib/extensions.dart new file mode 100644 index 0000000..fac403c --- /dev/null +++ b/example/lib/extensions.dart @@ -0,0 +1,6 @@ +extension StringExtension on String { + /// prefer_single_quotes + String get capitalize { + return "${this[0].toUpperCase()}${this.substring(1)}"; + } +} diff --git a/example/pubspec.yaml b/example/pubspec.yaml new file mode 100644 index 0000000..36a3965 --- /dev/null +++ b/example/pubspec.yaml @@ -0,0 +1,10 @@ +name: crispy_analysis_example +version: 0.0.1 +publish_to: none + +environment: + sdk: ">=2.18.0 <3.0.0" + +dependencies: + crispy_analysis: + path: ../ diff --git a/lib/analysis_options.1.0.0.yaml b/lib/analysis_options.1.0.0.yaml new file mode 100644 index 0000000..330d006 --- /dev/null +++ b/lib/analysis_options.1.0.0.yaml @@ -0,0 +1,207 @@ +analyzer: + language: + strict-casts: true + strict-inference: true + strict-raw-types: true + + errors: + close_sinks: ignore + missing_required_param: error + missing_return: error + record_literal_one_positional_no_trailing_comma: error + collection_methods_unrelated_type: warning + unrelated_type_equality_checks: warning + + exclude: + - test/.test_coverage.dart + - lib/generated_plugin_registrant.dart + - '**/*.locator.dart' + - '**/*.logger.dart' + - '**/*.router.dart' + +linter: + rules: + - always_declare_return_types + - always_put_required_named_parameters_first + - always_use_package_imports + - annotate_overrides + - avoid_bool_literals_in_conditional_expressions + - avoid_catching_errors + - avoid_double_and_int_checks + - avoid_dynamic_calls + - avoid_empty_else + - avoid_equals_and_hash_code_on_mutable_classes + - avoid_escaping_inner_quotes + - avoid_field_initializers_in_const_classes + - avoid_final_parameters + - avoid_function_literals_in_foreach_calls + - avoid_init_to_null + - avoid_js_rounded_ints + - avoid_multiple_declarations_per_line + - avoid_null_checks_in_equality_operators + - avoid_positional_boolean_parameters + - avoid_print + - avoid_private_typedef_functions + - avoid_redundant_argument_values + - avoid_relative_lib_imports + - avoid_renaming_method_parameters + - avoid_return_types_on_setters + - avoid_returning_null_for_void + - avoid_returning_this + - avoid_setters_without_getters + - avoid_shadowing_type_parameters + - avoid_single_cascade_in_expression_statements + - avoid_slow_async_io + - avoid_type_to_string + - avoid_types_as_parameter_names + - avoid_unnecessary_containers + - avoid_unused_constructor_parameters + - avoid_void_async + - avoid_web_libraries_in_flutter + - await_only_futures + - camel_case_extensions + - camel_case_types + - cancel_subscriptions + - cascade_invocations + - cast_nullable_to_non_nullable + - collection_methods_unrelated_type + - combinators_ordering + - comment_references + - conditional_uri_does_not_exist + - constant_identifier_names + - control_flow_in_finally + - curly_braces_in_flow_control_structures + - dangling_library_doc_comments + - depend_on_referenced_packages + - deprecated_consistency + - directives_ordering + - empty_catches + - empty_constructor_bodies + - empty_statements + - eol_at_end_of_file + - exhaustive_cases + - file_names + - flutter_style_todos + - hash_and_equals + - implicit_call_tearoffs + - implementation_imports + - implicit_reopen + - invalid_case_patterns + - join_return_with_assignment + - leading_newlines_in_multiline_strings + - library_annotations + - library_names + - library_prefixes + - library_private_types_in_public_api + - literal_only_boolean_expressions + - missing_whitespace_between_adjacent_strings + - no_adjacent_strings_in_list + - no_default_cases + - no_duplicate_case_values + - no_leading_underscores_for_library_prefixes + - no_leading_underscores_for_local_identifiers + - no_logic_in_create_state + - no_runtimeType_toString + - non_constant_identifier_names + - noop_primitive_operations + - null_check_on_nullable_type_parameter + - null_closures + - one_member_abstracts + - only_throw_errors + - overridden_fields + - package_api_docs + - package_names + - package_prefixed_library_names + - parameter_assignments + - prefer_adjacent_string_concatenation + - prefer_asserts_in_initializer_lists + - prefer_asserts_with_message + - prefer_collection_literals + - prefer_conditional_assignment + - prefer_const_constructors + - prefer_const_constructors_in_immutables + - prefer_const_declarations + - prefer_const_literals_to_create_immutables + - prefer_constructors_over_static_methods + - prefer_contains + - prefer_final_fields + - prefer_final_in_for_each + - prefer_final_locals + - prefer_for_elements_to_map_fromIterable + - prefer_function_declarations_over_variables + - prefer_generic_function_type_aliases + - prefer_if_elements_to_conditional_expressions + - prefer_if_null_operators + - prefer_initializing_formals + - prefer_inlined_adds + - prefer_int_literals + - prefer_interpolation_to_compose_strings + - prefer_is_empty + - prefer_is_not_empty + - prefer_is_not_operator + - prefer_iterable_whereType + - prefer_null_aware_method_calls + - prefer_null_aware_operators + - prefer_single_quotes + - prefer_spread_collections + - prefer_typing_uninitialized_variables + - prefer_void_to_null + - provide_deprecation_message + - public_member_api_docs + - recursive_getters + - require_trailing_commas + - secure_pubspec_urls + - sized_box_for_whitespace + - sized_box_shrink_expand + - slash_for_doc_comments + - sort_child_properties_last + - sort_constructors_first + - sort_unnamed_constructors_first + - test_types_in_equals + - throw_in_finally + - tighten_type_of_initializing_formals + - type_annotate_public_apis + - type_init_formals + - unawaited_futures + - unnecessary_await_in_return + - unnecessary_breaks + - unnecessary_brace_in_string_interps + - unnecessary_const + - unnecessary_constructor_name + - unnecessary_getters_setters + - unnecessary_lambdas + - unnecessary_late + - unnecessary_library_directive + - unnecessary_new + - unnecessary_null_aware_assignments + - unnecessary_null_checks + - unnecessary_null_in_if_null_operators + - unnecessary_nullable_for_final_variable_declarations + - unnecessary_overrides + - unnecessary_parenthesis + - unnecessary_raw_strings + - unnecessary_statements + - unnecessary_string_escapes + - unnecessary_string_interpolations + - unnecessary_this + - unnecessary_to_list_in_spreads + - unrelated_type_equality_checks + - use_colored_box + - use_enums + - use_full_hex_values_for_flutter_colors + - use_function_type_syntax_for_parameters + - use_if_null_to_convert_nulls_to_bools + - use_is_even_rather_than_modulo + - use_key_in_widget_constructors + - use_late_for_private_fields_and_variables + - use_named_constants + - use_raw_strings + - use_rethrow_when_possible + - use_setters_to_change_properties + - use_string_buffers + - use_string_in_part_of_directives + - use_super_parameters + - use_test_throws_matchers + - use_to_and_as_if_applicable + - valid_regexps + - void_checks diff --git a/lib/analysis_options.yaml b/lib/analysis_options.yaml new file mode 100644 index 0000000..f090fcc --- /dev/null +++ b/lib/analysis_options.yaml @@ -0,0 +1 @@ +include: package:crispy_analysis/analysis_options.1.0.0.yaml diff --git a/lib/crispy_analysis.dart b/lib/crispy_analysis.dart new file mode 100644 index 0000000..8e465b8 --- /dev/null +++ b/lib/crispy_analysis.dart @@ -0,0 +1,3 @@ +/// CrispyBacon Dart analyzer settings and best practices +/// used internally at [CrispyBacon](https://crispybacon.it). +library crispy_analysis; diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..83c9e89 --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,11 @@ +name: crispy_analysis +version: 1.0.0 +description: Lint rules for Dart and Flutter used at CrispyBacon SRL. +repository: https://github.com/crispybaconsrl/crispy_analysis +issue_tracker: https://github.com/crispybaconsrl/crispy_analysis/issues +homepage: https://github.com/crispybaconsrl/crispy_analysis +documentation: https://github.com/crispybaconsrl/crispy_analysis +topics: [lints, analyzer, analysis] + +environment: + sdk: ">=3.0.0 <4.0.0"