Giter Club home page Giter Club logo

prettier-plugin-organize-imports's People

Contributors

duniul avatar johnsoncodehk avatar mathieumg avatar mattlish avatar qbolt avatar simonhaenisch avatar thorn0 avatar zamiell avatar zclhlmgqzc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

prettier-plugin-organize-imports's Issues

It it possible to only run this as part of lint-staged?

Hello!
While I found import sorting very useful, sometimes, if it's used along with auto-save, it creates a problems, like for example when it removes imports for the code you just cut to paste to another place, or commented out for a second to test something.

Is it possible to only run when you commit changes, as part of pre-commit hook, but not on every single format?
Or may be just disable it by default, so it would be possible to use different prettier config for those scenarios?

Thanks!

Node 12/NodeNext module resolution breaks vue file organize imports

Noticed this issue after I tried my fix in PR #62 . While that PR fixes the initial issue, ran into a new one that only occurs for vue files.

Note my editor organize imports command works just fine to sort the vue file

Repro found here

Error Stack Trace

apps/app-ui/src/components/CoreAdminDashboard.vue[error] apps/app-ui/src/components/CoreAdminDashboard.vue: TypeError: node.insertChildBefore is not a function
[error]     at /home/kalvens/rtvision-app/mono/node_modules/.pnpm/[email protected]/node_modules/prettier/index.js:34818:22
[error]     at vs.walk (/home/kalvens/open-source/prettier-plugin-organize-imports/node_modules/.pnpm/[email protected]/node_modules/prettier/parser-html.js:61:37526)
[error]     at vs.walk (/home/kalvens/open-source/prettier-plugin-organize-imports/node_modules/.pnpm/[email protected]/node_modules/prettier/parser-html.js:61:37518)
[error]     at vs.walk (/home/kalvens/open-source/prettier-plugin-organize-imports/node_modules/.pnpm/[email protected]/node_modules/prettier/parser-html.js:61:37518)
[error]     at vs.walk (/home/kalvens/open-source/prettier-plugin-organize-imports/node_modules/.pnpm/[email protected]/node_modules/prettier/parser-html.js:61:37518)
[error]     at vs.walk (/home/kalvens/open-source/prettier-plugin-organize-imports/node_modules/.pnpm/[email protected]/node_modules/prettier/parser-html.js:61:37518)
[error]     at vs.walk (/home/kalvens/open-source/prettier-plugin-organize-imports/node_modules/.pnpm/[email protected]/node_modules/prettier/parser-html.js:61:37518)
[error]     at vs.walk (/home/kalvens/open-source/prettier-plugin-organize-imports/node_modules/.pnpm/[email protected]/node_modules/prettier/parser-html.js:61:37518)
[error]     at extractInterpolation (/home/kalvens/rtvision-app/mono/node_modules/.pnpm/[email protected]/node_modules/prettier/index.js:34802:11)
[error]     at Object.preprocess (/home/kalvens/rtvision-app/mono/node_modules/.pnpm/[email protected]/node_modules/prettier/index.js:34709:9)

Feature request: Preserve grouping of imports

Hi Simon,

Thank you for the plugin! I love how it works after simply installing it, without having to configure anything.

I would like to be able to preserve the grouping and order of some imports if possible. Notably, I have one library with tons of named imports, and I would like to preserve how we always choose to have those imports come last:

import { useState } from 'react'
import api from '../shared/api'
import d from '../shared/data-types'

import {
    Button,
    Col,
    Div,
    Flex,
    FlexFix,
    Input,
    makeReactiveUI,
    Margin,
    OnTap,
    Padding,
    Row,
    ScrollCol,
    Span,
    Style,
    TextArea,
    TextView,
} from '../shared/lib/js.ui/dom'

Would you be willing to consider adding the ability to preserve newline-separated groups of imports? My suggestion would be to treat each newline-separated group as a standalone group, and never change their order, but to change the order of import statements within any given group.

Whether you say yes to making that happen or not: thank you for the library!

Cheers,
Marcus

Use the `source.sortImports` code action instead to avoid deleting "unused" imports

The source.sortImports code action is similar to the what organize imports does, except it doesn't doesn't remove "unused" imports.

