2 * @fileoverview require default case in switch statements
3 * @author Aliaksei Shytkin
7 var COMMENT_VALUE = "no default";
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 module.exports = function(context) {
15 //--------------------------------------------------------------------------
17 //--------------------------------------------------------------------------
20 * Shortcut to get last element of array
21 * @param {*[]} collection Array
22 * @returns {*} Last element
24 function last(collection) {
25 return collection[collection.length - 1];
28 //--------------------------------------------------------------------------
30 //--------------------------------------------------------------------------
34 "SwitchStatement": function(node) {
36 if (!node.cases.length) {
37 // skip check of empty switch because there is no easy way
38 // to extract comments inside it now
42 var hasDefault = node.cases.some(function(v) {
43 return v.test === null;
51 var lastCase = last(node.cases);
52 comments = context.getComments(lastCase).trailing;
54 if (comments.length) {
55 comment = last(comments);
58 if (!comment || comment.value.trim() !== COMMENT_VALUE) {
59 context.report(node, "Expected a default case.");