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

Distinguish between signal reads and creations for build-method access lint #306

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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: 2 additions & 2 deletions packages/signals_lint/example/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ linter:
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
Expand Down
17 changes: 17 additions & 0 deletions packages/signals_lint/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:example/newfile.dart';
import 'package:flutter/material.dart';
import 'package:signals/signals_flutter.dart';

Expand Down Expand Up @@ -25,12 +26,28 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> {
late final counter = createSignal(context, 1);

Signal field() => counter;
Signal get sameFileGetter => Signal(1);
var abc = Abc().externalGetter;

@override
Widget build(BuildContext context) {
var counter3 = Counter(1);
final counterX = () => sameFileGetter;
final counter2 = sameFileGetter;
final third = counter3.y;
final other = Counter(2).y;
final nun = counter3.externalGetter;
final n = Signal(1);
return Text('Count: $counter');
}
}

class Counter extends ValueNotifier<int> {
Counter(super.value);

final x = Signal(4);
final y = Counter(1).x;

Signal get externalGetter => Signal(1);
}
9 changes: 9 additions & 0 deletions packages/signals_lint/example/lib/newfile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:example/main.dart';
import 'package:signals/signals.dart';

class Abc {
final x = Signal(4);
final y = Counter(1).x;

Signal get externalGetter => Signal(1);
}
1 change: 1 addition & 0 deletions packages/signals_lint/lib/signals_lint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
library signals_lint;

import 'package:custom_lint_builder/custom_lint_builder.dart';

import 'src/fixes/wrap_with_watch.dart';
import 'src/lints/avoid_create_in_build_method.dart';

Expand Down
132 changes: 38 additions & 94 deletions packages/signals_lint/lib/src/lints/avoid_create_in_build_method.dart
Original file line number Diff line number Diff line change
@@ -1,116 +1,60 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:signals_lint/src/lints/definitions.dart';
import 'package:signals_lint/src/lints/extensions.dart';

const TypeChecker buildContextType = TypeChecker.fromName(
'BuildContext',
packageName: 'flutter',
);

const buildMethod = 'build';

const _errorMessage = '''
Signals should not be created in the build method because will
create a new signal every time the element is rebuilt.
''';

const _correctionMessage = '''
Create the new signals outside the build() method in the class or globally.

For StatelessWidgets you can define the signals as a static variable, pass in
from the constructor or declare globally.

```diff
+ final counter = signal(0);
...
@override
Widget build(BuildContext context) {
- final counter = signal(0);
return ...;
}
```
''';

