From b17875fa0e236b0bb657ae19a06b91338c4c9990 Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Wed, 5 May 2021 16:54:33 -0700 Subject: [PATCH] Add flutter_lints package (#343) --- packages/flutter_lints/CHANGELOG.md | 3 ++ packages/flutter_lints/LICENSE | 27 +++++++++++ packages/flutter_lints/README.md | 64 +++++++++++++++++++++++++ packages/flutter_lints/lib/flutter.yaml | 17 +++++++ packages/flutter_lints/pubspec.yaml | 11 +++++ packages/flutter_lints/run_tests.sh | 9 ++++ 6 files changed, 131 insertions(+) create mode 100644 packages/flutter_lints/CHANGELOG.md create mode 100644 packages/flutter_lints/LICENSE create mode 100644 packages/flutter_lints/README.md create mode 100644 packages/flutter_lints/lib/flutter.yaml create mode 100644 packages/flutter_lints/pubspec.yaml create mode 100755 packages/flutter_lints/run_tests.sh diff --git a/packages/flutter_lints/CHANGELOG.md b/packages/flutter_lints/CHANGELOG.md new file mode 100644 index 00000000000..f84d7e49256 --- /dev/null +++ b/packages/flutter_lints/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0-dev.0 + +* Initial pre-release diff --git a/packages/flutter_lints/LICENSE b/packages/flutter_lints/LICENSE new file mode 100644 index 00000000000..7063d084ef1 --- /dev/null +++ b/packages/flutter_lints/LICENSE @@ -0,0 +1,27 @@ +Copyright 2021, The Flutter Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of Google LLC nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/packages/flutter_lints/README.md b/packages/flutter_lints/README.md new file mode 100644 index 00000000000..8b775ab122f --- /dev/null +++ b/packages/flutter_lints/README.md @@ -0,0 +1,64 @@ +[![pub package](https://img.shields.io/pub/v/flutter_lints.svg)](https://pub.dev/packages/flutter_lints) + +This package contains a recommended set of lints for [Flutter] apps, packages, +and plugins to encourage good coding practices. + +Lints are surfaced by the [dart analyzer], which statically checks dart code. +[Dart-enabled IDEs] typically present the issues identified by the analyzer in +their UI. Alternatively, the analyzer can be invoked manually by running +`flutter analyze`. + +This package is built on top of Dart's `recommended.yaml` set of lints from +[package:lints]. + +## Usage + +Flutter apps, packages, and plugins created with `flutter create` starting with +Flutter version 2.xx are already set up to use the lints defined in this +package. Entities created before that version can use these lints by following +these instructions: + +1. Depend on this package as a **dev_dependency** by running + `flutter pub add --dev flutter_lints`. +2. Create an `analyzer_options.yaml` file at the root of the package (alongside + the `pubspec.yaml` file) and `include: package:flutter_lints/flutter.yaml` + from it. + +Example `analyzer_options.yaml` file: + +```yaml +# This file configures the analyzer, which statically analyzes Dart code to +# check for errors, warnings, and lints. +# +# The issues identified by the analyzer are surfaced in the UI of Dart-enabled +# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be +# invoked from the command line by running `flutter analyze`. + +# The following line activates a set of recommended lints for Flutter apps, +# packages, and plugins designed to encourage good coding practices. +include: package:flutter_lints/flutter.yaml + +linter: + # The lint rules applied to this project can be customized in the + # section below to disable rules from the `package:flutter_lints/flutter.yaml` + # included above or to enable additional rules. A list of all available lints + # and their documentation is published at + # https://dart-lang.github.io/linter/lints/index.html. + # + # Instead of disabling a lint rule for the entire project in the + # section below, it can also be suppressed for a single line of code + # or a specific dart file by using the `// ignore: name_of_lint` and + # `// ignore_for_file: name_of_lint` syntax on the line or in the file + # producing the lint. + rules: + # avoid_print: false # Uncomment to disable the `avoid_print` rule + # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule + +# Additional information about this file can be found at +# https://dart.dev/guides/language/analysis-options +``` + +[Flutter]: https://flutter.dev +[dart analyzer]: https://dart.dev/guides/language/analysis-options +[Dart-enabled IDEs]: https://dart.dev/tools#ides-and-editors +[package:lints]: https://pub.dev/packages/lints diff --git a/packages/flutter_lints/lib/flutter.yaml b/packages/flutter_lints/lib/flutter.yaml new file mode 100644 index 00000000000..05276da1f2c --- /dev/null +++ b/packages/flutter_lints/lib/flutter.yaml @@ -0,0 +1,17 @@ +# Recommended lints for Flutter apps, packages, and plugins. + +include: package:lints/recommended.yaml + +linter: + rules: + - avoid_print + - avoid_unnecessary_containers + - avoid_web_libraries_in_flutter + - no_logic_in_create_state + - prefer_const_constructors + - prefer_const_constructors_in_immutables + - prefer_const_declarations + - prefer_const_literals_to_create_immutables + - sized_box_for_whitespace + - use_full_hex_values_for_flutter_colors + - use_key_in_widget_constructors diff --git a/packages/flutter_lints/pubspec.yaml b/packages/flutter_lints/pubspec.yaml new file mode 100644 index 00000000000..8f4212c77d3 --- /dev/null +++ b/packages/flutter_lints/pubspec.yaml @@ -0,0 +1,11 @@ +name: flutter_lints +description: Recommended lints for Flutter apps, packages, and plugins. +version: 1.0.0-dev.0 +homepage: https://github.com/flutter/packages/tree/master/packages/flutter_lints + +environment: + sdk: '>=2.12.0 <3.0.0' + +dependencies: + lints: ^1.0.0-dev.0 + # Code is not allowed in this package. Do not add any dependencies or dev_dependencies. diff --git a/packages/flutter_lints/run_tests.sh b/packages/flutter_lints/run_tests.sh new file mode 100755 index 00000000000..aa9a0b0342b --- /dev/null +++ b/packages/flutter_lints/run_tests.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +filecount=`find . -name '*.dart' | wc -l` +if [ $filecount -ne 0 ] +then + echo 'Dart sources are not allowed in this package:' + find . -name '*.dart' + exit 1 +fi