diff --git a/packages/jaspr_lints/lib/src/assists/component_assist.dart b/packages/jaspr_lints/lib/src/assists/component_assist.dart index b2699eab..06574e67 100644 --- a/packages/jaspr_lints/lib/src/assists/component_assist.dart +++ b/packages/jaspr_lints/lib/src/assists/component_assist.dart @@ -66,7 +66,11 @@ class ComponentAssistProvider extends DartAssist { } void createStatelessComponent( - ChangeReporter reporter, SourceRange target, String nameSuggestion, bool hasJasprImport) { + ChangeReporter reporter, + SourceRange target, + String nameSuggestion, + bool hasJasprImport, + ) { reporter.createChangeBuilder(priority: 1, message: 'Create StatelessComponent').addDartFileEdit((builder) { builder.addInsertion(target.end == 0 ? 1 : target.end, (edit) { edit.write('class '); @@ -87,7 +91,11 @@ class ComponentAssistProvider extends DartAssist { } void createStatefulComponent( - ChangeReporter reporter, SourceRange target, String nameSuggestion, bool hasJasprImport) { + ChangeReporter reporter, + SourceRange target, + String nameSuggestion, + bool hasJasprImport, + ) { reporter.createChangeBuilder(priority: 1, message: 'Create StatefulComponent').addDartFileEdit((builder) { builder.addInsertion(target.end == 0 ? 1 : target.end, (edit) { edit.write('class '); @@ -114,7 +122,11 @@ class ComponentAssistProvider extends DartAssist { } void createInheritedComponent( - ChangeReporter reporter, SourceRange target, String nameSuggestion, bool hasJasprImport) { + ChangeReporter reporter, + SourceRange target, + String nameSuggestion, + bool hasJasprImport, + ) { reporter.createChangeBuilder(priority: 1, message: 'Create InheritedComponent').addDartFileEdit((builder) { if (!hasJasprImport) { builder.importLibrary(Uri.parse('package:jaspr/jaspr.dart')); diff --git a/packages/jaspr_lints/lib/src/assists/css_assist.dart b/packages/jaspr_lints/lib/src/assists/css_assist.dart index e787b5d4..fa1499d0 100644 --- a/packages/jaspr_lints/lib/src/assists/css_assist.dart +++ b/packages/jaspr_lints/lib/src/assists/css_assist.dart @@ -2,6 +2,7 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/source/source_range.dart'; import 'package:analyzer_plugin/protocol/protocol_common.dart'; +import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart'; import 'package:custom_lint_builder/custom_lint_builder.dart'; import '../utils.dart'; @@ -78,44 +79,36 @@ class CssAssistProvider extends DartAssist { .whereType() .firstOrNull; - final cb = reporter.createChangeBuilder( - priority: 1, - message: 'Add styles', - ); - cb.addDartFileEdit((builder) { + reporter.createChangeBuilder(priority: 1, message: 'Add styles').addDartFileEdit((builder) { + void writeCssRule(DartEditBuilder edit) { + edit.write(' css(\''); + if (idVal != null) { + edit.write('#$idVal\').'); + } else if (classesVal != null) { + edit.write('.$classesVal\').'); + } else { + edit.write('.'); + edit.addSimpleLinkedEdit('className', 'classname'); + edit.write('\').'); + } + edit.addSimpleLinkedEdit( + 'styles', + 'box', + kind: LinkedEditSuggestionKind.METHOD, + suggestions: ['box', 'text', 'background', 'flexbox', 'flexItem', 'grid', 'gridItem', 'list'], + ); + edit.write('(),\n '); + } + if (styles == null) { builder.addInsertion(comp.$2.end, (edit) { - edit.write('\n\n @css\n static final List styles = [\n css(\''); - if (idVal != null) { - edit.write('#$idVal\').'); - } else if (classesVal != null) { - edit.write('.$classesVal\').'); - } else { - edit.write('.'); - edit.addSimpleLinkedEdit('className', 'classname'); - edit.write('\').'); - } - edit.addSimpleLinkedEdit('styles', 'box', - kind: LinkedEditSuggestionKind.METHOD, - suggestions: ['box', 'text', 'background', 'flexbox', 'flexItem', 'grid', 'gridItem', 'list']); - edit.write('(),\n ];'); + edit.write('\n\n @css\n static final List styles = [\n '); + writeCssRule(edit); + edit.write('];'); }); } else { builder.addInsertion(styles.rightBracket.offset, (edit) { - edit.write(' css(\''); - if (idVal != null) { - edit.write('#$idVal\').'); - } else if (classesVal != null) { - edit.write('.$classesVal\').'); - } else { - edit.write('.'); - edit.addSimpleLinkedEdit('className', 'classname'); - edit.write('\').'); - } - edit.addSimpleLinkedEdit('styles', 'box', - kind: LinkedEditSuggestionKind.METHOD, - suggestions: ['box', 'text', 'background', 'flexbox', 'flexItem', 'grid', 'gridItem', 'list']); - edit.write('(),\n '); + writeCssRule(edit); }); } @@ -141,7 +134,11 @@ class CssAssistProvider extends DartAssist { } void convertToNested( - CustomLintResolver resolver, ChangeReporter reporter, FunctionExpressionInvocation node, int lineIndent) { + CustomLintResolver resolver, + ChangeReporter reporter, + FunctionExpressionInvocation node, + int lineIndent, + ) { if (node.argumentList.arguments.length != 1) { return; } @@ -149,12 +146,7 @@ class CssAssistProvider extends DartAssist { var selector = node.argumentList.arguments.first; var chain = getFullChain(node.parent); - final cb = reporter.createChangeBuilder( - priority: 1, - message: 'Convert to nested styles', - ); - - cb.addDartFileEdit((builder) { + reporter.createChangeBuilder(priority: 1, message: 'Convert to nested styles').addDartFileEdit((builder) { builder.addInsertion(selector.end, (edit) { edit.write(', [\n${''.padLeft(lineIndent)} css(\'&\''); }); diff --git a/packages/jaspr_lints/lib/src/assists/tree_assist.dart b/packages/jaspr_lints/lib/src/assists/tree_assist.dart index 319b7c2c..2c19e8f2 100644 --- a/packages/jaspr_lints/lib/src/assists/tree_assist.dart +++ b/packages/jaspr_lints/lib/src/assists/tree_assist.dart @@ -13,8 +13,8 @@ class TreeAssistProvider extends DartAssist { CustomLintContext context, SourceRange target, ) { - context.registry.addInvocationExpression((node) { - if (!target.coveredBy(node.function.sourceRange)) { + void treeAssistsFor(Expression node, AstNode anchor, ArgumentList argumentList) { + if (!target.coveredBy(anchor.sourceRange)) { return; } if (!isComponentType(node.staticType)) { @@ -22,20 +22,15 @@ class TreeAssistProvider extends DartAssist { } final indent = getLineIndent(resolver.lineInfo, node); wrapComponent(resolver, reporter, node, indent); - removeComponent(resolver, reporter, node, indent, node.argumentList); + removeComponent(resolver, reporter, node, indent, argumentList); extractComponent(resolver, reporter, node, indent); + } + + context.registry.addInvocationExpression((node) { + treeAssistsFor(node, node.function, node.argumentList); }); context.registry.addInstanceCreationExpression((node) { - if (!target.coveredBy(node.constructorName.sourceRange)) { - return; - } - if (!isComponentType(node.staticType)) { - return; - } - final indent = getLineIndent(resolver.lineInfo, node); - wrapComponent(resolver, reporter, node, indent); - removeComponent(resolver, reporter, node, indent, node.argumentList); - extractComponent(resolver, reporter, node, indent); + treeAssistsFor(node, node.constructorName, node.argumentList); }); } @@ -47,16 +42,15 @@ class TreeAssistProvider extends DartAssist { '([\n${''.padLeft(lineIndent)}${lines.map((s) => ' $s').join('\n')},\n${''.padLeft(lineIndent)}])'; void wrapWith(String name, [List? suggestions]) { - final cb = reporter.createChangeBuilder( - priority: 2, - message: 'Wrap with $name', - ); - - cb.addDartFileEdit((builder) { + reporter.createChangeBuilder(priority: 2, message: 'Wrap with $name').addDartFileEdit((builder) { builder.addReplacement(node.sourceRange, (edit) { if (suggestions != null) { - edit.addSimpleLinkedEdit(name, suggestions.first, - kind: LinkedEditSuggestionKind.METHOD, suggestions: suggestions); + edit.addSimpleLinkedEdit( + name, + suggestions.first, + kind: LinkedEditSuggestionKind.METHOD, + suggestions: suggestions, + ); } else { edit.write(name); } @@ -95,8 +89,13 @@ class TreeAssistProvider extends DartAssist { }); } - void removeComponent(CustomLintResolver resolver, ChangeReporter reporter, Expression node, int lineIndent, - ArgumentList argumentList) { + void removeComponent( + CustomLintResolver resolver, + ChangeReporter reporter, + Expression node, + int lineIndent, + ArgumentList argumentList, + ) { var children = [ for (var arg in argumentList.arguments) if (arg is NamedExpression) @@ -165,12 +164,7 @@ class TreeAssistProvider extends DartAssist { var content = resolver.source.contents.data; var source = content.substring(node.offset, node.end).reIndent(4 - lineIndent, skipFirst: true); - final cb = reporter.createChangeBuilder( - priority: 4, - message: 'Extract component', - ); - - cb.addDartFileEdit((builder) { + reporter.createChangeBuilder(priority: 4, message: 'Extract component').addDartFileEdit((builder) { builder.addInsertion(resolver.source.contents.data.length, (edit) { edit.write('\nclass '); edit.addSimpleLinkedEdit('name', 'MyComponent');