-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
114 additions
and
3 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,108 @@ | ||
module.exports = { | ||
meta: { | ||
type: "suggestion", | ||
docs: { | ||
description: "Enforce import statements to be grouped and preceded by a comment", | ||
category: "Best Practices", | ||
recommended: false, | ||
}, | ||
fixable: null, // This rule is not auto-fixable | ||
}, | ||
|
||
create(context) { | ||
let currentGroupComment = null; | ||
|
||
// Extract the directory name from the file path | ||
function getDirectoryName(filePath) { | ||
const pathParts = filePath.split('/'); | ||
return pathParts[pathParts.length - 2].toUpperCase(); // Directory name | ||
} | ||
|
||
function extractGroupFromPath(path, filePath) { | ||
// Relative path import? | ||
if (path.startsWith("./")) { | ||
return getDirectoryName(filePath); | ||
} | ||
|
||
// Alias path import? | ||
if (path.startsWith("@/")) { | ||
const parts = path.split("/"); | ||
|
||
if (parts.length === 2) { | ||
return "MAIN"; | ||
} | ||
|
||
return parts[1].toUpperCase(); | ||
} | ||
|
||
return "NPM"; | ||
} | ||
|
||
function generateExpectedComment(group) { | ||
if (group === "NPM") { | ||
return group; | ||
} | ||
|
||
return `PROJECT: ${group}`; | ||
} | ||
|
||
function isGroupComment(comment) { | ||
return ( | ||
comment.type === "Line" && | ||
( | ||
comment.value.trim().startsWith("PROJECT:") || | ||
comment.value.trim().startsWith("NPM") | ||
) | ||
); | ||
} | ||
|
||
function isIgnoreComment(comment) { | ||
// Skip some comments | ||
return ( | ||
comment.type === "Line" && | ||
comment.value.trim().startsWith("@ts-ignore") | ||
); | ||
} | ||
|
||
function updateCurrentGroupComment(node) { | ||
const comments = context.getSourceCode().getCommentsBefore(node); | ||
|
||
if (comments.length > 0) { | ||
for (let i = comments.length - 1; i >= 0; i--) { | ||
if (isIgnoreComment(comments[i])) { | ||
continue; | ||
} | ||
|
||
if (isGroupComment(comments[i])) { | ||
currentGroupComment = comments[i]; | ||
|
||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function checkImportGroup(node) { | ||
const importPath = node.source.value; | ||
const filePath = context.getFilename(); | ||
|
||
// Get group from import path | ||
const expectedGroup = extractGroupFromPath(importPath, filePath); | ||
const expectedComment = generateExpectedComment(expectedGroup); | ||
|
||
// Get comment for current group | ||
updateCurrentGroupComment(node); | ||
|
||
if (!currentGroupComment || !currentGroupComment.value.toUpperCase().includes(expectedComment.toUpperCase())) { | ||
context.report({ | ||
node, | ||
message: `Import "${importPath}" should be in the "${expectedComment}" group.`, | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
ImportDeclaration: checkImportGroup, | ||
}; | ||
} | ||
}; |