Skip to content

Commit 0bbf8d2

Browse files
Add lint usage examples (#505)
1 parent 974f7e5 commit 0bbf8d2

File tree

13 files changed

+284
-4
lines changed

13 files changed

+284
-4
lines changed

packages/leancode_lint/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 20.0.1
2+
3+
- Add the `example/` directory with default and custom configuration examples.
4+
15
# 20.0.0
26

37
- **Breaking:** Migrate from custom_lint to the analysis server plugin.

packages/leancode_lint/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ analyzer:
5050
- '**/*.g.dart'
5151
```
5252
53+
> [!TIP]
54+
> See the [example/](./example) directory for complete, runnable examples
55+
> with both default and custom configuration.
56+
5357
## Configuration (custom plugin package)
5458
5559
This section is optional. You only need it if you want to customize the
@@ -581,9 +585,9 @@ Assists are IDE refactorings not related to a particular issue. They can be trig
581585

582586
See linked source code containing explanation in dart doc.
583587

584-
- [Convert positional to named formal](./lib/assists/convert_positional_to_named_formal.dart)
585-
- [Convert record into nominal type](./lib/assists/convert_record_into_nominal_type.dart)
586-
- [Convert iterable map to collection-for](./lib/assists/convert_iterable_map_to_collection_for.dart)
588+
- [Convert positional to named formal](./lib/src/assists/convert_positional_to_named_formal.dart)
589+
- [Convert record into nominal type](./lib/src/assists/convert_record_into_nominal_type.dart)
590+
- [Convert iterable map to collection-for](./lib/src/assists/convert_iterable_map_to_collection_for.dart)
587591

588592
[pub-badge]: https://img.shields.io/pub/v/leancode_lint
589593
[pub-badge-link]: https://pub.dev/packages/leancode_lint
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include: package:leancode_lint/analysis_options.yaml
2+
3+
plugins:
4+
my_lints:
5+
path: ../my_lints
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'package:flutter/material.dart';
2+
3+
/// Design system text widget.
4+
class AppText extends StatelessWidget {
5+
const AppText(this.data, {super.key});
6+
7+
final String data;
8+
9+
@override
10+
Widget build(BuildContext context) {
11+
// When ignoring custom lints, use the prefix with the package name.
12+
// If you provide a custom name to the `LeanCodeLintPlugin`, use it here:
13+
// ignore: my_lints/use_design_system_item_AppText
14+
return Text(data);
15+
}
16+
}
17+
18+
/// Design system scaffold widget.
19+
class AppScaffold extends StatelessWidget {
20+
const AppScaffold({super.key, this.appBar, required this.body});
21+
22+
final PreferredSizeWidget? appBar;
23+
final Widget body;
24+
25+
@override
26+
Widget build(BuildContext context) {
27+
// Ignores are case-insensitive:
28+
// ignore: my_lints/use_design_system_item_appscaffold
29+
return Scaffold(appBar: appBar, body: body);
30+
}
31+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import 'package:flutter/material.dart';
2+
3+
import 'design_system.dart';
4+
5+
void main() {
6+
runApp(const CustomExampleApp());
7+
}
8+
9+
Future<void> fetchData() async {
10+
try {
11+
await Future<void>.delayed(const Duration(seconds: 1));
12+
} catch (error, stackTrace) {
13+
debugPrint('$error\n$stackTrace');
14+
}
15+
}
16+
17+
class CustomExampleApp extends StatelessWidget {
18+
const CustomExampleApp({super.key});
19+
20+
@override
21+
Widget build(BuildContext context) {
22+
return MaterialApp(
23+
title: 'Custom Config Example',
24+
theme: ThemeData(colorScheme: .fromSeed(seedColor: Colors.green)),
25+
home: const CustomExampleHome(),
26+
);
27+
}
28+
}
29+
30+
class CustomExampleHome extends StatelessWidget {
31+
const CustomExampleHome({super.key});
32+
33+
@override
34+
Widget build(BuildContext context) {
35+
return AppScaffold(
36+
appBar: AppBar(title: const AppText('leancode_lint custom config')),
37+
body: const Center(
38+
child: ElevatedButton(
39+
onPressed: fetchData,
40+
child: AppText('Trigger try-catch'),
41+
),
42+
),
43+
);
44+
}
45+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: custom_example_app
2+
description: Example app using leancode_lint with custom configuration.
3+
publish_to: none
4+
5+
environment:
6+
sdk: '>=3.10.0 <4.0.0'
7+
8+
dependency_overrides:
9+
# Flutter pins meta to 1.17.0, but the plugin packages require 1.18.0
10+
meta: ^1.18.0
11+
12+
dependencies:
13+
flutter:
14+
sdk: flutter
15+
16+
dev_dependencies:
17+
flutter_test:
18+
sdk: flutter
19+
my_lints:
20+
path: ../my_lints
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'package:leancode_lint/plugin.dart';
2+
3+
/// Custom plugin with project-specific configuration.
4+
///
5+
/// - Uses `error` and `stackTrace` for catch parameters
6+
/// - Configures design system replacements (e.g. use AppText instead of Text)
7+
final plugin = LeanCodeLintPlugin(
8+
name: 'my_lints',
9+
config: const LeanCodeLintConfig(
10+
catchParameterNames: CatchParameterNamesConfig(
11+
exception: 'error',
12+
stackTrace: 'stackTrace',
13+
),
14+
designSystemItemReplacements: {
15+
'AppText': [
16+
DesignSystemForbiddenItem(name: 'Text', packageName: 'flutter'),
17+
DesignSystemForbiddenItem(name: 'RichText', packageName: 'flutter'),
18+
],
19+
'AppScaffold': [
20+
DesignSystemForbiddenItem(name: 'Scaffold', packageName: 'flutter'),
21+
],
22+
},
23+
),
24+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: my_lints
2+
description: Custom analyzer plugin with leancode_lint configuration.
3+
publish_to: none
4+
5+
environment:
6+
sdk: '>=3.10.0 <4.0.0'
7+
8+
dependencies:
9+
leancode_lint:
10+
path: ../../..
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include: package:leancode_lint/analysis_options.yaml
2+
3+
plugins:
4+
leancode_lint:
5+
path: ../..
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import 'package:flutter/material.dart';
2+
3+
void main() {
4+
runApp(const DefaultExampleApp());
5+
}
6+
7+
Future<void> fetchData() async {
8+
try {
9+
await Future<void>.delayed(const Duration(seconds: 1));
10+
} catch (err, st) {
11+
debugPrint('$err\n$st');
12+
}
13+
}
14+
15+
class DefaultExampleApp extends StatelessWidget {
16+
const DefaultExampleApp({super.key});
17+
18+
@override
19+
Widget build(BuildContext context) {
20+
return MaterialApp(
21+
title: 'Default Config Example',
22+
theme: ThemeData(colorScheme: .fromSeed(seedColor: Colors.blue)),
23+
home: const DefaultExampleHome(),
24+
);
25+
}
26+
}
27+
28+
class DefaultExampleHome extends StatelessWidget {
29+
const DefaultExampleHome({super.key});
30+
31+
@override
32+
Widget build(BuildContext context) {
33+
return Scaffold(
34+
appBar: AppBar(title: const Text('leancode_lint default config')),
35+
body: const Center(
36+
child: ElevatedButton(
37+
onPressed: fetchData,
38+
child: Text('Trigger try-catch'),
39+
),
40+
),
41+
);
42+
}
43+
}

0 commit comments

Comments
 (0)