github-pages-deploy-action/node_modules/eslint/lib/rules/spaced-comment.js

383 lines
13 KiB
JavaScript
Raw Permalink Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview Source code for spaced-comments rule
* @author Gyandeep Singh
*/
"use strict";
const lodash = require("lodash");
2020-03-31 20:40:00 +08:00
const astUtils = require("./utils/ast-utils");
2020-03-07 11:45:40 +08:00
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Escapes the control characters of a given string.
2020-03-31 20:40:00 +08:00
* @param {string} s A string to escape.
2020-03-07 11:45:40 +08:00
* @returns {string} An escaped string.
*/
function escape(s) {
return `(?:${lodash.escapeRegExp(s)})`;
}
/**
* Escapes the control characters of a given string.
* And adds a repeat flag.
2020-03-31 20:40:00 +08:00
* @param {string} s A string to escape.
2020-03-07 11:45:40 +08:00
* @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.
2020-03-31 20:40:00 +08:00
* @param {string[]} [markers] A marker list.
2020-03-07 11:45:40 +08:00
* @returns {string[]} A marker list.
*/
function parseMarkersOption(markers) {
// `*` is a marker for JSDoc comments.
if (markers.indexOf("*") === -1) {
return markers.concat("*");
}
return markers;
}
/**
* Creates string pattern for exceptions.
* Generated pattern:
*
* 1. A space or an exception pattern sequence.
2020-03-31 20:40:00 +08:00
* @param {string[]} exceptions An exception pattern list.
2020-03-07 11:45:40 +08:00
* @returns {string} A regular expression string for exceptions.
*/
function createExceptionsPattern(exceptions) {
let pattern = "";
/*
* 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 the exception patterns.
pattern += "(?:";
pattern += exceptions.map(escapeAndRepeat).join("|");
pattern += ")";
}
pattern += `(?:$|[${Array.from(astUtils.LINEBREAKS).join("")}]))`;
}
return pattern;
}
/**
* Creates RegExp object for `always` mode.
* Generated pattern for beginning of comment:
*
* 1. First, a marker or nothing.
* 2. Next, a space or an exception pattern sequence.
2020-03-31 20:40:00 +08:00
* @param {string[]} markers A marker list.
* @param {string[]} exceptions An exception pattern list.
2020-03-07 11:45:40 +08:00
* @returns {RegExp} A RegExp object for the beginning of a comment in `always` mode.
*/
function createAlwaysStylePattern(markers, exceptions) {
let 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.
pattern += createExceptionsPattern(exceptions);
2020-03-31 20:40:00 +08:00
return new RegExp(pattern, "u");
2020-03-07 11:45:40 +08:00
}
/**
* Creates RegExp object for `never` mode.
* Generated pattern for beginning of comment:
*
* 1. First, a marker or nothing (captured).
* 2. Next, a space or a tab.
2020-03-31 20:40:00 +08:00
* @param {string[]} markers A marker list.
2020-03-07 11:45:40 +08:00
* @returns {RegExp} A RegExp object for `never` mode.
*/
function createNeverStylePattern(markers) {
const pattern = `^(${markers.map(escape).join("|")})?[ \t]+`;
2020-03-31 20:40:00 +08:00
return new RegExp(pattern, "u");
2020-03-07 11:45:40 +08:00
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "enforce consistent spacing after the `//` or `/*` in a comment",
category: "Stylistic Issues",
recommended: false,
url: "https://eslint.org/docs/rules/spaced-comment"
},
fixable: "whitespace",
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"
}
},
balanced: {
type: "boolean",
default: false
}
},
additionalProperties: false
}
},
additionalProperties: false
}
2020-05-15 05:33:08 +08:00
],
messages: {
unexpectedSpaceAfterMarker: "Unexpected space or tab after marker ({{refChar}}) in comment.",
expectedExceptionAfter: "Expected exception block, space or tab after '{{refChar}}' in comment.",
unexpectedSpaceBefore: "Unexpected space or tab before '*/' in comment.",
unexpectedSpaceAfter: "Unexpected space or tab after '{{refChar}}' in comment.",
expectedSpaceBefore: "Expected space or tab before '*/' in comment.",
expectedSpaceAfter: "Expected space or tab after '{{refChar}}' in comment."
}
2020-03-07 11:45:40 +08:00
},
create(context) {
const sourceCode = context.getSourceCode();
// Unless the first option is never, require a space
const requireSpace = context.options[0] !== "never";
/*
* Parse the second options.
* If markers don't include `"*"`, it's added automatically for JSDoc
* comments.
*/
const config = context.options[1] || {};
const balanced = config.block && config.block.balanced;
const styleRules = ["block", "line"].reduce((rule, type) => {
const markers = parseMarkersOption(config[type] && config[type].markers || config.markers || []);
const exceptions = config[type] && config[type].exceptions || config.exceptions || [];
const endNeverPattern = "[ \t]+$";
// Create RegExp object for valid patterns.
rule[type] = {
beginRegex: requireSpace ? createAlwaysStylePattern(markers, exceptions) : createNeverStylePattern(markers),
2020-03-31 20:40:00 +08:00
endRegex: balanced && requireSpace ? new RegExp(`${createExceptionsPattern(exceptions)}$`, "u") : new RegExp(endNeverPattern, "u"),
2020-03-07 11:45:40 +08:00
hasExceptions: exceptions.length > 0,
2020-03-31 20:40:00 +08:00
captureMarker: new RegExp(`^(${markers.map(escape).join("|")})`, "u"),
markers: new Set(markers)
2020-03-07 11:45:40 +08:00
};
return rule;
}, {});
/**
* Reports a beginning spacing error with an appropriate message.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A comment node to check.
2020-05-15 05:33:08 +08:00
* @param {string} messageId An error message to report.
2020-03-31 20:40:00 +08:00
* @param {Array} match An array of match results for markers.
* @param {string} refChar Character used for reference in the error message.
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
2020-05-15 05:33:08 +08:00
function reportBegin(node, messageId, match, refChar) {
2020-03-07 11:45:40 +08:00
const type = node.type.toLowerCase(),
commentIdentifier = type === "block" ? "/*" : "//";
context.report({
node,
fix(fixer) {
const start = node.range[0];
let end = start + 2;
if (requireSpace) {
if (match) {
end += match[0].length;
}
return fixer.insertTextAfterRange([start, end], " ");
}
end += match[0].length;
return fixer.replaceTextRange([start, end], commentIdentifier + (match[1] ? match[1] : ""));
},
2020-05-15 05:33:08 +08:00
messageId,
2020-03-07 11:45:40 +08:00
data: { refChar }
});
}
/**
* Reports an ending spacing error with an appropriate message.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A comment node to check.
2020-05-15 05:33:08 +08:00
* @param {string} messageId An error message to report.
2020-03-31 20:40:00 +08:00
* @param {string} match An array of the matched whitespace characters.
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
2020-05-15 05:33:08 +08:00
function reportEnd(node, messageId, match) {
2020-03-07 11:45:40 +08:00
context.report({
node,
fix(fixer) {
if (requireSpace) {
return fixer.insertTextAfterRange([node.range[0], node.range[1] - 2], " ");
}
const end = node.range[1] - 2,
start = end - match[0].length;
return fixer.replaceTextRange([start, end], "");
},
2020-05-15 05:33:08 +08:00
messageId
2020-03-07 11:45:40 +08:00
});
}
/**
* Reports a given comment if it's invalid.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node a comment node to check.
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
function checkCommentForSpace(node) {
const type = node.type.toLowerCase(),
rule = styleRules[type],
commentIdentifier = type === "block" ? "/*" : "//";
2020-03-31 20:40:00 +08:00
// Ignores empty comments and comments that consist only of a marker.
if (node.value.length === 0 || rule.markers.has(node.value)) {
2020-03-07 11:45:40 +08:00
return;
}
const beginMatch = rule.beginRegex.exec(node.value);
const endMatch = rule.endRegex.exec(node.value);
// Checks.
if (requireSpace) {
if (!beginMatch) {
2020-03-31 20:40:00 +08:00
const hasMarker = rule.captureMarker.exec(node.value);
2020-03-07 11:45:40 +08:00
const marker = hasMarker ? commentIdentifier + hasMarker[0] : commentIdentifier;
if (rule.hasExceptions) {
2020-05-15 05:33:08 +08:00
reportBegin(node, "expectedExceptionAfter", hasMarker, marker);
2020-03-07 11:45:40 +08:00
} else {
2020-05-15 05:33:08 +08:00
reportBegin(node, "expectedSpaceAfter", hasMarker, marker);
2020-03-07 11:45:40 +08:00
}
}
if (balanced && type === "block" && !endMatch) {
2020-05-15 05:33:08 +08:00
reportEnd(node, "expectedSpaceBefore");
2020-03-07 11:45:40 +08:00
}
} else {
if (beginMatch) {
if (!beginMatch[1]) {
2020-05-15 05:33:08 +08:00
reportBegin(node, "unexpectedSpaceAfter", beginMatch, commentIdentifier);
2020-03-07 11:45:40 +08:00
} else {
2020-05-15 05:33:08 +08:00
reportBegin(node, "unexpectedSpaceAfterMarker", beginMatch, beginMatch[1]);
2020-03-07 11:45:40 +08:00
}
}
if (balanced && type === "block" && endMatch) {
2020-05-15 05:33:08 +08:00
reportEnd(node, "unexpectedSpaceBefore", endMatch);
2020-03-07 11:45:40 +08:00
}
}
}
return {
Program() {
const comments = sourceCode.getAllComments();
comments.filter(token => token.type !== "Shebang").forEach(checkCommentForSpace);
}
};
}
};