Blame view
node_modules/eslint/lib/rules/spaced-comment.js
7.72 KB
c39994410
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
/** * @fileoverview Source code for spaced-comments rule * @author Gyandeep Singh * @copyright 2015 Toru Nagashima. All rights reserved. * @copyright 2015 Gyandeep Singh. All rights reserved. * @copyright 2014 Greg Cochard. All rights reserved. */ "use strict"; var escapeStringRegexp = require("escape-string-regexp"); //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ /** * Escapes the control characters of a given string. * @param {string} s - A string to escape. * @returns {string} An escaped string. */ function escape(s) { var isOneChar = s.length === 1; s = escapeStringRegexp(s); return isOneChar ? s : "(?:" + s + ")"; } /** * Escapes the control characters of a given string. * And adds a repeat flag. * @param {string} s - A string to escape. * @returns {string} An escaped string. */ function escapeAndRepeat(s) { return escape(s) + "+"; } /** * Parses `markers` option. * If markers don't include `"*"`, this adds `"*"` to allow JSDoc comments. * @param {string[]} [markers] - A marker list. * @returns {string[]} A marker list. */ function parseMarkersOption(markers) { markers = markers ? markers.slice(0) : []; // `*` is a marker for JSDoc comments. if (markers.indexOf("*") === -1) { markers.push("*"); } return markers; } /** * Creates RegExp object for `always` mode. * Generated pattern is below: * * 1. First, a marker or nothing. * 2. Next, a space or an exception pattern sequence. * * @param {string[]} markers - A marker list. * @param {string[]} exceptions - A exception pattern list. * @returns {RegExp} A RegExp object for `always` mode. */ function createAlwaysStylePattern(markers, exceptions) { var pattern = "^"; // A marker or nothing. // ["*"] ==> "\*?" // ["*", "!"] ==> "(?:\*|!)?" // ["*", "/", "!<"] ==> "(?:\*|\/|(?:!<))?" ==> https://jex.im/regulex/#!embed=false&flags=&re=(%3F%3A%5C*%7C%5C%2F%7C(%3F%3A!%3C))%3F if (markers.length === 1) { // the marker. pattern += escape(markers[0]); } else { // one of markers. pattern += "(?:"; pattern += markers.map(escape).join("|"); pattern += ")"; } pattern += "?"; // or nothing. // A space or an exception pattern sequence. // [] ==> "\s" // ["-"] ==> "(?:\s|\-+$)" // ["-", "="] ==> "(?:\s|(?:\-+|=+)$)" // ["-", "=", "--=="] ==> "(?:\s|(?:\-+|=+|(?:\-\-==)+)$)" ==> https://jex.im/regulex/#!embed=false&flags=&re=(%3F%3A%5Cs%7C(%3F%3A%5C-%2B%7C%3D%2B%7C(%3F%3A%5C-%5C-%3D%3D)%2B)%24) if (exceptions.length === 0) { // a space. pattern += "\\s"; } else { // a space or... pattern += "(?:\\s|"; if (exceptions.length === 1) { // a sequence of the exception pattern. pattern += escapeAndRepeat(exceptions[0]); } else { // a sequence of one of exception patterns. pattern += "(?:"; pattern += exceptions.map(escapeAndRepeat).join("|"); pattern += ")"; } pattern += "(?:$|[ \r]))"; // the sequence continues until the end. } return new RegExp(pattern); } /** * Creates RegExp object for `never` mode. * Generated pattern is below: * * 1. First, a marker or nothing (captured). * 2. Next, a space or a tab. * * @param {string[]} markers - A marker list. * @returns {RegExp} A RegExp object for `never` mode. */ function createNeverStylePattern(markers) { var pattern = "^(" + markers.map(escape).join("|") + ")?[ \t]"; return new RegExp(pattern); } //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = function(context) { // Unless the first option is never, require a space var requireSpace = context.options[0] !== "never"; // Parse the second options. // If markers don't include `"*"`, it's added automatically for JSDoc comments. var config = context.options[1] || {}; var styleRules = ["block", "line"].reduce(function(rule, type) { var markers = parseMarkersOption(config[type] && config[type].markers || config.markers); var exceptions = config[type] && config[type].exceptions || config.exceptions || []; // Create RegExp object for valid patterns. rule[type] = { regex: requireSpace ? createAlwaysStylePattern(markers, exceptions) : createNeverStylePattern(markers), hasExceptions: exceptions.length > 0 }; return rule; }, {}); /** * Reports a given comment if it's invalid. * @param {ASTNode} node - a comment node to check. * @returns {void} */ function checkCommentForSpace(node) { var type = node.type.toLowerCase(), rule = styleRules[type], commentIdentifier = type === "block" ? "/*" : "//"; // Ignores empty comments. if (node.value.length === 0) { return; } // Checks. if (requireSpace) { if (!rule.regex.test(node.value)) { if (rule.hasExceptions) { context.report(node, "Expected exception block, space or tab after \"" + commentIdentifier + "\" in comment."); } else { context.report(node, "Expected space or tab after \"" + commentIdentifier + "\" in comment."); } } } else { var matched = rule.regex.exec(node.value); if (matched) { if (!matched[1]) { context.report(node, "Unexpected space or tab after \"" + commentIdentifier + "\" in comment."); } else { context.report(node, "Unexpected space or tab after marker (" + matched[1] + ") in comment."); } } } } return { "LineComment": checkCommentForSpace, "BlockComment": checkCommentForSpace }; }; module.exports.schema = [ { "enum": ["always", "never"] }, { "type": "object", "properties": { "exceptions": { "type": "array", "items": { "type": "string" } }, "markers": { "type": "array", "items": { "type": "string" } }, "line": { "type": "object", "properties": { "exceptions": { "type": "array", "items": { "type": "string" } }, "markers": { "type": "array", "items": { "type": "string" } } }, "additionalProperties": false }, "block": { "type": "object", "properties": { "exceptions": { "type": "array", "items": { "type": "string" } }, "markers": { "type": "array", "items": { "type": "string" } } }, "additionalProperties": false } }, "additionalProperties": false } ]; |