Automatic removal of "unused" imports by a Prettier plugin is unacceptable, for a few reasons:

  1. Automatically deleting imports that could have intended side-effects is more risky than just reordering them.

  2. It destructively modifies the code you are working on. Often you begin writing a new module by writing the imports you expect to use, then start writing the module code. If you trigger a Prettier format at any point (e.g. by saving the file) before you have used all the imports in your module code, the imports unhelpfully get stripped out.

  3. It destroys JS code examples in markdown readme files that demo how to import things. In these situations the imports are expected to be unused, since the JS is just an example snippet. The result after running Prettier on the markdown is all the JS fenced code blocks containing just imports are empty. Here is an example of "unused" imports within markdown:

    Screen Shot 2021-10-17 at 2 26 17 pm

    I've found it difficult to reliably reproduce when the TS language server considers the imports "unused", when using Prettier with your prettier-plugin-organize-imports in a VS Code editor open to a markdown file with JS fenced code blocks containing single import statements, they don't get deleted on format. But, the exact same markdown when Prettier formatted programmatically as part of the jsdoc-md readme generation does result in deleted imports.

Somewhat related:

Importing react at tsx files gets removed

I have a line: import React from "react";

It gets removed by the plugin because React is not directly used. However this import should be present in all *.tsx and *spec.tsx files so even it could be automatically added, but not really removed.

After the import gets removed, I get the following error: "React must be in scope when using JSX".

Is it possible at least to add a rule to ignore this one particular import? Do you propose any other solution or workaround?

Importing react at tsx files gets removed

the same as #24
I'm just reporting it because I have the same problem and I can share my tsconfig

Screenshot from 2021-08-04 13-55-07 (1)

as you can see, there isn't Typescript errors, just prettier one, which I think it's related to this project

my ts config:

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "noUnusedLocals": false,
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "baseUrl": "src",
    "noFallthroughCasesInSwitch": true,
    "noImplicitAny": false,
    "downlevelIteration": true
  },
  "include": ["src"]
}

Use `vue-typescript` (from Volar) for supporting vue, instead of directly using sfc compiler

I was looking for a solution for #40, and I asked in the Volar Discord channel. and the main developer told me that:

johnsoncodehk — 06/13/2022
I see, they could consider using vue-typescript instead
So they do not need to handle SFC parsing and edit range mapping.

Shayan.To — 06/13/2022
Does vue-typescript also support vue 2?

johnsoncodehk — 06/13/2022
If just for organize imports, yes

So it seems like you can entrust the work of parsing and modifying Vue file to vue-tsc project.

Vue - plugin removes imports if components are kebap-cased

Hello, unfortunately there seems to be a bug currently when imported components are written in kebap case in Vue SFC templates. The plugin falsely recognizes them as unimported and removes them.

This works fine:

<template>
  <div>
    <NDivider />
  </div>
</template>

<script setup lang="ts">
  import { NDivider } from 'naive-ui'
</script>

This doesn't:

<template>
  <div>
    <n-divider />
  </div>
</template>

<script setup lang="ts">
  import { NDivider } from 'naive-ui'
</script>

Both are valid template syntax.
A fix would be greatly appreciated. Thanks a lot for developing this plugin :)

One-time donation

Great work! I would like to make a one-time donation, but I cannot find the way. I believe it is up to you to set up the kinds of "plans". Can you set up this? Thanks

How should I use this plugin

i'm not sure how this plugin works.so i haven't ben able to use this plugin.
i just using commands npm i prettier-plugin-organize-imports, and then i using vscode commands alt+shift+F,
but it don't work,
GIF 2021-5-26 7-03-01
So how should I use this plugin?

Import organization adds extra empty lines

On a typescript react project when you have (2+) named imports commented out, this prettier plugin will insert new lines and duplicate them unnecessarily.

Steps to reproduce:

  1. Have a typescript react project (not sure if it only applies to this)
  2. Have a block comment around 2 imports, one of these has to be a named import
    For example:
    /* import { styled } from "@mui/material/styles"; import TextField from "@mui/material/TextField"; */
  3. Prettify the file, and it will insert a blank line between those 2 imports, if you keep prettifying the file, it will duplicate the amount of lines and add an extra one

Here's a gif of the problem
2021-10-15_14-52-10

Also a note: It does not happen with the default VSCode's "Organize Import" (shift + alt + o) functionality.

