Skip to content

Commit 1e8bf34

Browse files
committed
fix(require-jsdoc) for publicOnly, check named export declarations appearing as assignments
refactoring(require-jsdoc): compare anonymous default found with node in case may ever differ in future
1 parent 0bd35d1 commit 1e8bf34

File tree

3 files changed

+105
-7
lines changed

3 files changed

+105
-7
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3673,6 +3673,25 @@ be checked by the rule.
36733673
The following patterns are considered problems:
36743674

36753675
````js
3676+
export var test = function () {
3677+
3678+
};
3679+
// Options: [{"publicOnly":true,"require":{"FunctionExpression":true}}]
3680+
// Message: Missing JSDoc comment.
3681+
3682+
function test () {
3683+
3684+
}
3685+
export var test2 = test;
3686+
// Options: [{"publicOnly":true,"require":{"FunctionDeclaration":true}}]
3687+
// Message: Missing JSDoc comment.
3688+
3689+
export const test = () => {
3690+
3691+
};
3692+
// Options: [{"publicOnly":true,"require":{"ArrowFunctionExpression":true}}]
3693+
// Message: Missing JSDoc comment.
3694+
36763695
export default function () {}
36773696
// Options: [{"publicOnly":{"cjs":false,"esm":true,"window":false},"require":{"FunctionDeclaration":true}}]
36783697
// Message: Missing JSDoc comment.

src/exportParser.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,17 @@ const initVariables = function (node, globals, opts) {
206206
}
207207
});
208208
break;
209+
} case 'ExportNamedDeclaration': {
210+
if (node.declaration) {
211+
initVariables(node.declaration, globals, opts);
212+
}
213+
break;
209214
}
210215
}
211216
};
212217

213218
// Populates variable maps using AST
214-
const mapVariables = function (node, globals, opt) {
219+
const mapVariables = function (node, globals, opt, isExport) {
215220
/* istanbul ignore next */
216221
const opts = opt || {};
217222
/* istanbul ignore next */
@@ -234,7 +239,10 @@ const mapVariables = function (node, globals, opt) {
234239
} case 'VariableDeclaration': {
235240
node.declarations.forEach((declaration) => {
236241
const isGlobal = opts.initWindow && node.kind === 'var' && globals.props.window;
237-
createSymbol(declaration.id, globals, declaration.init, globals, isGlobal);
242+
const symbol = createSymbol(declaration.id, globals, declaration.init, globals, isGlobal);
243+
if (symbol && isExport) {
244+
symbol.exported = true;
245+
}
238246
});
239247
break;
240248
} case 'FunctionDeclaration': {
@@ -253,10 +261,14 @@ const mapVariables = function (node, globals, opt) {
253261
break;
254262
} case 'ExportNamedDeclaration': {
255263
if (node.declaration) {
256-
const symbol = createSymbol(node.declaration, globals, node.declaration);
257-
/* istanbul ignore next */
258-
if (symbol) {
259-
symbol.exported = true;
264+
if (node.declaration.type === 'VariableDeclaration') {
265+
mapVariables(node.declaration, globals, opts, true);
266+
} else {
267+
const symbol = createSymbol(node.declaration, globals, node.declaration);
268+
/* istanbul ignore next */
269+
if (symbol) {
270+
symbol.exported = true;
271+
}
260272
}
261273
}
262274
node.specifiers.forEach((specifier) => {
@@ -314,7 +326,7 @@ const findNode = function (node, block, cache) {
314326
};
315327

316328
const findExportedNode = function (block, node, cache) {
317-
if (block.ANONYMOUS_DEFAULT) {
329+
if (block.ANONYMOUS_DEFAULT === node) {
318330
return true;
319331
}
320332
/* istanbul ignore next */

test/rules/assertions/requireJsdoc.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,73 @@
44

55
export default {
66
invalid: [
7+
{
8+
code: `
9+
export var test = function () {
10+
11+
};
12+
`,
13+
errors: [
14+
{
15+
message: 'Missing JSDoc comment.',
16+
type: 'FunctionExpression'
17+
}
18+
],
19+
options: [{
20+
publicOnly: true,
21+
require: {
22+
FunctionExpression: true
23+
}
24+
}],
25+
parserOptions: {
26+
sourceType: 'module'
27+
}
28+
},
29+
{
30+
code: `
31+
function test () {
32+
33+
}
34+
export var test2 = test;
35+
`,
36+
errors: [
37+
{
38+
message: 'Missing JSDoc comment.',
39+
type: 'FunctionDeclaration'
40+
}
41+
],
42+
options: [{
43+
publicOnly: true,
44+
require: {
45+
FunctionDeclaration: true
46+
}
47+
}],
48+
parserOptions: {
49+
sourceType: 'module'
50+
}
51+
},
52+
{
53+
code: `
54+
export const test = () => {
55+
56+
};
57+
`,
58+
errors: [
59+
{
60+
message: 'Missing JSDoc comment.',
61+
type: 'ArrowFunctionExpression'
62+
}
63+
],
64+
options: [{
65+
publicOnly: true,
66+
require: {
67+
ArrowFunctionExpression: true
68+
}
69+
}],
70+
parserOptions: {
71+
sourceType: 'module'
72+
}
73+
},
774
{
875
code: `
976
export default function () {}

0 commit comments

Comments
 (0)