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

New customPath parameter with test #199

Merged
merged 25 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
138d860
add new parameter customPath to method addGenericFileEdit
matthiaslarisch Sep 8, 2023
14c6d43
Update change_reporter.dart
matthiaslarisch Sep 12, 2023
211f869
Create main.yml
laurentschall Oct 12, 2023
6af9622
Update main.yml
laurentschall Oct 12, 2023
fb39771
Update build.yml
laurentschall Oct 12, 2023
65a81ce
Update build.yml
laurentschall Oct 12, 2023
62f605c
[Github Actions] Align build content with invertase
laurentschall Oct 12, 2023
cc8d9eb
[Github Actions] Align build content with invertase
laurentschall Oct 12, 2023
9255bb3
[Github Actions] Do not schedule every day
laurentschall Oct 12, 2023
8b4ea40
[Dart Format] Fix format
laurentschall Oct 13, 2023
ebc8bff
[Build Action] Reduce scope to stable and get
laurentschall Oct 13, 2023
c85ae96
[Build Action] Adjust scope to master and get
laurentschall Oct 13, 2023
03f74a3
[Assist Test] Extend test (ongoing)
laurentschall Oct 13, 2023
07258c9
[Assist Test] Extend test (failing for now)
laurentschall Oct 13, 2023
0ea886b
[Custom Path] Extend customPath parameter test
laurentschall Oct 24, 2023
2ff3288
[Custom Path] Rename lists; Await on the beginning;
laurentschall Oct 24, 2023
42b3162
[Github Actions] Restore full test scope
laurentschall Oct 25, 2023
7034490
[Github Actions] Restore invertase file content
laurentschall Oct 25, 2023
5540faf
[Github Actions] Do not schedule every day
laurentschall Oct 25, 2023
0d73ee0
[Github Actions] Restore invertase file content
laurentschall Oct 31, 2023
415aceb
[Custom Path] Extract fine name from internal change structure rather…
laurentschall Oct 31, 2023
d9ae436
[Custom Path] Remove test files that are not necessary anymore
laurentschall Oct 31, 2023
01220fc
[Custom Path] Remove unnecessary method
laurentschall Oct 31, 2023
978fab8
Merge branch 'main' into main
rrousselGit Nov 13, 2023
c56f6b6
Merge branch 'main' into main
rrousselGit Dec 5, 2023
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
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,
);
});
}
}