Skip to content

Commit

Permalink
fix test, add indentation support, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
cedeber committed Sep 9, 2024
1 parent d3e5492 commit c95273b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 13 deletions.
10 changes: 3 additions & 7 deletions examples/invalid.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
*
* @param {string} foo
* @returns {string}
*/
export function quux1(foo) {
throw new Error("err");
Expand All @@ -8,9 +9,7 @@ export function quux1(foo) {

quux1();

/**
*
*/
/** @param {string} foo */
const quux2 = function (foo) {
throw new Error("err");
};
Expand All @@ -24,9 +23,6 @@ const quux3 = (foo) => {
};
// Message: Missing JSDoc @throws declaration.

/**
*
*/
function quux4(foo) {
while (true) {
throw new Error("err");
Expand Down
2 changes: 1 addition & 1 deletion examples/package-lock.json

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

15 changes: 13 additions & 2 deletions lib/rules/require-throws-doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ export default {
messageId: "missingThrows",
data: { type: Array.from(throwTypes).join(", ") },
fix(fixer) {
const jsDoc = `/**\n * @throws {${Array.from(throwTypes).join(", ")}}\n */\n`;
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);
},
});
Expand All @@ -57,10 +63,15 @@ export default {
data: { type: Array.from(throwTypes).join(", ") },
fix(fixer) {
const jsDoc = `@throws {${Array.from(throwTypes).join(", ")}}`;

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

const updatedJsDoc = isMultiLine
? `${jsDocValue}* ${jsDoc}`
: `*\n ${jsDocValue}\n * ${jsDoc}`;
return fixer.replaceText(jsDocComment, `/*${updatedJsDoc}\n */`);
return fixer.replaceText(jsDocComment, `/*${updatedJsDoc}\n${indentation} */`);
},
});
} else {
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-throw-aware",
"version": "1.0.0-beta.4",
"version": "1.0.0-beta.5",
"description": "An ESLint plugin to enforce naming conventions and JSDoc annotations for functions that throw exceptions.",
"type": "module",
"main": "lib/index.mjs",
Expand Down
58 changes: 58 additions & 0 deletions tests/require-throws-doc.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ ruleTester.run("require-throws-doc", rule, {
}
`,
errors: [{ messageId: "missingThrows", data: { type: "Error" } }],
output: `
/**
* @throws {Error}
*/
function test() {
throw new Error('test');
}
`,
},
{
code: `
Expand All @@ -111,6 +119,15 @@ ruleTester.run("require-throws-doc", rule, {
}
`,
errors: [{ messageId: "missingThrows", data: { type: "Error" } }],
output: `
/**
* This function throws an error
* @throws {Error}
*/
function test() {
throw new Error('test');
}
`,
},
{
code: `
Expand All @@ -119,6 +136,14 @@ ruleTester.run("require-throws-doc", rule, {
}
`,
errors: [{ messageId: "missingThrows", data: { type: "Error" } }],
output: `
/**
* @throws {Error}
*/
const test = () => {
throw new Error('test');
}
`,
},
{
code: `
Expand All @@ -131,6 +156,18 @@ ruleTester.run("require-throws-doc", rule, {
}
`,
errors: [{ messageId: "missingThrows", data: { type: "Error" } }],
output: `
/**
* @throws {Error}
*/
const test = () => {
try {
console.log('test');
} catch {
throw new Error("Test error");
}
}
`,
},
{
code: `
Expand All @@ -143,6 +180,18 @@ ruleTester.run("require-throws-doc", rule, {
}
`,
errors: [{ messageId: "missingThrows", data: { type: "Error" } }],
output: `
/**
* @throws {Error}
*/
const test = () => {
try {
console.log('test');
} finally {
throw new Error("Test error");
}
}
`,
},
{
code: `
Expand All @@ -154,6 +203,15 @@ ruleTester.run("require-throws-doc", rule, {
}
`,
errors: [{ messageId: "missingThrows", data: { type: "Error" } }],
output: `
/**
* This function throws an error
* @throws {Error}
*/
const test = () => {
throw new Error('test');
}
`,
},
{
code: `
Expand Down

0 comments on commit c95273b

Please sign in to comment.