Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions .github/workflows/dart_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ jobs:
# Runs analysis, formatting, and dependency validation against the dart source.
checks:
uses: Workiva/gha-dart-oss/.github/workflows/checks.yaml@v0.1.14
with:
sdk: 3.13.4
format-check: false

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will follow up with a format-all-the-code PR


# Generates an SBOM and uploads it via anchore/sbom-action.
build:
uses: Workiva/gha-dart-oss/.github/workflows/build.yaml@v0.1.14
with:
sdk: 3.13.4

# Runs unit tests in dev mode.
unit-tests:
uses: Workiva/gha-dart-oss/.github/workflows/test-unit.yaml@v0.1.14
with:
sdk: 3.13.4
2 changes: 2 additions & 0 deletions .github/workflows/dart_publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ jobs:
# Generates and uploads an SBOM, then publishes to pub.dev.
publish:
uses: Workiva/gha-dart-oss/.github/workflows/publish.yaml@v0.1.14
with:
sdk: 3.13.4
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased

- **Breaking:** Require analyzer `^14.0.0` and Dart SDK `^3.13.0`.

## [1.3.0](https://github.com/Workiva/dart_codemod/compare/1.2.0...1.3.0)

- Allow up to analyzer 10
Expand Down
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ the AST.

Inspired by and based on [Facebook's `codemod` library][facebook-codemod].

## Requirements

- Dart SDK `^3.13.0`
- [`package:analyzer`][analyzer] `^14.0.0`

Suggestors that use the analyzer's AST or element APIs should be written
against analyzer 14. Notably, library names are represented by `DottedName`
nodes (use `DottedName.tokens`), `LibraryIdentifier` no longer exists, and
parse/analysis diagnostics expose `diagnosticCode` instead of `errorCode`.

## Demo

![demo](images/demo.gif)
Expand All @@ -21,10 +31,10 @@ apply code modifications and refactors via an interactive CLI. To that end,
the following function is provided:

```dart
Future<int> runInteractiveCodemod(Iterable<File> files, Suggestor suggestor);
Future<int> runInteractiveCodemod(Iterable<String> filePaths, Suggestor suggestor);
Comment on lines -24 to +34

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self - this is just aligning the docs with the code; no breaking change was made in this PR.

```

Calling this will tell codemod run the `suggestor` on each file in `files`. For
Calling this will tell codemod run the `suggestor` on each file in `filePaths`. For
each file, the suggestor will return a stream of patches that should be
suggested to the user. As patches are suggested and accepted by the user,
codemod handles applying them to the files and writing the result to disk.
Expand Down Expand Up @@ -122,7 +132,8 @@ Consider the following suggestor that removes all deprecated declarations
(i.e. classes, constructors, variables, methods, etc.):

```dart
import 'package:analyzer/analyzer.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:codemod/codemod.dart';

class DeprecatedRemover extends GeneralizingAstVisitor<void>
Expand Down Expand Up @@ -326,8 +337,7 @@ Let's use the `DeprecatedRemover` suggestor example from above to demonstrate
testing:

```dart
import 'package:codemod/codemod.dart';
import 'package:source_span/source_span.dart';
import 'package:codemod/test.dart';
import 'package:test/test.dart';

void main() {
Expand Down Expand Up @@ -365,8 +375,7 @@ that has access to the whole package and its dependencies. You can then add
source file(s) and use the wrapping `FileContext`s to test suggestors.

```dart
import 'package:codemod/codemod.dart';
import 'package:source_span/source_span.dart';
import 'package:codemod/test.dart';
import 'package:test/test.dart';

void main() {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/ast_visiting_suggestor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ final _log = Logger('AstVisitingSuggestor');
/// The easiest way to understand this pattern is to see an example. Consider
/// the following suggestor that aims to remove all deprecated declarations:
///
/// import 'package:analyzer/analyzer.dart';
/// import 'package:analyzer/dart/ast/ast.dart';
/// import 'package:analyzer/dart/ast/visitor.dart';
/// import 'package:codemod/codemod.dart';
///
/// class DeprecatedRemover extends GeneralizingAstVisitor
Expand Down
4 changes: 2 additions & 2 deletions lib/src/file_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ class FileContext {
var buffer = StringBuffer();
for (var error in result.errors) {
var location = result.lineInfo.getLocation(error.offset);
// ignore: deprecated_member_use
buffer.writeln(' ${error.errorCode.name}: ${error.message} - '
buffer.writeln(
' ${error.diagnosticCode.lowerCaseName}: ${error.message} - '
'${location.lineNumber}:${location.columnNumber}');
}
throw ArgumentError(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/run_interactive_codemod.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Future<int> runInteractiveCodemodSequence(
}
return ExitCode.success.code;
}
return overrideAnsiOutput<Future<int>>(
return await overrideAnsiOutput<Future<int>>(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to fix analyzer warnings since it was failing now under newer dart

stdout.supportsAnsiEscapes,
() => _runInteractiveCodemod(filePaths, suggestors, parsedArgs,
defaultYes: defaultYes,
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ description: >
towards updating and refactoring Dart code, but can modify any files.

environment:
sdk: '>=2.19.0 <4.0.0'
sdk: ^3.13.0

dependencies:
analyzer: '>=5.13.0 <11.0.0'
analyzer: ^14.0.0
args: ^2.0.0
glob: ^2.0.1
io: ^1.0.0
Expand Down
2 changes: 0 additions & 2 deletions test/aggregate_suggestor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ import 'package:test/test.dart';

class MockPatch extends Mock implements Patch {}

@override
Stream<Patch> fooSuggestor(_) async* {
yield FooPatch();
}

@override

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more analyzer warnings

Stream<Patch> barSuggestor(_) async* {
yield BarPatch();
}
Expand Down
6 changes: 3 additions & 3 deletions test/ast_visiting_suggestor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class Duplicate extends SimpleAstVisitor<void> with AstVisitingSuggestor {
class LibNameDoubler extends RecursiveAstVisitor<void>
with AstVisitingSuggestor {
@override
void visitLibraryIdentifier(LibraryIdentifier node) {
for (final component in node.components) {
yieldPatch(component.name * 2, component.offset, component.end);
void visitDottedName(DottedName node) {
for (final token in node.tokens) {
yieldPatch(token.lexeme * 2, token.offset, token.end);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/functional/run_interactive_codemod_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import 'dart:convert';
import 'dart:io';

import 'package:io/io.dart' show ExitCode;
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
Expand Down Expand Up @@ -200,7 +201,7 @@ void main() {

testCodemod(
'quits codemod via prompts when overlapping patches', _afterNoPatches,
expectedExitCode: 255,
expectedExitCode: ExitCode.software.code,
stdinLines: ['y', 'y', 'q'],
script: 'codemod_overlapping_patches.dart', body: (out, err) {
final file1Path = p.canonicalize(d.path('project/file1.txt'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ void main(List<String> args) async {
args: args);
}

@override
Stream<Patch> overlappingPatchSuggestor(FileContext context) async* {
yield Patch('overlap', 1, 3);
yield Patch('dov', 0, 3);
Expand Down
Loading