/
index.ts
117 lines (112 loc) · 3.57 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {IMatcherFunction, IStyle, IStyleAPI, IStyleItem} from "import-sort-style";
type Operator = IStyleAPI["and"] | IStyleAPI["or"];
const generateItems: IStyle = (styleApi, baseFile) => {
if (typeof baseFile === "undefined") {
throw new Error(`"isInstalledModule" matcher function requires "baseFile" argument to be defined`);
}
const {
and,
hasDefaultMember,
hasNamedMembers,
hasNamespaceMember,
hasNoMember,
hasOnlyDefaultMember,
hasOnlyNamedMembers,
hasOnlyNamespaceMember,
isInstalledModule,
isNodeModule,
member,
name,
not,
or,
startsWithAlphanumeric,
startsWithLowerCase,
startsWithUpperCase,
unicode,
} = styleApi;
const attach = (
items: IStyleItem[],
matcher: IMatcherFunction,
operator: Operator = and,
): IStyleItem[] => items.map((item) => typeof item.match !== "undefined" ? {...item, match: operator(item.match, matcher)} : item);
const multiply = (
item: IStyleItem,
multipliers: IMatcherFunction[],
operator: Operator = and,
): IStyleItem[] => multipliers.reduce(
(accumulator, multiplier) => accumulator.concat(attach([item], multiplier, operator)),
[] as ReturnType<typeof multiply>,
);
const multiplyByStartsWithMatchers = (item: IStyleItem) => multiply(item, [
not(member(startsWithAlphanumeric)),
member(startsWithUpperCase),
member(startsWithLowerCase),
]);
const thirdPartyModulesMatcher = or(isNodeModule, isInstalledModule(baseFile));
const separationGroup = [
// import "…"
{
match: and(hasNoMember),
},
// import * as _ from "…";
// import * as Foo from "…";
// import * as foo from "…";
...multiplyByStartsWithMatchers({
match: and(
hasOnlyNamespaceMember,
),
sort: member(unicode),
}),
// import _, * as bar from "…";
// import Foo, * as bar from "…";
// import foo, * as bar from "…";
...multiplyByStartsWithMatchers({
match: and(
hasDefaultMember,
hasNamespaceMember,
),
sort: member(unicode),
}),
// import _ from "…";
// import Foo from "…";
// import foo from "…";
...multiplyByStartsWithMatchers({
match: and(
hasOnlyDefaultMember,
),
sort: member(unicode),
}),
// import _, {bar, …} from "…";
// import Foo, {bar, …} from "…";
// import foo, {bar, …} from "…";
...multiplyByStartsWithMatchers({
match: and(
hasDefaultMember,
hasNamedMembers,
),
sort: member(unicode),
sortNamedMembers: name(unicode),
}),
// import {_, bar, …} from "…";
// import {Foo, bar, …} from "…";
// import {foo, bar, …} from "…";
...multiplyByStartsWithMatchers({
match: and(
hasOnlyNamedMembers,
),
sort: member(unicode),
sortNamedMembers: name(unicode),
}),
];
return [
...attach(separationGroup, thirdPartyModulesMatcher),
{
separator: true,
},
...attach(separationGroup, not(thirdPartyModulesMatcher)),
{
separator: true,
},
];
};
export default generateItems;