Skip to content

Commit

Permalink
apply review
Browse files Browse the repository at this point in the history
  • Loading branch information
Kilian Schulte committed Sep 12, 2024
1 parent fc41946 commit 57bf532
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 72 deletions.
18 changes: 15 additions & 3 deletions packages/jaspr_lints/lib/src/assists/component_assist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 ');
Expand All @@ -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 ');
Expand All @@ -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'));
Expand Down
72 changes: 32 additions & 40 deletions packages/jaspr_lints/lib/src/assists/css_assist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -78,44 +79,36 @@ class CssAssistProvider extends DartAssist {
.whereType<ListLiteral>()
.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<StyleRule> 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<StyleRule> 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);
});
}

Expand All @@ -141,20 +134,19 @@ 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;
}

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(\'&\'');
});
Expand Down
52 changes: 23 additions & 29 deletions packages/jaspr_lints/lib/src/assists/tree_assist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,24 @@ 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)) {
return;
}
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);
});
}

Expand All @@ -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<String>? 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);
}
Expand Down Expand Up @@ -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 = <AstNode>[
for (var arg in argumentList.arguments)
if (arg is NamedExpression)
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 57bf532

Please sign in to comment.