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

INTL-1332: Add import smarts to codemod #241

Merged
merged 65 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
59475a6
INTL-1332: Updated code for Intl class import statement
chetansinghchoudhary-wk Sep 12, 2023
f169e23
INTL-1332: Minor changes in import format
chetansinghchoudhary-wk Sep 12, 2023
5e38c29
INTL-1332: minor change in condition
chetansinghchoudhary-wk Sep 13, 2023
78fcfe6
INTL-1332: minor change in import
chetansinghchoudhary-wk Sep 13, 2023
afdedc3
Added test case for importer
chetansinghchoudhary-wk Sep 14, 2023
2765fc3
INTL-1332: PR comment resolved
chetansinghchoudhary-wk Sep 19, 2023
fd61b49
INTL-1332: order change for testcase
chetansinghchoudhary-wk Sep 21, 2023
6d20e1f
INTL-1332: minor changes in test case
chetansinghchoudhary-wk Sep 25, 2023
2556824
INTL-1332: Updated the migration and importer_test file
chetansinghchoudhary-wk Sep 25, 2023
120193c
INTL-1332: Updated the order of imports
chetansinghchoudhary-wk Sep 25, 2023
ae1c9d5
INTL-1332: Updated test cases
chetansinghchoudhary-wk Sep 25, 2023
2563523
INTL-1332: Updated test case
chetansinghchoudhary-wk Sep 25, 2023
b61886a
INTL-1332: Updated test case
chetansinghchoudhary-wk Sep 25, 2023
dc10e48
INTL-1332: Updated test case
chetansinghchoudhary-wk Sep 25, 2023
3f9c44b
INTL-1332: Added custom input
chetansinghchoudhary-wk Sep 26, 2023
fb41781
INTL-1332: Added custom input
chetansinghchoudhary-wk Sep 26, 2023
31586e8
INTL-1332: format & align
chetansinghchoudhary-wk Sep 26, 2023
f5a39f2
INTL-1332: format & align
chetansinghchoudhary-wk Sep 26, 2023
f0b8d70
INTL-1332: format & align
chetansinghchoudhary-wk Sep 26, 2023
32c85b4
INTL-1332: minor changes
chetansinghchoudhary-wk Sep 26, 2023
9659774
INTL-1332: minor changes
chetansinghchoudhary-wk Sep 26, 2023
04a2930
INTL-1332: format code
chetansinghchoudhary-wk Sep 26, 2023
0b2cf66
INTL-1332: format code
chetansinghchoudhary-wk Sep 26, 2023
e4e1e70
INTL-1332: indentation updated
chetansinghchoudhary-wk Sep 26, 2023
19a9987
INTL-1332: formated the code
chetansinghchoudhary-wk Sep 26, 2023
cee0c09
INTL-1332: formated the code
chetansinghchoudhary-wk Sep 26, 2023
30a4069
INTL-1332: indentation the code
chetansinghchoudhary-wk Sep 26, 2023
dce91a9
INTL-1332: added extraInput
chetansinghchoudhary-wk Sep 27, 2023
440bfe4
INTL-1332: added extraInput function
chetansinghchoudhary-wk Sep 27, 2023
951515b
INTL-1332: updated test case
chetansinghchoudhary-wk Sep 27, 2023
c22d80c
Change order for import file
chetansinghchoudhary-wk Sep 27, 2023
a6453be
Remove intlImport
chetansinghchoudhary-wk Sep 27, 2023
b8b9fbc
Remove intlImport & format
chetansinghchoudhary-wk Sep 27, 2023
6ac09fd
order change
chetansinghchoudhary-wk Sep 28, 2023
2bdc01f
Minor changes
chetansinghchoudhary-wk Sep 28, 2023
5fa051f
Minor changes in expectedSecondoutput
chetansinghchoudhary-wk Sep 28, 2023
10e38b3
Updated test case
chetansinghchoudhary-wk Sep 28, 2023
ff158cc
Optimized the code and updated the test cases
chetansinghchoudhary-wk Oct 3, 2023
7b8cfa7
Minor update in import
chetansinghchoudhary-wk Oct 3, 2023
8865e1c
Updated the importer class
chetansinghchoudhary-wk Oct 4, 2023
8e4228f
INTl-1332: Test case issue resolve and update
akshaybhardwaj-wk Oct 5, 2023
4fdcd8a
INTL-1332: remove print
akshaybhardwaj-wk Oct 5, 2023
683240c
INTL-1322: Test case debug
akshaybhardwaj-wk Oct 5, 2023
d38b84e
INTL-1322: relative import test case debug
akshaybhardwaj-wk Oct 5, 2023
cefb0ca
INTL-1322: Test case debug
akshaybhardwaj-wk Oct 5, 2023
9acf12f
INTL-1332: PR comment resolve
akshaybhardwaj-wk Oct 6, 2023
81bb028
INTL:1332: resolve case for no import
akshaybhardwaj-wk Oct 9, 2023
6e8156b
INTl-1332: Code refactor for bool
akshaybhardwaj-wk Oct 9, 2023
1efc33d
INTL-1332: Resolve PR comment
akshaybhardwaj-wk Oct 10, 2023
a4c3f5f
INTL-1332: undo PR commet
akshaybhardwaj-wk Oct 10, 2023
cc04546
INTL-1332: Resolve PR comment intlURL
akshaybhardwaj-wk Oct 10, 2023
2c70a40
INTL-1322: Skynet issue resolve
akshaybhardwaj-wk Oct 11, 2023
41dcf37
Merge branch 'master' into INTL-1332
akshaybhardwaj-wk Oct 11, 2023
eaa80fd
INTL-1322: URI content to uri.stringValue
akshaybhardwaj-wk Oct 11, 2023
bf17566
INTL-1332: run dart format
akshaybhardwaj-wk Oct 11, 2023
5aea1b7
INTL-1332: New test cases added PR comment
akshaybhardwaj-wk Oct 12, 2023
34187cd
INTL-1332: New test cases added PR comment
akshaybhardwaj-wk Oct 12, 2023
4207f2e
INTL-1332: contex.root and test case space update
akshaybhardwaj-wk Oct 16, 2023
cc34f33
INTL-1332: uri scheme updated
akshaybhardwaj-wk Oct 16, 2023
aa6fa7b
INTL-1332: code refactor in loop
akshaybhardwaj-wk Oct 17, 2023
f84c1a2
INTl-1332: Run format
akshaybhardwaj-wk Oct 17, 2023
4af088b
INTL-1332: Move code outer loop
akshaybhardwaj-wk Oct 18, 2023
5f4096b
INTl-1322: Updated test case for dart and package both
akshaybhardwaj-wk Oct 31, 2023
29c4829
INTL-1332: update test case and check for package and dart both
akshaybhardwaj-wk Nov 1, 2023
a926209
INTl-1332: apply dart format
akshaybhardwaj-wk Nov 1, 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
55 changes: 42 additions & 13 deletions lib/src/intl_suggestors/intl_importer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:io';

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/source/line_info.dart';
import 'package:codemod/codemod.dart';
import 'package:collection/collection.dart';
import 'package:path/path.dart' as path;

