eslint-plugin-simple-import-sort
Advanced tools
@@ -11,4 +11,3 @@ "use strict"; | ||
docs: { | ||
url: | ||
"https://github.com/lydell/eslint-plugin-simple-import-sort#sort-order", | ||
url: "https://github.com/lydell/eslint-plugin-simple-import-sort#sort-order", | ||
}, | ||
@@ -19,17 +18,35 @@ messages: { | ||
}, | ||
create: (context) => ({ | ||
Program: (programNode) => { | ||
const sourceCode = context.getSourceCode(); | ||
for (const chunk of shared.extractChunks(programNode, (node, lastNode) => | ||
isPartOfChunk(node, lastNode, sourceCode) | ||
)) { | ||
maybeReportChunkSorting(chunk, context); | ||
create: (context) => { | ||
const parents = new Set(); | ||
const addParent = (node) => { | ||
if (isExportFrom(node)) { | ||
parents.add(node.parent); | ||
} | ||
}, | ||
ExportNamedDeclaration: (node) => { | ||
if (node.source == null && node.declaration == null) { | ||
maybeReportExportSpecifierSorting(node, context); | ||
} | ||
}, | ||
}), | ||
}; | ||
return { | ||
ExportNamedDeclaration: (node) => { | ||
if (node.source == null && node.declaration == null) { | ||
maybeReportExportSpecifierSorting(node, context); | ||
} else { | ||
addParent(node); | ||
} | ||
}, | ||
ExportAllDeclaration: addParent, | ||
"Program:exit": () => { | ||
const sourceCode = context.getSourceCode(); | ||
for (const parent of parents) { | ||
for (const chunk of shared.extractChunks(parent, (node, lastNode) => | ||
isPartOfChunk(node, lastNode, sourceCode) | ||
)) { | ||
maybeReportChunkSorting(chunk, context); | ||
} | ||
} | ||
parents.clear(); | ||
}, | ||
}; | ||
}, | ||
}; | ||
@@ -36,0 +53,0 @@ |
@@ -8,2 +8,4 @@ "use strict"; | ||
["^\\u0000"], | ||
// Node.js builtins prefixed with `node:`. | ||
["^node:"], | ||
// Packages. | ||
@@ -42,4 +44,3 @@ // Things that start with a letter (or digit or underscore), or `@` followed by a letter. | ||
docs: { | ||
url: | ||
"https://github.com/lydell/eslint-plugin-simple-import-sort#sort-order", | ||
url: "https://github.com/lydell/eslint-plugin-simple-import-sort#sort-order", | ||
}, | ||
@@ -52,12 +53,23 @@ messages: { | ||
const { groups: rawGroups = defaultGroups } = context.options[0] || {}; | ||
const outerGroups = rawGroups.map((groups) => | ||
groups.map((item) => RegExp(item, "u")) | ||
); | ||
const parents = new Set(); | ||
return { | ||
Program: (programNode) => { | ||
for (const chunk of shared.extractChunks(programNode, (node) => | ||
isImport(node) ? "PartOfChunk" : "NotPartOfChunk" | ||
)) { | ||
maybeReportChunkSorting(chunk, context, outerGroups); | ||
ImportDeclaration: (node) => { | ||
parents.add(node.parent); | ||
}, | ||
"Program:exit": () => { | ||
for (const parent of parents) { | ||
for (const chunk of shared.extractChunks(parent, (node) => | ||
isImport(node) ? "PartOfChunk" : "NotPartOfChunk" | ||
)) { | ||
maybeReportChunkSorting(chunk, context, outerGroups); | ||
} | ||
} | ||
parents.clear(); | ||
}, | ||
@@ -64,0 +76,0 @@ }; |
{ | ||
"name": "eslint-plugin-simple-import-sort", | ||
"version": "7.0.0", | ||
"version": "10.0.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "author": "Simon Lydell", |
@@ -14,2 +14,3 @@ # eslint-plugin-simple-import-sort | ||
- ✅️ 100% code coverage | ||
- ✅️ No dependencies | ||
- ❌ [Does not support `require`][no-require] | ||
@@ -20,3 +21,3 @@ | ||
[@typescript-eslint/parser]: https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser | ||
[eslint-plugin-import]: https://github.com/benmosher/eslint-plugin-import/ | ||
[eslint-plugin-import]: https://github.com/import-js/eslint-plugin-import/ | ||
[no-require]: https://github.com/lydell/eslint-plugin-simple-import-sort/#does-it-support-require | ||
@@ -26,2 +27,4 @@ [prettier]: https://prettier.io/ | ||
## Example | ||
**[➡️ Full readme](https://github.com/lydell/eslint-plugin-simple-import-sort/)** |
@@ -5,3 +5,3 @@ "use strict"; | ||
// and whitespace between. | ||
function extractChunks(programNode, isPartOfChunk) { | ||
function extractChunks(parentNode, isPartOfChunk) { | ||
const chunks = []; | ||
@@ -11,3 +11,3 @@ let chunk = []; | ||
for (const node of programNode.body) { | ||
for (const node of parentNode.body) { | ||
const result = isPartOfChunk(node, lastNode); | ||
@@ -735,8 +735,3 @@ switch (result) { | ||
(itemA, itemB) => | ||
// Put type imports/exports before regular ones. | ||
compare( | ||
getImportExportKind(itemA.node), | ||
getImportExportKind(itemB.node) | ||
) || | ||
// Then compare by imported or exported name (external interface name). | ||
// Compare by imported or exported name (external interface name). | ||
// import { a as b } from "a" | ||
@@ -756,5 +751,10 @@ // ^ | ||
compare(itemA.node.local.name, itemB.node.local.name) || | ||
// Then put type specifiers before regular ones. | ||
compare( | ||
getImportExportKind(itemA.node), | ||
getImportExportKind(itemB.node) | ||
) || | ||
// Keep the original order if the names are the same. It’s not worth | ||
// trying to compare anything else, `import {a, a} from "mod"` is a syntax | ||
// error anyway (but babel-eslint kind of supports it). | ||
// error anyway (but @babel/eslint-parser kind of supports it). | ||
// istanbul ignore next | ||
@@ -761,0 +761,0 @@ itemA.index - itemB.index |
Sorry, the diff of this file is not supported yet
37285
1.74%1005
2.13%27
12.5%