amatiasq / vsc-sort-imports

Sort ES6 imports automatically.
ISC License
59 stars 24 forks source link

Option to detect the sort-import package in the project instead of using the global one #76

Closed juliovedovatto closed 3 years ago

juliovedovatto commented 3 years ago

Hi @amatiasq

First of all, thanks for creating this awesome VSCode extension. I use it a lot in my projects.

I managed to make it work with .vue files, using https://www.npmjs.com/package/import-sort-parser-babel-vue package, but due natural behavior of import-sort, it leaves a blank line before the imports part.

Original:

<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png" />
    <HelloWorld msg="Welcome to Your Vue.js + TypeScript App" />
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import HelloWorld from '@/components/HelloWorld.vue' // @ is an alias to /src

export default Vue.extend({
  name: 'Home',
  components: {
    HelloWorld
  }
})
</script>

Output (after saving):

<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png" />
    <HelloWorld msg="Welcome to Your Vue.js + TypeScript App" />
  </div>
</template>

<script lang="ts">
// <- eslint warning about invalid new line
import HelloWorld from '@/components/HelloWorld.vue' // @ is an alias to /src
import Vue from 'vue'

export default Vue.extend({
  name: 'Home',
  components: {
    HelloWorld
  }
})
</script>

I opened a PR https://github.com/renke/import-sort/pull/133 to address this weird behavior. But it seems the project is kinda "inactive". I tried to ping the author, but I don't believe he will approve it, due the inactive state (and the high number of open PRs without his answer).

My question is: is there a way to use import-sort package locally (like eslint)? If the package exists locally, extension will use it, otherwise it will use global one. In this way, I'll be able to use my forked package to sort imports correctly.

jerome-benoit commented 3 years ago

Have you tried to install your fork of import-sort in your project dev dependencies in place of the current released version? I think the extension is then already using the project installation.

Otherwise, here we are still merging properly done PRs ;)

juliovedovatto commented 3 years ago

@jerome-benoit My apologies for the delay, I was stuck on other projects.

I was able to generate a forked package (using gitpkg) and installed in my project devDependencies. But it looks like the extension still loads the one it installed and instead my local package.

My forked package: https://github.com/Konnng/import-sort/tree/build/packages/import-sort

If I use import-sort-cli added in my project I'm able to see the correct output (since is relying on my forked package).

So my question still remains: is there a way to rely on the package added in my workspace rather the installed by vsc-sort-imports? Does it make sense?

PS: I asked on StackOverflow to see if there is a way to accomplish this.

secondfry commented 3 years ago

vscode-eslint does this: https://github.com/microsoft/vscode-eslint/blob/e4b2738e713b7523824e0c72166f5cdd44f47052/server/src/eslintServer.ts#L821-L871 and this is loadNodeModule which get called later https://github.com/microsoft/vscode-eslint/blob/e4b2738e713b7523824e0c72166f5cdd44f47052/server/src/eslintServer.ts#L379-L389.

Seems like one would need to change: https://github.com/amatiasq/vsc-sort-imports/blob/c9a5ae715e8089f588f1da7775dadab7b01ecaa2/src/sort.ts#L6 into something resembling eslint shenanigans.

Feels like much easier approach would be to extend NODE_PATH to include project directory.

Also #82 seems to be duplicate of this issue. Also #56 seems to be first occurance of this issue with either https://github.com/amatiasq/vsc-sort-imports/issues/56#issuecomment-602412472 or https://github.com/amatiasq/vsc-sort-imports/issues/56#issuecomment-638755819 being a workaround.

jerome-benoit commented 3 years ago

Work as expected, closing.