Files
codeql-action/node_modules/eslint/lib/rules/spaced-comment.js
dependabot[bot] 80150c2141 Bump the npm group with 12 updates (#2003)
* Bump the npm group with 12 updates

Bumps the npm group with 12 updates:

| Package | From | To |
| --- | --- | --- |
| [@octokit/types](https://github.com/octokit/types.ts) | `12.1.1` | `12.3.0` |
| [@types/uuid](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/uuid) | `9.0.6` | `9.0.7` |
| [@types/adm-zip](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/adm-zip) | `0.5.3` | `0.5.4` |
| [@types/js-yaml](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/js-yaml) | `4.0.8` | `4.0.9` |
| [@types/semver](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/semver) | `7.5.4` | `7.5.5` |
| [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/eslint-plugin) | `6.9.1` | `6.12.0` |
| [@typescript-eslint/parser](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/parser) | `6.9.1` | `6.12.0` |
| [eslint](https://github.com/eslint/eslint) | `8.52.0` | `8.54.0` |
| [nock](https://github.com/nock/nock) | `13.3.7` | `13.3.8` |
| [sinon](https://github.com/sinonjs/sinon) | `17.0.0` | `17.0.1` |
| [@types/sinon](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/sinon) | `10.0.20` | `17.0.1` |
| [typescript](https://github.com/Microsoft/TypeScript) | `5.2.2` | `5.3.2` |


Updates `@octokit/types` from 12.1.1 to 12.3.0
- [Release notes](https://github.com/octokit/types.ts/releases)
- [Commits](https://github.com/octokit/types.ts/compare/v12.1.1...v12.3.0)

Updates `@types/uuid` from 9.0.6 to 9.0.7
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/uuid)

Updates `@types/adm-zip` from 0.5.3 to 0.5.4
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/adm-zip)

Updates `@types/js-yaml` from 4.0.8 to 4.0.9
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/js-yaml)

Updates `@types/semver` from 7.5.4 to 7.5.5
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/semver)

Updates `@typescript-eslint/eslint-plugin` from 6.9.1 to 6.12.0
- [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases)
- [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/CHANGELOG.md)
- [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v6.12.0/packages/eslint-plugin)

Updates `@typescript-eslint/parser` from 6.9.1 to 6.12.0
- [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases)
- [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/parser/CHANGELOG.md)
- [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v6.12.0/packages/parser)

Updates `eslint` from 8.52.0 to 8.54.0
- [Release notes](https://github.com/eslint/eslint/releases)
- [Changelog](https://github.com/eslint/eslint/blob/main/CHANGELOG.md)
- [Commits](https://github.com/eslint/eslint/compare/v8.52.0...v8.54.0)

Updates `nock` from 13.3.7 to 13.3.8
- [Release notes](https://github.com/nock/nock/releases)
- [Changelog](https://github.com/nock/nock/blob/main/CHANGELOG.md)
- [Commits](https://github.com/nock/nock/compare/v13.3.7...v13.3.8)

Updates `sinon` from 17.0.0 to 17.0.1
- [Release notes](https://github.com/sinonjs/sinon/releases)
- [Changelog](https://github.com/sinonjs/sinon/blob/main/docs/changelog.md)
- [Commits](https://github.com/sinonjs/sinon/compare/v17.0.0...v17.0.1)

Updates `@types/sinon` from 10.0.20 to 17.0.1
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/sinon)

Updates `typescript` from 5.2.2 to 5.3.2
- [Release notes](https://github.com/Microsoft/TypeScript/releases)
- [Commits](https://github.com/Microsoft/TypeScript/commits)

---
updated-dependencies:
- dependency-name: "@octokit/types"
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: npm
- dependency-name: "@types/uuid"
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: npm
- dependency-name: "@types/adm-zip"
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: npm
- dependency-name: "@types/js-yaml"
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: npm
- dependency-name: "@types/semver"
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: npm
- dependency-name: "@typescript-eslint/eslint-plugin"
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: npm
- dependency-name: "@typescript-eslint/parser"
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: npm
- dependency-name: eslint
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: npm
- dependency-name: nock
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: npm
- dependency-name: sinon
  dependency-type: direct:development
  update-type: version-update:semver-patch
  dependency-group: npm
- dependency-name: "@types/sinon"
  dependency-type: direct:development
  update-type: version-update:semver-major
  dependency-group: npm
- dependency-name: typescript
  dependency-type: direct:development
  update-type: version-update:semver-minor
  dependency-group: npm
...

Signed-off-by: dependabot[bot] <support@github.com>

* Update checked-in dependencies

* Rebuild

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2023-11-20 14:35:28 -08:00

386 lines
13 KiB
JavaScript

/**
* @fileoverview Source code for spaced-comments rule
* @author Gyandeep Singh
* @deprecated in ESLint v8.53.0
*/
"use strict";
const escapeRegExp = require("escape-string-regexp");
const astUtils = require("./utils/ast-utils");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Escapes the control characters of a given string.
* @param {string} s A string to escape.
* @returns {string} An escaped string.
*/
function escape(s) {
return `(?:${escapeRegExp(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) {
// `*` is a marker for JSDoc comments.
if (!markers.includes("*")) {
return markers.concat("*");
}
return markers;
}
/**
* Creates string pattern for exceptions.
* Generated pattern:
*
* 1. A space or an exception pattern sequence.
* @param {string[]} exceptions An exception pattern list.
* @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.
* @param {string[]} markers A marker list.
* @param {string[]} exceptions An exception pattern list.
* @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);
return new RegExp(pattern, "u");
}
/**
* 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.
* @param {string[]} markers A marker list.
* @returns {RegExp} A RegExp object for `never` mode.
*/
function createNeverStylePattern(markers) {
const pattern = `^(${markers.map(escape).join("|")})?[ \t]+`;
return new RegExp(pattern, "u");
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('../shared/types').Rule} */
module.exports = {
meta: {
deprecated: true,
replacedBy: [],
type: "suggestion",
docs: {
description: "Enforce consistent spacing after the `//` or `/*` in a comment",
recommended: false,
url: "https://eslint.org/docs/latest/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
}
],
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."
}
},
create(context) {
const sourceCode = context.sourceCode;
// 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),
endRegex: balanced && requireSpace ? new RegExp(`${createExceptionsPattern(exceptions)}$`, "u") : new RegExp(endNeverPattern, "u"),
hasExceptions: exceptions.length > 0,
captureMarker: new RegExp(`^(${markers.map(escape).join("|")})`, "u"),
markers: new Set(markers)
};
return rule;
}, {});
/**
* Reports a beginning spacing error with an appropriate message.
* @param {ASTNode} node A comment node to check.
* @param {string} messageId An error message to report.
* @param {Array} match An array of match results for markers.
* @param {string} refChar Character used for reference in the error message.
* @returns {void}
*/
function reportBegin(node, messageId, match, refChar) {
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] : ""));
},
messageId,
data: { refChar }
});
}
/**
* Reports an ending spacing error with an appropriate message.
* @param {ASTNode} node A comment node to check.
* @param {string} messageId An error message to report.
* @param {string} match An array of the matched whitespace characters.
* @returns {void}
*/
function reportEnd(node, messageId, match) {
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], "");
},
messageId
});
}
/**
* Reports a given comment if it's invalid.
* @param {ASTNode} node a comment node to check.
* @returns {void}
*/
function checkCommentForSpace(node) {
const type = node.type.toLowerCase(),
rule = styleRules[type],
commentIdentifier = type === "block" ? "/*" : "//";
// Ignores empty comments and comments that consist only of a marker.
if (node.value.length === 0 || rule.markers.has(node.value)) {
return;
}
const beginMatch = rule.beginRegex.exec(node.value);
const endMatch = rule.endRegex.exec(node.value);
// Checks.
if (requireSpace) {
if (!beginMatch) {
const hasMarker = rule.captureMarker.exec(node.value);
const marker = hasMarker ? commentIdentifier + hasMarker[0] : commentIdentifier;
if (rule.hasExceptions) {
reportBegin(node, "expectedExceptionAfter", hasMarker, marker);
} else {
reportBegin(node, "expectedSpaceAfter", hasMarker, marker);
}
}
if (balanced && type === "block" && !endMatch) {
reportEnd(node, "expectedSpaceBefore");
}
} else {
if (beginMatch) {
if (!beginMatch[1]) {
reportBegin(node, "unexpectedSpaceAfter", beginMatch, commentIdentifier);
} else {
reportBegin(node, "unexpectedSpaceAfterMarker", beginMatch, beginMatch[1]);
}
}
if (balanced && type === "block" && endMatch) {
reportEnd(node, "unexpectedSpaceBefore", endMatch);
}
}
}
return {
Program() {
const comments = sourceCode.getAllComments();
comments.filter(token => token.type !== "Shebang").forEach(checkCommentForSpace);
}
};
}
};