| 1 | /**
|
|---|
| 2 | * @fileoverview Source code for spaced-comments rule
|
|---|
| 3 | * @author Gyandeep Singh
|
|---|
| 4 | * @deprecated in ESLint v8.53.0
|
|---|
| 5 | */
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const escapeRegExp = require("escape-string-regexp");
|
|---|
| 9 | const astUtils = require("./utils/ast-utils");
|
|---|
| 10 |
|
|---|
| 11 | //------------------------------------------------------------------------------
|
|---|
| 12 | // Helpers
|
|---|
| 13 | //------------------------------------------------------------------------------
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Escapes the control characters of a given string.
|
|---|
| 17 | * @param {string} s A string to escape.
|
|---|
| 18 | * @returns {string} An escaped string.
|
|---|
| 19 | */
|
|---|
| 20 | function escape(s) {
|
|---|
| 21 | return `(?:${escapeRegExp(s)})`;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * Escapes the control characters of a given string.
|
|---|
| 26 | * And adds a repeat flag.
|
|---|
| 27 | * @param {string} s A string to escape.
|
|---|
| 28 | * @returns {string} An escaped string.
|
|---|
| 29 | */
|
|---|
| 30 | function escapeAndRepeat(s) {
|
|---|
| 31 | return `${escape(s)}+`;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Parses `markers` option.
|
|---|
| 36 | * If markers don't include `"*"`, this adds `"*"` to allow JSDoc comments.
|
|---|
| 37 | * @param {string[]} [markers] A marker list.
|
|---|
| 38 | * @returns {string[]} A marker list.
|
|---|
| 39 | */
|
|---|
| 40 | function parseMarkersOption(markers) {
|
|---|
| 41 |
|
|---|
| 42 | // `*` is a marker for JSDoc comments.
|
|---|
| 43 | if (!markers.includes("*")) {
|
|---|
| 44 | return markers.concat("*");
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | return markers;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Creates string pattern for exceptions.
|
|---|
| 52 | * Generated pattern:
|
|---|
| 53 | *
|
|---|
| 54 | * 1. A space or an exception pattern sequence.
|
|---|
| 55 | * @param {string[]} exceptions An exception pattern list.
|
|---|
| 56 | * @returns {string} A regular expression string for exceptions.
|
|---|
| 57 | */
|
|---|
| 58 | function createExceptionsPattern(exceptions) {
|
|---|
| 59 | let pattern = "";
|
|---|
| 60 |
|
|---|
| 61 | /*
|
|---|
| 62 | * A space or an exception pattern sequence.
|
|---|
| 63 | * [] ==> "\s"
|
|---|
| 64 | * ["-"] ==> "(?:\s|\-+$)"
|
|---|
| 65 | * ["-", "="] ==> "(?:\s|(?:\-+|=+)$)"
|
|---|
| 66 | * ["-", "=", "--=="] ==> "(?:\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)
|
|---|
| 67 | */
|
|---|
| 68 | if (exceptions.length === 0) {
|
|---|
| 69 |
|
|---|
| 70 | // a space.
|
|---|
| 71 | pattern += "\\s";
|
|---|
| 72 | } else {
|
|---|
| 73 |
|
|---|
| 74 | // a space or...
|
|---|
| 75 | pattern += "(?:\\s|";
|
|---|
| 76 |
|
|---|
| 77 | if (exceptions.length === 1) {
|
|---|
| 78 |
|
|---|
| 79 | // a sequence of the exception pattern.
|
|---|
| 80 | pattern += escapeAndRepeat(exceptions[0]);
|
|---|
| 81 | } else {
|
|---|
| 82 |
|
|---|
| 83 | // a sequence of one of the exception patterns.
|
|---|
| 84 | pattern += "(?:";
|
|---|
| 85 | pattern += exceptions.map(escapeAndRepeat).join("|");
|
|---|
| 86 | pattern += ")";
|
|---|
| 87 | }
|
|---|
| 88 | pattern += `(?:$|[${Array.from(astUtils.LINEBREAKS).join("")}]))`;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | return pattern;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * Creates RegExp object for `always` mode.
|
|---|
| 96 | * Generated pattern for beginning of comment:
|
|---|
| 97 | *
|
|---|
| 98 | * 1. First, a marker or nothing.
|
|---|
| 99 | * 2. Next, a space or an exception pattern sequence.
|
|---|
| 100 | * @param {string[]} markers A marker list.
|
|---|
| 101 | * @param {string[]} exceptions An exception pattern list.
|
|---|
| 102 | * @returns {RegExp} A RegExp object for the beginning of a comment in `always` mode.
|
|---|
| 103 | */
|
|---|
| 104 | function createAlwaysStylePattern(markers, exceptions) {
|
|---|
| 105 | let pattern = "^";
|
|---|
| 106 |
|
|---|
| 107 | /*
|
|---|
| 108 | * A marker or nothing.
|
|---|
| 109 | * ["*"] ==> "\*?"
|
|---|
| 110 | * ["*", "!"] ==> "(?:\*|!)?"
|
|---|
| 111 | * ["*", "/", "!<"] ==> "(?:\*|\/|(?:!<))?" ==> https://jex.im/regulex/#!embed=false&flags=&re=(%3F%3A%5C*%7C%5C%2F%7C(%3F%3A!%3C))%3F
|
|---|
| 112 | */
|
|---|
| 113 | if (markers.length === 1) {
|
|---|
| 114 |
|
|---|
| 115 | // the marker.
|
|---|
| 116 | pattern += escape(markers[0]);
|
|---|
| 117 | } else {
|
|---|
| 118 |
|
|---|
| 119 | // one of markers.
|
|---|
| 120 | pattern += "(?:";
|
|---|
| 121 | pattern += markers.map(escape).join("|");
|
|---|
| 122 | pattern += ")";
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | pattern += "?"; // or nothing.
|
|---|
| 126 | pattern += createExceptionsPattern(exceptions);
|
|---|
| 127 |
|
|---|
| 128 | return new RegExp(pattern, "u");
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Creates RegExp object for `never` mode.
|
|---|
| 133 | * Generated pattern for beginning of comment:
|
|---|
| 134 | *
|
|---|
| 135 | * 1. First, a marker or nothing (captured).
|
|---|
| 136 | * 2. Next, a space or a tab.
|
|---|
| 137 | * @param {string[]} markers A marker list.
|
|---|
| 138 | * @returns {RegExp} A RegExp object for `never` mode.
|
|---|
| 139 | */
|
|---|
| 140 | function createNeverStylePattern(markers) {
|
|---|
| 141 | const pattern = `^(${markers.map(escape).join("|")})?[ \t]+`;
|
|---|
| 142 |
|
|---|
| 143 | return new RegExp(pattern, "u");
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | //------------------------------------------------------------------------------
|
|---|
| 147 | // Rule Definition
|
|---|
| 148 | //------------------------------------------------------------------------------
|
|---|
| 149 |
|
|---|
| 150 | /** @type {import('../shared/types').Rule} */
|
|---|
| 151 | module.exports = {
|
|---|
| 152 | meta: {
|
|---|
| 153 | deprecated: true,
|
|---|
| 154 | replacedBy: [],
|
|---|
| 155 | type: "suggestion",
|
|---|
| 156 |
|
|---|
| 157 | docs: {
|
|---|
| 158 | description: "Enforce consistent spacing after the `//` or `/*` in a comment",
|
|---|
| 159 | recommended: false,
|
|---|
| 160 | url: "https://eslint.org/docs/latest/rules/spaced-comment"
|
|---|
| 161 | },
|
|---|
| 162 |
|
|---|
| 163 | fixable: "whitespace",
|
|---|
| 164 |
|
|---|
| 165 | schema: [
|
|---|
| 166 | {
|
|---|
| 167 | enum: ["always", "never"]
|
|---|
| 168 | },
|
|---|
| 169 | {
|
|---|
| 170 | type: "object",
|
|---|
| 171 | properties: {
|
|---|
| 172 | exceptions: {
|
|---|
| 173 | type: "array",
|
|---|
| 174 | items: {
|
|---|
| 175 | type: "string"
|
|---|
| 176 | }
|
|---|
| 177 | },
|
|---|
| 178 | markers: {
|
|---|
| 179 | type: "array",
|
|---|
| 180 | items: {
|
|---|
| 181 | type: "string"
|
|---|
| 182 | }
|
|---|
| 183 | },
|
|---|
| 184 | line: {
|
|---|
| 185 | type: "object",
|
|---|
| 186 | properties: {
|
|---|
| 187 | exceptions: {
|
|---|
| 188 | type: "array",
|
|---|
| 189 | items: {
|
|---|
| 190 | type: "string"
|
|---|
| 191 | }
|
|---|
| 192 | },
|
|---|
| 193 | markers: {
|
|---|
| 194 | type: "array",
|
|---|
| 195 | items: {
|
|---|
| 196 | type: "string"
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 | },
|
|---|
| 200 | additionalProperties: false
|
|---|
| 201 | },
|
|---|
| 202 | block: {
|
|---|
| 203 | type: "object",
|
|---|
| 204 | properties: {
|
|---|
| 205 | exceptions: {
|
|---|
| 206 | type: "array",
|
|---|
| 207 | items: {
|
|---|
| 208 | type: "string"
|
|---|
| 209 | }
|
|---|
| 210 | },
|
|---|
| 211 | markers: {
|
|---|
| 212 | type: "array",
|
|---|
| 213 | items: {
|
|---|
| 214 | type: "string"
|
|---|
| 215 | }
|
|---|
| 216 | },
|
|---|
| 217 | balanced: {
|
|---|
| 218 | type: "boolean",
|
|---|
| 219 | default: false
|
|---|
| 220 | }
|
|---|
| 221 | },
|
|---|
| 222 | additionalProperties: false
|
|---|
| 223 | }
|
|---|
| 224 | },
|
|---|
| 225 | additionalProperties: false
|
|---|
| 226 | }
|
|---|
| 227 | ],
|
|---|
| 228 |
|
|---|
| 229 | messages: {
|
|---|
| 230 | unexpectedSpaceAfterMarker: "Unexpected space or tab after marker ({{refChar}}) in comment.",
|
|---|
| 231 | expectedExceptionAfter: "Expected exception block, space or tab after '{{refChar}}' in comment.",
|
|---|
| 232 | unexpectedSpaceBefore: "Unexpected space or tab before '*/' in comment.",
|
|---|
| 233 | unexpectedSpaceAfter: "Unexpected space or tab after '{{refChar}}' in comment.",
|
|---|
| 234 | expectedSpaceBefore: "Expected space or tab before '*/' in comment.",
|
|---|
| 235 | expectedSpaceAfter: "Expected space or tab after '{{refChar}}' in comment."
|
|---|
| 236 | }
|
|---|
| 237 | },
|
|---|
| 238 |
|
|---|
| 239 | create(context) {
|
|---|
| 240 |
|
|---|
| 241 | const sourceCode = context.sourceCode;
|
|---|
| 242 |
|
|---|
| 243 | // Unless the first option is never, require a space
|
|---|
| 244 | const requireSpace = context.options[0] !== "never";
|
|---|
| 245 |
|
|---|
| 246 | /*
|
|---|
| 247 | * Parse the second options.
|
|---|
| 248 | * If markers don't include `"*"`, it's added automatically for JSDoc
|
|---|
| 249 | * comments.
|
|---|
| 250 | */
|
|---|
| 251 | const config = context.options[1] || {};
|
|---|
| 252 | const balanced = config.block && config.block.balanced;
|
|---|
| 253 |
|
|---|
| 254 | const styleRules = ["block", "line"].reduce((rule, type) => {
|
|---|
| 255 | const markers = parseMarkersOption(config[type] && config[type].markers || config.markers || []);
|
|---|
| 256 | const exceptions = config[type] && config[type].exceptions || config.exceptions || [];
|
|---|
| 257 | const endNeverPattern = "[ \t]+$";
|
|---|
| 258 |
|
|---|
| 259 | // Create RegExp object for valid patterns.
|
|---|
| 260 | rule[type] = {
|
|---|
| 261 | beginRegex: requireSpace ? createAlwaysStylePattern(markers, exceptions) : createNeverStylePattern(markers),
|
|---|
| 262 | endRegex: balanced && requireSpace ? new RegExp(`${createExceptionsPattern(exceptions)}$`, "u") : new RegExp(endNeverPattern, "u"),
|
|---|
| 263 | hasExceptions: exceptions.length > 0,
|
|---|
| 264 | captureMarker: new RegExp(`^(${markers.map(escape).join("|")})`, "u"),
|
|---|
| 265 | markers: new Set(markers)
|
|---|
| 266 | };
|
|---|
| 267 |
|
|---|
| 268 | return rule;
|
|---|
| 269 | }, {});
|
|---|
| 270 |
|
|---|
| 271 | /**
|
|---|
| 272 | * Reports a beginning spacing error with an appropriate message.
|
|---|
| 273 | * @param {ASTNode} node A comment node to check.
|
|---|
| 274 | * @param {string} messageId An error message to report.
|
|---|
| 275 | * @param {Array} match An array of match results for markers.
|
|---|
| 276 | * @param {string} refChar Character used for reference in the error message.
|
|---|
| 277 | * @returns {void}
|
|---|
| 278 | */
|
|---|
| 279 | function reportBegin(node, messageId, match, refChar) {
|
|---|
| 280 | const type = node.type.toLowerCase(),
|
|---|
| 281 | commentIdentifier = type === "block" ? "/*" : "//";
|
|---|
| 282 |
|
|---|
| 283 | context.report({
|
|---|
| 284 | node,
|
|---|
| 285 | fix(fixer) {
|
|---|
| 286 | const start = node.range[0];
|
|---|
| 287 | let end = start + 2;
|
|---|
| 288 |
|
|---|
| 289 | if (requireSpace) {
|
|---|
| 290 | if (match) {
|
|---|
| 291 | end += match[0].length;
|
|---|
| 292 | }
|
|---|
| 293 | return fixer.insertTextAfterRange([start, end], " ");
|
|---|
| 294 | }
|
|---|
| 295 | end += match[0].length;
|
|---|
| 296 | return fixer.replaceTextRange([start, end], commentIdentifier + (match[1] ? match[1] : ""));
|
|---|
| 297 |
|
|---|
| 298 | },
|
|---|
| 299 | messageId,
|
|---|
| 300 | data: { refChar }
|
|---|
| 301 | });
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | /**
|
|---|
| 305 | * Reports an ending spacing error with an appropriate message.
|
|---|
| 306 | * @param {ASTNode} node A comment node to check.
|
|---|
| 307 | * @param {string} messageId An error message to report.
|
|---|
| 308 | * @param {string} match An array of the matched whitespace characters.
|
|---|
| 309 | * @returns {void}
|
|---|
| 310 | */
|
|---|
| 311 | function reportEnd(node, messageId, match) {
|
|---|
| 312 | context.report({
|
|---|
| 313 | node,
|
|---|
| 314 | fix(fixer) {
|
|---|
| 315 | if (requireSpace) {
|
|---|
| 316 | return fixer.insertTextAfterRange([node.range[0], node.range[1] - 2], " ");
|
|---|
| 317 | }
|
|---|
| 318 | const end = node.range[1] - 2,
|
|---|
| 319 | start = end - match[0].length;
|
|---|
| 320 |
|
|---|
| 321 | return fixer.replaceTextRange([start, end], "");
|
|---|
| 322 |
|
|---|
| 323 | },
|
|---|
| 324 | messageId
|
|---|
| 325 | });
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | /**
|
|---|
| 329 | * Reports a given comment if it's invalid.
|
|---|
| 330 | * @param {ASTNode} node a comment node to check.
|
|---|
| 331 | * @returns {void}
|
|---|
| 332 | */
|
|---|
| 333 | function checkCommentForSpace(node) {
|
|---|
| 334 | const type = node.type.toLowerCase(),
|
|---|
| 335 | rule = styleRules[type],
|
|---|
| 336 | commentIdentifier = type === "block" ? "/*" : "//";
|
|---|
| 337 |
|
|---|
| 338 | // Ignores empty comments and comments that consist only of a marker.
|
|---|
| 339 | if (node.value.length === 0 || rule.markers.has(node.value)) {
|
|---|
| 340 | return;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | const beginMatch = rule.beginRegex.exec(node.value);
|
|---|
| 344 | const endMatch = rule.endRegex.exec(node.value);
|
|---|
| 345 |
|
|---|
| 346 | // Checks.
|
|---|
| 347 | if (requireSpace) {
|
|---|
| 348 | if (!beginMatch) {
|
|---|
| 349 | const hasMarker = rule.captureMarker.exec(node.value);
|
|---|
| 350 | const marker = hasMarker ? commentIdentifier + hasMarker[0] : commentIdentifier;
|
|---|
| 351 |
|
|---|
| 352 | if (rule.hasExceptions) {
|
|---|
| 353 | reportBegin(node, "expectedExceptionAfter", hasMarker, marker);
|
|---|
| 354 | } else {
|
|---|
| 355 | reportBegin(node, "expectedSpaceAfter", hasMarker, marker);
|
|---|
| 356 | }
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | if (balanced && type === "block" && !endMatch) {
|
|---|
| 360 | reportEnd(node, "expectedSpaceBefore");
|
|---|
| 361 | }
|
|---|
| 362 | } else {
|
|---|
| 363 | if (beginMatch) {
|
|---|
| 364 | if (!beginMatch[1]) {
|
|---|
| 365 | reportBegin(node, "unexpectedSpaceAfter", beginMatch, commentIdentifier);
|
|---|
| 366 | } else {
|
|---|
| 367 | reportBegin(node, "unexpectedSpaceAfterMarker", beginMatch, beginMatch[1]);
|
|---|
| 368 | }
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | if (balanced && type === "block" && endMatch) {
|
|---|
| 372 | reportEnd(node, "unexpectedSpaceBefore", endMatch);
|
|---|
| 373 | }
|
|---|
| 374 | }
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | return {
|
|---|
| 378 | Program() {
|
|---|
| 379 | const comments = sourceCode.getAllComments();
|
|---|
| 380 |
|
|---|
| 381 | comments.filter(token => token.type !== "Shebang").forEach(checkCommentForSpace);
|
|---|
| 382 | }
|
|---|
| 383 | };
|
|---|
| 384 | }
|
|---|
| 385 | };
|
|---|