Skip to content

Commit

Permalink
Update: commentPattern option for default-case rule (fixes #5803)
Browse files Browse the repository at this point in the history
  • Loading branch information
Artyom Lvov committed Apr 14, 2016
1 parent 9b7f6a7 commit 2b39461
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 5 deletions.
29 changes: 29 additions & 0 deletions docs/rules/default-case.md
Expand Up @@ -80,6 +80,35 @@ switch (a) {

```

## Options

This rule accepts a single options argument:

* Set the `commentPattern` option to a regular expression string to change the default `^no default$` comment test pattern

### commentPattern

Examples of **correct** code for the `{ "commentPattern": "^skip\\sdefault" }` option:

```js
/*eslint default-case: ["error", { "commentPattern": "^skip\\sdefault" }]*/

switch(a) {
case 1:
/* code */
break;

// skip default
}

switch(a) {
case 1:
/* code */
break;

// skip default case
}
```

## When Not To Use It

Expand Down
18 changes: 15 additions & 3 deletions lib/rules/default-case.js
Expand Up @@ -4,7 +4,7 @@
*/
"use strict";

var COMMENT_VALUE = "no default";
var DEFAULT_COMMENT_PATTERN = /^no default$/;

//------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -18,10 +18,22 @@ module.exports = {
recommended: false
},

schema: []
schema: [{
"type": "object",
"properties": {
"commentPattern": {
"type": "string"
}
},
"additionalProperties": false
}]
},

create: function(context) {
var options = context.options[0] || {};
var commentPattern = options.commentPattern ?
new RegExp(options.commentPattern) :
DEFAULT_COMMENT_PATTERN;

//--------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -70,7 +82,7 @@ module.exports = {
comment = last(comments);
}

if (!comment || comment.value.trim() !== COMMENT_VALUE) {
if (!comment || !commentPattern.test(comment.value.trim())) {
context.report(node, "Expected a default case.");
}
}
Expand Down
57 changes: 55 additions & 2 deletions tests/lib/rules/default-case.js
Expand Up @@ -29,7 +29,31 @@ ruleTester.run("default-case", rule, {
"switch (a) { \n case 1: a = 4; \n\n/* no default */\n }",
"switch (a) { \n case 1: a = 4; break; break; \n\n// no default\n }",
"switch (a) { // no default\n }",
"switch (a) { }"
"switch (a) { }",
{
code: "switch (a) { case 1: break; default: break; }",
options: [{
commentPattern: "default case omitted"
}]
},
{
code: "switch (a) { case 1: break; \n // skip default case \n }",
options: [{
commentPattern: "^skip default"
}]
},
{
code: "switch (a) { case 1: break; \n /*\nTODO:\n throw error in default case\n*/ \n }",
options: [{
commentPattern: "default"
}]
},
{
code: "switch (a) { case 1: break; \n// \n }",
options: [{
commentPattern: ".?"
}]
}
],

invalid: [
Expand All @@ -53,7 +77,36 @@ ruleTester.run("default-case", rule, {
message: "Expected a default case.",
type: "SwitchStatement"
}]
},
{
code: "switch (a) { case 1: break; \n // no default \n }",
options: [{
commentPattern: "skipped default case"
}],
errors: [{
message: "Expected a default case.",
type: "SwitchStatement"
}]
},
{
code: "switch (a) {\ncase 1: break; \n// default omitted intentionally \n// TODO: add default case \n}",
options: [{
commentPattern: "default omitted"
}],
errors: [{
message: "Expected a default case.",
type: "SwitchStatement"
}]
},
{
code: "switch (a) {\ncase 1: break;\n}",
options: [{
commentPattern: ".?"
}],
errors: [{
message: "Expected a default case.",
type: "SwitchStatement"
}]
}

]
});

0 comments on commit 2b39461

Please sign in to comment.