Skip to content

Commit

Permalink
v1.0.70
Browse files Browse the repository at this point in the history
Add new rule for import comments
  • Loading branch information
eliottvincent committed Jan 12, 2024
1 parent 1f86492 commit 0bbb2ae
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Each item has emojis denoting:
- [crisp/enforce-optional](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/enforce-optional.js) (🟠🟢): Enforces use of optional chaining
- [crisp/header-check](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/header-check.js) (🟠🟢): Enforces files to start with Crisp header
- [crisp/header-comments-check](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/header-comments-check.js) (🟠🟢): Enforces different comment blocks before different groups (imports, constants, instances and exports)
- [crisp/import-group-comment](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/import-group-comment.js) (🟢): Ensures `import` statements are preceded by a comment stating their type
- [crisp/methods-naming](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/methods-naming.js) (🟠🟢): Ensures methods are named according to their access (`public`, `private`, `protected`)
- [crisp/methods-ordering](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/methods-ordering.js) (🟠🟢): Ensures methods order according to their access: `public` then `protected` then `private`
- [crisp/multiline-comment-end-backslash](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/multiline-comment-end-backslash.js) (🟠🟢): Enforces multiline comments to end with a backslash
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
"enforce-optional": require("./rules/enforce-optional"),
"header-check": require("./rules/header-check"),
"header-comments-check": require("./rules/header-comments-check"),
"import-group-comment": require("./rules/import-group-comment"),
"jsdoc-align-params": require("./rules/jsdoc-align-params"),
"jsdoc-check-indentation": require("./rules/jsdoc-check-indentation"),
"jsdoc-check-optional-params": require("./rules/jsdoc-check-optional-params"),
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-crisp",
"version": "1.0.69",
"version": "1.0.70",
"description": "Custom ESLint Rules for Crisp",
"author": "Crisp IM SAS",
"main": "index.js",
Expand Down
1 change: 1 addition & 0 deletions recommended-vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ module.exports = {
"crisp/enforce-optional": "error",
"crisp/header-check": "error",
"crisp/header-comments-check": "error",
"crisp/import-group-comment": "error",
"crisp/methods-naming": "error",
"crisp/methods-ordering": "error",
"crisp/multiline-comment-end-backslash": "error",
Expand Down
108 changes: 108 additions & 0 deletions rules/import-group-comment.js
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,
};
}
};

0 comments on commit 0bbb2ae

Please sign in to comment.