class SignalsAvoidCreateInBuildMethod extends DartLintRule {
const SignalsAvoidCreateInBuildMethod() : super(code: _code);

static const _code = LintCode(
name: 'signals_avoid_create_in_build_method',
problemMessage: _errorMessage,
correctionMessage: _correctionMessage,
errorSeverity: ErrorSeverity.WARNING,
);
const SignalsAvoidCreateInBuildMethod() : super(code: lintCode);

@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
) {
const types = [
TypeChecker.fromName(
'Signal',
packageName: 'signals_core',
),
TypeChecker.fromName(
'Computed',
packageName: 'signals_core',
),
TypeChecker.fromName(
'SignalValueNotifier',
packageName: 'signals_flutter',
),
TypeChecker.fromName(
'SignalValueListenable',
packageName: 'signals_flutter',
),
];

context.registry.addVariableDeclaration((node) {
final element = node.declaredElement;
if (element == null) return;

final ancestor = node.thisOrAncestorMatching((method) {
final isMethod =
method is MethodDeclaration && method.name.lexeme == buildMethod;
if (!isMethod) return false;

if (_findStateClass(node) != null) {
return types.any((e) => e.isAssignableFromType(element.type));
context.registry.addExpression(
(node) {
if (node.staticType?.isSignal() != true) return;

final hasMatch = node.hierarchyMatches((node) {
if (node is! Expression || node.staticType?.isSignal() != true) {
return false;
}
if (node is InstanceCreationExpression &&
node.isWidgetBuildMember()) {
return true;
}
final checkCreatesSignal = switch (node) {
PropertyAccess(:final realTarget) => realTarget,
Identifier() => node,
InvocationExpression(:final function) => function,
_ => null,
};
return checkCreatesSignal != null &&
node.isWidgetBuildMember() &&
createsSignal(checkCreatesSignal);
});

if (hasMatch) {
reporter.atNode(node, code);
}
},
);
}

return false;
});

if (ancestor != null) {
reporter.reportErrorForElement(code, element);
}
bool createsSignal(Expression expression) {
return expression.hierarchyMatches((node) {
return (node is InstanceCreationExpression &&
node.thisOrAncestorOfType<VariableDeclaration>() != null) ||
node.isIdentifier() ||
node.createsSignal() ||
(node is FunctionExpression && node.body.createsSignal());
});
}
}

const TypeChecker stateClass = TypeChecker.fromName(
'State',
packageName: 'flutter',
);

const TypeChecker statelessClass = TypeChecker.fromName(
'StatelessWidget',
packageName: 'flutter',
);

AstNode? _findStateClass(AstNode node) {
return node.parent?.thisOrAncestorMatching((node) {
if (node is! ClassDeclaration) return false;

/// Looking for the class which is a [ConsumerState]
final extendsClause = node.extendsClause;
if (extendsClause == null) return false;
final extendsType = extendsClause.superclass.type;
if (extendsType == null) return false;

return stateClass.isExactlyType(extendsType) ||
statelessClass.isExactlyType(extendsType);
});
}
57 changes: 57 additions & 0 deletions packages/signals_lint/lib/src/lints/definitions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:analyzer/error/error.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';

const lintCode = LintCode(
name: 'signals_avoid_create_in_build_method',
problemMessage: _errorMessage,
correctionMessage: _correctionMessage,
errorSeverity: ErrorSeverity.WARNING,
);

const _errorMessage = '''
This expression causes a new Signal instance to be created on every build,
instead of accessing an existing one.
''';

const _correctionMessage = '''
Create the new signals outside the build() method in the class or globally.

For StatelessWidgets you can define the signals as a static variable, pass in
from the constructor or declare globally.

```diff
+ final counter = signal(0);
...
@override
Widget build(BuildContext context) {
- final counter = signal(0);
return ...;
}
```
''';

const signalTypes = [
TypeChecker.fromName(
'Signal',
packageName: 'signals_core',
),
TypeChecker.fromName(
'Computed',
packageName: 'signals_core',
),
TypeChecker.fromName(
'SignalValueNotifier',
packageName: 'signals_flutter',
),
TypeChecker.fromName(
'SignalValueListenable',
packageName: 'signals_flutter',
),
];

const widgetClasses = [
TypeChecker.fromName('State', packageName: 'flutter'),
TypeChecker.fromName('StatelessWidget', packageName: 'flutter'),
];

const buildMethodName = 'build';
76 changes: 76 additions & 0 deletions packages/signals_lint/lib/src/lints/extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:signals_lint/src/lints/definitions.dart';

extension AstNodeX on AstNode {
bool createsSignal() {
if (this case Identifier(staticElement: final ExecutableElement element)) {
if (element case PropertyAccessorElement(isSynthetic: false)) {
return element.returnType.isSignal();
}
}
return false;
}

bool isIdentifier() {
final candidate = switch (this) {
PrefixedIdentifier(:final prefix) => prefix.staticElement,
Identifier(:final staticElement) => staticElement,
_ => null,
};
return candidate?.declaration?.calledInBuild() ?? false;
}

bool hierarchyMatches(bool Function(AstNode node) predicate) =>
thisOrAncestorMatching(predicate) != null;

bool isWidgetBuildMember() => _isWidgetMember && _isBuildMember;

bool get _isBuildMember => hierarchyMatches(
(node) =>
node is MethodDeclaration && node.name.lexeme == buildMethodName,
);

bool get _isWidgetMember {
return parent != null &&
parent!.hierarchyMatches((node) {
if (node
case ClassDeclaration(
extendsClause: ExtendsClause(
superclass: NamedType(:final DartType type)
)
)) {
return type.isTypeWidgetClass();
}
return false;
});
}
}

extension TypeX on DartType {
bool isSignal() =>
signalTypes.any((checker) => checker.isAssignableFromType(this));

bool isTypeWidgetClass() =>
widgetClasses.any((checker) => checker.isAssignableFromType(this));
}

extension ElementX on Element {
bool calledInBuild() {
final ancestor = thisOrAncestorMatching(
(element) {
if (element case MethodElement(name: buildMethodName)) {
final widgetParent = element.enclosingElement.thisOrAncestorMatching(
(element) => element._isElementWidgetClass());
return widgetParent != null;
}
return false;
},
);
return ancestor != null;
}

bool _isElementWidgetClass() =>
widgetClasses.any((checker) => checker.isAssignableFrom(this));
}
Loading