Skip to content

Commit 8a60089

Browse files
authored
Merge pull request #69 from Xvezda/feature/handle-export-function
Handle exported function declaration
2 parents 49d89dd + e314116 commit 8a60089

File tree

2 files changed

+286
-0
lines changed

2 files changed

+286
-0
lines changed

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

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,266 @@ ruleTester.run(
20902090
{ messageId: 'missingThrowsTag' },
20912091
],
20922092
},
2093+
{
2094+
code: `
2095+
export function foo() {
2096+
throw new Error();
2097+
}
2098+
`,
2099+
output: `
2100+
/**
2101+
* @throws {Error}
2102+
*/
2103+
export function foo() {
2104+
throw new Error();
2105+
}
2106+
`,
2107+
errors: [
2108+
{ messageId: 'missingThrowsTag' },
2109+
],
2110+
},
2111+
{
2112+
code: `
2113+
export default function foo() {
2114+
throw new Error();
2115+
}
2116+
`,
2117+
output: `
2118+
/**
2119+
* @throws {Error}
2120+
*/
2121+
export default function foo() {
2122+
throw new Error();
2123+
}
2124+
`,
2125+
errors: [
2126+
{ messageId: 'missingThrowsTag' },
2127+
],
2128+
},
2129+
{
2130+
code: `
2131+
export async function foo() {
2132+
throw new Error();
2133+
}
2134+
`,
2135+
output: `
2136+
/**
2137+
* @throws {Promise<Error>}
2138+
*/
2139+
export async function foo() {
2140+
throw new Error();
2141+
}
2142+
`,
2143+
errors: [
2144+
{ messageId: 'missingThrowsTag' },
2145+
],
2146+
},
2147+
{
2148+
code: `
2149+
export default async function foo() {
2150+
throw new Error();
2151+
}
2152+
`,
2153+
output: `
2154+
/**
2155+
* @throws {Promise<Error>}
2156+
*/
2157+
export default async function foo() {
2158+
throw new Error();
2159+
}
2160+
`,
2161+
errors: [
2162+
{ messageId: 'missingThrowsTag' },
2163+
],
2164+
},
2165+
{
2166+
code: `
2167+
export function foo() {
2168+
return new Promise((resolve, reject) => {
2169+
reject(new Error());
2170+
});
2171+
}
2172+
`,
2173+
output: `
2174+
/**
2175+
* @throws {Promise<Error>}
2176+
*/
2177+
export function foo() {
2178+
return new Promise((resolve, reject) => {
2179+
reject(new Error());
2180+
});
2181+
}
2182+
`,
2183+
errors: [
2184+
{ messageId: 'missingThrowsTag' },
2185+
],
2186+
},
2187+
{
2188+
code: `
2189+
export default function foo() {
2190+
return new Promise((resolve, reject) => {
2191+
reject(new Error());
2192+
});
2193+
}
2194+
`,
2195+
output: `
2196+
/**
2197+
* @throws {Promise<Error>}
2198+
*/
2199+
export default function foo() {
2200+
return new Promise((resolve, reject) => {
2201+
reject(new Error());
2202+
});
2203+
}
2204+
`,
2205+
errors: [
2206+
{ messageId: 'missingThrowsTag' },
2207+
],
2208+
},
2209+
{
2210+
code: `
2211+
export const foo = function () {
2212+
throw new Error();
2213+
};
2214+
`,
2215+
output: `
2216+
/**
2217+
* @throws {Error}
2218+
*/
2219+
export const foo = function () {
2220+
throw new Error();
2221+
};
2222+
`,
2223+
errors: [
2224+
{ messageId: 'missingThrowsTag' },
2225+
],
2226+
},
2227+
{
2228+
code: `
2229+
export let foo = function () {
2230+
throw new Error();
2231+
};
2232+
`,
2233+
output: `
2234+
/**
2235+
* @throws {Error}
2236+
*/
2237+
export let foo = function () {
2238+
throw new Error();
2239+
};
2240+
`,
2241+
errors: [
2242+
{ messageId: 'missingThrowsTag' },
2243+
],
2244+
},
2245+
{
2246+
code: `
2247+
export const foo = () => {
2248+
throw new Error();
2249+
};
2250+
`,
2251+
output: `
2252+
/**
2253+
* @throws {Error}
2254+
*/
2255+
export const foo = () => {
2256+
throw new Error();
2257+
};
2258+
`,
2259+
errors: [
2260+
{ messageId: 'missingThrowsTag' },
2261+
],
2262+
},
2263+
{
2264+
code: `
2265+
export let foo = () => {
2266+
throw new Error();
2267+
};
2268+
`,
2269+
output: `
2270+
/**
2271+
* @throws {Error}
2272+
*/
2273+
export let foo = () => {
2274+
throw new Error();
2275+
};
2276+
`,
2277+
errors: [
2278+
{ messageId: 'missingThrowsTag' },
2279+
],
2280+
},
2281+
{
2282+
code: `
2283+
export const foo = async function () {
2284+
throw new Error();
2285+
};
2286+
`,
2287+
output: `
2288+
/**
2289+
* @throws {Promise<Error>}
2290+
*/
2291+
export const foo = async function () {
2292+
throw new Error();
2293+
};
2294+
`,
2295+
errors: [
2296+
{ messageId: 'missingThrowsTag' },
2297+
],
2298+
},
2299+
{
2300+
code: `
2301+
export let foo = async function () {
2302+
throw new Error();
2303+
};
2304+
`,
2305+
output: `
2306+
/**
2307+
* @throws {Promise<Error>}
2308+
*/
2309+
export let foo = async function () {
2310+
throw new Error();
2311+
};
2312+
`,
2313+
errors: [
2314+
{ messageId: 'missingThrowsTag' },
2315+
],
2316+
},
2317+
{
2318+
code: `
2319+
export const foo = async () => {
2320+
throw new Error();
2321+
};
2322+
`,
2323+
output: `
2324+
/**
2325+
* @throws {Promise<Error>}
2326+
*/
2327+
export const foo = async () => {
2328+
throw new Error();
2329+
};
2330+
`,
2331+
errors: [
2332+
{ messageId: 'missingThrowsTag' },
2333+
],
2334+
},
2335+
{
2336+
code: `
2337+
export let foo = async () => {
2338+
throw new Error();
2339+
};
2340+
`,
2341+
output: `
2342+
/**
2343+
* @throws {Promise<Error>}
2344+
*/
2345+
export let foo = async () => {
2346+
throw new Error();
2347+
};
2348+
`,
2349+
errors: [
2350+
{ messageId: 'missingThrowsTag' },
2351+
],
2352+
},
20932353
],
20942354
},
20952355
);

