UNPKG

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
'use strict'; var _includesInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/includes'); var _concatInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/concat'); var _mapInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/map'); var _Array$from = require('@babel/runtime-corejs3/core-js/array/from'); var _reduceInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/reduce'); var _Set = require('@babel/runtime-corejs3/core-js/set'); var _startsWithInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/starts-with'); var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/for-each'); var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/filter'); var utils = require('../utils.js'); var vendor = require('../vendor.js'); require('@babel/runtime-corejs3/core-js/object/from-entries'); require('@babel/runtime-corejs3/core-js/object/entries'); require('@babel/runtime-corejs3/core-js/instance/flags'); require('@babel/runtime-corejs3/core-js/array/is-array'); require('@babel/runtime-corejs3/core-js/object/keys'); require('@babel/runtime-corejs3/core-js/object/assign'); require('@babel/runtime-corejs3/core-js/instance/keys'); require('@babel/runtime-corejs3/core-js/instance/find'); require('@babel/runtime-corejs3/core-js/instance/reverse'); require('@babel/runtime-corejs3/core-js/object/define-property'); require('@babel/runtime-corejs3/core-js/instance/slice'); require('@babel/runtime-corejs3/core-js/object/create'); require('@babel/runtime-corejs3/core-js/object/freeze'); require('@babel/runtime-corejs3/core-js/instance/last-index-of'); require('@babel/runtime-corejs3/core-js/object/define-properties'); require('@babel/runtime-corejs3/core-js/instance/index-of'); require('@babel/runtime-corejs3/core-js/instance/at'); require('@babel/runtime-corejs3/core-js/symbol'); require('@babel/runtime-corejs3/core-js/symbol/iterator'); require('@babel/runtime-corejs3/core-js/parse-int'); require('@babel/runtime-corejs3/core-js/parse-float'); require('@babel/runtime-corejs3/core-js/weak-map'); require('@babel/runtime-corejs3/core-js/global-this'); require('eslint/use-at-your-own-risk'); require('eslint'); require('@babel/runtime-corejs3/core-js/object/get-own-property-descriptor'); require('@babel/runtime-corejs3/core-js/map'); require('@babel/runtime-corejs3/core-js/instance/some'); require('@babel/runtime-corejs3/core-js/instance/every'); require('@babel/runtime-corejs3/core-js/instance/sort'); require('@babel/runtime-corejs3/core-js/array/of'); require('@babel/runtime-corejs3/core-js/instance/entries'); require('@babel/runtime-corejs3/core-js/instance/find-index'); require('@babel/runtime-corejs3/core-js/instance/flat'); require('@babel/runtime-corejs3/core-js/instance/values'); require('@babel/runtime-corejs3/core-js/object/get-own-property-names'); require('@babel/runtime-corejs3/core-js/number/is-finite'); require('@babel/runtime-corejs3/core-js/number/is-nan'); require('@babel/runtime-corejs3/core-js/number/parse-float'); require('@babel/runtime-corejs3/core-js/number/parse-int'); require('@babel/runtime-corejs3/core-js/object/is'); require('@babel/runtime-corejs3/core-js/object/is-extensible'); require('@babel/runtime-corejs3/core-js/object/is-frozen'); require('@babel/runtime-corejs3/core-js/object/is-sealed'); require('@babel/runtime-corejs3/core-js/object/values'); require('@babel/runtime-corejs3/core-js/string/from-code-point'); require('@babel/runtime-corejs3/core-js/string/raw'); require('@babel/runtime-corejs3/core-js/instance/code-point-at'); require('@babel/runtime-corejs3/core-js/instance/ends-with'); require('@babel/runtime-corejs3/core-js/instance/pad-end'); require('@babel/runtime-corejs3/core-js/instance/pad-start'); require('@babel/runtime-corejs3/core-js/instance/trim'); require('@babel/runtime-corejs3/core-js/instance/trim-end'); require('@babel/runtime-corejs3/core-js/instance/trim-left'); require('@babel/runtime-corejs3/core-js/instance/trim-right'); require('@babel/runtime-corejs3/core-js/instance/trim-start'); require('@babel/runtime-corejs3/core-js/symbol/for'); require('@babel/runtime-corejs3/core-js/symbol/key-for'); require('@babel/runtime-corejs3/core-js/object/prevent-extensions'); require('@babel/runtime-corejs3/core-js/object/seal'); require('@babel/runtime-corejs3/core-js/object/get-prototype-of'); require('@babel/runtime-corejs3/core-js/symbol/replace'); require('@babel/runtime-corejs3/core-js/instance/bind'); require('@babel/runtime-corejs3/core-js/instance/splice'); require('@babel/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;