UNPKG

eslint-plugin-stylistic-compat

Version:

Compatibility version of @stylistic/eslint-plugin for Node.js >= 12 and ESLint v8

380 lines (376 loc) 20.1 kB
'use strict'; var _everyInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/every'); var _sliceInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/slice'); var _atInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/at'); var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/filter'); var _mapInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/map'); var _trimInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/trim'); var _startsWithInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/starts-with'); var _entriesInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/entries'); var _someInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/some'); var _includesInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/includes'); var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/for-each'); var _reduceInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/reduce'); var utils = require('../utils.js'); require('@babel/runtime-corejs3/core-js/object/from-entries'); require('@babel/runtime-corejs3/core-js/object/entries'); require('@babel/runtime-corejs3/core-js/set'); require('@babel/runtime-corejs3/core-js/instance/flags'); require('../vendor.js'); require('@babel/runtime-corejs3/core-js/object/assign'); require('@babel/runtime-corejs3/core-js/object/create'); require('@babel/runtime-corejs3/core-js/object/freeze'); require('@babel/runtime-corejs3/core-js/array/is-array'); 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/object/keys'); require('@babel/runtime-corejs3/core-js/instance/index-of'); 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/object/define-property'); require('@babel/runtime-corejs3/core-js/global-this'); require('@babel/runtime-corejs3/core-js/array/from'); 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/find'); require('@babel/runtime-corejs3/core-js/instance/sort'); require('@babel/runtime-corejs3/core-js/array/of'); require('@babel/runtime-corejs3/core-js/instance/concat'); require('@babel/runtime-corejs3/core-js/instance/find-index'); require('@babel/runtime-corejs3/core-js/instance/flat'); require('@babel/runtime-corejs3/core-js/instance/keys'); 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-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'); require('@babel/runtime-corejs3/core-js/instance/reverse'); var multilineCommentStyle = utils.createRule({ name: "multiline-comment-style", package: "js", meta: { type: "suggestion", docs: { description: "Enforce a particular style for multiline comments" }, fixable: "whitespace", schema: { anyOf: [{ type: "array", items: [{ enum: ["starred-block", "bare-block"], type: "string" }], additionalItems: false }, { type: "array", items: [{ enum: ["separate-lines"], type: "string" }, { type: "object", properties: { checkJSDoc: { type: "boolean" } }, additionalProperties: false }], additionalItems: false }] }, messages: { expectedBlock: "Expected a block comment instead of consecutive line comments.", expectedBareBlock: "Expected a block comment without padding stars.", startNewline: "Expected a linebreak after '/*'.", endNewline: "Expected a linebreak before '*/'.", missingStar: "Expected a '*' at the start of this line.", alignment: "Expected this line to be aligned with the start of the comment.", expectedLines: "Expected multiple line comments instead of a block comment." } }, create(context) { const sourceCode = context.sourceCode; const option = context.options[0] || "starred-block"; const params = context.options[1] || {}; const checkJSDoc = !!params.checkJSDoc; function isStarredCommentLine(line) { return /^\s*\*/u.test(line); } function isStarredBlockComment([firstComment]) { if (firstComment.type !== "Block") return false; const lines = firstComment.value.split(utils.LINEBREAK_MATCHER); return lines.length > 0 && _everyInstanceProperty(lines).call(lines, (line, i) => (i === 0 || i === lines.length - 1 ? /^\s*$/u : /^\s*\*/u).test(line)); } function isJSDocComment([firstComment]) { var _context; if (firstComment.type !== "Block") return false; const lines = firstComment.value.split(utils.LINEBREAK_MATCHER); return /^\*\s*$/u.test(lines[0]) && _everyInstanceProperty(_context = _sliceInstanceProperty(lines).call(lines, 1, -1)).call(_context, line => /^\s* /u.test(line)) && /^\s*$/u.test(_atInstanceProperty(lines).call(lines, -1)); } function processSeparateLineComments(commentGroup) { var _context2, _context3; const allLinesHaveLeadingSpace = _everyInstanceProperty(_context2 = _filterInstanceProperty(_context3 = _mapInstanceProperty(commentGroup).call(commentGroup, ({ value }) => value)).call(_context3, line => _trimInstanceProperty(line).call(line).length)).call(_context2, line => _startsWithInstanceProperty(line).call(line, " ")); return _mapInstanceProperty(commentGroup).call(commentGroup, ({ value }) => allLinesHaveLeadingSpace ? value.replace(/^ /u, "") : value); } function processStarredBlockComment(comment) { var _context4, _context5, _context6, _context7; const lines = _mapInstanceProperty(_context4 = _filterInstanceProperty(_context5 = comment.value.split(utils.LINEBREAK_MATCHER)).call(_context5, (line, i, linesArr) => !(i === 0 || i === linesArr.length - 1))).call(_context4, line => line.replace(/^\s*$/u, "")); const allLinesHaveLeadingSpace = _everyInstanceProperty(_context6 = _filterInstanceProperty(_context7 = _mapInstanceProperty(lines).call(lines, line => line.replace(/\s*\*/u, ""))).call(_context7, line => _trimInstanceProperty(line).call(line).length)).call(_context6, line => _startsWithInstanceProperty(line).call(line, " ")); return _mapInstanceProperty(lines).call(lines, line => line.replace(allLinesHaveLeadingSpace ? /\s*\* ?/u : /\s*\*/u, "")); } function processBareBlockComment(comment) { var _context8, _context9; const lines = _mapInstanceProperty(_context8 = comment.value.split(utils.LINEBREAK_MATCHER)).call(_context8, line => line.replace(/^\s*$/u, "")); const leadingWhitespace = `${_sliceInstanceProperty(_context9 = sourceCode.text).call(_context9, comment.range[0] - comment.loc.start.column, comment.range[0])} `; let offset = ""; for (const [i, line] of _entriesInstanceProperty(lines).call(lines)) { if (!_trimInstanceProperty(line).call(line).length || i === 0) continue; const [, lineOffset] = line.match(/^(\s*\*?\s*)/u); if (lineOffset.length < leadingWhitespace.length) { const newOffset = _sliceInstanceProperty(leadingWhitespace).call(leadingWhitespace, lineOffset.length - leadingWhitespace.length); if (newOffset.length > offset.length) offset = newOffset; } } return _mapInstanceProperty(lines).call(lines, line => { const match = line.match(/^(\s*\*?\s*)(.*)/u); const [, lineOffset, lineContents] = match; if (lineOffset.length > leadingWhitespace.length) return `${_sliceInstanceProperty(lineOffset).call(lineOffset, leadingWhitespace.length - (offset.length + lineOffset.length))}${lineContents}`; if (lineOffset.length < leadingWhitespace.length) return `${_sliceInstanceProperty(lineOffset).call(lineOffset, leadingWhitespace.length)}${lineContents}`; return lineContents; }); } function getCommentLines(commentGroup) { const [firstComment] = commentGroup; if (firstComment.type === "Line") return processSeparateLineComments(commentGroup); if (isStarredBlockComment(commentGroup)) return processStarredBlockComment(firstComment); return processBareBlockComment(firstComment); } function getInitialOffset(comment) { var _context0; return _sliceInstanceProperty(_context0 = sourceCode.text).call(_context0, comment.range[0] - comment.loc.start.column, comment.range[0]); } function convertToStarredBlock(firstComment, commentLinesList) { const initialOffset = getInitialOffset(firstComment); return `/* ${_mapInstanceProperty(commentLinesList).call(commentLinesList, line => `${initialOffset} * ${line}`).join("\n")} ${initialOffset} */`; } function convertToSeparateLines(firstComment, commentLinesList) { return _mapInstanceProperty(commentLinesList).call(commentLinesList, line => `// ${line}`).join(` ${getInitialOffset(firstComment)}`); } function convertToBlock(firstComment, commentLinesList) { return `/* ${commentLinesList.join(` ${getInitialOffset(firstComment)} `)} */`; } const commentGroupCheckers = { "starred-block": function (commentGroup) { const [firstComment] = commentGroup; const commentLines = getCommentLines(commentGroup); if (_someInstanceProperty(commentLines).call(commentLines, value => _includesInstanceProperty(value).call(value, "*/"))) return; if (commentGroup.length > 1) { context.report({ loc: { start: firstComment.loc.start, end: _atInstanceProperty(commentGroup).call(commentGroup, -1).loc.end }, messageId: "expectedBlock", fix(fixer) { const range = [firstComment.range[0], _atInstanceProperty(commentGroup).call(commentGroup, -1).range[1]]; return _someInstanceProperty(commentLines).call(commentLines, value => _startsWithInstanceProperty(value).call(value, "/")) ? null : fixer.replaceTextRange(range, convertToStarredBlock(firstComment, commentLines)); } }); } else { const lines = firstComment.value.split(utils.LINEBREAK_MATCHER); const expectedLeadingWhitespace = getInitialOffset(firstComment); const expectedLinePrefix = `${expectedLeadingWhitespace} *`; if (!/^\*?\s*$/u.test(lines[0])) { var _context1; const start = _startsWithInstanceProperty(_context1 = firstComment.value).call(_context1, "*") ? firstComment.range[0] + 1 : firstComment.range[0]; context.report({ loc: { start: firstComment.loc.start, end: { line: firstComment.loc.start.line, column: firstComment.loc.start.column + 2 } }, messageId: "startNewline", fix: fixer => fixer.insertTextAfterRange([start, start + 2], ` ${expectedLinePrefix}`) }); } if (!/^\s*$/u.test(_atInstanceProperty(lines).call(lines, -1))) { context.report({ loc: { start: { line: firstComment.loc.end.line, column: firstComment.loc.end.column - 2 }, end: firstComment.loc.end }, messageId: "endNewline", fix: fixer => fixer.replaceTextRange([firstComment.range[1] - 2, firstComment.range[1]], ` ${expectedLinePrefix}/`) }); } for (let lineNumber = firstComment.loc.start.line + 1; lineNumber <= firstComment.loc.end.line; lineNumber++) { const lineText = sourceCode.lines[lineNumber - 1]; const errorType = isStarredCommentLine(lineText) ? "alignment" : "missingStar"; if (!_startsWithInstanceProperty(lineText).call(lineText, expectedLinePrefix)) { context.report({ loc: { start: { line: lineNumber, column: 0 }, end: { line: lineNumber, column: lineText.length } }, messageId: errorType, fix(fixer) { const lineStartIndex = sourceCode.getIndexFromLoc({ line: lineNumber, column: 0 }); if (errorType === "alignment") { const [, commentTextPrefix2 = ""] = lineText.match(/^(\s*\*)/u) || []; const commentTextStartIndex2 = lineStartIndex + commentTextPrefix2.length; return fixer.replaceTextRange([lineStartIndex, commentTextStartIndex2], expectedLinePrefix); } const [, commentTextPrefix = ""] = lineText.match(/^(\s*)/u) || []; const commentTextStartIndex = lineStartIndex + commentTextPrefix.length; let offset; for (const [idx, line] of _entriesInstanceProperty(lines).call(lines)) { if (!/\S+/u.test(line)) continue; const lineTextToAlignWith = sourceCode.lines[firstComment.loc.start.line - 1 + idx]; const [, prefix = "", initialOffset = ""] = lineTextToAlignWith.match(/^(\s*(?:\/?\*)?(\s*))/u) || []; offset = `${_sliceInstanceProperty(commentTextPrefix).call(commentTextPrefix, prefix.length)}${initialOffset}`; if (/^\s*\//u.test(lineText) && offset.length === 0) offset += " "; break; } return fixer.replaceTextRange([lineStartIndex, commentTextStartIndex], `${expectedLinePrefix}${offset}`); } }); } } } }, "separate-lines": function (commentGroup) { const [firstComment] = commentGroup; const isJSDoc = isJSDocComment(commentGroup); if (firstComment.type !== "Block" || !checkJSDoc && isJSDoc) return; let commentLines = getCommentLines(commentGroup); if (isJSDoc) commentLines = _sliceInstanceProperty(commentLines).call(commentLines, 1, commentLines.length - 1); const tokenAfter = sourceCode.getTokenAfter(firstComment, { includeComments: true }); if (tokenAfter && firstComment.loc.end.line === tokenAfter.loc.start.line) return; context.report({ loc: { start: firstComment.loc.start, end: { line: firstComment.loc.start.line, column: firstComment.loc.start.column + 2 } }, messageId: "expectedLines", fix(fixer) { return fixer.replaceText(firstComment, convertToSeparateLines(firstComment, commentLines)); } }); }, "bare-block": function (commentGroup) { if (isJSDocComment(commentGroup)) return; const [firstComment] = commentGroup; const commentLines = getCommentLines(commentGroup); if (firstComment.type === "Line" && commentLines.length > 1 && !_someInstanceProperty(commentLines).call(commentLines, value => _includesInstanceProperty(value).call(value, "*/"))) { context.report({ loc: { start: firstComment.loc.start, end: _atInstanceProperty(commentGroup).call(commentGroup, -1).loc.end }, messageId: "expectedBlock", fix(fixer) { return fixer.replaceTextRange([firstComment.range[0], _atInstanceProperty(commentGroup).call(commentGroup, -1).range[1]], convertToBlock(firstComment, commentLines)); } }); } if (isStarredBlockComment(commentGroup)) { context.report({ loc: { start: firstComment.loc.start, end: { line: firstComment.loc.start.line, column: firstComment.loc.start.column + 2 } }, messageId: "expectedBareBlock", fix(fixer) { return fixer.replaceText(firstComment, convertToBlock(firstComment, commentLines)); } }); } } }; return { Program() { var _context10, _context11, _context12, _context13, _context14, _context15; return _forEachInstanceProperty(_context10 = _filterInstanceProperty(_context11 = _reduceInstanceProperty(_context12 = _filterInstanceProperty(_context13 = _filterInstanceProperty(_context14 = _filterInstanceProperty(_context15 = sourceCode.getAllComments()).call(_context15, comment => comment.type !== "Shebang")).call(_context14, comment => !utils.COMMENTS_IGNORE_PATTERN.test(comment.value))).call(_context13, comment => { const tokenBefore = sourceCode.getTokenBefore(comment, { includeComments: true }); return !tokenBefore || tokenBefore.loc.end.line < comment.loc.start.line; })).call(_context12, (commentGroups, comment, index, commentList) => { const tokenBefore = sourceCode.getTokenBefore(comment, { includeComments: true }); if (comment.type === "Line" && index && commentList[index - 1].type === "Line" && tokenBefore && tokenBefore.loc.end.line === comment.loc.start.line - 1 && tokenBefore === commentList[index - 1]) { _atInstanceProperty(commentGroups).call(commentGroups, -1).push(comment); } else { commentGroups.push([comment]); } return commentGroups; }, [])).call(_context11, commentGroup => !(commentGroup.length === 1 && commentGroup[0].loc.start.line === commentGroup[0].loc.end.line))).call(_context10, commentGroupCheckers[option]); } }; } }); module.exports = multilineCommentStyle;