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

Feature: importOrderCombineTypeAndValueImports #20

Merged
merged 26 commits into from Oct 24, 2022

Conversation

fbartho
Copy link
Collaborator

@fbartho fbartho commented May 18, 2022

Fixes: #4

Allows the ability for users to convert import type expressions into import {type …} expressions via a boolean flag.

Should this be controlled via a string parameter?

@IanVS
Copy link
Owner

IanVS commented May 18, 2022

I think we need to talk about whether this warrants yet another option, but in the meantime, do you think you could add some tests? You might be able to use the test cases from my codemod as a starting point: https://github.com/IanVS/type-import-codemod/blob/9f3210861a77216f07317537a04ce61467e0df7e/src/transform.test.ts

@fbartho
Copy link
Collaborator Author

fbartho commented May 18, 2022

Happy to add those tests. I do tend to have macro-tests that assert a ton of functionality all at once. Not sure that's a good sign, but code-coverage likes it 😉

@fbartho
Copy link
Collaborator Author

fbartho commented May 18, 2022

@IanVS Test cases imported, the only differences I can see

  • this plugin sorts the output by source, while the codemod didn't
  • this the codemod grouped type imports together, while this plugin just sorts all the fields by name.

Do we need/want to group type imports when sorting?

Base automatically changed from fb/simplify-import-duplicates to main June 9, 2022 20:20
@IanVS
Copy link
Owner

IanVS commented Jun 9, 2022

Sorry, it looks like there are a number of conflicts on this PR now. Your best bet might be to reset hard to main, and then cherry-pick the commits from this PR back onto the branch.

# Conflicts:
#	README.md
#	src/index.ts
#	src/preprocessor.ts
#	src/types.ts
#	src/utils/__tests__/get-all-comments-from-nodes.spec.ts
#	src/utils/__tests__/get-code-from-ast.spec.ts
#	src/utils/__tests__/get-sorted-nodes-by-import-order.spec.ts
#	src/utils/__tests__/get-sorted-nodes.spec.ts
#	src/utils/__tests__/merge-nodes-with-matching-flavors.spec.ts
#	src/utils/__tests__/remove-nodes-from-original-code.spec.ts
#	src/utils/get-sorted-nodes.ts
#	src/utils/merge-nodes-with-matching-flavors.ts
@fbartho
Copy link
Collaborator Author

fbartho commented Jun 15, 2022

@IanVS sorry about the lag re: updates, I was under the weather last week! main is now merged in. Most conflicts were trivially solved with a 3-headed merge (using p4merge), and the few that weren't automatically solved don't seem too bad.

Please take another look, and let me know if this can proceed.

Copy link
Owner

@IanVS IanVS left a comment

Choose a reason for hiding this comment

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

It took me a while to get to this, sorry about that. I think it mostly looks good. I had a few small comments/questions.

Unless you or @blutorange feels strongly, I'd like to sort the type imports to the end. I think it looks better that way, and is more readable. Would you mind making that change? Then we can merge and release this, and prepare for the breaking changes release.

!importOrderMergeDuplicateImports
) {
console.warn(
"[@ianvs/prettier-plugin-sort-imports]: Option combination of both importOrderMergeTypeImportsIntoRegular: true and importOrderMergeDuplicateImports: false is not won't do anything!",
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
"[@ianvs/prettier-plugin-sort-imports]: Option combination of both importOrderMergeTypeImportsIntoRegular: true and importOrderMergeDuplicateImports: false is not won't do anything!",
"[@ianvs/prettier-plugin-sort-imports]: Enabling importOrderMergeTypeImportsIntoRegular will have no effect unless importOrderMergeDuplicateImports is also enabled.",

function nodeIsImportNamespaceSpecifier(
node: ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier,
): node is ImportNamespaceSpecifier {
return node.type === 'ImportNamespaceSpecifier';
}

/** import Default from "someModule" */
Copy link
Owner

Choose a reason for hiding this comment

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

Why remove the comments?

return node.type === 'ImportSpecifier';
}

function convertImportSpecifierType(node: ImportSpecifier) {
Copy link
Owner

Choose a reason for hiding this comment

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

Can we call this convertImportSpecifierToType?

@IanVS
Copy link
Owner

IanVS commented Jul 25, 2022

Hi @fbartho. How would you like to proceed on this one? We can merge it as-is if you don't have time to make changes, and I can play around with re-ordering the type/value import specifiers in a separate PR.

@fbartho
Copy link
Collaborator Author

fbartho commented Jul 25, 2022

Hiya! I was traveling 2 weeks ago, and totally missed these messages!

No objections to the changes, I’m happy to make them — should be able to get to them this week or this coming Saturday

@IanVS
Copy link
Owner

IanVS commented Jul 25, 2022

Great, thanks! I hope you had a good time on your travels!

@IanVS
Copy link
Owner

IanVS commented Aug 17, 2022

Hi @fbartho just checking in to see if this is still something you'll be able to get to. NBD if not, just was wondering.

@fbartho
Copy link
Collaborator Author

fbartho commented Sep 7, 2022

@IanVS whoops! It's been a little bit of a scramble. I'll take a look at this, this week!

@IanVS
Copy link
Owner

IanVS commented Oct 24, 2022

Thanks for your work on this @fbartho! I pushed up a few tweaks, and am going to merge / release. We can continue to iterate, but I'd like to start preparing for a breaking release, to include prettier 3 support.

@IanVS IanVS merged commit b7c8ca5 into main Oct 24, 2022
@IanVS IanVS deleted the fb/merge-type-imports-with-regular-imports branch October 24, 2022 12:44
@IanVS IanVS changed the title Feature: importOrderMergeTypeImportsIntoRegular Feature: importOrderCombineTypeAndValueImports Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Combine type and value imports
2 participants