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