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 14 commits into
base: develop
Choose a base branch
from
Open
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
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.
48 changes: 48 additions & 0 deletions packages/uni_lint/lib/uni_lint.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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) {
final fileUri = node.thisOrAncestorOfType<CompilationUnit>()?.declaredElement?.source?.uri;
final fileName = fileUri?.pathSegments.last;
if (isInsideWidgetClass(node) && fileName != "main.dart") {
Copy link
Member

Choose a reason for hiding this comment

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

String literals inside functions inside widgets will also trigger the lint.
So the suggestion is incomplete and there is still some more code missing. What is the AST node responsible for the construction of a widget? (what node corresponds to Text("hi")?) Because then we might be able to target that node type instead.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess string literals in functions should be avoided as well, right? We can follow the same method, passing them as parameters

Copy link
Member

Choose a reason for hiding this comment

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

Not really, I might want to do something like Navigator.push('/...'), which would incorrectly generate a warning with this lint...
I'm trying to think of easy ways to fix this, but I think the only way would be doing a check if the function call returns a widget or something like that...

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;
return superclass.element?.displayName == "StatelessWidget" || superclass.element?.displayName == "StatefulWidget" || superclass.element?.displayName == "State";
}
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
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
- string_literals_lint
- no_string_literals_in_widgets

Loading
Loading