src/utils.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,23 @@ const findNodeToComment = (node) => {
474474
* ```
475475
*/
476476
case AST_NODE_TYPES.FunctionDeclaration:
477+
/**
478+
* Exported function declaration should be commented at export node,
479+
* not at the function declaration itself.
480+
*
481+
* @example
482+
* ```
483+
* // here
484+
* export default function target() { ... }
485+
* // ^ not here
486+
* ```
487+
*/
488+
if (
489+
node.parent?.type === AST_NODE_TYPES.ExportNamedDeclaration ||
490+
node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration
491+
) {
492+
return node.parent;
493+
}
477494
return node;
478495
case AST_NODE_TYPES.Identifier:
479496
case AST_NODE_TYPES.FunctionExpression:
@@ -539,6 +556,15 @@ const findNodeToComment = (node) => {
539556
* ```
540557
*/
541558
findParent(node, (n) => n.type === AST_NODE_TYPES.Property) ??
559+
/**
560+
* @example
561+
* ```
562+
* // here
563+
* export const target = () => { ... };
564+
* // ^ node
565+
* ```
566+
*/
567+
findParent(node, (n) => n.type === AST_NODE_TYPES.ExportNamedDeclaration) ??
542568
/**
543569
* @example
544570
* ```

0 commit comments

Comments
 (0)