Intellij - prettier-plugin-organize-imports removes all imports

Hi,

thanks for this great plugin - it works great when running prettier command line.

However in Intellij, it removes all imports when Intellij calls prettier (by means of the format code shortcut). After removing this plugin again, all is well again in Intellij.

Any idea why this is?

Handle JSX Pragma

While generally import order doesn't matter, there are some cases where it does. One of those is when importing a custom JSX pragma. In those cases, the file contains a @jsx comment before importing the JSX pragma.

/** @jsx jsx */
import { jsx } from './jsx';
import { ElementType, forwardRef } from 'react';
import { ResponsiveValue, system } from 'styled-system';

Organizing the imports on this file, results in a new order that breaks the code:

/** @jsx jsx */
import { ElementType, forwardRef } from 'react';
import { ResponsiveValue, system } from 'styled-system';
import { jsx } from './jsx';

I'm not sure if this is something this library can even fix, or if it would be part of the TypeScript implementation?

Using multiple plugins that use typescript and babel parser.

Hi there,

I'm developing my own prettier plugin to organize JS/TS class members. I use this plugin and love it, but I noticed this line in the docs.

This plugin inherits, extends and overrides the built-in Prettier parsers for babel, babel-ts, typescript and vue, i. e., it's incompatible with other plugins that do the same... so only the last loaded plugin that exports one of those parsers will function.

I also override the built-in Prettier parsers for typescript and babel parser to do my manipulation. I'm wondering if there is any way I can modify my code to work with this plugin. I'm new to developing these plugins so I would I really appreciate any advice on how I can make mine work with this one at the same time.

From the docs, it looks like you can run prettier and select with plugins you want to use. Therefore, you could run mine manually this way.

prettier --write src --no-plugin-search --plugin=my-plugin-that-conflicts

Module resolution NodeNext or Node12 seems to break import organization

Running with debug on I am getting an error about host.fileExists is not a function.

Repro here

Full error stack

TypeError: host.fileExists is not a function
    at getPackageJsonInfo (/home/kalvens/rtvision-app/mono/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:43802:37)                                                                                                                                                                                                                                            at Object.getPackageScopeForPath (/home/kalvens/rtvision-app/mono/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:43765:23)
    at lookupFromPackageJson (/home/kalvens/rtvision-app/mono/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:116492:28)
    at Object.getImpliedNodeFormatForFile (/home/kalvens/rtvision-app/mono/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:116486:200)
    at acquireOrUpdateDocument (/home/kalvens/rtvision-app/mono/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:136053:47)
    at Object.acquireDocumentWithKey (/home/kalvens/rtvision-app/mono/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:136030:20)
    at getOrCreateSourceFileByPath (/home/kalvens/rtvision-app/mono/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:165213:41)
    at Object.getOrCreateSourceFile [as getSourceFile] (/home/kalvens/rtvision-app/mono/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:165160:24)
    at findSourceFileWorker (/home/kalvens/rtvision-app/mono/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:118302:29)
    at findSourceFile (/home/kalvens/rtvision-app/mono/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:118200:26)

Cannot read property 'textChanges' of undefined

Hi!
The program sometimes crashes, could you fix it? :)

C:\Users\mokuo\Documents\Projects\scrum-board-front-end\node_modules\prettier\index.js:9755
    throw error.stack;
    ^
TypeError: Cannot read property 'textChanges' of undefined
    at Object.organizeImports [as preprocess] (C:\Users\mokuo\Documents\Projects\scrum-board-front-end\node_modules\prettier-plugin-organize-imports\index.js:17:43)
    at Object.parse (C:\Users\mokuo\Documents\Projects\scrum-board-front-end\node_modules\prettier\index.js:9734:21)
    at coreFormat (C:\Users\mokuo\Documents\Projects\scrum-board-front-end\node_modules\prettier\index.js:13252:23)
    at format (C:\Users\mokuo\Documents\Projects\scrum-board-front-end\node_modules\prettier\index.js:13510:73)
    at formatWithCursor (C:\Users\mokuo\Documents\Projects\scrum-board-front-end\node_modules\prettier\index.js:13526:12)
    at C:\Users\mokuo\Documents\Projects\scrum-board-front-end\node_modules\prettier\index.js:44207:15
    at Object.format (C:\Users\mokuo\Documents\Projects\scrum-board-front-end\node_modules\prettier\index.js:44226:12)
    at _default (C:\Users\mokuo\Documents\Projects\scrum-board-front-end\node_modules\pretty-quick\dist\processFiles.js:42:29)
    at _default (C:\Users\mokuo\Documents\Projects\scrum-board-front-end\node_modules\pretty-quick\dist\index.js:58:29)
    at Object.<anonymous> (C:\Users\mokuo\Documents\Projects\scrum-board-front-end\node_modules\pretty-quick\bin\pretty-quick.js:12:27)  

