-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
2,005 additions
and
409 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export * from "./"; | ||
/* This one does not. */ export * from "a"; // Neither does this one. | ||
/* Nor this | ||
one */ export * from "b"; | ||
export * from "x"; | ||
export * from "y"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export * from "x"; | ||
export * from "y"; | ||
|
||
// This comment starts a new group. | ||
/* This one does not. */ export * from "a"; // Neither does this one. | ||
/* Nor this | ||
one */ export * from "b"; | ||
/* But this one does. */ | ||
export * from "./"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { | ||
// First, type imports. (`export { type x, typeof y }` is a syntax error). | ||
type x, | ||
typeof y, | ||
// Numbers are sorted by their numeric value: | ||
img1, | ||
img2, | ||
img10, | ||
// Then everything else, alphabetically: | ||
k, | ||
L, // Case insensitive. | ||
m as anotherName, // Sorted by the “external interface” name “m”, not “anotherName”. | ||
m as tie, // But do use the file-local name in case of a tie. | ||
n, | ||
} from "./x"; | ||
|
||
export { | ||
k, | ||
L, // Case insensitive. | ||
anotherName as m, // Sorted by the “external interface” name “m”, not “anotherName”. | ||
// tie as m, // For exports there can’t be ties – all exports must be unique. | ||
n, | ||
}; | ||
export type { A, B, A as C }; | ||
|
||
var k_, L_, anotherName_, n_; | ||
type A = 1; | ||
type B = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
"use strict"; | ||
|
||
const shared = require("./shared"); | ||
|
||
module.exports = { | ||
meta: { | ||
type: "layout", | ||
fixable: "code", | ||
schema: [], | ||
docs: { | ||
url: | ||
"https://github.com/lydell/eslint-plugin-simple-import-sort#sort-order", | ||
}, | ||
messages: { | ||
sort: "Run autofix to sort these exports!", | ||
}, | ||
}, | ||
create: (context) => ({ | ||
Program: (programNode) => { | ||
const sourceCode = context.getSourceCode(); | ||
for (const chunk of shared.extractChunks(programNode, (node, lastNode) => | ||
isPartOfChunk(node, lastNode, sourceCode) | ||
)) { | ||
maybeReportChunkSorting(chunk, context); | ||
} | ||
}, | ||
ExportNamedDeclaration: (node) => { | ||
if (node.source == null && node.declaration == null) { | ||
maybeReportExportSpecifierSorting(node, context); | ||
} | ||
}, | ||
}), | ||
}; | ||
|
||
function maybeReportChunkSorting(chunk, context) { | ||
const sourceCode = context.getSourceCode(); | ||
const items = shared.getImportExportItems( | ||
chunk, | ||
sourceCode, | ||
() => false, // isSideEffectImport | ||
getSpecifiers | ||
); | ||
const sortedItems = [[shared.sortImportExportItems(items)]]; | ||
const sorted = shared.printSortedItems(sortedItems, items, sourceCode); | ||
const { start } = items[0]; | ||
const { end } = items[items.length - 1]; | ||
shared.maybeReportSorting(context, sorted, start, end); | ||
} | ||
|
||
function maybeReportExportSpecifierSorting(node, context) { | ||
const sorted = shared.printWithSortedSpecifiers( | ||
node, | ||
context.getSourceCode(), | ||
getSpecifiers | ||
); | ||
const [start, end] = node.range; | ||
shared.maybeReportSorting(context, sorted, start, end); | ||
} | ||
|
||
// `export * from "a"` does not have `.specifiers`. | ||
function getSpecifiers(exportNode) { | ||
return exportNode.specifiers || []; | ||
} | ||
|
||
function isPartOfChunk(node, lastNode, sourceCode) { | ||
if (!isExportFrom(node)) { | ||
return "NotPartOfChunk"; | ||
} | ||
|
||
const hasGroupingComment = sourceCode | ||
.getCommentsBefore(node) | ||
.some( | ||
(comment) => | ||
(lastNode == null || comment.loc.start.line > lastNode.loc.end.line) && | ||
comment.loc.end.line < node.loc.start.line | ||
); | ||
|
||
return hasGroupingComment ? "PartOfNewChunk" : "PartOfChunk"; | ||
} | ||
|
||
// Full export-from statement. | ||
// export {a, b} from "A" | ||
// export * from "A" | ||
// export * as A from "A" | ||
function isExportFrom(node) { | ||
return ( | ||
(node.type === "ExportNamedDeclaration" || | ||
node.type === "ExportAllDeclaration") && | ||
node.source != null | ||
); | ||
} |
Oops, something went wrong.