Skip to content

Commit 5cae034

Browse files
committed
Update Rule: add arrow exception to enforceExistence rule
Ref #159 Closes gh-163
1 parent 739ed3f commit 5cae034

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

lib/rules/validate-jsdoc.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ var jsdoc = require('../jsdoc');
44
var esprimaHelpers = require('../esprima-helpers');
55
var validators = require('./validate-jsdoc/index');
66

7+
/** @type {string[]} - list of function node types */
8+
var functionNodeTypes = [
9+
'FunctionDeclaration',
10+
'FunctionExpression',
11+
'ArrowFunctionExpression',
12+
];
13+
14+
/** @type {string[]} - list of node types with jsdocs */
15+
var jsdocableTypes = []
16+
.concat(functionNodeTypes);
17+
718
/**
819
* Rule constructor
920
*
@@ -81,10 +92,7 @@ module.exports.prototype = {
8192

8293
var _this = this;
8394
var scopes = {
84-
'function': [
85-
'FunctionDeclaration',
86-
'FunctionExpression'
87-
]
95+
'function': functionNodeTypes,
8896
};
8997

9098
// classic checker
@@ -213,9 +221,7 @@ function patchNodesInFile(file) {
213221
}
214222

215223
// jsdoc property for nodes
216-
file.iterateNodesByType([
217-
'FunctionDeclaration', 'FunctionExpression'
218-
], function(node) {
224+
file.iterateNodesByType(jsdocableTypes, function(node) {
219225
Object.defineProperty(node, 'jsdoc', {
220226
get: getJsdoc
221227
});
@@ -228,12 +234,16 @@ function patchNodesInFile(file) {
228234
* @returns {DocComment}
229235
*/
230236
function getJsdoc() {
231-
if (!this.hasOwnProperty('_jsdoc')) {
232-
var node = this.type === 'FunctionExpression' || this.type === 'FunctionDeclaration' ?
233-
findFirstNodeInLine(this) : this;
234-
var res = findDocCommentBeforeNode(node);
235-
this._jsdoc = res ? jsdoc.createDocCommentByCommentNode(res) : null;
237+
if (this.hasOwnProperty('_jsdoc')) {
238+
return this._jsdoc;
236239
}
240+
241+
var node = functionNodeTypes.indexOf(this.type) !== -1 ?
242+
findFirstNodeInLine(this) : this;
243+
var res = findDocCommentBeforeNode(node);
244+
245+
this._jsdoc = res ? jsdoc.createDocCommentByCommentNode(res) : null;
246+
237247
return this._jsdoc;
238248
}
239249

lib/rules/validate-jsdoc/enforce-existence.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enforceExistence.configure = function(options) {
1616
var policy = this._enforceExistencePolicy = {
1717
all: true,
1818
anonymous: false,
19+
arrow: true,
1920
exports: true,
2021
expressions: true,
2122
'paramless-procedures': true,
@@ -65,8 +66,11 @@ function enforceExistence(node, err) {
6566
// don't check anything
6667
return;
6768
}
68-
if (policy.anonymous === false) {
69-
if (!node.id && ['AssignmentExpression', 'VariableDeclarator', 'Property'].indexOf(parentNode.type) === -1) {
69+
70+
if (policy.anonymous === false && node.type !== 'ArrowFunctionExpression' ||
71+
policy.arrow === false && node.type === 'ArrowFunctionExpression') {
72+
if (!node.id && ['AssignmentExpression', 'VariableDeclarator', 'Property',
73+
'ExportDefaultDeclaration'].indexOf(parentNode.type) === -1) {
7074
// don't check anonymous functions
7175
return;
7276
}

test/lib/rules/validate-jsdoc/enforce-existence.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,10 @@ describe('lib/rules/validate-jsdoc/enforce-existence', function () {
174174
'export function named (v) {};',
175175
].join('\n'),
176176
}, {
177-
skip: true,
178177
it: 'should report jsdoc absence for default arrow function export (#159)',
179178
code: 'export default (v) => {};',
180179
errors: 1,
181180
}, {
182-
skip: true,
183181
it: 'should not report jsdoc absence for default arrow function export (#159)',
184182
code: [
185183
'/** Foo bar */',
@@ -190,6 +188,22 @@ describe('lib/rules/validate-jsdoc/enforce-existence', function () {
190188
]);
191189
});
192190

191+
describe('with allExcept arrows and esnext', function() {
192+
checker.rules({enforceExistence: {allExcept: ['arrow']}}, {esnext: true});
193+
194+
checker.cases([
195+
/* jshint ignore:start */
196+
{
197+
it: 'should not report arrows without jsdoc',
198+
code: '() => {};',
199+
}, {
200+
it: 'should not report anonymous without jsdoc',
201+
code: '(function() {})',
202+
}
203+
/* jshint ignore:end */
204+
]);
205+
});
206+
193207
describe('with exceptExports', function() {
194208
checker.rules({enforceExistence: 'exceptExports'});
195209

@@ -224,7 +238,6 @@ describe('lib/rules/validate-jsdoc/enforce-existence', function () {
224238
code: 'export default function (v) {};',
225239
errors: 0,
226240
}, {
227-
skip: true,
228241
it: 'should not report jsdoc absence for default arrow function export (#159)',
229242
code: 'export default (v) => {};',
230243
errors: 0,

0 commit comments

Comments
 (0)