@serverless-guru/prettier-plugin-import-order
Version:
A prettier plugin to sort TS/JS import declarations by provided Regular Expression order
41 lines (40 loc) • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMatchedGroup = void 0;
var constants_1 = require("../constants");
function evaluate(group) {
return function match(importNode) {
if (group === constants_1.TYPE_IMPORTS_SPECIAL_WORD) {
return { matched: importNode.importKind === 'type', group: group };
}
else if (importNode.source.value) {
return { matched: new RegExp(group).test(importNode.source.value), group: group };
}
return { matched: false, group: group };
};
}
var getMatchedGroup = function (node, importOrderWithoutEmptyLinesOrThirdPartyRegex, handleTypes) {
var reorderesList = importOrderWithoutEmptyLinesOrThirdPartyRegex;
if (handleTypes) {
var indexOfTypeGroup = importOrderWithoutEmptyLinesOrThirdPartyRegex.findIndex(function (group) { return group === constants_1.TYPE_IMPORTS_SPECIAL_WORD; });
if (indexOfTypeGroup === 0) {
}
else if (indexOfTypeGroup === importOrderWithoutEmptyLinesOrThirdPartyRegex.length - 1) {
reorderesList = [constants_1.TYPE_IMPORTS_SPECIAL_WORD].concat(importOrderWithoutEmptyLinesOrThirdPartyRegex.slice(0, importOrderWithoutEmptyLinesOrThirdPartyRegex.length - 1));
}
else {
reorderesList = [constants_1.TYPE_IMPORTS_SPECIAL_WORD]
.concat(importOrderWithoutEmptyLinesOrThirdPartyRegex.slice(0, indexOfTypeGroup))
.concat(importOrderWithoutEmptyLinesOrThirdPartyRegex.slice(indexOfTypeGroup + 1, importOrderWithoutEmptyLinesOrThirdPartyRegex.length));
}
}
var allGroupsRegexesFuncs = reorderesList.map(function (group) { return evaluate(group); });
for (var _i = 0, allGroupsRegexesFuncs_1 = allGroupsRegexesFuncs; _i < allGroupsRegexesFuncs_1.length; _i++) {
var evaluateFunc = allGroupsRegexesFuncs_1[_i];
var _a = evaluateFunc(node), matched = _a.matched, group = _a.group;
if (matched)
return group;
}
return constants_1.THIRD_PARTY_MODULES_SPECIAL_WORD;
};
exports.getMatchedGroup = getMatchedGroup;