-
Notifications
You must be signed in to change notification settings - Fork 139
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
feat: type imports #153
Conversation
I'd love to see this as well to specify the location of third-party & local type imports 🙇 |
@ayusharma Is this good to merge? |
Let's merge this one! |
Hi, any update on this PR? |
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 yarn install
yarn compile
yarn pack Copy the tarball over to your project and change the import for this NPM package in "@trivago/prettier-plugin-sort-imports": "file:./the_path_to_tarball" Then to finish up remove |
Thank you so much. Patch file for anyone using
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;
|
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>`.
When it will merged? |
Hey everyone, we'll try to get this out before the year's end. |
There was a problem hiding this 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?
There was a problem hiding this 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? ❤️
Any updates on when we will see this PR merged and a new version released? |
any updates? |
Thank you for being so patient. We will put this next week when @byara is back. |
Revert:Type imports[#153](#153) by[Xenfo](https://github.com/broofa)
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. |
closes #151
Config:
Before:
After: