@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@so1ve/eslint-plugin-sort-imports",
3
- "version": "3.26.0",
3
+ "version": "4.0.0",
4
4
  "author": "Simon Lydell",
5
5
  "contributors": [
6
6
  "Ray <i@mk1.io> (https://github.com/so1ve)"
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
- (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
- if (!isExportFrom(node)) {
19
- return "NotPartOfChunk";
20
- }
18
+ if (!isExportFrom(node)) {
19
+ return "NotPartOfChunk";
20
+ }
21
21
 
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
- );
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
- return hasGroupingComment ? "PartOfNewChunk" : "PartOfChunk";
30
+ return hasGroupingComment ? "PartOfNewChunk" : "PartOfChunk";
31
31
  }
32
32
 
33
33
  function maybeReportChunkSorting(chunk, context) {
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);
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
- 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);
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
- 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();
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
- function addParent(node) {
74
- if (isExportFrom(node)) {
75
- parents.add(node.parent);
76
- }
77
- }
73
+ function addParent(node) {
74
+ if (isExportFrom(node)) {
75
+ parents.add(node.parent);
76
+ }
77
+ }
78
78
 
79
- return {
80
- "ExportNamedDeclaration": (node) => {
81
- if (node.source == null && node.declaration == null) {
82
- maybeReportExportSpecifierSorting(node, context);
83
- } else {
84
- addParent(node);
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
- "ExportAllDeclaration": addParent,
88
+ "ExportAllDeclaration": addParent,
89
89
 
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
- },
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
- // 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"],
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
- importNode.specifiers.filter((node) => isImportSpecifier(node));
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
- importNode.specifiers.length === 0 &&
37
- (!importNode.importKind || importNode.importKind === "value") &&
38
- !shared.isPunctuator(sourceCode.getFirstToken(importNode, { skip: 1 }), "{");
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
- 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] || {};
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
- const outerGroups = rawGroups.map((groups) =>
72
- groups.map((item) => new RegExp(item, "u")),
73
- );
71
+ const outerGroups = rawGroups.map((groups) =>
72
+ groups.map((item) => new RegExp(item, "u")),
73
+ );
74
74
 
75
- const parents = new Set();
75
+ const parents = new Set();
76
76
 
77
- return {
78
- "ImportDeclaration": (node) => {
79
- parents.add(node.parent);
80
- },
77
+ return {
78
+ "ImportDeclaration": (node) => {
79
+ parents.add(node.parent);
80
+ },
81
81
 
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
- },
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
- 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);
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
- const itemGroups = outerGroups.map((groups) =>
113
- groups.map((regex) => ({ regex, items: [] })),
114
- );
115
- const rest = [];
112
+ const itemGroups = outerGroups.map((groups) =>
113
+ groups.map((regex) => ({ regex, items: [] })),
114
+ );
115
+ const rest = [];
116
116
 
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
- }
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
- 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
- );
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
@@ -4,8 +4,8 @@ const importsRule = require("./imports");
4
4
  const exportsRule = require("./exports");
5
5
 
6
6
  module.exports = {
7
- rules: {
8
- imports: importsRule,
9
- exports: exportsRule,
10
- },
7
+ rules: {
8
+ imports: importsRule,
9
+ exports: exportsRule,
10
+ },
11
11
  };