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

fix: prevent removing imports if a rangeEnd is passed #112

Conversation

MattLish
Copy link
Contributor

@MattLish MattLish commented Nov 7, 2023

This prevents destructive operations when a rangeEnd is passed.

Prior to this, if rangeEnd was passed, the imports would be removed because the plugin considered them unused.

This should solve a bug experienced in JetBrains IDEs https://youtrack.jetbrains.com/issue/WEB-50178/Code-reformat-with-prettier-and-prettier-plugin-organize-imports-deletes-imports-if-Optimize-imports-is-enabled

Closes #110

@alexander-doroshko
Copy link

It might also make sense to check for rangeStart being 0.

@MattLish
Copy link
Contributor Author

MattLish commented Nov 7, 2023

It might also make sense to check for rangeStart being 0.

From what I could tell, rangeStart seemed to have no impact on the imports at all

@simonhaenisch
Copy link
Owner

simonhaenisch commented Nov 8, 2023

Just testing this and the following happens:

Prettier initially calls the plugin's preprocess with (omitting non-relevant fields):

{
  code: '<original-code>',
  options: {
    rangeEnd: 10, // or whatever was set
    originalText: undefined,
  }
}

Prettier calling the plugin at that point seems a bit weird but I guess maybe it's just because we're hooking into the preprocess hook.

Then it calls the plugin's preprocess a second time with

{
  code: '<just-the-code-in-the-range>',
  options: {
    rangeEnd: Infinity, // default value
    originalText: '<original-code>',
  }
}

I think the most correct (and performant) behavior is to

  • not do anything if options.originalText is defined (check this first because Infinity !== code.length is always true)
  • not do anything if options.rangeEnd !== code.length or options.rangeStart !== 0

wdyt?

@simonhaenisch
Copy link
Owner

An alternative would be to pass originalText instead of code to the TypeScript API, but then it's not trivial to figure out which range of the organizeImports result to pass back (since the formatted result could have a different length).

@simonhaenisch simonhaenisch merged commit 5ebe7d1 into simonhaenisch:master Nov 9, 2023
@MattLish
Copy link
Contributor Author

@simonhaenisch Sorry for not getting back to you. That works for me, thanks for sorting the PR!

renovate bot referenced this pull request in json-derulo/angular-ecmascript-intl Nov 13, 2023
….2.4 (#211)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[prettier-plugin-organize-imports](https://togithub.com/simonhaenisch/prettier-plugin-organize-imports)
| [`3.2.3` ->
`3.2.4`](https://renovatebot.com/diffs/npm/prettier-plugin-organize-imports/3.2.3/3.2.4)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/prettier-plugin-organize-imports/3.2.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/prettier-plugin-organize-imports/3.2.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/prettier-plugin-organize-imports/3.2.3/3.2.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/prettier-plugin-organize-imports/3.2.3/3.2.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>simonhaenisch/prettier-plugin-organize-imports
(prettier-plugin-organize-imports)</summary>

###
[`v3.2.4`](https://togithub.com/simonhaenisch/prettier-plugin-organize-imports/releases/tag/v3.2.4):
3.2.4

[Compare
Source](https://togithub.com/simonhaenisch/prettier-plugin-organize-imports/compare/v3.2.3...v3.2.4)

#### What's Changed

- fix: skip when formatting ranges — by
[@&#8203;MattLish](https://togithub.com/MattLish) in
[https://github.com/simonhaenisch/prettier-plugin-organize-imports/pull/112](https://togithub.com/simonhaenisch/prettier-plugin-organize-imports/pull/112)

#### New Contributors

- [@&#8203;MattLish](https://togithub.com/MattLish) made their first
contribution in
[https://github.com/simonhaenisch/prettier-plugin-organize-imports/pull/112](https://togithub.com/simonhaenisch/prettier-plugin-organize-imports/pull/112)

**Full Changelog**:
simonhaenisch/prettier-plugin-organize-imports@v3.2.3...v3.2.4

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/json-derulo/angular-ecmascript-intl).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40Ni4wIiwidXBkYXRlZEluVmVyIjoiMzcuNDYuMCIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
renovate bot referenced this pull request in simonknittel/sinister-incorporated Dec 19, 2023
….2.4 (#38)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[prettier-plugin-organize-imports](https://togithub.com/simonhaenisch/prettier-plugin-organize-imports)
| [`3.2.3` ->
`3.2.4`](https://renovatebot.com/diffs/npm/prettier-plugin-organize-imports/3.2.3/3.2.4)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/prettier-plugin-organize-imports/3.2.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/prettier-plugin-organize-imports/3.2.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/prettier-plugin-organize-imports/3.2.3/3.2.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/prettier-plugin-organize-imports/3.2.3/3.2.4?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>simonhaenisch/prettier-plugin-organize-imports
(prettier-plugin-organize-imports)</summary>

###
[`v3.2.4`](https://togithub.com/simonhaenisch/prettier-plugin-organize-imports/releases/tag/v3.2.4):
3.2.4

[Compare
Source](https://togithub.com/simonhaenisch/prettier-plugin-organize-imports/compare/v3.2.3...v3.2.4)

#### What's Changed

- fix: skip when formatting ranges — by
[@&#8203;MattLish](https://togithub.com/MattLish) in
[https://github.com/simonhaenisch/prettier-plugin-organize-imports/pull/112](https://togithub.com/simonhaenisch/prettier-plugin-organize-imports/pull/112)

#### New Contributors

- [@&#8203;MattLish](https://togithub.com/MattLish) made their first
contribution in
[https://github.com/simonhaenisch/prettier-plugin-organize-imports/pull/112](https://togithub.com/simonhaenisch/prettier-plugin-organize-imports/pull/112)

**Full Changelog**:
simonhaenisch/prettier-plugin-organize-imports@v3.2.3...v3.2.4

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/simonknittel/sinister-incorporated).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xMDMuMSIsInVwZGF0ZWRJblZlciI6IjM3LjEwMy4xIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCJ9-->
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.

Imports may get removed if using Prettier range-start/range-end options
3 participants