Skip to content

Commit

Permalink
Update Rule: add arrow exception to enforceExistence rule
Browse files Browse the repository at this point in the history
Ref #159
Closes gh-163
  • Loading branch information
qfox committed Dec 5, 2015
1 parent 739ed3f commit 5cae034
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
34 changes: 22 additions & 12 deletions lib/rules/validate-jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ var jsdoc = require('../jsdoc');
var esprimaHelpers = require('../esprima-helpers');
var validators = require('./validate-jsdoc/index');

/** @type {string[]} - list of function node types */
var functionNodeTypes = [
'FunctionDeclaration',
'FunctionExpression',
'ArrowFunctionExpression',
];

/** @type {string[]} - list of node types with jsdocs */
var jsdocableTypes = []
.concat(functionNodeTypes);

/**
* Rule constructor
*
Expand Down Expand Up @@ -81,10 +92,7 @@ module.exports.prototype = {

var _this = this;
var scopes = {
'function': [
'FunctionDeclaration',
'FunctionExpression'
]
'function': functionNodeTypes,
};

// classic checker
Expand Down Expand Up @@ -213,9 +221,7 @@ function patchNodesInFile(file) {
}

// jsdoc property for nodes
file.iterateNodesByType([
'FunctionDeclaration', 'FunctionExpression'
], function(node) {
file.iterateNodesByType(jsdocableTypes, function(node) {
Object.defineProperty(node, 'jsdoc', {
get: getJsdoc
});
Expand All @@ -228,12 +234,16 @@ function patchNodesInFile(file) {
* @returns {DocComment}
*/
function getJsdoc() {
if (!this.hasOwnProperty('_jsdoc')) {
var node = this.type === 'FunctionExpression' || this.type === 'FunctionDeclaration' ?
findFirstNodeInLine(this) : this;
var res = findDocCommentBeforeNode(node);
this._jsdoc = res ? jsdoc.createDocCommentByCommentNode(res) : null;
if (this.hasOwnProperty('_jsdoc')) {
return this._jsdoc;
}

var node = functionNodeTypes.indexOf(this.type) !== -1 ?
findFirstNodeInLine(this) : this;
var res = findDocCommentBeforeNode(node);

this._jsdoc = res ? jsdoc.createDocCommentByCommentNode(res) : null;

return this._jsdoc;
}

Expand Down
8 changes: 6 additions & 2 deletions lib/rules/validate-jsdoc/enforce-existence.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enforceExistence.configure = function(options) {
var policy = this._enforceExistencePolicy = {
all: true,
anonymous: false,
arrow: true,
exports: true,
expressions: true,
'paramless-procedures': true,
Expand Down Expand Up @@ -65,8 +66,11 @@ function enforceExistence(node, err) {
// don't check anything
return;
}
if (policy.anonymous === false) {
if (!node.id && ['AssignmentExpression', 'VariableDeclarator', 'Property'].indexOf(parentNode.type) === -1) {

if (policy.anonymous === false && node.type !== 'ArrowFunctionExpression' ||
policy.arrow === false && node.type === 'ArrowFunctionExpression') {
if (!node.id && ['AssignmentExpression', 'VariableDeclarator', 'Property',
'ExportDefaultDeclaration'].indexOf(parentNode.type) === -1) {
// don't check anonymous functions
return;
}
Expand Down
19 changes: 16 additions & 3 deletions test/lib/rules/validate-jsdoc/enforce-existence.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,10 @@ describe('lib/rules/validate-jsdoc/enforce-existence', function () {
'export function named (v) {};',
].join('\n'),
}, {
skip: true,
it: 'should report jsdoc absence for default arrow function export (#159)',
code: 'export default (v) => {};',
errors: 1,
}, {
skip: true,
it: 'should not report jsdoc absence for default arrow function export (#159)',
code: [
'/** Foo bar */',
Expand All @@ -190,6 +188,22 @@ describe('lib/rules/validate-jsdoc/enforce-existence', function () {
]);
});

describe('with allExcept arrows and esnext', function() {
checker.rules({enforceExistence: {allExcept: ['arrow']}}, {esnext: true});

checker.cases([
/* jshint ignore:start */
{
it: 'should not report arrows without jsdoc',
code: '() => {};',
}, {
it: 'should not report anonymous without jsdoc',
code: '(function() {})',
}
/* jshint ignore:end */
]);
});

describe('with exceptExports', function() {
checker.rules({enforceExistence: 'exceptExports'});

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

0 comments on commit 5cae034

Please sign in to comment.