2.3.x wants to remove synthetic default imports that are actually used

Version 2.2.x works fine for my project, but with 2.3.x, it's started removing a bunch of imports that are actually used.

I tried pulling a minimal repro into the tests here but it (surprisingly) passed just fine, which led me to believe it might only occur with particular TS settings. I think it's related to the allowSyntheticDefaultImports setting, mainly because A) it only seems to remove default imports erroneously and B) when turning that setting off in my project, the TS compiler seems to error on the same imports it wants to remove.

Minimal-ish repro code is:

import Koa from 'koa'

export interface FileStore {
  write(filename: string, data: Readable, options: any): Promise<any>
  addMiddleware(app: Koa): void
}

2.2.x will leave the Koa import, 2.3.x removes it

[Feature request] Add Svelte support

I have used this plugin in Angular, React and Vue projects. Recently we started migrating a codebase to Svelte and I was eager to keep using this plugin for .svelte files... to my surprise there is no support for Svelte and actually I ran into issues where imports were getting removed when using svelte-language-tools + prettier-plugin-organize-imports...

Since there is Vue support for sfc, I think adding support for Svelte should be doable, right?

support sorting exports too

assume a module like this

function a () {
  // do module internal stuff
}

function b () {
  var aResult = a();
  // do stuff with aResult
  return aResult;
}

function c () {
  // do something else
  return x;
}

// I would like to have these exports ordered too
export {c, b}

Do you think your plugin should order exports too or do you see this out of scope?

.vue file with <script> is "fixed" with a lot of "//"

The problem only happens when I have javascript code inside <script></script>

The linter output is like this:

error: Insert `//⏎//⏎//⏎//⏎//⏎//⏎⏎` (prettier/prettier)

But works ok when the code is inside <script lang="ts"></script>

I'm attaching a screenshot of a diff after applying the plugin

Screenshot from 2021-08-13 16-30-44

Versions:
"prettier-plugin-organize-imports": "2.3.3"
"vue": "2.6.14"
"babel-eslint": "10.1.0"
"eslint": "^6.7.2"
"eslint-plugin-prettier": "^3.3.1"
"eslint-plugin-vue": "^6.2.2"
"prettier": "2.3.2"

Pasting into import in VS Code with formatOnPaste and plugin enabled deletes entire import that is not unused

Repro steps

  1. Install VS Code and Prettier extension
  2. git clone https://github.com/nguerrera/repro-import-paste
  3. open newly clone repo folder in VS Code
  4. open test.ts.
  5. Copy the 'b' to the clipboard
  6. Place cursor after 'a', type ',', Ctrl+V to paste

repro

Known workarounds

  • Disable editor.formatOnPaste in .vscode/settings.json
  • Ctrl-Z to undo a bad import removal

Environment

VS Code

Version: 1.69.0-insider (user setup)
Commit: ab6ba241145bb81c8bc0f323453bbec45fd66109
Date: 2022-06-10T05:16:34.957Z (14 hrs ago)
Electron: 17.4.7
Chromium: 98.0.4758.141
Node.js: 16.13.0
V8: 9.8.177.13-electron.0
OS: Windows_NT x64 10.0.22621

Prettier Extension

v9.5.0

RangeError: Maximum call stack size exceeded

Hello there!

I recently switched from JavaScript to TypeScript. I always liked Prettier to keep a consistent code style.
I found your extension very promising but I ran into an issue when using it.

I have several plugins and packages on my TypeScript project but after trying for a long time, I was able to reproduce it and it definitely is caused by your plugin since it works without it.

When I save a TS file in my larger TS project, it won't be formatted at all. It seems like prettier crashes completely.
I have a little exclamation mark in the task bar next to the prettier label and if I click on it, the following message appears:

