@so1ve/eslint-plugin-sort-imports 3.26.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/exports.js +73 -73
- package/src/imports.js +114 -114
- package/src/index.js +4 -4
- package/src/shared.js +732 -732
package/package.json
CHANGED
package/src/exports.js
CHANGED
|
@@ -10,94 +10,94 @@ const getSpecifiers = (exportNode) => exportNode.specifiers || [];
|
|
|
10
10
|
// export * from "A"
|
|
11
11
|
// export * as A from "A"
|
|
12
12
|
const isExportFrom = (node) =>
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
(node.type === "ExportNamedDeclaration" ||
|
|
14
|
+
node.type === "ExportAllDeclaration") &&
|
|
15
|
+
node.source != null;
|
|
16
16
|
|
|
17
17
|
function isPartOfChunk(node, lastNode, sourceCode) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
if (!isExportFrom(node)) {
|
|
19
|
+
return "NotPartOfChunk";
|
|
20
|
+
}
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
const hasGroupingComment = sourceCode
|
|
23
|
+
.getCommentsBefore(node)
|
|
24
|
+
.some(
|
|
25
|
+
(comment) =>
|
|
26
|
+
(lastNode == null || comment.loc.start.line > lastNode.loc.end.line) &&
|
|
27
|
+
comment.loc.end.line < node.loc.start.line,
|
|
28
|
+
);
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
return hasGroupingComment ? "PartOfNewChunk" : "PartOfChunk";
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
function maybeReportChunkSorting(chunk, context) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
34
|
+
const { sourceCode } = context;
|
|
35
|
+
const items = shared.getImportExportItems(
|
|
36
|
+
chunk,
|
|
37
|
+
sourceCode,
|
|
38
|
+
() => false, // isSideEffectImport
|
|
39
|
+
getSpecifiers,
|
|
40
|
+
);
|
|
41
|
+
const sortedItems = [[shared.sortImportExportItems(items)]];
|
|
42
|
+
const sorted = shared.printSortedItems(sortedItems, items, sourceCode);
|
|
43
|
+
const { start } = items[0];
|
|
44
|
+
const { end } = items[items.length - 1];
|
|
45
|
+
shared.maybeReportSorting(context, sorted, start, end);
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
function maybeReportExportSpecifierSorting(node, context) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
const sorted = shared.printWithSortedSpecifiers(
|
|
50
|
+
node,
|
|
51
|
+
context.sourceCode,
|
|
52
|
+
getSpecifiers,
|
|
53
|
+
);
|
|
54
|
+
const [start, end] = node.range;
|
|
55
|
+
shared.maybeReportSorting(context, sorted, start, end);
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
module.exports = {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
59
|
+
meta: {
|
|
60
|
+
type: "layout",
|
|
61
|
+
fixable: "code",
|
|
62
|
+
schema: [],
|
|
63
|
+
docs: {
|
|
64
|
+
url: "https://github.com/lydell/eslint-plugin-simple-import-sort#sort-order",
|
|
65
|
+
},
|
|
66
|
+
messages: {
|
|
67
|
+
sort: "Run autofix to sort these exports!",
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
create: (context) => {
|
|
71
|
+
const parents = new Set();
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
73
|
+
function addParent(node) {
|
|
74
|
+
if (isExportFrom(node)) {
|
|
75
|
+
parents.add(node.parent);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
79
|
+
return {
|
|
80
|
+
"ExportNamedDeclaration": (node) => {
|
|
81
|
+
if (node.source == null && node.declaration == null) {
|
|
82
|
+
maybeReportExportSpecifierSorting(node, context);
|
|
83
|
+
} else {
|
|
84
|
+
addParent(node);
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
"ExportAllDeclaration": addParent,
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
90
|
+
"Program:exit": () => {
|
|
91
|
+
const sourceCode = context.sourceCode;
|
|
92
|
+
for (const parent of parents) {
|
|
93
|
+
for (const chunk of shared.extractChunks(parent, (node, lastNode) =>
|
|
94
|
+
isPartOfChunk(node, lastNode, sourceCode),
|
|
95
|
+
)) {
|
|
96
|
+
maybeReportChunkSorting(chunk, context);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
parents.clear();
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
},
|
|
103
103
|
};
|
package/src/imports.js
CHANGED
|
@@ -3,19 +3,19 @@
|
|
|
3
3
|
const shared = require("./shared");
|
|
4
4
|
|
|
5
5
|
const defaultGroups = [
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
6
|
+
// Node.js builtins prefixed with `node:`.
|
|
7
|
+
["^node:"],
|
|
8
|
+
// Packages.
|
|
9
|
+
// Things that start with a letter (or digit or underscore), or `@` followed by a letter.
|
|
10
|
+
["^@?\\w"],
|
|
11
|
+
// Absolute imports and other imports such as Vue-style `@/foo`.
|
|
12
|
+
// Anything not matched in another group.
|
|
13
|
+
["^"],
|
|
14
|
+
// Relative imports.
|
|
15
|
+
// Anything that starts with a dot.
|
|
16
|
+
["^\\."],
|
|
17
|
+
// Side effect imports.
|
|
18
|
+
["^\\u0000"],
|
|
19
19
|
];
|
|
20
20
|
|
|
21
21
|
// import def, { a, b as c, type d } from "A"
|
|
@@ -24,7 +24,7 @@ const isImportSpecifier = (node) => node.type === "ImportSpecifier";
|
|
|
24
24
|
|
|
25
25
|
// Exclude "ImportDefaultSpecifier" – the "def" in `import def, {a, b}`.
|
|
26
26
|
const getSpecifiers = (importNode) =>
|
|
27
|
-
|
|
27
|
+
importNode.specifiers.filter((node) => isImportSpecifier(node));
|
|
28
28
|
|
|
29
29
|
// Full import statement.
|
|
30
30
|
const isImport = (node) => node.type === "ImportDeclaration";
|
|
@@ -33,118 +33,118 @@ const isImport = (node) => node.type === "ImportDeclaration";
|
|
|
33
33
|
// But not: import {} from "setup"
|
|
34
34
|
// And not: import type {} from "setup"
|
|
35
35
|
const isSideEffectImport = (importNode, sourceCode) =>
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
importNode.specifiers.length === 0 &&
|
|
37
|
+
(!importNode.importKind || importNode.importKind === "value") &&
|
|
38
|
+
!shared.isPunctuator(sourceCode.getFirstToken(importNode, { skip: 1 }), "{");
|
|
39
39
|
|
|
40
40
|
module.exports = {
|
|
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
|
-
|
|
41
|
+
meta: {
|
|
42
|
+
type: "layout",
|
|
43
|
+
fixable: "code",
|
|
44
|
+
schema: [
|
|
45
|
+
{
|
|
46
|
+
type: "object",
|
|
47
|
+
properties: {
|
|
48
|
+
groups: {
|
|
49
|
+
type: "array",
|
|
50
|
+
items: {
|
|
51
|
+
type: "array",
|
|
52
|
+
items: {
|
|
53
|
+
type: "string",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
additionalProperties: false,
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
docs: {
|
|
62
|
+
url: "https://github.com/lydell/eslint-plugin-simple-import-sort#sort-order",
|
|
63
|
+
},
|
|
64
|
+
messages: {
|
|
65
|
+
sort: "Run autofix to sort these imports!",
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
create: (context) => {
|
|
69
|
+
const { groups: rawGroups = defaultGroups } = context.options[0] || {};
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
const outerGroups = rawGroups.map((groups) =>
|
|
72
|
+
groups.map((item) => new RegExp(item, "u")),
|
|
73
|
+
);
|
|
74
74
|
|
|
75
|
-
|
|
75
|
+
const parents = new Set();
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
77
|
+
return {
|
|
78
|
+
"ImportDeclaration": (node) => {
|
|
79
|
+
parents.add(node.parent);
|
|
80
|
+
},
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
82
|
+
"Program:exit": () => {
|
|
83
|
+
for (const parent of parents) {
|
|
84
|
+
for (const chunk of shared.extractChunks(parent, (node) =>
|
|
85
|
+
isImport(node) ? "PartOfChunk" : "NotPartOfChunk",
|
|
86
|
+
)) {
|
|
87
|
+
maybeReportChunkSorting(chunk, context, outerGroups);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
parents.clear();
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
94
|
};
|
|
95
95
|
|
|
96
96
|
function maybeReportChunkSorting(chunk, context, outerGroups) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
97
|
+
const sourceCode = context.sourceCode;
|
|
98
|
+
const items = shared.getImportExportItems(
|
|
99
|
+
chunk,
|
|
100
|
+
sourceCode,
|
|
101
|
+
isSideEffectImport,
|
|
102
|
+
getSpecifiers,
|
|
103
|
+
);
|
|
104
|
+
const sortedItems = makeSortedItems(items, outerGroups);
|
|
105
|
+
const sorted = shared.printSortedItems(sortedItems, items, sourceCode);
|
|
106
|
+
const { start } = items[0];
|
|
107
|
+
const { end } = items[items.length - 1];
|
|
108
|
+
shared.maybeReportSorting(context, sorted, start, end);
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
function makeSortedItems(items, outerGroups) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
112
|
+
const itemGroups = outerGroups.map((groups) =>
|
|
113
|
+
groups.map((regex) => ({ regex, items: [] })),
|
|
114
|
+
);
|
|
115
|
+
const rest = [];
|
|
116
116
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
117
|
+
for (const item of items) {
|
|
118
|
+
const { originalSource } = item.source;
|
|
119
|
+
const source = item.isSideEffectImport
|
|
120
|
+
? `\0${originalSource}`
|
|
121
|
+
: item.source.kind === "value"
|
|
122
|
+
? originalSource
|
|
123
|
+
: `${originalSource}\0`;
|
|
124
|
+
const [matchedGroup] = shared
|
|
125
|
+
// eslint-disable-next-line unicorn/no-array-method-this-argument
|
|
126
|
+
.flatMap(itemGroups, (groups) =>
|
|
127
|
+
groups.map((group) => [group, group.regex.exec(source)]),
|
|
128
|
+
)
|
|
129
|
+
.reduce(
|
|
130
|
+
([group, longestMatch], [nextGroup, nextMatch]) =>
|
|
131
|
+
nextMatch != null &&
|
|
132
|
+
(longestMatch == null || nextMatch[0].length > longestMatch[0].length)
|
|
133
|
+
? [nextGroup, nextMatch]
|
|
134
|
+
: [group, longestMatch],
|
|
135
|
+
[undefined, undefined],
|
|
136
|
+
);
|
|
137
|
+
if (matchedGroup == null) {
|
|
138
|
+
rest.push(item);
|
|
139
|
+
} else {
|
|
140
|
+
matchedGroup.items.push(item);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
143
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
144
|
+
return [...itemGroups, [{ regex: /^/, items: rest }]]
|
|
145
|
+
.map((groups) => groups.filter((group) => group.items.length > 0))
|
|
146
|
+
.filter((groups) => groups.length > 0)
|
|
147
|
+
.map((groups) =>
|
|
148
|
+
groups.map((group) => shared.sortImportExportItems(group.items)),
|
|
149
|
+
);
|
|
150
150
|
}
|
package/src/index.js
CHANGED