summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/spaced-line-comment.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/rules/spaced-line-comment.js')
-rw-r--r--tools/eslint/lib/rules/spaced-line-comment.js71
1 files changed, 71 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/spaced-line-comment.js b/tools/eslint/lib/rules/spaced-line-comment.js
new file mode 100644
index 0000000000..af5ba8cdb6
--- /dev/null
+++ b/tools/eslint/lib/rules/spaced-line-comment.js
@@ -0,0 +1,71 @@
+/**
+ * @fileoverview Enforces or disallows a space beginning a single-line comment.
+ * @author Greg Cochard
+ * @copyright 2014 Greg Cochard. All rights reserved.
+ */
+"use strict";
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = function(context) {
+
+ // Unless the first option is never, require a space
+ var requireSpace = context.options[0] !== "never";
+
+ // Default to match anything, so all will fail if there are no exceptions
+ var exceptionMatcher = new RegExp(" ");
+
+ // Grab the exceptions array and build a RegExp matcher for it
+ var hasExceptions = context.options.length === 2;
+ var unescapedExceptions = hasExceptions ? context.options[1].exceptions : [];
+ var exceptions;
+
+ if (unescapedExceptions.length) {
+ exceptions = unescapedExceptions.map(function(s) {
+ return s.replace(/([.*+?${}()|\^\[\]\/\\])/g, "\\$1");
+ });
+ exceptionMatcher = new RegExp("(^(" + exceptions.join(")+$)|(^(") + ")+$)");
+ }
+
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+
+ return {
+
+ "LineComment": function checkCommentForSpace(node) {
+
+ if (requireSpace) {
+
+ // If length is zero, ignore it
+ if (node.value.length === 0) {
+ return;
+ }
+
+ // Space expected and not found
+ if (node.value.indexOf(" ") !== 0 && node.value.indexOf("\t") !== 0) {
+
+ /*
+ * Do two tests; one for space starting the line,
+ * and one for a comment comprised only of exceptions
+ */
+ if (hasExceptions && !exceptionMatcher.test(node.value)) {
+ context.report(node, "Expected exception block, space or tab after // in comment.");
+ } else if (!hasExceptions) {
+ context.report(node, "Expected space or tab after // in comment.");
+ }
+ }
+
+ } else {
+
+ if (node.value.indexOf(" ") === 0 || node.value.indexOf("\t") === 0) {
+ context.report(node, "Unexpected space or tab after // in comment.");
+ }
+ }
+ }
+
+ };
+};