Stream<Patch> intlImporter(
FileContext context, String projectName, String className) async* {
Expand All @@ -38,27 +41,43 @@ Stream<Patch> intlImporter(

if (!needsIntlImport) return;

final intlUri = 'package:${projectName}/src/intl/${projectName}_intl.dart';

final intlFilePath = '/src/intl/${projectName}_intl.dart';
final intlUri = 'package:${projectName}' + intlFilePath;
akshaybhardwaj-wk marked this conversation as resolved.
Show resolved Hide resolved
final intlDirectory = path.join(context.root, intlFilePath);
final relativePathToIntlDir =
path.relative(intlDirectory, from: Directory.current.path);
final insertInfo = _insertionLocationForPackageImport(
intlUri, mainLibraryUnitResult.unit, mainLibraryUnitResult.lineInfo);
yield Patch(
insertInfo.leadingNewlines +
"import '$intlUri';" +
insertInfo.trailingNewlines,
insertInfo.offset,
insertInfo.offset);

final importStatement = insertInfo.usePackageImports
? packageImport(intlUri, insertInfo)
: relativeImport(relativePathToIntlDir, insertInfo);

yield Patch(importStatement, insertInfo.offset, insertInfo.offset);
}

String packageImport(String intlUri, _InsertionLocation insertInfo) =>
insertInfo.leadingNewlines +
"import '$intlUri';" +
insertInfo.trailingNewlines;

String relativeImport(
String relativeImportPath, _InsertionLocation insertInfo) =>
insertInfo.leadingNewlines +
"import '$relativeImportPath';" +
insertInfo.trailingNewlines;

class _InsertionLocation {
final int offset;
final int leadingNewlineCount;
final int trailingNewlineCount;
final bool usePackageImports;

_InsertionLocation(
this.offset, {
this.leadingNewlineCount = 0,
this.trailingNewlineCount = 0,
this.usePackageImports = false,
});

String get leadingNewlines => '\n' * leadingNewlineCount;
Expand Down Expand Up @@ -94,6 +113,7 @@ _InsertionLocation _insertionLocationForPackageImport(
final AstNode relativeNode;
final bool insertAfter;
final bool inOwnSection;
bool hasOnlyPackageImports;
if (firstPackageImportSortedAfterNewImport != null) {
relativeNode = firstPackageImportSortedAfterNewImport;
insertAfter = false;
Expand Down Expand Up @@ -121,12 +141,21 @@ _InsertionLocation _insertionLocationForPackageImport(
// No directive to insert relative to; insert before the first member or
// at the beginning of the file.
return _InsertionLocation(unit.declarations.firstOrNull?.offset ?? 0,
trailingNewlineCount: 2);
trailingNewlineCount: 2, usePackageImports: true);
}

hasOnlyPackageImports = !imports.any((importDirective) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Very close. But these lines are not necessary This is taking something that returns a Uri, turning it into a string, then parsing it so you can ask it for its scheme. Just get the uri and check if it's null and check the scheme.

Copy link
Contributor

Choose a reason for hiding this comment

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

Initially, we are writing code like this
final uriContent = importDirective.uriContent;
but after merge conflicts resolve there is updating in Analyzer 5 uriContent is archived and we are using importDirective.uri.stringValue
and if we check scheme with uri like this

hasOnlyPackageImports = !imports.any((importDirective) {
    final uri = importDirective.uri;
    return uri != null && (uri.scheme != 'package' && uri.scheme != 'dart:');
  });

then it's giving error error: The getter 'scheme' isn't defined for the type 'StringLiteral'. (undefined_getter at [over_react_codemod] lib/src/intl_suggestors/intl_importer.dart:158)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, ok. My mistake. I assumed importDirective.uri was returning a Uri, since we needed to convert it to a string. It's a StringLiteral, so this code is good.

final uriContent = importDirective.uri.stringValue;
if (uriContent != null) {
final uri = Uri.parse(uriContent);
akshaybhardwaj-wk marked this conversation as resolved.
Show resolved Hide resolved
return uri != null && uri.scheme != 'package' && uri.scheme != 'dart';
}
return true;
});

return _InsertionLocation(
insertAfter ? relativeNode.end : relativeNode.offset,
leadingNewlineCount: insertAfter ? (inOwnSection ? 2 : 1) : 0,
trailingNewlineCount: !insertAfter ? (inOwnSection ? 2 : 1) : 0,
);
insertAfter ? relativeNode.end : relativeNode.offset,
leadingNewlineCount: insertAfter ? (inOwnSection ? 2 : 1) : 0,
trailingNewlineCount: !insertAfter ? (inOwnSection ? 2 : 1) : 0,
usePackageImports: hasOnlyPackageImports);
}
48 changes: 36 additions & 12 deletions test/intl_suggestors/intl_importer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ void main() {
expectedOutput: /*language=dart*/ '''
import 'package:over_react/over_react.dart';
import 'package:test_project/src/intl/test_project_intl.dart';

content() => TestProjectIntl.testString;
''',
);
Expand All @@ -75,15 +74,13 @@ void main() {
await testSuggestor(
input: /*language=dart*/ '''
import 'package:z_fake_package/z_fake_package.dart';

content() => TestProjectIntl.testString;
''',
isExpectedError: (e) =>
isUndefinedIntlError(e) || isFakeUriError(e),
expectedOutput: /*language=dart*/ '''
import 'package:test_project/src/intl/test_project_intl.dart';
import 'package:z_fake_package/z_fake_package.dart';

content() => TestProjectIntl.testString;
''',
);
Expand All @@ -94,7 +91,6 @@ void main() {
input: /*language=dart*/ '''
import 'package:over_react/over_react.dart';
import 'package:z_fake_package/z_fake_package.dart';

content() => TestProjectIntl.testString;
''',
isExpectedError: (e) =>
Expand All @@ -103,7 +99,6 @@ void main() {
import 'package:over_react/over_react.dart';
import 'package:test_project/src/intl/test_project_intl.dart';
import 'package:z_fake_package/z_fake_package.dart';

content() => TestProjectIntl.testString;
''',
);
Expand All @@ -117,7 +112,6 @@ void main() {
import 'package:over_react/components.dart';
import 'package:z_fake_package/z_fake_package_1.dart';
import 'package:z_fake_package/z_fake_package_2.dart';

content() => TestProjectIntl.testString;
''',
isExpectedError: (e) =>
Expand All @@ -128,7 +122,6 @@ void main() {
import 'package:test_project/src/intl/test_project_intl.dart';
import 'package:z_fake_package/z_fake_package_1.dart';
import 'package:z_fake_package/z_fake_package_2.dart';

content() => TestProjectIntl.testString;
''',
);
Expand All @@ -139,19 +132,15 @@ void main() {
await testSuggestor(
input: /*language=dart*/ '''
import 'package:over_react/over_react.dart';

import 'a/fake_relative_file.dart';

content() => TestProjectIntl.testString;
''',
isExpectedError: (e) =>
isUndefinedIntlError(e) || isFakeUriError(e),
expectedOutput: /*language=dart*/ '''
import 'package:over_react/over_react.dart';
akshaybhardwaj-wk marked this conversation as resolved.
Show resolved Hide resolved
import 'package:test_project/src/intl/test_project_intl.dart';

import '../../../../../src/intl/test_project_intl.dart';
import 'a/fake_relative_file.dart';

content() => TestProjectIntl.testString;
''',
);
Expand All @@ -163,6 +152,7 @@ void main() {
import 'dart:html';

content() => TestProjectIntl.testString;

''',
isExpectedError: (e) =>
isUndefinedIntlError(e) || isFakeUriError(e),
Expand All @@ -177,6 +167,40 @@ void main() {
});
});

test('when there are Dart and Package', () async {
await testSuggestor(
input: /*language=dart*/ '''
import 'dart:html';
import 'package:over_react/over_react.dart';


content() => TestProjectIntl.testString;
''',
isExpectedError: (e) => isUndefinedIntlError(e) || isFakeUriError(e),
expectedOutput: /*language=dart*/ '''
import 'dart:html';
import 'package:over_react/over_react.dart';
import 'package:test_project/src/intl/test_project_intl.dart';

content() => TestProjectIntl.testString;
''',
);
});
test('package import in the same package should produce relative import',
() async {
await testSuggestor(
input: /*language=dart*/ '''

content() => TestProjectIntl.testString;
''',
isExpectedError: (e) => isUndefinedIntlError(e) || isFakeUriError(e),
expectedOutput: /*language=dart*/ '''
import 'package:test_project/src/intl/test_project_intl.dart';
content() => TestProjectIntl.testString;
''',
);
});

test('when there is just a library declaration', () async {
await testSuggestor(
input: /*language=dart*/ '''
Expand Down
Loading