summaryrefslogtreecommitdiff
path: root/tools/node_modules/eslint/lib/rules/default-case.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/node_modules/eslint/lib/rules/default-case.js')
-rw-r--r--tools/node_modules/eslint/lib/rules/default-case.js90
1 files changed, 90 insertions, 0 deletions
diff --git a/tools/node_modules/eslint/lib/rules/default-case.js b/tools/node_modules/eslint/lib/rules/default-case.js
new file mode 100644
index 0000000000..32cd8dfe49
--- /dev/null
+++ b/tools/node_modules/eslint/lib/rules/default-case.js
@@ -0,0 +1,90 @@
+/**
+ * @fileoverview require default case in switch statements
+ * @author Aliaksei Shytkin
+ */
+"use strict";
+
+const DEFAULT_COMMENT_PATTERN = /^no default$/i;
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = {
+ meta: {
+ docs: {
+ description: "require `default` cases in `switch` statements",
+ category: "Best Practices",
+ recommended: false
+ },
+
+ schema: [{
+ type: "object",
+ properties: {
+ commentPattern: {
+ type: "string"
+ }
+ },
+ additionalProperties: false
+ }]
+ },
+
+ create(context) {
+ const options = context.options[0] || {};
+ const commentPattern = options.commentPattern
+ ? new RegExp(options.commentPattern)
+ : DEFAULT_COMMENT_PATTERN;
+
+ const sourceCode = context.getSourceCode();
+
+ //--------------------------------------------------------------------------
+ // Helpers
+ //--------------------------------------------------------------------------
+
+ /**
+ * Shortcut to get last element of array
+ * @param {*[]} collection Array
+ * @returns {*} Last element
+ */
+ function last(collection) {
+ return collection[collection.length - 1];
+ }
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+
+ return {
+
+ SwitchStatement(node) {
+
+ if (!node.cases.length) {
+
+ /*
+ * skip check of empty switch because there is no easy way
+ * to extract comments inside it now
+ */
+ return;
+ }
+
+ const hasDefault = node.cases.some(v => v.test === null);
+
+ if (!hasDefault) {
+
+ let comment;
+
+ const lastCase = last(node.cases);
+ const comments = sourceCode.getCommentsAfter(lastCase);
+
+ if (comments.length) {
+ comment = last(comments);
+ }
+
+ if (!comment || !commentPattern.test(comment.value.trim())) {
+ context.report({ node, message: "Expected a default case." });
+ }
+ }
+ }
+ };
+ }
+};