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 7 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,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.
59 changes: 59 additions & 0 deletions packages/uni_lint/lib/uni_lint.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:analyzer/dart/ast/ast.dart';

PluginBase createPlugin() => _ExampleLinter();

class _ExampleLinter extends PluginBase {
Pinho13 marked this conversation as resolved.
Show resolved Hide resolved
@override
List<LintRule> getLintRules(CustomLintConfigs configs) => [
StringLiteralsLint(),
];
}

class StringLiteralsLint extends DartLintRule {
Pinho13 marked this conversation as resolved.
Show resolved Hide resolved
StringLiteralsLint() : 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: 'There is a string literal inside a widget',
Pinho13 marked this conversation as resolved.
Show resolved Hide resolved
);

@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
) {
context.registry.addVariableDeclaration((node) {
Pinho13 marked this conversation as resolved.
Show resolved Hide resolved
if (isInsideWidgetClass(node)) {
if (isStringLiteral(node)) {
reporter.atNode(node, code);
}
}
});
}

bool isInsideWidgetClass(VariableDeclaration 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");
return true;
// return superclass.runtimeType == StatelessWidget ||
// superclass.runtimeType == StatefulWidget;
Pinho13 marked this conversation as resolved.
Show resolved Hide resolved
}
return false;
}

bool isStringLiteral(VariableDeclaration node) {
final initializer = node.initializer;
if (initializer is StringLiteral) {
return true;
}
return false;
}
}
Loading
Loading