Skip to content

Commit 955d3ac

Browse files
committed
Merge pull request #44 from zxqfox/hotfix/invalid-configuration-should-throws
fixup rule configuration asserts and add tests
2 parents c6f52bc + ef018b9 commit 955d3ac

12 files changed

+193
-31
lines changed

lib/rules/validate-jsdoc/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ var validatorsByName = module.exports = {
1919

2020
Object.defineProperty(validatorsByName, 'load', {
2121
value: function loadValidators(passedOptions) {
22-
var validators = [];
23-
2422
if (!passedOptions) {
25-
return validators;
23+
return [];
2624
}
2725

26+
var validators = [];
27+
2828
Object.keys(validatorsByName).forEach(function(name) {
2929
var v = validatorsByName[name];
3030

@@ -50,22 +50,22 @@ Object.defineProperty(validatorsByName, 'load', {
5050

5151
Object.defineProperty(validatorsByName, 'checkOptions', {
5252
value: function checkOptions(validator, options) {
53-
Object.keys(validator.options).forEach(function(data, option) {
53+
Object.keys(validator.options).forEach(function(option) {
54+
var data = validator.options[option];
5455
if (!data.allowedValues) {
5556
return;
5657
}
5758

58-
var values;
59-
if (typeof data.allowedValues === 'function') {
60-
values = data.allowedValues();
59+
var values = data.allowedValues;
60+
if (typeof values === 'function') {
61+
values = values();
6162
}
6263

63-
if (!Array.isArray(values)) {
64-
throw new Error('Internal error in jsDoc validator ' + validator._name);
64+
assert(Array.isArray(values), 'Internal error in jsDoc validator ' + validator._name);
6565

66-
} else if (values.length > 1) {
66+
if (values.length > 1) {
6767
assert(values.indexOf(options[option]) !== -1,
68-
'Available values for option jsDoc.' + option + ' are ' + values.map(JSON.stringify).join(', '));
68+
'Accepted values for option jsDoc.' + option + ' are ' + values.map(JSON.stringify).join(', '));
6969

7070
} else if (values.length) {
7171
assert(values[0] === options[option],

test/init.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var expect = chai.expect;
66
global.parse = parse;
77
global.fnBody = fnBody;
88
global.checker = rulesChecker;
9+
global.expect = expect;
910

1011
function fnBody(func) {
1112
var str = func.toString();

test/lib/rules/validate-jsdoc/check-param-names.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
describe('rules/validate-jsdoc', function () {
1+
describe('lib/rules/validate-jsdoc/check-param-names', function () {
22
var checker = global.checker({
33
additionalRules: ['lib/rules/validate-jsdoc.js']
44
});
55

6-
describe('check-param-names', function () {
6+
describe('configured', function() {
7+
8+
it('with undefined should throws', function() {
9+
global.expect(function() {
10+
checker.configure({checkParamNames: undefined});
11+
}).to.throws(/accepted value/i);
12+
});
13+
14+
it('with undefined should throws', function() {
15+
global.expect(function() {
16+
checker.configure({checkParamNames: {}});
17+
}).to.throws(/accepted value/i);
18+
});
19+
20+
});
21+
22+
describe('with true', function() {
723
checker.rules({checkParamNames: true});
824

925
checker.cases([

test/lib/rules/validate-jsdoc/check-redundant-access.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
describe('rules/validate-jsdoc', function () {
1+
describe('lib/rules/validate-jsdoc/check-redundant-access', function () {
22
var checker = global.checker({
33
additionalRules: ['lib/rules/validate-jsdoc.js']
44
});
55

6-
describe('check-redundant-access', function () {
6+
describe('configured', function() {
77

8+
it('with undefined should throws', function() {
9+
global.expect(function() {
10+
checker.configure({checkRedundantAccess: undefined});
11+
}).to.throws(/accepted value/i);
12+
});
13+
14+
it('with undefined should throws', function() {
15+
global.expect(function() {
16+
checker.configure({checkRedundantAccess: {}});
17+
}).to.throws(/accepted value/i);
18+
});
19+
20+
});
21+
22+
describe('with true', function() {
823
checker.rules({checkRedundantAccess: true});
24+
925
checker.cases([
1026
/* jshint ignore:start */
1127
{

test/lib/rules/validate-jsdoc/check-redundant-params.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
describe('rules/validate-jsdoc', function () {
1+
describe('lib/rules/validate-jsdoc/check-redundant-params', function () {
22
var checker = global.checker({
33
additionalRules: ['lib/rules/validate-jsdoc.js']
44
});
55

6-
describe('check-redundant-params', function () {
6+
describe('configured', function() {
7+
8+
it('with undefined should throws', function() {
9+
global.expect(function() {
10+
checker.configure({checkRedundantParams: undefined});
11+
}).to.throws(/accepted value/i);
12+
});
13+
14+
it('with undefined should throws', function() {
15+
global.expect(function() {
16+
checker.configure({checkRedundantParams: {}});
17+
}).to.throws(/accepted value/i);
18+
});
19+
20+
});
21+
22+
describe('with this', function() {
723
checker.rules({checkRedundantParams: true});
824

925
checker.cases([

test/lib/rules/validate-jsdoc/check-redundant-returns.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
describe('rules/validate-jsdoc', function () {
1+
describe('lib/rules/validate-jsdoc/check-redundant-returns', function () {
22
var checker = global.checker({
33
additionalRules: ['lib/rules/validate-jsdoc.js']
44
});
55

6-
describe('check-redundant-returns', function() {
6+
describe('configured', function() {
7+
8+
it('with undefined should throws', function() {
9+
global.expect(function() {
10+
checker.configure({checkRedundantReturns: undefined});
11+
}).to.throws(/accepted value/i);
12+
});
13+
14+
it('with undefined should throws', function() {
15+
global.expect(function() {
16+
checker.configure({checkRedundantReturns: {}});
17+
}).to.throws(/accepted value/i);
18+
});
19+
20+
});
21+
22+
describe('with true', function() {
723
checker.rules({checkRedundantReturns: true});
824

925
checker.cases([

test/lib/rules/validate-jsdoc/check-return-types.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
describe('rules/validate-jsdoc', function () {
1+
describe('lib/rules/validate-jsdoc/check-return-types', function () {
22
var checker = global.checker({
33
additionalRules: ['lib/rules/validate-jsdoc.js']
44
});
55

6-
describe('check-return-types', function () {
6+
describe('configured', function() {
77

8+
it('with undefined should throws', function() {
9+
global.expect(function() {
10+
checker.configure({checkReturnTypes: undefined});
11+
}).to.throws(/accepted value/i);
12+
});
13+
14+
it('with undefined should throws', function() {
15+
global.expect(function() {
16+
checker.configure({checkReturnTypes: {}});
17+
}).to.throws(/accepted value/i);
18+
});
19+
20+
});
21+
22+
describe('with true', function() {
823
checker.rules({checkReturnTypes: true});
24+
925
checker.cases([
1026
/* jshint ignore:start */
1127
{

test/lib/rules/validate-jsdoc/check-types.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
describe('rules/validate-jsdoc', function() {
1+
describe('lib/rules/validate-jsdoc/check-types', function() {
22
var checker = global.checker({
33
additionalRules: ['lib/rules/validate-jsdoc.js']
44
});
55

6-
describe('check-types', function() {
6+
describe('configured', function() {
77

8+
it('with undefined should throws', function() {
9+
global.expect(function() {
10+
checker.configure({checkTypes: undefined});
11+
}).to.throws(/accepted value/i);
12+
});
13+
14+
it('with undefined should throws', function() {
15+
global.expect(function() {
16+
checker.configure({checkTypes: {}});
17+
}).to.throws(/accepted value/i);
18+
});
19+
20+
});
21+
22+
describe('with true', function() {
823
checker.rules({checkTypes: true});
24+
925
checker.cases([
1026
/* jshint ignore:start */
1127
{

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
describe('rules/validate-jsdoc', function () {
1+
describe('lib/rules/validate-jsdoc/enforce-existence', function () {
22
var checker = global.checker({
33
additionalRules: ['lib/rules/validate-jsdoc.js']
44
});
55

6-
describe('enforce-existence', function () {
6+
describe('configured', function() {
77

8+
it('with undefined should throws', function() {
9+
global.expect(function() {
10+
checker.configure({enforceExistence: undefined});
11+
}).to.throws(/accepted value/i);
12+
});
13+
14+
it('with undefined should throws', function() {
15+
global.expect(function() {
16+
checker.configure({enforceExistence: {}});
17+
}).to.throws(/accepted value/i);
18+
});
19+
20+
});
21+
22+
describe('with true', function() {
823
checker.rules({enforceExistence: true});
24+
925
checker.cases([
1026
/* jshint ignore:start */
1127
{

test/lib/rules/validate-jsdoc/leading-underscore-access.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
1-
describe('rules/validate-jsdoc', function () {
1+
describe('lib/rules/validate-jsdoc/leading-underscore-access', function () {
22
var checker = global.checker({
33
additionalRules: ['lib/rules/validate-jsdoc.js']
44
});
55

6-
describe('leading-underscore-access', function () {
6+
describe('configured', function() {
7+
8+
it('with undefined should throws', function() {
9+
global.expect(function() {
10+
checker.configure({leadingUnderscoreAccess: undefined});
11+
}).to.throws(/accepted value/i);
12+
});
13+
14+
it('with object should throws', function() {
15+
global.expect(function() {
16+
checker.configure({leadingUnderscoreAccess: {}});
17+
}).to.throws(/accepted value/i);
18+
});
19+
20+
});
21+
22+
describe('common', function() {
723

824
checker.cases([
925
/* jshint ignore:start */
1026
{
1127
it: 'should not throw',
28+
rules: {leadingUnderscoreAccess: true},
1229
code: function() {
1330
function yay(yey) {
1431
}

0 commit comments

Comments
 (0)