["ERROR" - 7:38:37 PM] Error formatting document.
RangeError: Maximum call stack size exceeded

The whole log can is here:
log.log

Although it says "Formatting completed" at the end, nothing happens.
And this doesn't happen without this plugin.

Versions:
organize imports: v1.1.1
prettier: v2.2.1
VSCode: 1.52.1
OS: Windows 10 64 bit 2004 but my TS project is living in a WSL2 environment running on Ubuntu 20.04

Support compatibility with other prettier plugins

In the readme, it states:

Caveat

This plugin inherits, extends and overrides the built-in Prettier parsers for babel, babel-ts, typescript and vue, i. e., it's incompatible with other plugins that do the same... so only the last loaded plugin that exports one of those parsers will function.

I want to use this with prettier-plugin-multiline-arrays, but when this plugin is installed then it doesn't work at all, despite that plugin saying it's compatible with this one, so maybe something changed?

That plugin recommends fiddling with the plugin order to get it to work, but no order works when this plugin is installed, possibly something to do with how it just works out the box without requiring being added to prettier plugins list?

    plugins: [
        "prettier-plugin-multiline-arrays", // doesn't work regardless of order
        "prettier-plugin-organize-imports"
    ],

Prettier doesn't work anymore on Vue files

Hi,
I installed your plugin on my Vue.js project.

When running prettier on my project (npx prettier -w .), it changed the order of imports if several Typescript files (as expected), but not any .vue files.

I though "The plugin is not working on Vue files, but it's not a big deal"

But then I noticed that Prettier completely stopped working on Vue files ! Running prettier doesn't throw any error, it just acts like my Vue files are correctly formatted (even if they are not).

Is it expected?
Thanks

Does it not work with pnpm? :thinking:

I'm not sure if there is a problem on my side or if it doesn't really work anymore since I switched a project to use pnpm instead of yarn

Also I'm not sure if this is then a bug of prettier-plugin-organize-imports, prettier or pnpm 🤔

[Feature request] Support Vue SFCs with setup

I like the very slim approach of this lib.

I tried to use it in a Vue 3 project with <script setup> tags, but they are not recognised.
It would be nice to have support for them, too.

Reasoning behind this plugin

I'm curious of the reasons why you decided to create this plugin.
Isn't using in vscode:

"editor.codeActionsOnSave": {
    "source.organizeImports": true ⬅️ 
}

Enough?
Good job on the plugin by the way!

When exports and imports are mixed, imports may be sorted last, possibly leading to errors

Hi there,

thanks for the plugin, it's very useful.

One problem I encountered is the following scenario, where I had mixed exports and imports

export * from './a';
import * as B from './b';
export { B };

the plugin would reorganize this as:

export * from './a';
export { B }; // <----------- export before import
import * as B from './b';

I fixed instances of this problem in my code by always clearly separating them (and will update to the new syntax export * as B from './b' in the future when I upgrade TypeScript), but it's difficult to enforce for the entire team, since this is not a compile time error and will only crash at runtime.

[Feature request] Ignore next line

I encountered the following use case.

Before:

import {...} from 'a-reflect-metadata-dependency';
import 'reflect-metadata';
import {...} from 'another-reflect-metadata-dependency';

After:

import {...} from 'another-reflect-metadata-dependency';
import {...} from 'a-reflect-metadata-dependency';
import 'reflect-metadata';

The reflect-metadata import should be imported before all files what use reflect meta data. The only way out is to ignore organize imports completely, via // organize-imports-ignore. However this is not ideal in my case. Ideally I would like to do something like:

Before:

// organize-imports-ignore-next-line
import 'reflect-metadata';
import {...} from 'a-reflect-metadata-dependency';
import {...} from 'another-reflect-metadata-dependency';

After:

// organize-imports-ignore-next-line
import 'reflect-metadata';
import {...} from 'another-reflect-metadata-dependency';
import {...} from 'a-reflect-metadata-dependency';

3.0.1 incorrectly deleting required imports from typescript files

Hi,

The 3.0.1 update seems to be incorrectly deleting required imports.

The following code demonstrates the issue:

If I have TypeScript:

import { NormalizedCacheObject } from 'apollo-cache-inmemory'
import AWSAppSyncClient from 'aws-appsync'

