Skip to content
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
18 changes: 7 additions & 11 deletions .github/workflows/code-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@ jobs:

- uses: subosito/flutter-action@v2
with:
channel: 'stable'
channel: "stable"
cache: true

- name: Install Melos
uses: bluefireteam/melos-action@v3
- name: Format
run: dart format --set-exit-if-changed packages/altive_lints/lib

- name: Analyze packages
run: melos run analyze
- name: Analyze
run: flutter analyze

- name: Custom lint
run: melos custom_lint

- name: Check for the existence of unformatted files
# Cannot use `melos format` as it requires excluding files generated from the target file
run: melos run format:ci --no-select
- name: Test
run: dart test packages/altive_lints --fail-fast
149 changes: 75 additions & 74 deletions packages/altive_lints/README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# Altive Lints

Provides `all_lint_rules.yaml` that activates all rules and `altive_lints.yaml` with Altive recommended rule selection.
Provides `all_lint_rules.yaml` that activates all lint rules and `altive_lints.yaml` with Altive recommended lint rule selection.

There are also Altive-made rules by custom_lint.
`altive_lints` also includes Altive-made analysis rules created with the Analyzer Plugin (analysis_server_plugin).

## Table of content

- [Table of content](#table-of-content)
- [Getting started](#getting-started)
- [altive\_lints](#altive_lints)
- [Enable custom\_lint](#enable-custom_lint)
- [Disabling lint rules](#disabling-lint-rules)
- [All custom-lint rules in altive\_lints](#all-custom-lint-rules-in-altive_lints)
- [Disabling lint rules/analysis rules](#disabling-lint-rulesanalysis-rules)
- [Ignoring analysis rules](#ignoring-analysis-rules)
- [All custom analysis rules in altive\_lints](#all-custom-analysis-rules-in-altive_lints)
- [avoid\_consecutive\_sliver\_to\_box\_adapter](#avoid_consecutive_sliver_to_box_adapter)
- [avoid\_hardcoded\_color](#avoid_hardcoded_color)
- [avoid\_hardcoded\_japanese](#avoid_hardcoded_japanese)
- [avoid\_shrink\_wrap\_in\_list\_view](#avoid_shrink_wrap_in_list_view)
- [avoid\_single\_child](#avoid_single_child)
- [prefer\_clock\_now](#prefer_clock_now)
- [prefer\_dedicated\_media\_query\_methods](#prefer_dedicated_media_query_methods)
- [prefer\_to\_include\_sliver\_in\_name](#prefer_to_include_sliver_in_name)
- [prefer\_space\_between\_elements](#prefer_space_between_elements)
- [prefer\_to\_include\_sliver\_in\_name](#prefer_to_include_sliver_in_name)
- [All assists in altive\_lints](#all-assists-in-altive_lints)
- [Add macro template documentation comment](#add-macro-template-documentation-comment)
- [Add macro documentation comment](#add-macro-documentation-comment)
Expand All @@ -46,48 +46,49 @@ If not, create a new one or copy [analysis_options.yaml](https://github.com/alti
include: package:altive_lints/altive_lints.yaml
```

### Enable custom_lint

altive_lints comes bundled with its own rules using custom_lints.

- Add both altive_lints and custom_lint to your `pubspec.yaml`:
```yaml
dev_dependencies:
altive_lints:
custom_lint: # <- add this
```
- Enable `custom_lint`'s plugin in your `analysis_options.yaml`:

```yaml
include: package:altive_lints/altive_lints.yaml
analyzer:
plugins:
- custom_lint
```

### Disabling lint rules
### Disabling lint rules/analysis rules

By default when installing altive_lints, most of the lints will be enabled.
To change this, you have a few options.

```yaml
include: package:altive_lints/altive_lints.yaml
analyzer:
plugins:
- custom_lint

linter:
rules:
# Explicitly disable one lint rule.
- public_member_api_docs: false

custom_lint:
rules:
# Explicitly disable one custom-lint rule.
- avoid_hardcoded_color: false
plugins:
altive_lints: ^2.0.0-dev.1
diagnostics:
# Explicitly disable one analysis rule.
avoid_consecutive_sliver_to_box_adapter: false
avoid_hardcoded_color: false
avoid_hardcoded_japanese: false
avoid_shrink_wrap_in_list_view: false
avoid_single_child: false
prefer_clock_now: false
prefer_dedicated_media_query_methods: false
prefer_space_between_elements: false
prefer_to_include_sliver_in_name: false
```

## All custom-lint rules in altive_lints
### Ignoring analysis rules

You can ignore analysis rules by using `ignore: altive_lints/{rule_name}`.

```dart
// for file.
// ignore_for_file: altive_lints/avoid_hardcoded_color

...

// for line.
// ignore: altive_lints/avoid_hardcoded_color
Color(0xFF00FF00);
```

## All custom analysis rules in altive_lints

### avoid_consecutive_sliver_to_box_adapter

Expand Down Expand Up @@ -247,77 +248,83 @@ This is to reduce unnecessary widget rebuilding and improve performance by using

```dart
var size = MediaQuery.of(context).size; // LINT
var width = MediaQuery.sizeOf(context).width; // LINT
var height = MediaQuery.sizeOf(context).height; // LINT
var padding = MediaQuery.maybeOf(context)?.padding; // LINT
var viewInsets = MediaQuery.viewInsetsOf(context); // LINT
```

**Good**:

```dart
var size = MediaQuery.sizeOf(context);
var padding = MediaQuery.viewInsetsOf(context);
var width = MediaQuery.widthOf(context);
var height = MediaQuery.heightOf(context);
var padding = MediaQuery.paddingOf(context);
var viewInsets = MediaQuery.viewInsetsOf(context);
```

### prefer_to_include_sliver_in_name
### prefer_space_between_elements

Prefer to include β€˜Sliver’ in the class name or named constructor of a widget that returns a Sliver-type widget.
Prefer to insert blank lines for spacing rules in class definitions.
between constructors and fields, and between constructors and build methods.

The purpose of proper spacing is to improve code readability and organization and to make it easier to visually distinguish between different sections of a class.

This makes it easy for the user to know at a glance that it is a Sliver type Widget, and improves readability and consistency.

**Bad**:

```dart
class MyCustomList extends StatelessWidget { // LINT
@override
class MyWidget extends StatelessWidget {
MyWidget(this.title);
final String title; // LINT
@override // LINT
Widget build(BuildContext context) {
return SliverList(...);
return Text(title);
}
}

```

**Good**:

```dart
class SliverMyCustomList extends StatelessWidget {
class MyWidget extends StatelessWidget {
MyWidget(this.title);

final String title;

@override
Widget build(BuildContext context) {
return SliverList(...);
return Text(title);
}
}
```

### prefer_space_between_elements

Prefer to insert blank lines for spacing rules in class definitions.
between constructors and fields, and between constructors and build methods.
### prefer_to_include_sliver_in_name

The purpose of proper spacing is to improve code readability and organization and to make it easier to visually distinguish between different sections of a class.
Prefer to include β€˜Sliver’ in the class name or named constructor of a widget that returns a Sliver-type widget.

This makes it easy for the user to know at a glance that it is a Sliver type Widget, and improves readability and consistency.

**Bad**:

```dart
class MyWidget extends StatelessWidget {
MyWidget(this.title);
final String title; // LINT
@override // LINT
class MyCustomList extends StatelessWidget { // LINT
@override
Widget build(BuildContext context) {
return Text(title);
return SliverList(...);
}
}

```

**Good**:

```dart
class MyWidget extends StatelessWidget {
MyWidget(this.title);

final String title;

class SliverMyCustomList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text(title);
return SliverList(...);
}
}
```
Expand Down Expand Up @@ -356,18 +363,14 @@ When you place the cursor on a constructor or method declaration and execute **"
**Before:**

```dart
void myFunction() {
// Function implementation
}
const myClass();
```

**After applying the assist:**

```dart
/// {@macro my_package.myFunction}
void myFunction() {
// Function implementation
}
const myClass();
```

### Wrap with macro template documentation comment
Expand All @@ -380,9 +383,7 @@ When you select the documentation comment and execute **"Wrap with macro templat
```dart
/// Some comment
/// More comments
class MyClass {
// Class implementation
}
class MyClass {}
```

**After applying the assist:**
Expand All @@ -392,11 +393,11 @@ class MyClass {
/// Some comment
/// More comments
/// {@endtemplate}
class MyClass {
// Class implementation
}
class MyClass {}
```

For class, enum, mixin, and extension type.

## Lint rules adopted by altive_lints and why

Reasons for adopting each lint rule.
Expand Down Expand Up @@ -457,4 +458,4 @@ class DashboardCard extends StatelessWidget {
The [public_member_api_docs](https://dart.dev/tools/linter-rules/public_member_api_docs) prompt to add documents has been activated.
Please add documentation comments to public members.

If there are too many issues, disable them in analysis_options.yaml.
If there are too many issues, disable them in analysis_options.yaml.
9 changes: 4 additions & 5 deletions packages/altive_lints/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
include: lib/altive_lints.yaml
analyzer:
errors:
# Remove once we require analyzer 8.0.0
# We currently support both 7.0 and 8.0, so we can't migrate yet.
deprecated_member_use: ignore
linter:
rules:
# use `test_` prefix in test_reflective_loader
non_constant_identifier_names: false
4 changes: 0 additions & 4 deletions packages/altive_lints/example/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# https://pub.dev/packages/altive_lints
include: package:altive_lints/altive_lints.yaml
analyzer:
plugins:
- custom_lint

linter:
rules:
# Disabled so that you don't have to write a doc since it is an `example`.
- public_member_api_docs: false

47 changes: 47 additions & 0 deletions packages/altive_lints/example/assists.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// This file is for testing the Assists feature of the Analyzer Plugin.

import 'package:flutter/material.dart';

@immutable
class MyClass {
MyClass({required this.myField});

MyClass.emptyName() : myField = '';

final String myField;

void myMethod() {
print('Hello, $myField!');
}
}

void myFunction() {
print('Hello, World!');
}

mixin MyMixin {
void myMixinMethod() {
print('Hello, World!');
}
}

enum MyEnum {
one,
two,
three,
}

extension type MyExtensionType(int value) implements int {
bool get isZero => value == 0;
}

class MyWidget extends StatelessWidget {
const MyWidget({super.key});

@override
Widget build(BuildContext context) {
return Container(
child: Text('Hello, World!'),
);
}
}
Loading