Skip to content

Commit

Permalink
add unicorn eslint plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
cedeber committed Sep 14, 2024
1 parent 09b5abe commit 5016573
Show file tree
Hide file tree
Showing 6 changed files with 769 additions and 14 deletions.
3 changes: 3 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import pluginJs from "@eslint/js";
import eslintPlugin from "eslint-plugin-eslint-plugin";
import nodePlugin from "eslint-plugin-n";
import avaPlugin from "eslint-plugin-ava";
import eslintPluginUnicorn from "eslint-plugin-unicorn";

/** @type import("eslint").Linter.Config[] */
export default [
{ languageOptions: { globals: globals.node } },
pluginJs.configs.recommended,
eslintPlugin.configs["flat/recommended"],
eslintPluginUnicorn.configs["flat/recommended"],
nodePlugin.configs["flat/recommended-script"],
{
rules: {
"eslint-plugin/require-meta-docs-description": "error",
"n/exports-style": ["error", "module.exports"],
"unicorn/prevent-abbreviations": "off",
},
},
{ languageOptions: { ecmaVersion: 2020, sourceType: "module" } },
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync } from "fs";
import { readFileSync } from "node:fs";
import throwDocumentation from "./rules/require-throws-doc.js";
import throwNaming from "./rules/throw-function-naming.js";

Expand Down
10 changes: 5 additions & 5 deletions lib/rules/require-throws-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
context.report({
node,
messageId: "missingThrows",
data: { type: Array.from(throwTypes).join(", ") },
data: { type: [...throwTypes].join(", ") },
fix(fixer) {
const nodeStart = node.range[0];
const tokenBefore = sourceCode.getTokenBefore(node, {
Expand Down Expand Up @@ -58,9 +58,9 @@ export default {
context.report({
node,
messageId: "missingThrows",
data: { type: Array.from(throwTypes).join(", ") },
data: { type: [...throwTypes].join(", ") },
fix(fixer) {
const jsDoc = `@throws {${Array.from(throwTypes).join(", ")}}`;
const jsDoc = `@throws {${[...throwTypes].join(", ")}}`;

// Calculate indentation
const line = sourceCode.lines[node.loc.start.line - 1];
Expand All @@ -74,14 +74,14 @@ export default {
});
} else {
const throwsTag = jsDocComment.value.includes(
`@throws {${Array.from(throwTypes).join(", ")}}`,
`@throws {${[...throwTypes].join(", ")}}`,
);

if (!throwsTag) {
context.report({
node,
messageId: "missingThrows",
data: { type: Array.from(throwTypes).join(", ") },
data: { type: [...throwTypes].join(", ") },
});
}
}
Expand Down
16 changes: 8 additions & 8 deletions lib/rules/throw-function-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ export default {
const fixes = [];

// Handle renaming in import and export statements
allNodes.forEach((statement) => {
for (const statement of allNodes) {
if (statement.type === "ImportDeclaration") {
statement.specifiers.forEach((specifier) => {
for (const specifier of statement.specifiers) {
if (specifier.imported && specifier.imported.name === functionName) {
// Handle import renaming
fixes.push(fixer.replaceText(specifier.imported, newName));
}
});
}
} else if (statement.type === "ExportNamedDeclaration" && !statement.declaration) {
// Handle named export specifiers like `export { funcName }`
statement.specifiers.forEach((specifier) => {
for (const specifier of statement.specifiers) {
if (specifier.exported.name === functionName) {
fixes.push(
fixer.replaceText(
Expand All @@ -71,7 +71,7 @@ export default {
),
);
}
});
}
} else if (
statement.type === "ExportDefaultDeclaration" &&
statement.declaration.id &&
Expand All @@ -80,7 +80,7 @@ export default {
// Handle default export renaming
fixes.push(fixer.replaceText(statement.declaration.id, newName));
}
});
}

// Check if this function is directly exported at its declaration
if (
Expand All @@ -93,11 +93,11 @@ export default {
}

// Search and replace function usage
sourceCode.ast.tokens.forEach((token) => {
for (const token of sourceCode.ast.tokens) {
if (token.type === "Identifier" && token.value === functionName) {
fixes.push(fixer.replaceText(token, newName));
}
});
}

return fixes;
},
Expand Down
Loading

0 comments on commit 5016573

Please sign in to comment.