-
Notifications
You must be signed in to change notification settings - Fork 19
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
base: develop
Are you sure you want to change the base?
Changes from all commits
1f9da2d
ae06cd2
001a5c3
9142c2d
4188e4f
c66638d
7dc30f5
efd0fbb
de4c176
fb10bc8
607cca4
928ca16
28bd55c
b954bac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
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', | ||
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") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. String literals inside functions inside widgets will also trigger the lint. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really, I might want to do something like |
||
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; | ||
} | ||
} |
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 |
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.