Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/custom uni UI lint #1400

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ build/
*.bin
.flutter-plugins*
packages/uni_ui/pubspec.lock
packages/uni_lint/pubspec.lock

# IDE files
.DS_Store
Expand All @@ -19,3 +20,6 @@ packages/uni_ui/pubspec.lock
# Key properties
uni/android/key.properties
**.jks

# Custom Lint log
custom_lint.log
7 changes: 7 additions & 0 deletions packages/uni_lint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# uni_lint

This package contains custom lint rules for uni development.

Try to keep dependencies to the minimum. This package, ideally,
should not have any dependencies, except for the analyzer package
and custom_lint_builder.
58 changes: 58 additions & 0 deletions packages/uni_lint/lib/uni_lint.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:analyzer/dart/ast/ast.dart';

PluginBase createPlugin() => UniUILint();

class UniUILint extends PluginBase {
@override
List<LintRule> getLintRules(CustomLintConfigs configs) => [
NoStringLiteralsInWidgetsLint(),
];
}

class NoStringLiteralsInWidgetsLint extends DartLintRule {
NoStringLiteralsInWidgetsLint() : super(code: _code);

static const _code = LintCode(
name: 'string_literals_lint',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: 'string_literals_lint',
name: 'no_string_literals_in_widgets',

problemMessage: 'String literals are not allowed inside a widget. Please pass this value as a parameter for the widget.',
);

@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
) {
context.registry.addStringLiteral((node) {
if (isInsideWidgetClass(node) && node.parent is VariableDeclaration) {
reporter.atNode(node, code);
//if (isStringLiteral(node)) {
// reporter.atNode(node, code);
//}
}
});
}

bool isInsideWidgetClass(StringLiteral node) {
var parent = node.thisOrAncestorOfType<ClassDeclaration>();
if (parent == null) return false;

final extendsClause = parent.extendsClause;
if (extendsClause != null) {
final superclass = extendsClause.superclass;
print(superclass.element?.displayName == "StatelessWidget" || superclass.element?.displayName == "StatefulWidget");
return superclass.element?.displayName == "StatelessWidget" || superclass.element?.displayName == "StatefulWidget";
}
return false;
}

bool isStringLiteral(VariableDeclaration node) {
final initializer = node.initializer;
if (initializer is StringLiteral) {
return true;
}
return false;
}
}
18 changes: 18 additions & 0 deletions packages/uni_lint/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: uni_lint
description: Custom lint rules for Uni.
publish_to: none
version: 1.0.0

environment:
sdk: ">=3.4.0 <4.0.0"
# flutter: 3.24.3

dependencies:
custom_lint_builder: ^0.6.8
analyzer: ^6.7.0
analyzer_plugin: ^0.11.3
# flutter:
# sdk: flutter

dev_dependencies:
test: any
8 changes: 8 additions & 0 deletions packages/uni_ui/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
analyzer:
plugins:
- custom_lint

custom_lint:
debug: true
rules:
- string_literals_lint
Pinho13 marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading