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

feat: add organizeImportsMode option #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

oodzchen
Copy link

@oodzchen oodzchen commented Dec 5, 2024

I've try to use this plugin to auto remove unused imports, but it always sort the imports at the same time, which is not what I want. So I add the organizeImportsMode option to accomplish it, you can config AllSortAndCombine or RemoveUnused to determine how to organize the imports, and also, it's followed the latest typescript updates.

Copy link
Owner

@simonhaenisch simonhaenisch left a comment

Choose a reason for hiding this comment

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

Hey thanks a lot for the PR 👏

Looks good to me overall, just a couple minor questions/feedback (:

lib/organize.js Outdated
filepath = 'file.ts',
organizeImportsSkipDestructiveCodeActions,
// @ts-ignore
organizeImportsMode = 'All',
Copy link
Owner

Choose a reason for hiding this comment

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

Do you really need to set a default value here, since we're already setting the default in the plugin options?

Also what is the @ts-ignore ignoring? 🙃

Copy link
Author

Choose a reason for hiding this comment

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

Since the default value is unnecessary here, this one could be delete.

lib/organize.js Outdated
{
type: 'file',
fileName: filepath,
skipDestructiveCodeActions: organizeImportsSkipDestructiveCodeActions,
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
skipDestructiveCodeActions: organizeImportsSkipDestructiveCodeActions,

I assume we can skip this now that we set mode based on it, since the property is deprecated anyway.

type: 'file',
fileName: filepath,
skipDestructiveCodeActions: organizeImportsSkipDestructiveCodeActions,
// @ts-ignore
Copy link
Owner

Choose a reason for hiding this comment

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

Same here, why a ts-ignore?

Copy link
Author

Choose a reason for hiding this comment

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

It's the problem of use enum type in javascript code, since the languageService.organizeImports only accept mode type as OrganizeImportsMode, we must use something like OrganizeImportsMode.All to configure it, and it was imported from the typescript code, that's not a good idea. I couldn't find any other way to solve this problem, the simple way is just ignore it.

Copy link
Owner

Choose a reason for hiding this comment

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

ok we can just type case it instead. i'll try to finish this PR soon, sorry somehow was too busy lately 🫣

@@ -1,9 +1,9 @@
declare module 'prettier' {
interface Options {
organizeImportsSkipDestructiveCodeActions?: boolean;
Copy link
Owner

Choose a reason for hiding this comment

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

Sry i didn't mean dropping the deprecated option entirely. just when calling languageService.organizeImports, to only pass mode there, like

languageService.organizeImports(
  // ...
  mode: organizeImportsMode ?? (organizeImportsSkipDestructiveCodeActions ? 'SortAndCombine' : undefined),
  // ...
)

Copy link
Author

Choose a reason for hiding this comment

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

Got it, restored and updated.

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.

2 participants