eslint-plugin-stylistic-compat
Version:
Compatibility version of @stylistic/eslint-plugin for Node.js >= 12 and ESLint v8
292 lines (288 loc) • 12.4 kB
JavaScript
'use strict';
var _includesInstanceProperty = require('/runtime-corejs3/core-js/instance/includes');
var _concatInstanceProperty = require('/runtime-corejs3/core-js/instance/concat');
var _mapInstanceProperty = require('/runtime-corejs3/core-js/instance/map');
var _Array$from = require('/runtime-corejs3/core-js/array/from');
var _reduceInstanceProperty = require('/runtime-corejs3/core-js/instance/reduce');
var _Set = require('/runtime-corejs3/core-js/set');
var _startsWithInstanceProperty = require('/runtime-corejs3/core-js/instance/starts-with');
var _forEachInstanceProperty = require('/runtime-corejs3/core-js/instance/for-each');
var _filterInstanceProperty = require('/runtime-corejs3/core-js/instance/filter');
var utils = require('../utils.js');
var vendor = require('../vendor.js');
require('/runtime-corejs3/core-js/object/from-entries');
require('/runtime-corejs3/core-js/object/entries');
require('/runtime-corejs3/core-js/instance/flags');
require('/runtime-corejs3/core-js/array/is-array');
require('/runtime-corejs3/core-js/object/keys');
require('/runtime-corejs3/core-js/object/assign');
require('/runtime-corejs3/core-js/instance/keys');
require('/runtime-corejs3/core-js/instance/find');
require('/runtime-corejs3/core-js/instance/reverse');
require('/runtime-corejs3/core-js/object/define-property');
require('/runtime-corejs3/core-js/instance/slice');
require('/runtime-corejs3/core-js/object/create');
require('/runtime-corejs3/core-js/object/freeze');
require('/runtime-corejs3/core-js/instance/last-index-of');
require('/runtime-corejs3/core-js/object/define-properties');
require('/runtime-corejs3/core-js/instance/index-of');
require('/runtime-corejs3/core-js/instance/at');
require('/runtime-corejs3/core-js/symbol');
require('/runtime-corejs3/core-js/symbol/iterator');
require('/runtime-corejs3/core-js/parse-int');
require('/runtime-corejs3/core-js/parse-float');
require('/runtime-corejs3/core-js/weak-map');
require('/runtime-corejs3/core-js/global-this');
require('eslint/use-at-your-own-risk');
require('eslint');
require('/runtime-corejs3/core-js/object/get-own-property-descriptor');
require('/runtime-corejs3/core-js/map');
require('/runtime-corejs3/core-js/instance/some');
require('/runtime-corejs3/core-js/instance/every');
require('/runtime-corejs3/core-js/instance/sort');
require('/runtime-corejs3/core-js/array/of');
require('/runtime-corejs3/core-js/instance/entries');
require('/runtime-corejs3/core-js/instance/find-index');
require('/runtime-corejs3/core-js/instance/flat');
require('/runtime-corejs3/core-js/instance/values');
require('/runtime-corejs3/core-js/object/get-own-property-names');
require('/runtime-corejs3/core-js/number/is-finite');
require('/runtime-corejs3/core-js/number/is-nan');
require('/runtime-corejs3/core-js/number/parse-float');
require('/runtime-corejs3/core-js/number/parse-int');
require('/runtime-corejs3/core-js/object/is');
require('/runtime-corejs3/core-js/object/is-extensible');
require('/runtime-corejs3/core-js/object/is-frozen');
require('/runtime-corejs3/core-js/object/is-sealed');
require('/runtime-corejs3/core-js/object/values');
require('/runtime-corejs3/core-js/string/from-code-point');
require('/runtime-corejs3/core-js/string/raw');
require('/runtime-corejs3/core-js/instance/code-point-at');
require('/runtime-corejs3/core-js/instance/ends-with');
require('/runtime-corejs3/core-js/instance/pad-end');
require('/runtime-corejs3/core-js/instance/pad-start');
require('/runtime-corejs3/core-js/instance/trim');
require('/runtime-corejs3/core-js/instance/trim-end');
require('/runtime-corejs3/core-js/instance/trim-left');
require('/runtime-corejs3/core-js/instance/trim-right');
require('/runtime-corejs3/core-js/instance/trim-start');
require('/runtime-corejs3/core-js/symbol/for');
require('/runtime-corejs3/core-js/symbol/key-for');
require('/runtime-corejs3/core-js/object/prevent-extensions');
require('/runtime-corejs3/core-js/object/seal');
require('/runtime-corejs3/core-js/object/get-prototype-of');
require('/runtime-corejs3/core-js/symbol/replace');
require('/runtime-corejs3/core-js/instance/bind');
require('/runtime-corejs3/core-js/instance/splice');
require('/runtime-corejs3/core-js/instance/repeat');
function escape(s) {
return `(?:${vendor.escapeStringRegexp(s)})`;
}
function escapeAndRepeat(s) {
return `${escape(s)}+`;
}
function parseMarkersOption(markers) {
if (!_includesInstanceProperty(markers).call(markers, "*")) return _concatInstanceProperty(markers).call(markers, "*");
return markers;
}
function createExceptionsPattern(exceptions) {
let pattern = "";
if (exceptions.length === 0) {
pattern += "\\s";
} else {
pattern += "(?:\\s|";
if (exceptions.length === 1) {
pattern += escapeAndRepeat(exceptions[0]);
} else {
pattern += "(?:";
pattern += _mapInstanceProperty(exceptions).call(exceptions, escapeAndRepeat).join("|");
pattern += ")";
}
pattern += `(?:$|[${_Array$from(utils.LINEBREAKS).join("")}]))`;
}
return pattern;
}
function createAlwaysStylePattern(markers, exceptions) {
let pattern = "^";
if (markers.length === 1) {
pattern += escape(markers[0]);
} else {
pattern += "(?:";
pattern += _mapInstanceProperty(markers).call(markers, escape).join("|");
pattern += ")";
}
pattern += "?";
pattern += createExceptionsPattern(exceptions);
return new RegExp(pattern, "u");
}
function createNeverStylePattern(markers) {
const pattern = `^(${_mapInstanceProperty(markers).call(markers, escape).join("|")})?[ ]+`;
return new RegExp(pattern, "u");
}
var spacedComment = utils.createRule({
name: "spaced-comment",
package: "js",
meta: {
type: "layout",
docs: {
description: "Enforce consistent spacing after the `//` or `/*` in a comment"
},
fixable: "whitespace",
schema: [{
type: "string",
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) {
var _context;
const sourceCode = context.sourceCode;
const requireSpace = context.options[0] !== "never";
const config = context.options[1] || {};
const balanced = config.block && config.block.balanced;
const styleRules = _reduceInstanceProperty(_context = ["block", "line"]).call(_context, (rule, type) => {
var _config$nodeType, _config$nodeType2;
const nodeType = type;
const markers = parseMarkersOption(config[nodeType] && ((_config$nodeType = config[nodeType]) == null ? undefined : _config$nodeType.markers) || config.markers || []);
const exceptions = config[nodeType] && ((_config$nodeType2 = config[nodeType]) == null ? undefined : _config$nodeType2.exceptions) || config.exceptions || [];
const endNeverPattern = "[ ]+$";
rule[nodeType] = {
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(`^(${_mapInstanceProperty(markers).call(markers, escape).join("|")})`, "u"),
markers: new _Set(markers)
};
return rule;
}, {});
function reportBegin(node, messageId, match, refChar) {
const type = node.type.toLowerCase();
const 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], " ");
}
if (match) end += match[0].length;
return fixer.replaceTextRange([start, end], commentIdentifier + (match && match[1] ? match[1] : ""));
},
messageId,
data: {
refChar
}
});
}
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;
let start = end;
if (match) start -= match[0].length;
return fixer.replaceTextRange([start, end], "");
},
messageId
});
}
function checkCommentForSpace(node) {
var _context2, _context3;
const type = node.type.toLowerCase();
const rule = styleRules[type];
const commentIdentifier = type === "block" ? "/*" : "//";
if (node.value.length === 0 || rule.markers.has(node.value)) return;
if (type === "line" && (_startsWithInstanceProperty(_context2 = node.value).call(_context2, "/ <reference") || _startsWithInstanceProperty(_context3 = node.value).call(_context3, "/ <amd"))) return;
const beginMatch = rule.beginRegex.exec(node.value);
const endMatch = rule.endRegex.exec(node.value);
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", null);
} 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() {
var _context4;
const comments = sourceCode.getAllComments();
_forEachInstanceProperty(_context4 = _filterInstanceProperty(comments).call(comments, token => token.type !== "Shebang")).call(_context4, checkCommentForSpace);
}
};
}
});
module.exports = spacedComment;