prettier-plugin-sort-imports icon indicating copy to clipboard operation
prettier-plugin-sort-imports copied to clipboard

feat: type imports

Open samchouse opened this issue 2 years ago • 6 comments

closes #151

Config:

{
  "importOrder": ["<THIRD_PARTY_TYPES>", "^[./]", "<TYPE>^[./]"],
  "importOrderSeparation": true
}

Before:

import type { ImportDeclaration } from '@babel/types';
import type { ImportGroups } from './src/types';
import { getImportNodesMatchedGroup } from './src/utils/get-import-nodes-matched-group';
import { abort } from 'process';

After:

import { abort } from 'process';

import type { ImportDeclaration } from '@babel/types';

import { getImportNodesMatchedGroup } from './src/utils/get-import-nodes-matched-group';

import type { ImportGroups } from './src/types';

samchouse avatar May 14 '22 14:05 samchouse

I'd love to see this as well to specify the location of third-party & local type imports 🙇

DJTB avatar Jun 11 '22 04:06 DJTB

@ayusharma Is this good to merge?

TomBeckett avatar Aug 01 '22 09:08 TomBeckett

Let's merge this one!

vbylen avatar Aug 23 '22 03:08 vbylen

Hi, any update on this PR?

QingyuChai avatar Sep 01 '22 23:09 QingyuChai

For everyone that's waiting for the PR to be merged, if you really want to use this feature you guys can build the package locally to use until this PR merged. I know it's impractical but it's the best we can do right now.

gh repo clone trivago/prettier-plugin-sort-imports
cd prettier-plugin-sort-imports/
gh pr checkout 153

Next bump the version in package.json. After that run this.

yarn install
yarn compile
yarn pack

Copy the tarball over to your project and change the import for this NPM package in package.json to this.

"@trivago/prettier-plugin-sort-imports": "file:./the_path_to_tarball"

Then to finish up remove node_modules and run the install command of your package manager.

samchouse avatar Sep 02 '22 11:09 samchouse

Thank you so much.

Patch file for anyone using patch-package

@trivago+prettier-plugin-sort-imports+3.3.0.patch

diff --git a/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/constants.js b/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/constants.js
index e662721..3cbbefe 100644
--- a/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/constants.js
+++ b/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/constants.js
@@ -1,6 +1,6 @@
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
-exports.newLineNode = exports.THIRD_PARTY_MODULES_SPECIAL_WORD = exports.newLineCharacters = exports.jsx = exports.typescript = exports.flow = void 0;
+exports.newLineNode = exports.TYPE_SPECIAL_WORD = exports.THIRD_PARTY_TYPES_SPECIAL_WORD = exports.THIRD_PARTY_MODULES_SPECIAL_WORD = exports.newLineCharacters = exports.jsx = exports.typescript = exports.flow = void 0;
 var types_1 = require("@babel/types");
 exports.flow = 'flow';
 exports.typescript = 'typescript';
@@ -11,5 +11,7 @@ exports.newLineCharacters = '\n\n';
  * where the not matched imports should be placed
  */
 exports.THIRD_PARTY_MODULES_SPECIAL_WORD = '<THIRD_PARTY_MODULES>';
+exports.THIRD_PARTY_TYPES_SPECIAL_WORD = '<THIRD_PARTY_TYPES>';
+exports.TYPE_SPECIAL_WORD = '<TYPE>';
 var PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE = 'PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE';
 exports.newLineNode = types_1.expressionStatement(types_1.stringLiteral(PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE));
diff --git a/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/utils/get-import-nodes-matched-group.js b/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/utils/get-import-nodes-matched-group.js
index 3d6bf88..c3a7e5c 100644
--- a/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/utils/get-import-nodes-matched-group.js
+++ b/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/utils/get-import-nodes-matched-group.js
@@ -8,16 +8,42 @@ var constants_1 = require("../constants");
  * @param importOrder
  */
 var getImportNodesMatchedGroup = function (node, importOrder) {
-    var groupWithRegExp = importOrder.map(function (group) { return ({
+    var groupWithRegExp = importOrder
+        .sort(function (a, b) {
+        if (a.startsWith(constants_1.TYPE_SPECIAL_WORD) &&
+            !b.startsWith(constants_1.TYPE_SPECIAL_WORD)) {
+            return -1;
+        }
+        if (!a.startsWith(constants_1.TYPE_SPECIAL_WORD) &&
+            b.startsWith(constants_1.TYPE_SPECIAL_WORD)) {
+            return 1;
+        }
+        return 0;
+    })
+        .map(function (group) { return ({
         group: group,
-        regExp: new RegExp(group),
+        regExp: group.startsWith(constants_1.TYPE_SPECIAL_WORD)
+            ? new RegExp(group.replace(constants_1.TYPE_SPECIAL_WORD, ''))
+            : new RegExp(group),
     }); });
     for (var _i = 0, groupWithRegExp_1 = groupWithRegExp; _i < groupWithRegExp_1.length; _i++) {
         var _a = groupWithRegExp_1[_i], group = _a.group, regExp = _a.regExp;
-        var matched = node.source.value.match(regExp) !== null;
-        if (matched)
-            return group;
+        if (group.startsWith(constants_1.TYPE_SPECIAL_WORD)) {
+            if (node.importKind === 'type') {
+                var matched = node.source.value.match(regExp) !== null;
+                if (matched)
+                    return group;
+            }
+        }
+        else {
+            var matched = node.source.value.match(regExp) !== null;
+            if (matched)
+                return group;
+        }
     }
-    return constants_1.THIRD_PARTY_MODULES_SPECIAL_WORD;
+    return node.importKind === 'type' &&
+        importOrder.find(function (group) { return group === constants_1.THIRD_PARTY_TYPES_SPECIAL_WORD; })
+        ? constants_1.THIRD_PARTY_TYPES_SPECIAL_WORD
+        : constants_1.THIRD_PARTY_MODULES_SPECIAL_WORD;
 };
 exports.getImportNodesMatchedGroup = getImportNodesMatchedGroup;

quanglam2807 avatar Sep 14 '22 05:09 quanglam2807

When it will merged?

popuguytheparrot avatar Nov 14 '22 10:11 popuguytheparrot

Hey everyone, we'll try to get this out before the year's end.

byara avatar Nov 24 '22 09:11 byara

Any updates on when we will see this PR merged and a new version released?

instagibb avatar Jan 15 '23 23:01 instagibb

any updates?

popuguytheparrot avatar Feb 15 '23 20:02 popuguytheparrot

Thank you for being so patient. We will put this next week when @byara is back.

ayusharma avatar Feb 16 '23 07:02 ayusharma

Hi everyone, Sorry, I had to revert this change due to #211. This change is still in v4.1.0 and the v4.1.1 revets it. Please give us some more time to release it properly in v5 or the next release.

ayusharma avatar Feb 24 '23 15:02 ayusharma