export class Foo {
  public constructor(private readonly appSyncClient: AWSAppSyncClient<NormalizedCacheObject>) {}
  
  getClient(): any {
    return this.appSyncClient
  }
}

with package.json

{
  "name": "scratch",
  "version": "1.0.0",
  "main": "index.js",
  "license": "UNLICENSED",
  "private": true,
  "scripts": {
    "prettier": "prettier --list-different \"**/*.{json,js,ts,yml,}\""
  },
  "resolutions": {
    "apollo-cache-inmemory": "^1.6.6"
  },
  "devDependencies": {
    "prettier": "^2.7.1",
    "prettier-plugin-organize-imports": "^3.0.1",
    "typescript": "^4.7.4"
  },
  "dependencies": {
    "apollo-cache-inmemory": "^1.6.6",
    "aws-appsync": "^4.1.7"
  }
}

then the import:

import AWSAppSyncClient from 'aws-appsync'

is flagged as needing to be deleted.

If I use 3.0.0 then the import is not flagged.

Support JavaScript

Pretty sure the TypeScript API (organizeImports) can already do that.

Feel free to open a PR or upvote... I don't currently need it so not very high on my priority list.

Buggy experience with vscode formatOnSave + codeactions

It may not be the scope of this project, but I'm just reporting it, so you may judge if it makes sense to fix it.

I think that vscode doesn't expect the formatting to change the file (that much), so it breaks.

Repro

  • Add .vscode/settings.json
{
  "editor.formatOnSave": true,
  "editor.formatOnSaveMode": "modificationsIfAvailable",
  "editor.codeActionsOnSave": {
    // this can be true or false there are bugs with and without it.
    "source.organizeImports": true,
    "source.fixAll": true
  },
}
  • Add a JS file with imports and hit ctrl + s

I had to uninstall it because it was removing all imports.

v2.0.0 is extremely slow in comparaison of v1.1.1

Hi 👋

First, thanks for this amazing project, we use it on all our codebases!
I noticed that since the latest update, the formatting (with prettier --write) is extremely slow to perform.

Before (1.1.1):

Screenshot 2021-05-12 at 15 27 43

After (2.0.0):

Screenshot 2021-05-12 at 15 27 31

[Bug] Unused import = require() are not removed

Hi there,

It seems that if we use import = require() style imports and we do not use the import, then it is not removed after running prettier with this plugin enabled.

This import is common in typescript. See here for more details.

Steps to reproduce:

  1. set up a project with prettier-plugin-organize-imports.
  2. import a node library that uses export = syntax (faker will work).
  3. add import faker = require('faker') to the top of a file.
  4. run prettier and notice that the import is not removed even though it is unused.

I can submit a small demo project as well.

Support for typescript path aliases

Hello, great plugin. I think this is more of a feature request than a bug, but when using typescript path aliases it organizes the global imports from inside the project first than the node_modules global imports.

Expected sorting

  • global node_modules imports
  • global project imports (defined by path aliases)
  • relative imports

But with path aliases (depending on the configuration), global project imports will appear first before the node_modules imports. Right now there's no distinction between global node_modules and global project imports.

tsconfig.json:

{
  "baseUrl": "./",
  "paths": { "@/*": ["./src/*"], },
}

file system:

.
├── src
│   ├── app.js
│   ├── components
│   │   ├── Button.js
│   │   └── Home.js
│   └── shared
│       ├── log.js
│       └── utils.js
└── tsconfig.json

Home.js expected order:

import React from 'react';
import { log } from '@/shared/log';
import Button from './Button';

Instead the result is:

import { log } from '@/shared/log';
import React from 'react';
import Button from './Button';

@/shared/log will be aliased after compilation to ../shared/log, so I expect it to be after react which is a node_module import

Global-to-relative spacing

Is there any way, configuration-wise, that one could leave an empty line between global imports and relative ones?

Instead of this:

import { Observable, Subscription } from 'rxjs'
import { skipWhile, take } from 'rxjs/operators'
import { Test } from '../../test.model'
import * as TestStore from '../../test.action'

It would organize like this:

import { Observable, Subscription } from 'rxjs'
import { skipWhile, take } from 'rxjs/operators'

import { Test } from '../../test.model'
import * as TestStore from '../../test.action'

Great plugin nonetheless, keep up the good work!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.