Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-romanenko authored Dec 8, 2023
2 parents ee12bf4 + fd5e4c9 commit f9cc496
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 8 deletions.
5 changes: 5 additions & 0 deletions packages/custom_lint_core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased patch

Added an optional `customPath` to the various `ChangeReporter` methods (thanks to @laurentschall)

## 0.5.7 - 2023-11-20

- `custom_lint` upgraded to `0.5.7`
Expand Down Expand Up @@ -50,6 +54,7 @@ Support analyzer v6
- `custom_lint` upgraded to `0.3.2`

## 0.3.1 - 2023-03-09

Update dependencies

## 0.3.0 - 2023-03-09
Expand Down
34 changes: 26 additions & 8 deletions packages/custom_lint_core/lib/src/change_reporter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ abstract class ChangeBuilder {
///
/// The builder passed to the [buildFileEdit] function has additional support
/// for working with Dart source files.
///
/// Use the [customPath] if the collection of edits should be written to another
/// dart file.
void addDartFileEdit(
void Function(DartFileEditBuilder builder) buildFileEdit, {
ImportPrefixGenerator importPrefixGenerator,
String? customPath,
});

/// Use the [buildFileEdit] function to create a collection of edits to the
Expand All @@ -76,18 +80,26 @@ abstract class ChangeBuilder {
///
/// The builder passed to the [buildFileEdit] function has no special support
/// for any particular kind of file.
///
/// Use the [customPath] if the collection of edits should be written to another
/// file.
void addGenericFileEdit(
void Function(analyzer_plugin.FileEditBuilder builder) buildFileEdit,
);
void Function(analyzer_plugin.FileEditBuilder builder) buildFileEdit, {
String? customPath,
});

/// Use the [buildFileEdit] function to create a collection of edits to the
/// currently analyzed file. The edits will be added to the source change
/// that is being built.
///
/// The builder passed to the [buildFileEdit] function has additional support
/// for working with YAML source files.
///
/// Use the [customPath] if the collection of edits should be written to another
/// YAML file.
void addYamlFileEdit(
void Function(YamlFileEditBuilder builder) buildFileEdit,
String? customPath,
);
}

Expand All @@ -110,12 +122,16 @@ class _ChangeBuilderImpl implements ChangeBuilder {
void addDartFileEdit(
void Function(DartFileEditBuilder builder) buildFileEdit, {
ImportPrefixGenerator? importPrefixGenerator,
String? customPath,
}) {
_operations.add(
importPrefixGenerator == null
? _innerChangeBuilder.addDartFileEdit(path, buildFileEdit)
? _innerChangeBuilder.addDartFileEdit(
customPath ?? path,
buildFileEdit,
)
: _innerChangeBuilder.addDartFileEdit(
path,
customPath ?? path,
buildFileEdit,
importPrefixGenerator: importPrefixGenerator,
),
Expand All @@ -124,19 +140,21 @@ class _ChangeBuilderImpl implements ChangeBuilder {

@override
void addGenericFileEdit(
void Function(analyzer_plugin.FileEditBuilder builder) buildFileEdit,
) {
void Function(analyzer_plugin.FileEditBuilder builder) buildFileEdit, {
String? customPath,
}) {
_operations.add(
_innerChangeBuilder.addGenericFileEdit(path, buildFileEdit),
_innerChangeBuilder.addGenericFileEdit(customPath ?? path, buildFileEdit),
);
}

@override
void addYamlFileEdit(
void Function(YamlFileEditBuilder builder) buildFileEdit,
String? customPath,
) {
_operations.add(
_innerChangeBuilder.addYamlFileEdit(path, buildFileEdit),
_innerChangeBuilder.addYamlFileEdit(customPath ?? path, buildFileEdit),
);
}

Expand Down
74 changes: 74 additions & 0 deletions packages/custom_lint_core/test/assist_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:analyzer/source/source_range.dart';
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
import 'package:custom_lint_core/src/assist.dart';
import 'package:custom_lint_core/src/change_reporter.dart';
import 'package:custom_lint_core/src/lint_rule.dart';
Expand Down Expand Up @@ -58,6 +59,50 @@ void main() {
);
});

// Extract the name of the changed file and makes the test failing if the internal structure is not the expected one
String _extractFileName(PrioritizedSourceChange change) {
final map = change.toJson();

expect(map.containsKey('change'), true);
final changes = map['change']! as Map;

expect(changes.containsKey('edits'), true);
final edits = changes['edits']! as List;

expect(edits.length, 1);
final edit = edits[0]! as Map;

expect(edit.containsKey('file'), true);
final fileName = edit['file'] as String;

return fileName;
}

test('CustomAssist.testRun', () async {
final assist1 = MyCustomAssist('CustomAssist', 'custom_1.txt');
final assist2 = MyCustomAssist('AnotherCustom', 'custom_2.txt');

final file = writeToTemporaryFile('''
void main() {
print('Custom world');
}
''');
final result = await resolveFile2(path: file.path);
result as ResolvedUnitResult;

final changeList1 = await assist1.testRun(result, SourceRange.EMPTY);
final changeList2 = await assist2.testRun(result, SourceRange.EMPTY);

final change1 = changeList1[0];
final change2 = changeList2[0];

final file1 = _extractFileName(change1);
final file2 = _extractFileName(change2);

expect(file1, 'custom_1.txt');
expect(file2, 'custom_2.txt');
});

test('Assist.testAnalyzeAndRun', () async {
final assist = MyAssist('MyAssist');
final assist2 = MyAssist('Another');
Expand Down Expand Up @@ -115,3 +160,32 @@ class MyAssist extends DartAssist {
});
}
}

class MyCustomAssist extends DartAssist {
MyCustomAssist(this.name, this.customPath);

final String name;
final String customPath;

@override
void run(
CustomLintResolver resolver,
ChangeReporter reporter,
CustomLintContext context,
SourceRange target,
) {
context.registry.addMethodInvocation((node) {
final changebuilder = reporter.createChangeBuilder(
message: name,
priority: 1,
);

changebuilder.addGenericFileEdit(
(builder) {
builder.addSimpleInsertion(node.offset, 'Custom 2023');
},
customPath: customPath,
);
});
}
}

0 comments on commit f9cc496

Please sign in to comment.