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

CPLAT-9308 Codemod Utility to tell if Something is Public API #74

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
53d0862
Create SemverHelper class
sydneyjodon-wk Jan 30, 2020
4900548
Create utility function
sydneyjodon-wk Jan 30, 2020
070c5f9
Format
sydneyjodon-wk Jan 30, 2020
723a406
Add SemverHelper constructor
sydneyjodon-wk Feb 4, 2020
cadf6ae
Add doc comment
sydneyjodon-wk Feb 4, 2020
d806385
Add tests
sydneyjodon-wk Feb 4, 2020
c5e7e74
Merge branch 'CPLAT-9205-new-boilerplate-codemod' into CPLAT-9308-get…
sydneyjodon-wk Feb 4, 2020
7027deb
Merge base branch in
sydneyjodon-wk Feb 4, 2020
900a557
Update migrator tests
sydneyjodon-wk Feb 4, 2020
7c1f44e
Remove unused imports
sydneyjodon-wk Feb 4, 2020
457deb6
Merge CPLAT-9205-new-boilerplate-codemod into CPLAT-9308-getPublicExp…
aaronlademann-wf Feb 5, 2020
f9d81d9
Merge remote-tracking branch 'origin/CPLAT-9205-new-boilerplate-codem…
aaronlademann-wf Feb 5, 2020
8abec86
Merge branch 'CPLAT-9205-new-boilerplate-codemod' into CPLAT-9308-get…
sydneyjodon-wk Feb 6, 2020
c61a5c4
Address reviewer feedback
sydneyjodon-wk Feb 6, 2020
d53c197
Remove duplicate import
sydneyjodon-wk Feb 6, 2020
82e39f7
Add doc comment
sydneyjodon-wk Feb 6, 2020
c5430ab
Merge branch 'CPLAT-9205-new-boilerplate-codemod' into CPLAT-9308-get…
sydneyjodon-wk Feb 6, 2020
7d844b0
Fix errors from merge
sydneyjodon-wk Feb 6, 2020
5783dcb
Address reviewer feedback
sydneyjodon-wk Feb 11, 2020
23dc002
Merge branch 'CPLAT-9205-new-boilerplate-codemod' into CPLAT-9308-get…
sydneyjodon-wk Feb 11, 2020
8775714
Address reviewer feedback
sydneyjodon-wk Feb 11, 2020
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
30 changes: 30 additions & 0 deletions lib/src/boilerplate_suggestors/boilerplate_utilities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,34 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:convert';
import 'dart:io';

import 'package:analyzer/dart/ast/ast.dart';

class SemverHelper {
static Map _exportList;
sydneyjodon-wk marked this conversation as resolved.
Show resolved Hide resolved

void fromReport(String path) async {
var fileContents;
await File(path).readAsString().then((String contents) {
fileContents = contents;
});

Map decoded = jsonDecode(fileContents);
_exportList = decoded['exports'];
}

List<dynamic> getPublicExportLocations(ClassDeclaration node) {
sydneyjodon-wk marked this conversation as resolved.
Show resolved Hide resolved
final className = node.name.name;
String classKey;

_exportList.forEach((key, value) {
if (value['type'] == 'class' && value['grammar']['name'] == className) {
classKey = key;
}
});
sydneyjodon-wk marked this conversation as resolved.
Show resolved Hide resolved

return [classKey, _exportList[classKey]];
sydneyjodon-wk marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:codemod/codemod.dart';
import 'package:over_react_codemod/src/boilerplate_suggestors/boilerplate_utilities.dart';

/// Suggestor that updates props and state classes to new boilerplate.
class PropsAndStateClassesMigrator extends GeneralizingAstVisitor
with AstVisitingSuggestorMixin
implements Suggestor {
final SemverHelper helper;

PropsAndStateClassesMigrator(this.helper);

@override
visitClassDeclaration(ClassDeclaration node) {
super.visitClassDeclaration(node);
Expand Down
9 changes: 7 additions & 2 deletions lib/src/executables/boilerplate_upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import 'dart:io';

import 'package:codemod/codemod.dart';
import 'package:over_react_codemod/src/boilerplate_suggestors/boilerplate_utilities.dart';
import 'package:over_react_codemod/src/boilerplate_suggestors/props_and_state_classes_migrator.dart';
import 'package:over_react_codemod/src/ignoreable.dart';

Expand All @@ -26,18 +27,22 @@ const _changesRequiredOutput = """
Then, review the the changes, address any FIXMEs, and commit.
""";

void main(List<String> args) {
Future<void> main(List<String> args) async {
final query = FileQuery.dir(
pathFilter: (path) {
return isDartFile(path) && !isGeneratedDartFile(path);
},
recursive: true,
);

SemverHelper helper = SemverHelper();
await helper.fromReport(
'/Users/sydneyjodon/Documents/GitHub/over_react_codemod/lib/src/boilerplate_suggestors/test.json');

exitCode = runInteractiveCodemodSequence(
query,
<Suggestor>[
PropsAndStateClassesMigrator(),
PropsAndStateClassesMigrator(helper),
].map((s) => Ignoreable(s)),
args: args,
defaultYes: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:over_react_codemod/src/boilerplate_suggestors/boilerplate_utilities.dart';
import 'package:over_react_codemod/src/boilerplate_suggestors/props_and_state_classes_migrator.dart';
import 'package:test/test.dart';

import '../util.dart';

main() {
group('PropsAndStateClassesMigrator', () {
final testSuggestor = getSuggestorTester(PropsAndStateClassesMigrator());
SuggestorTester testSuggestor;

setUpAll(() async {
final helper = SemverHelper();
await helper.fromReport(
'/Users/sydneyjodon/Documents/GitHub/over_react_codemod/lib/src/boilerplate_suggestors/test.json');
Copy link
Contributor

Choose a reason for hiding this comment

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

We need a version of the report tracked in Git somehow, as opposed to this hard-coded API.

To make testing easier, perhaps having a constructor that takes in the map directly would be helpful. That way, you can embed the JSON in the test, and also choose how to load the report in actual usage.

SemverHelper.fromReport(this.exportList);
// in the test for SemverHelper
final helper = SemverHelper({'exports': ...});
// in usage:
final helper = SemverHelper(jsonDecode(await File('report.json').readAsString()));

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, a test for a Suggestor would be a case where we'd want to use the mock for SemverHelper as opposed to a real instance, to make test setup easier (by not having to set up the whole exports map).

testSuggestor = getSuggestorTester(PropsAndStateClassesMigrator(helper));
});

test('empty file', () {
testSuggestor(expectedPatchCount: 0, input: '');
Expand Down