Skip to content

Commit

Permalink
[[FEAT]] Introduce linting option leanswitch
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike committed Dec 23, 2018
1 parent 06f54d0 commit 1f008f2
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4266,6 +4266,12 @@ var JSHINT = (function() {
case "return":
case "switch":
case "throw":
break;
case "default":
if (state.option.leanswitch) {
warning("W145", state.tokens.next);
}

break;
default:
// You can tell JSHint that you don't use break intentionally by
Expand All @@ -4290,6 +4296,12 @@ var JSHINT = (function() {
case "continue":
case "return":
case "throw":
break;
case "case":
if (state.option.leanswitch) {
warning("W145", state.tokens.curr);
}

break;
default:
// Do not display a warning if 'default' is the first statement or if
Expand All @@ -4304,6 +4316,7 @@ var JSHINT = (function() {
advance("default");
g = true;
advance(":");
state.funct["(verb)"] = "default";
break;
case "}":
if (!noindent)
Expand Down
3 changes: 2 additions & 1 deletion src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ var warnings = {
W142: "Empty {a}: consider replacing with `import '{b}';`.",
W143: "Assignment to properties of a mapped arguments object may cause " +
"unexpected changes to formal parameters.",
W144: "'{a}' is a non-standard language feature. Enable it using the '{b}' unstable option."
W144: "'{a}' is a non-standard language feature. Enable it using the '{b}' unstable option.",
W145: "Superfluous 'case' clause."
};

var info = {
Expand Down
15 changes: 15 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ exports.bool = {
*/
immed : true,

/**
* This option prohibits unnecessary clauses within `switch` statements,
* e.g.
*
* switch (x) {
* case 1:
* default:
* z();
* }
*
* While clauses like these are techincally valid, they do not effect
* program behavior and may indicate an erroneous refactoring.
*/
leanswitch : true,

/**
* This option requires you to capitalize names of constructor functions.
* Capitalizing functions that are intended to be used with `new` operator
Expand Down
1 change: 0 additions & 1 deletion tests/unit/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ exports.switchFallThrough = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/switchFallThrough.js', 'utf8');
TestRun(test)
.addError(3, 18, "Expected a 'break' statement before 'case'.")
.addError(18, 7, "Expected a 'break' statement before 'default'.")
.addError(40, 12, "Unexpected ':'.")
.test(src);

Expand Down
87 changes: 87 additions & 0 deletions tests/unit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4264,3 +4264,90 @@ exports.unstable = function (test) {

test.done();
};

exports.leanswitch = function (test) {
var code = [
"switch (0) {",
" case 0:",
" default:",
" break;",
"}"
];
TestRun(test, "empty case clause followed by default")
.test(code);
TestRun(test, "empty case clause followed by default")
.addError(2, 9, "Superfluous 'case' clause.")
.test(code, { leanswitch: true });

code = [
"switch (0) {",
" case 0:",
" case 1:",
" break;",
"}"
];
TestRun(test, "empty case clause followed by case")
.test(code);
TestRun(test, "empty case clause followed by case")
.test(code, { leanswitch: true });

code = [
"switch (0) {",
" default:",
" case 0:",
" break;",
"}"
];
TestRun(test, "empty default clause followed by case")
.test(code);
TestRun(test, "empty default clause followed by case")
.addError(3, 3, "Superfluous 'case' clause.")
.test(code, { leanswitch: true });

code = [
"switch (0) {",
" case 0:",
" void 0;",
" default:",
" break;",
"}"
];
TestRun(test, "non-empty case clause followed by default")
.addError(3, 11, "Expected a 'break' statement before 'default'.")
.test(code);
TestRun(test, "non-empty case clause followed by default")
.addError(3, 11, "Expected a 'break' statement before 'default'.")
.test(code, { leanswitch: true });

code = [
"switch (0) {",
" case 0:",
" void 0;",
" case 1:",
" break;",
"}"
];
TestRun(test, "non-empty case clause followed by case")
.addError(3, 11, "Expected a 'break' statement before 'case'.")
.test(code);
TestRun(test, "non-empty case clause followed by case")
.addError(3, 11, "Expected a 'break' statement before 'case'.")
.test(code, { leanswitch: true });

code = [
"switch (0) {",
" default:",
" void 0;",
" case 0:",
" break;",
"}"
];
TestRun(test, "non-empty default clause followed by case")
.addError(3, 11, "Expected a 'break' statement before 'case'.")
.test(code);
TestRun(test, "non-empty default clause followed by case")
.addError(3, 11, "Expected a 'break' statement before 'case'.")
.test(code, { leanswitch: true });

test.done();
};

0 comments on commit 1f008f2

Please sign in to comment.