Skip to content

Commit

Permalink
support try, catch, finally. improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
cedeber committed Sep 8, 2024
1 parent 772bfd0 commit 8f90e05
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 53 deletions.
11 changes: 11 additions & 0 deletions examples/vite-project/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ function test2() {

const test3 = () => {
try {
console.log("Test");
} catch {
throw new Error("Test error");
}
};

/**
* @throws {Error}
*/
const test4 = () => {
try {
console.log("Test");
} finally {
throw new Error("Test error");
}
Expand Down
92 changes: 39 additions & 53 deletions lib/rules/throw-documentation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,51 @@ export default {
schema: [], // no options
},
create(context) {
return {
ArrowFunctionExpression(node) {
const sourceCode = context.sourceCode;
const jsDocComment = sourceCode.getJSDocComment(node);

// if (!jsDocComment) return; // If there's no JSDoc, skip

const hasThrow = node.body.body?.some(
(statement) => statement.type === "ThrowStatement"
);

if (hasThrow) {
// Missing JSDoc @throws
if (!jsDocComment) {
context.report({
node: node,
messageId: "missingThrows",
});
return;
}

const throwsTag = jsDocComment.value.includes("@throws");
if (!throwsTag) {
context.report({
node: node,
messageId: "missingThrows",
});
}
/** @param {import("estree").Statement[]} block */
function hasThrowInBlock(block = []) {
return block.some((statement) => {
if (statement.type === "ThrowStatement") return true;
if (statement.type === "TryStatement") {
return (
(statement.handler &&
hasThrowInBlock(statement.handler.body.body)) ||
(statement.finalizer && hasThrowInBlock(statement.finalizer.body))
);
}
},
FunctionDeclaration(node) {
const sourceCode = context.sourceCode;
const jsDocComment = sourceCode.getJSDocComment(node);
return false;
});
}

// if (!jsDocComment) return; // If there's no JSDoc, skip
/** @param {(import("estree").ArrowFunctionExpression | (import("estree").FunctionDeclaration)) & import("eslint").Rule.NodeParentExtension} node */
function checkThrows(node) {
const sourceCode = context.sourceCode;
const jsDocComment = sourceCode.getJSDocComment(node);

const hasThrow = node.body.body?.some(
(statement) => statement.type === "ThrowStatement"
);
const hasThrow = hasThrowInBlock(node.body.body);

if (hasThrow) {
// Missing JSDoc @throws
if (!jsDocComment) {
context.report({
node: node,
messageId: "missingThrows",
});
return;
}
if (hasThrow) {
// Missing JSDoc @throws
if (!jsDocComment) {
context.report({
node: node,
messageId: "missingThrows",
});
return;
}

const throwsTag = jsDocComment.value.includes("@throws");
if (!throwsTag) {
context.report({
node: node,
messageId: "missingThrows",
});
}
const throwsTag = jsDocComment.value.includes("@throws");
if (!throwsTag) {
context.report({
node: node,
messageId: "missingThrows",
});
}
},
}
}

return {
ArrowFunctionExpression: checkThrows,
FunctionDeclaration: checkThrows,
};
},
};
48 changes: 48 additions & 0 deletions lib/tests/throw-documentation.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ ruleTester.run("throw-documentation", rule, {
}
`,
},
{
code: `
/** @throws {Error} */
const test = () => {
try {
console.log('test');
} catch {
throw new Error("Test error");
}
}
`,
},
{
code: `
/** @throws {Error} */
const test = () => {
try {
console.log('test');
} finally {
throw new Error("Test error");
}
}
`,
},
// Arrow Function Expression
{
code: `
Expand Down Expand Up @@ -114,6 +138,30 @@ ruleTester.run("throw-documentation", rule, {
`,
errors: [{ messageId: "missingThrows" }],
},
{
code: `
const test = () => {
try {
console.log('test');
} catch {
throw new Error("Test error");
}
}
`,
errors: [{ messageId: "missingThrows" }],
},
{
code: `
const test = () => {
try {
console.log('test');
} finally {
throw new Error("Test error");
}
}
`,
errors: [{ messageId: "missingThrows" }],
},
{
code: `
/**
Expand Down

0 comments on commit 8f90e05

Please sign in to comment.