From a6738cb132d216511fa05012e5253df7f8721563 Mon Sep 17 00:00:00 2001 From: Laurent Schall Date: Tue, 5 Dec 2023 11:40:02 +0100 Subject: [PATCH 1/2] New customPath parameter with test (#199) --- .../lib/src/change_reporter.dart | 34 +++++++-- .../custom_lint_core/test/assist_test.dart | 74 +++++++++++++++++++ 2 files changed, 100 insertions(+), 8 deletions(-) diff --git a/packages/custom_lint_core/lib/src/change_reporter.dart b/packages/custom_lint_core/lib/src/change_reporter.dart index 47112da3..f0216e64 100644 --- a/packages/custom_lint_core/lib/src/change_reporter.dart +++ b/packages/custom_lint_core/lib/src/change_reporter.dart @@ -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 @@ -76,9 +80,13 @@ 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 @@ -86,8 +94,12 @@ abstract class ChangeBuilder { /// /// 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, ); } @@ -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, ), @@ -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), ); } diff --git a/packages/custom_lint_core/test/assist_test.dart b/packages/custom_lint_core/test/assist_test.dart index 84b1ce8b..a3d6c21b 100644 --- a/packages/custom_lint_core/test/assist_test.dart +++ b/packages/custom_lint_core/test/assist_test.dart @@ -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'; @@ -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'); @@ -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, + ); + }); + } +} From fd5e4c929a49719b943afd86f838f25fbef740a2 Mon Sep 17 00:00:00 2001 From: Remi Rousselet Date: Tue, 5 Dec 2023 11:41:59 +0100 Subject: [PATCH 2/2] Changelog --- packages/custom_lint_core/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/custom_lint_core/CHANGELOG.md b/packages/custom_lint_core/CHANGELOG.md index 11ca5bd4..26444565 100644 --- a/packages/custom_lint_core/CHANGELOG.md +++ b/packages/custom_lint_core/CHANGELOG.md @@ -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` @@ -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