@so1ve/eslint-plugin-sort-imports 3.3.2 → 3.5.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 +5 -5
- package/src/imports.js +9 -5
- package/src/shared.js +46 -41
package/package.json
CHANGED
package/src/exports.js
CHANGED
|
@@ -10,9 +10,9 @@ const getSpecifiers = (exportNode) => exportNode.specifiers || [];
|
|
|
10
10
|
// export * from "A"
|
|
11
11
|
// export * as A from "A"
|
|
12
12
|
const isExportFrom = (node) =>
|
|
13
|
-
(node.type === "ExportNamedDeclaration"
|
|
14
|
-
node.type === "ExportAllDeclaration")
|
|
15
|
-
node.source != null;
|
|
13
|
+
(node.type === "ExportNamedDeclaration"
|
|
14
|
+
|| node.type === "ExportAllDeclaration")
|
|
15
|
+
&& node.source != null;
|
|
16
16
|
|
|
17
17
|
function isPartOfChunk(node, lastNode, sourceCode) {
|
|
18
18
|
if (!isExportFrom(node)) {
|
|
@@ -23,8 +23,8 @@ function isPartOfChunk(node, lastNode, sourceCode) {
|
|
|
23
23
|
.getCommentsBefore(node)
|
|
24
24
|
.some(
|
|
25
25
|
(comment) =>
|
|
26
|
-
(lastNode == null || comment.loc.start.line > lastNode.loc.end.line)
|
|
27
|
-
comment.loc.end.line < node.loc.start.line,
|
|
26
|
+
(lastNode == null || comment.loc.start.line > lastNode.loc.end.line)
|
|
27
|
+
&& comment.loc.end.line < node.loc.start.line,
|
|
28
28
|
);
|
|
29
29
|
|
|
30
30
|
return hasGroupingComment ? "PartOfNewChunk" : "PartOfChunk";
|
package/src/imports.js
CHANGED
|
@@ -33,9 +33,12 @@ 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
|
-
importNode.specifiers.length === 0
|
|
37
|
-
(!importNode.importKind || importNode.importKind === "value")
|
|
38
|
-
!shared.isPunctuator(
|
|
36
|
+
importNode.specifiers.length === 0
|
|
37
|
+
&& (!importNode.importKind || importNode.importKind === "value")
|
|
38
|
+
&& !shared.isPunctuator(
|
|
39
|
+
sourceCode.getFirstToken(importNode, { skip: 1 }),
|
|
40
|
+
"{",
|
|
41
|
+
);
|
|
39
42
|
|
|
40
43
|
module.exports = {
|
|
41
44
|
meta: {
|
|
@@ -128,8 +131,9 @@ function makeSortedItems(items, outerGroups) {
|
|
|
128
131
|
)
|
|
129
132
|
.reduce(
|
|
130
133
|
([group, longestMatch], [nextGroup, nextMatch]) =>
|
|
131
|
-
nextMatch != null
|
|
132
|
-
(longestMatch == null
|
|
134
|
+
nextMatch != null
|
|
135
|
+
&& (longestMatch == null
|
|
136
|
+
|| nextMatch[0].length > longestMatch[0].length)
|
|
133
137
|
? [nextGroup, nextMatch]
|
|
134
138
|
: [group, longestMatch],
|
|
135
139
|
[undefined, undefined],
|
package/src/shared.js
CHANGED
|
@@ -190,8 +190,10 @@ function printCommentsBefore(node, comments, sourceCode) {
|
|
|
190
190
|
const next = index === lastIndex ? node : comments[index + 1];
|
|
191
191
|
|
|
192
192
|
return (
|
|
193
|
-
sourceCode.getText(comment)
|
|
194
|
-
removeBlankLines(
|
|
193
|
+
sourceCode.getText(comment)
|
|
194
|
+
+ removeBlankLines(
|
|
195
|
+
sourceCode.text.slice(comment.range[1], next.range[0]),
|
|
196
|
+
)
|
|
195
197
|
);
|
|
196
198
|
})
|
|
197
199
|
.join("");
|
|
@@ -255,16 +257,19 @@ const sortImportExportItems = (items) =>
|
|
|
255
257
|
: itemB.isSideEffectImport
|
|
256
258
|
? 1
|
|
257
259
|
: // Compare the `from` part.
|
|
258
|
-
compare(itemA.source.source, itemB.source.source)
|
|
260
|
+
compare(itemA.source.source, itemB.source.source)
|
|
259
261
|
// The `.source` has been slightly tweaked. To stay fully deterministic,
|
|
260
262
|
// also sort on the original value.
|
|
261
|
-
compare(
|
|
263
|
+
|| compare(
|
|
264
|
+
itemA.source.originalSource,
|
|
265
|
+
itemB.source.originalSource,
|
|
266
|
+
)
|
|
262
267
|
// Then put type imports/exports before regular ones.
|
|
263
|
-
compare(itemA.source.kind, itemB.source.kind)
|
|
268
|
+
|| compare(itemA.source.kind, itemB.source.kind)
|
|
264
269
|
// Keep the original order if the sources are the same. It’s not worth
|
|
265
270
|
// trying to compare anything else, and you can use `import/no-duplicates`
|
|
266
271
|
// to get rid of the problem anyway.
|
|
267
|
-
itemA.index - itemB.index,
|
|
272
|
+
|| itemA.index - itemB.index,
|
|
268
273
|
);
|
|
269
274
|
|
|
270
275
|
const sortSpecifierItems = (items) =>
|
|
@@ -278,23 +283,23 @@ const sortSpecifierItems = (items) =>
|
|
|
278
283
|
compare(
|
|
279
284
|
(itemA.node.imported || itemA.node.exported).name,
|
|
280
285
|
(itemB.node.imported || itemB.node.exported).name,
|
|
281
|
-
)
|
|
286
|
+
)
|
|
282
287
|
// Then compare by the file-local name.
|
|
283
288
|
// import { a as b } from "a"
|
|
284
289
|
// ^
|
|
285
290
|
// export { b as a }
|
|
286
291
|
// ^
|
|
287
|
-
compare(itemA.node.local.name, itemB.node.local.name)
|
|
292
|
+
|| compare(itemA.node.local.name, itemB.node.local.name)
|
|
288
293
|
// Then put type specifiers before regular ones.
|
|
289
|
-
compare(
|
|
294
|
+
|| compare(
|
|
290
295
|
getImportExportKind(itemA.node),
|
|
291
296
|
getImportExportKind(itemB.node),
|
|
292
|
-
)
|
|
297
|
+
)
|
|
293
298
|
// Keep the original order if the names are the same. It’s not worth
|
|
294
299
|
// trying to compare anything else, `import {a, a} from "mod"` is a syntax
|
|
295
300
|
// error anyway (but @babel/eslint-parser kind of supports it).
|
|
296
301
|
// istanbul ignore next
|
|
297
|
-
itemA.index - itemB.index,
|
|
302
|
+
|| itemA.index - itemB.index,
|
|
298
303
|
);
|
|
299
304
|
|
|
300
305
|
// A “chunk” is a sequence of statements of a certain type with only comments
|
|
@@ -380,16 +385,16 @@ function printSortedItems(sortedItems, originalItems, sourceCode) {
|
|
|
380
385
|
? sourceCode.getTokenAfter(lastOriginalItem.node, {
|
|
381
386
|
includeComments: true,
|
|
382
387
|
filter: (token) =>
|
|
383
|
-
!isLineComment(token)
|
|
384
|
-
!(
|
|
385
|
-
isBlockComment(token)
|
|
386
|
-
token.loc.end.line === lastOriginalItem.node.loc.end.line
|
|
388
|
+
!isLineComment(token)
|
|
389
|
+
&& !(
|
|
390
|
+
isBlockComment(token)
|
|
391
|
+
&& token.loc.end.line === lastOriginalItem.node.loc.end.line
|
|
387
392
|
),
|
|
388
393
|
})
|
|
389
394
|
: undefined;
|
|
390
395
|
const maybeNewline =
|
|
391
|
-
nextToken != null
|
|
392
|
-
nextToken.loc.start.line === lastOriginalItem.node.loc.end.line
|
|
396
|
+
nextToken != null
|
|
397
|
+
&& nextToken.loc.start.line === lastOriginalItem.node.loc.end.line
|
|
393
398
|
? newline
|
|
394
399
|
: "";
|
|
395
400
|
|
|
@@ -425,9 +430,9 @@ function getImportExportItems(
|
|
|
425
430
|
.getCommentsBefore(node)
|
|
426
431
|
.filter(
|
|
427
432
|
(comment) =>
|
|
428
|
-
comment.loc.start.line <= node.loc.start.line
|
|
429
|
-
comment.loc.end.line > lastLine
|
|
430
|
-
(nodeIndex > 0 || comment.loc.start.line > lastLine),
|
|
433
|
+
comment.loc.start.line <= node.loc.start.line
|
|
434
|
+
&& comment.loc.end.line > lastLine
|
|
435
|
+
&& (nodeIndex > 0 || comment.loc.start.line > lastLine),
|
|
431
436
|
);
|
|
432
437
|
|
|
433
438
|
// Get all comments after the import/export that are on the same line.
|
|
@@ -456,11 +461,11 @@ function getImportExportItems(
|
|
|
456
461
|
);
|
|
457
462
|
|
|
458
463
|
const code =
|
|
459
|
-
indentation
|
|
460
|
-
before
|
|
461
|
-
printWithSortedSpecifiers(node, sourceCode, getSpecifiers)
|
|
462
|
-
after
|
|
463
|
-
trailingSpaces;
|
|
464
|
+
indentation
|
|
465
|
+
+ before
|
|
466
|
+
+ printWithSortedSpecifiers(node, sourceCode, getSpecifiers)
|
|
467
|
+
+ after
|
|
468
|
+
+ trailingSpaces;
|
|
464
469
|
|
|
465
470
|
const all = [...commentsBefore, node, ...commentsAfter];
|
|
466
471
|
const [start] = all[0].range;
|
|
@@ -477,8 +482,8 @@ function getImportExportItems(
|
|
|
477
482
|
source,
|
|
478
483
|
index: nodeIndex,
|
|
479
484
|
needsNewline:
|
|
480
|
-
commentsAfter.length > 0
|
|
481
|
-
isLineComment(commentsAfter[commentsAfter.length - 1]),
|
|
485
|
+
commentsAfter.length > 0
|
|
486
|
+
&& isLineComment(commentsAfter[commentsAfter.length - 1]),
|
|
482
487
|
};
|
|
483
488
|
});
|
|
484
489
|
}
|
|
@@ -507,10 +512,10 @@ function handleLastSemicolon(chunk, sourceCode) {
|
|
|
507
512
|
}
|
|
508
513
|
|
|
509
514
|
const semicolonBelongsToNode =
|
|
510
|
-
nextToLastToken.loc.end.line === lastToken.loc.start.line
|
|
515
|
+
nextToLastToken.loc.end.line === lastToken.loc.start.line
|
|
511
516
|
// If there’s no more code after the last import/export the semicolon has to
|
|
512
517
|
// belong to the import/export, even if it is not on the same line.
|
|
513
|
-
sourceCode.getTokenAfter(lastToken) == null;
|
|
518
|
+
|| sourceCode.getTokenAfter(lastToken) == null;
|
|
514
519
|
|
|
515
520
|
if (semicolonBelongsToNode) {
|
|
516
521
|
return chunk;
|
|
@@ -541,9 +546,9 @@ function printWithSortedSpecifiers(node, sourceCode, getSpecifiers) {
|
|
|
541
546
|
const specifiers = getSpecifiers(node);
|
|
542
547
|
|
|
543
548
|
if (
|
|
544
|
-
openBraceIndex === -1
|
|
545
|
-
closeBraceIndex === -1
|
|
546
|
-
specifiers.length <= 1
|
|
549
|
+
openBraceIndex === -1
|
|
550
|
+
|| closeBraceIndex === -1
|
|
551
|
+
|| specifiers.length <= 1
|
|
547
552
|
) {
|
|
548
553
|
return printTokens(allTokens);
|
|
549
554
|
}
|
|
@@ -574,11 +579,11 @@ function printWithSortedSpecifiers(node, sourceCode, getSpecifiers) {
|
|
|
574
579
|
// Add a newline if the item needs one, unless the previous item (if any)
|
|
575
580
|
// already ends with a newline.
|
|
576
581
|
const maybeNewline =
|
|
577
|
-
previous != null
|
|
578
|
-
needsStartingNewline(item.before)
|
|
579
|
-
!(
|
|
580
|
-
previous.after.length > 0
|
|
581
|
-
isNewline(previous.after[previous.after.length - 1])
|
|
582
|
+
previous != null
|
|
583
|
+
&& needsStartingNewline(item.before)
|
|
584
|
+
&& !(
|
|
585
|
+
previous.after.length > 0
|
|
586
|
+
&& isNewline(previous.after[previous.after.length - 1])
|
|
582
587
|
)
|
|
583
588
|
? [{ type: "Newline", code: newline }]
|
|
584
589
|
: [];
|
|
@@ -610,8 +615,8 @@ function printWithSortedSpecifiers(node, sourceCode, getSpecifiers) {
|
|
|
610
615
|
});
|
|
611
616
|
|
|
612
617
|
const maybeNewline =
|
|
613
|
-
needsStartingNewline(itemsResult.after)
|
|
614
|
-
!isNewline(sorted[sorted.length - 1])
|
|
618
|
+
needsStartingNewline(itemsResult.after)
|
|
619
|
+
&& !isNewline(sorted[sorted.length - 1])
|
|
615
620
|
? [{ type: "Newline", code: newline }]
|
|
616
621
|
: [];
|
|
617
622
|
|
|
@@ -864,8 +869,8 @@ function needsStartingNewline(tokens) {
|
|
|
864
869
|
const firstToken = before[0];
|
|
865
870
|
|
|
866
871
|
return (
|
|
867
|
-
isLineComment(firstToken)
|
|
868
|
-
(isBlockComment(firstToken) && !hasNewline(firstToken.code))
|
|
872
|
+
isLineComment(firstToken)
|
|
873
|
+
|| (isBlockComment(firstToken) && !hasNewline(firstToken.code))
|
|
869
874
|
);
|
|
870
875
|
}
|
|
871
876
|
|