Skip to content

Commit 1560619

Browse files
authored
Merge pull request #11 from Xvezda/feature/factory-pattern
Add factory pattern support
2 parents 70120af + 0e316f7 commit 1560619

File tree

5 files changed

+100
-3
lines changed

5 files changed

+100
-3
lines changed

examples/fixed.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,13 @@ const lol = () => {
4747
} catch {}
4848
};
4949
lol();
50+
51+
function factory() {
52+
/**
53+
* @throws {Error}
54+
*/
55+
return function () {
56+
throw new Error();
57+
}
58+
}
59+
factory();

examples/unsafe.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ const lol = () => {
3333
console.log(egg.ham.spam);
3434
};
3535
lol();
36+
37+
function factory() {
38+
return function () {
39+
throw new Error();
40+
}
41+
}
42+
factory();

src/rules/no-undocumented-throws.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ module.exports = createRule({
7575
const isCommented =
7676
comments.length &&
7777
comments
78-
.map(({ value }) => value)
79-
.some(hasThrowsTag);
78+
.map(({ value }) => value)
79+
.some(hasThrowsTag);
8080

8181
/** @type {import('typescript').Type[]} */
8282
const throwTypes = throwStatementNodes
@@ -176,11 +176,13 @@ module.exports = createRule({
176176
'VariableDeclaration > VariableDeclarator[id.type="Identifier"] > ArrowFunctionExpression:exit': visitOnExit,
177177
'Property > ArrowFunctionExpression:exit': visitOnExit,
178178
'PropertyDefinition > ArrowFunctionExpression:exit': visitOnExit,
179+
'ReturnStatement > ArrowFunctionExpression:exit': visitOnExit,
179180

180181
'VariableDeclaration > VariableDeclarator[id.type="Identifier"] > FunctionExpression:exit': visitOnExit,
181182
'Property > FunctionExpression:exit': visitOnExit,
182183
'PropertyDefinition > FunctionExpression:exit': visitOnExit,
183184
'MethodDefinition > FunctionExpression:exit': visitOnExit,
185+
'ReturnStatement > FunctionExpression:exit': visitOnExit,
184186
};
185187
},
186188
});

src/rules/no-undocumented-throws.test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,74 @@ ruleTester.run(
568568
{ messageId: 'missingThrowsTag' },
569569
],
570570
},
571+
{
572+
code: `
573+
function factory() {
574+
return function () {
575+
throw new Error();
576+
}
577+
}
578+
`,
579+
output: `
580+
function factory() {
581+
/**
582+
* @throws {Error}
583+
*/
584+
return function () {
585+
throw new Error();
586+
}
587+
}
588+
`,
589+
errors: [
590+
{ messageId: 'missingThrowsTag' },
591+
],
592+
},
593+
{
594+
code: `
595+
function factory() {
596+
return () => {
597+
throw new Error();
598+
}
599+
}
600+
`,
601+
output: `
602+
function factory() {
603+
/**
604+
* @throws {Error}
605+
*/
606+
return () => {
607+
throw new Error();
608+
}
609+
}
610+
`,
611+
errors: [
612+
{ messageId: 'missingThrowsTag' },
613+
],
614+
},
615+
{
616+
code: `
617+
function factory() {
618+
function inner() {
619+
throw new Error();
620+
}
621+
return inner;
622+
}
623+
`,
624+
output: `
625+
function factory() {
626+
/**
627+
* @throws {Error}
628+
*/
629+
function inner() {
630+
throw new Error();
631+
}
632+
return inner;
633+
}
634+
`,
635+
errors: [
636+
{ messageId: 'missingThrowsTag' },
637+
],
638+
},
571639
],
572640
},
573641
);

src/utils.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,17 @@ const findNodeToComment = (node) => {
239239
* const target = () => { ... };
240240
* ```
241241
*/
242-
findParent(node, (n) => n.type === AST_NODE_TYPES.VariableDeclaration)
242+
findParent(node, (n) => n.type === AST_NODE_TYPES.VariableDeclaration) ??
243+
/**
244+
* @example
245+
* ```
246+
* function factory() {
247+
* // here
248+
* return function target() { ... };
249+
* }
250+
* ```
251+
*/
252+
findParent(node, (n) => n.type === AST_NODE_TYPES.ReturnStatement)
243253
);
244254
default:
245255
break;

0 commit comments

Comments
 (0)