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

Commit

Permalink
feat: ConvertToOtherButton
Browse files Browse the repository at this point in the history
  • Loading branch information
K9i-0 committed May 3, 2023
1 parent 0999c31 commit 68edebc
Show file tree
Hide file tree
Showing 16 changed files with 356 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"flutterSdkVersion": "3.7.12",
"flavors": {}
}
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/
build/

# fvm
.fvm/flutter_sdk

# example
packages/material_button_assist/example/web
2 changes: 2 additions & 0 deletions melos.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: material_widget_lint
repository: https://github.com/K9i-0/material_widget_lint

sdkPath: .fvm/flutter_sdk

packages:
- packages/**

Expand Down
3 changes: 3 additions & 0 deletions packages/material_button_assist/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
21 changes: 21 additions & 0 deletions packages/material_button_assist/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 K9i

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 39 additions & 0 deletions packages/material_button_assist/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.

## Usage

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.

```dart
const like = 'sample';
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
36 changes: 36 additions & 0 deletions packages/material_button_assist/example/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled.

version:
revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
channel: stable

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
- platform: android
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
- platform: ios
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
- platform: web
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
3 changes: 3 additions & 0 deletions packages/material_button_assist/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# example

A new Flutter project.
4 changes: 4 additions & 0 deletions packages/material_button_assist/example/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml
analyzer:
plugins:
- custom_lint
47 changes: 47 additions & 0 deletions packages/material_button_assist/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:flutter/material.dart';

void main() {
runApp(const MainApp());
}

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

@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
useMaterial3: true,
),
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {},
child: const Text('Hello World!'),
),
FilledButton.tonal(
onPressed: () {},
child: const Text('Hello World!'),
),
FilledButton.tonal(
onPressed: () {},
child: const Text('Hello World!'),
),
OutlinedButton(
onPressed: () {},
child: const Text('Hello World!'),
),
TextButton(
onPressed: () {},
child: const Text('Hello World!'),
),
],
),
),
),
);
}
}
22 changes: 22 additions & 0 deletions packages/material_button_assist/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: example
description: A new Flutter project.
publish_to: 'none'
version: 0.1.0

environment:
sdk: '>=2.19.6 <3.0.0'

dependencies:
flutter:
sdk: flutter

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.1
custom_lint: ^0.3.4
material_button_assist:
path: ../

flutter:
uses-material-design: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# melos_managed_dependency_overrides: material_button_assist
dependency_overrides:
material_button_assist:
path: ..
18 changes: 18 additions & 0 deletions packages/material_button_assist/lib/material_button_assist.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
library material_button_assist;

import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:material_button_assist/src/assists/convert_to_other_button.dart';

PluginBase createPlugin() => _MaterialButtonAssist();

class _MaterialButtonAssist extends PluginBase {
@override
List<LintRule> getLintRules(CustomLintConfigs configs) => const [];

@override
List<Assist> getAssists() => [
...MaterialButtonType.values
.map((buttonType) => ConvertToOtherButton(targetType: buttonType))
.toList(),
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import 'package:analyzer/source/source_range.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';

const convertToOtherButtonPriority = 26;

enum MaterialButtonType {
elevated(
buttonName: 'ElevatedButton',
constructorName: 'ElevatedButton',
priority: convertToOtherButtonPriority,
typeChecker: TypeChecker.fromName(
'ElevatedButton',
packageName: 'flutter',
),
),
filled(
buttonName: 'FilledButton',
constructorName: 'FilledButton',
priority: convertToOtherButtonPriority,
typeChecker: TypeChecker.fromName(
'FilledButton',
packageName: 'flutter',
),
),
filledTonal(
buttonName: 'FilledTonalButton',
constructorName: 'FilledButton.tonal',
priority: convertToOtherButtonPriority,
typeChecker: TypeChecker.fromName(
'FilledButton',
packageName: 'flutter',
),
),
outlined(
buttonName: 'OutlinedButton',
constructorName: 'OutlinedButton',
priority: convertToOtherButtonPriority,
typeChecker: TypeChecker.fromName(
'OutlinedButton',
packageName: 'flutter',
),
),
text(
buttonName: 'TextButton',
constructorName: 'TextButton',
priority: convertToOtherButtonPriority,
typeChecker: TypeChecker.fromName(
'TextButton',
packageName: 'flutter',
),
);

const MaterialButtonType({
required this.buttonName,
required this.constructorName,
required this.priority,
required this.typeChecker,
});
final String buttonName;
final String constructorName;
final int priority;
final TypeChecker typeChecker;
}

TypeChecker getBaseType({
required MaterialButtonType? exclude,
}) {
return TypeChecker.any(
MaterialButtonType.values
.where((e) => e != exclude)
.map((e) => e.typeChecker),
);
}

final filledButtonType = TypeChecker.any([
MaterialButtonType.filled.typeChecker,
MaterialButtonType.filledTonal.typeChecker,
]);

class ConvertToOtherButton extends DartAssist {
ConvertToOtherButton({
required this.targetType,
});
final MaterialButtonType targetType;
late final baseType = getBaseType(
exclude: targetType != MaterialButtonType.filled ? targetType : null,
);

@override
void run(
CustomLintResolver resolver,
ChangeReporter reporter,
CustomLintContext context,
SourceRange target,
) {
context.registry.addInstanceCreationExpression((node) {
// Select from "new" to the opening bracket
if (!target.intersects(node.constructorName.sourceRange)) return;

final createdType = node.constructorName.type.type;
if (createdType == null || !baseType.isExactlyType(createdType)) {
return;
}

final simpleIdentifier = node.constructorName.name;
final isFilledButton = filledButtonType.isExactlyType(createdType);
final isTonal =
simpleIdentifier != null && simpleIdentifier.name == 'tonal';

if (isFilledButton) {
if (isTonal) {
if (targetType == MaterialButtonType.filledTonal) return;
} else {
if (targetType == MaterialButtonType.filled) return;
}
}

final changeBuilder = reporter.createChangeBuilder(
message: 'Convert to ${targetType.buttonName}',
priority: targetType.priority,
);

changeBuilder.addDartFileEdit(
(builder) {
builder.addSimpleReplacement(
node.constructorName.sourceRange,
targetType.constructorName,
);
},
);
});
}
}
11 changes: 11 additions & 0 deletions packages/material_button_assist/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: material_button_assist
description: A starting point for Dart libraries or applications.
version: 1.0.0
repository: https://github.com/K9i-0/material_widget_lint

environment:
sdk: ">=2.17.0 <3.0.0"

dependencies:
custom_lint_builder: ^0.3.4
analyzer: ^5.7.0
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: material_widget_lint_workspace
publish_to: none

environment:
sdk: ">=2.18.0 <3.0.0"
sdk: ">=2.17.0 <3.0.0"

dev_dependencies:
melos: ^3.0.1
flutter_lints: ^2.0.1

0 comments on commit 68edebc

Please sign in to comment.