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: type imports #153

Merged
merged 10 commits into from Feb 23, 2023
Merged

feat: type imports #153

merged 10 commits into from Feb 23, 2023

Conversation

Xenfo
Copy link
Contributor

@Xenfo Xenfo commented May 14, 2022

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';

@DJTB
Copy link

DJTB commented Jun 11, 2022

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

@TomBeckett
Copy link

@ayusharma Is this good to merge?

@vbylen
Copy link

vbylen commented Aug 23, 2022

Let's merge this one!

@QingyuChai
Copy link

Hi, any update on this PR?

@Xenfo
Copy link
Contributor Author

Xenfo commented Sep 2, 2022

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.

@quanglam2807
Copy link

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;

@ayusharma ayusharma requested review from byara and ayusharma and removed request for byara October 20, 2022 10:22
@ayusharma ayusharma added the v3 label Oct 21, 2022
IanVS added a commit to IanVS/prettier-plugin-sort-imports that referenced this pull request Oct 27, 2022
Closes #35

Adapted from trivago/prettier-plugin-sort-imports#153, by @Xenfo.  

This adds a new special string, `<TYPES>` that can be added to the `importOrder` array.  When used, it will cause type imports to be sorted as specified by its location in the array.  

Notes:
- If it is used, it will disable `importOrderCombineTypeAndValueImports`, throwing a warning if both are used.  This is because:
- We will split apart type and value import declarations if `<TYPES>` is used, so that types can be sorted appropriately.  
- Thinking towards the next breaking change when we remove options, I think the default will be to enable `importOrderCombineTypeAndValueImports`, and this change will give users a good way to opt-out of that behavior if they want, by specifying the location for `<TYPES>`.
@popuguytheparrot
Copy link

When it will merged?

@byara
Copy link
Collaborator

byara commented Nov 24, 2022

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

Copy link
Collaborator

@byara byara left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution 🎉
I left a few comment. Would you be able to take a look?

src/constants.ts Outdated Show resolved Hide resolved
src/constants.ts Outdated Show resolved Hide resolved
src/utils/get-import-nodes-matched-group.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@ayusharma ayusharma left a comment

Choose a reason for hiding this comment

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

Thank you so much for being patient with the PR. Overall it looks good to me. I suggested a few minor changes. Could you please also add some snapshot tests? ❤️

src/utils/get-import-nodes-matched-group.ts Outdated Show resolved Hide resolved
@byara byara removed the v3 label Dec 14, 2022
@Xenfo Xenfo requested review from ayusharma and byara and removed request for ayusharma and byara December 15, 2022 23:12
@Xenfo Xenfo requested review from byara and removed request for ayusharma January 3, 2023 16:59
@instagibb
Copy link

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

@popuguytheparrot
Copy link

any updates?

@ayusharma
Copy link
Collaborator

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

@byara byara merged commit 5b2e3aa into trivago:master Feb 23, 2023
ayusharma added a commit that referenced this pull request Feb 24, 2023
Revert:Type imports[#153](#153) by[Xenfo](https://github.com/broofa)
@ayusharma
Copy link
Collaborator

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.

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.

Add the ability to order types
10 participants