prettier-plugin-sort-imports
prettier-plugin-sort-imports copied to clipboard
feat: type imports
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';
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 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.
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;
When it will merged?
Hey everyone, we'll try to get this out before the year's end.
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.
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.