Skip to content

Commit

Permalink
fix eslint report
Browse files Browse the repository at this point in the history
  • Loading branch information
cedeber committed Sep 18, 2024
1 parent 190040a commit ddd34ed
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
1 change: 1 addition & 0 deletions lib/rules/reject-documentation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* eslint-disable unicorn/no-empty-file */
73 changes: 36 additions & 37 deletions lib/rules/require-throws-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,24 @@ export default {
const throwTypes = getThrowTypes(node.body.body);

if (throwTypes.size > 0) {
// Missing JSDoc @throws
if (!jsDocComment) {
context.report({
node,
messageId: "missingThrows",
data: { type: [...throwTypes].join(", ") },
fix(fixer) {
const nodeStart = node.range[0];
const tokenBefore = sourceCode.getTokenBefore(node, {
filter: (token) => token.type === "Keyword", // "export" keyword
});
const isSameLine = tokenBefore && tokenBefore.loc.end.line === node.loc.start.line;
const insertPosition = tokenBefore && isSameLine ? tokenBefore.range[0] : nodeStart;

// Calculate indentation
const line = sourceCode.lines[node.loc.start.line - 1];
const indentation = line.match(/^\s*/)?.[0];

const jsDoc = `/**\n${indentation} * @throws {${Array.from(throwTypes).join(", ")}}\n${indentation} */\n${indentation}`;

return fixer.insertTextBeforeRange([insertPosition, nodeStart], jsDoc);
},
});
} else {
if (jsDocComment) {
// Check if @throws tag exists
const jsDocValue = jsDocComment.value;
const hasThrowsTag = jsDocValue.includes("@throws");

if (!hasThrowsTag) {
if (hasThrowsTag) {
const throwsTag = jsDocComment.value.includes(
`@throws {${[...throwTypes].join(", ")}}`,
);

if (!throwsTag) {
context.report({
node,
messageId: "missingThrows",
data: { type: [...throwTypes].join(", ") },
});
}
} else {
// Detect if jsDocValue is single or multi-line
const isMultiLine = jsDocValue.includes("\n");

Expand All @@ -72,19 +61,29 @@ export default {
return fixer.replaceText(jsDocComment, `/*${updatedJsDoc}\n${indentation} */`);
},
});
} else {
const throwsTag = jsDocComment.value.includes(
`@throws {${[...throwTypes].join(", ")}}`,
);

if (!throwsTag) {
context.report({
node,
messageId: "missingThrows",
data: { type: [...throwTypes].join(", ") },
});
}
}
} else {
context.report({
node,
messageId: "missingThrows",
data: { type: [...throwTypes].join(", ") },
fix(fixer) {
const nodeStart = node.range[0];
const tokenBefore = sourceCode.getTokenBefore(node, {
filter: (token) => token.type === "Keyword", // "export" keyword
});
const isSameLine = tokenBefore && tokenBefore.loc.end.line === node.loc.start.line;
const insertPosition = tokenBefore && isSameLine ? tokenBefore.range[0] : nodeStart;

// Calculate indentation
const line = sourceCode.lines[node.loc.start.line - 1];
const indentation = line.match(/^\s*/)?.[0];

const jsDoc = `/**\n${indentation} * @throws {${[...throwTypes].join(", ")}}\n${indentation} */\n${indentation}`;

return fixer.insertTextBeforeRange([insertPosition, nodeStart], jsDoc);
},
});
}
}
}
Expand Down

0 comments on commit ddd34ed

Please sign in to comment.