UNPKG

eslint-plugin-stylistic-compat

Version:

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

301 lines (297 loc) 13.2 kB
'use strict'; var _Array$from = require('@babel/runtime-corejs3/core-js/array/from'); var _Object$assign = require('@babel/runtime-corejs3/core-js/object/assign'); var _trimInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/trim'); var _sliceInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/slice'); var _Array$isArray = require('@babel/runtime-corejs3/core-js/array/is-array'); var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/filter'); var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/for-each'); var utils = require('../utils.js'); require('@babel/runtime-corejs3/core-js/object/from-entries'); require('@babel/runtime-corejs3/core-js/instance/map'); require('@babel/runtime-corejs3/core-js/object/entries'); require('@babel/runtime-corejs3/core-js/set'); require('@babel/runtime-corejs3/core-js/instance/flags'); require('@babel/runtime-corejs3/core-js/instance/includes'); require('@babel/runtime-corejs3/core-js/instance/starts-with'); require('../vendor.js'); 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/object/keys'); 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/object/define-property'); 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/find'); 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/concat'); 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/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/reduce'); require('@babel/runtime-corejs3/core-js/instance/reverse'); const OPTIONS_SCHEMA = { type: "object", properties: { code: { type: "integer", minimum: 0 }, comments: { type: "integer", minimum: 0 }, tabWidth: { type: "integer", minimum: 0 }, ignorePattern: { type: "string" }, ignoreComments: { type: "boolean" }, ignoreStrings: { type: "boolean" }, ignoreUrls: { type: "boolean" }, ignoreTemplateLiterals: { type: "boolean" }, ignoreRegExpLiterals: { type: "boolean" }, ignoreTrailingComments: { type: "boolean" } }, additionalProperties: false }; const OPTIONS_OR_INTEGER_SCHEMA = { anyOf: [OPTIONS_SCHEMA, { type: "integer", minimum: 0 }] }; var maxLen = utils.createRule({ name: "max-len", package: "js", meta: { type: "layout", docs: { description: "Enforce a maximum line length" }, schema: [OPTIONS_OR_INTEGER_SCHEMA, OPTIONS_OR_INTEGER_SCHEMA, OPTIONS_SCHEMA], messages: { max: "This line has a length of {{lineLength}}. Maximum allowed is {{maxLength}}.", maxComment: "This line has a comment length of {{lineLength}}. Maximum allowed is {{maxCommentLength}}." } }, create(context) { const URL_REGEXP = /[^:/?#]:\/\/[^?#]/u; const sourceCode = context.sourceCode; function computeLineLength(line, tabWidth2) { let extraCharacterCount = 0; line.replace(/\t/gu, (_, offset) => { const totalOffset = offset + extraCharacterCount; const previousTabStopOffset = tabWidth2 ? totalOffset % tabWidth2 : 0; const spaceCount = tabWidth2 - previousTabStopOffset; extraCharacterCount += spaceCount - 1; return ""; }); return _Array$from(line).length + extraCharacterCount; } const options = _Object$assign({}, context.options[context.options.length - 1]); if (typeof context.options[0] === "number") options.code = context.options[0]; if (typeof context.options[1] === "number") options.tabWidth = context.options[1]; const maxLength = typeof options.code === "number" ? options.code : 80; const tabWidth = typeof options.tabWidth === "number" ? options.tabWidth : 4; const ignoreComments = !!options.ignoreComments; const ignoreStrings = !!options.ignoreStrings; const ignoreTemplateLiterals = !!options.ignoreTemplateLiterals; const ignoreRegExpLiterals = !!options.ignoreRegExpLiterals; const ignoreTrailingComments = !!options.ignoreTrailingComments || !!options.ignoreComments; const ignoreUrls = !!options.ignoreUrls; const maxCommentLength = options.comments; let ignorePattern = null; if (options.ignorePattern) ignorePattern = new RegExp(options.ignorePattern, "u"); function isTrailingComment(line, lineNumber, comment) { return comment && comment.loc.start.line === lineNumber && lineNumber <= comment.loc.end.line && (comment.loc.end.line > lineNumber || comment.loc.end.column === line.length); } function isFullLineComment(line, lineNumber, comment) { var _context; const start = comment.loc.start; const end = comment.loc.end; const isFirstTokenOnLine = !_trimInstanceProperty(_context = _sliceInstanceProperty(line).call(line, 0, comment.loc.start.column)).call(_context); return comment && (start.line < lineNumber || start.line === lineNumber && isFirstTokenOnLine) && (end.line > lineNumber || end.line === lineNumber && end.column === line.length); } function isJSXEmptyExpressionInSingleLineContainer(node) { if (!node || !node.parent || node.type !== "JSXEmptyExpression" || node.parent.type !== "JSXExpressionContainer") return false; const parent = node.parent; return parent.loc.start.line === parent.loc.end.line; } function stripTrailingComment(line, comment) { return _sliceInstanceProperty(line).call(line, 0, comment.loc.start.column).replace(/\s+$/u, ""); } function ensureArrayAndPush(object, key, value) { if (!_Array$isArray(object[key])) object[key] = []; object[key].push(value); } function getAllStrings() { var _context2; return _filterInstanceProperty(_context2 = sourceCode.ast.tokens).call(_context2, token => token.type === "String" || token.type === "JSXText" && sourceCode.getNodeByRangeIndex(token.range[0] - 1).type === "JSXAttribute"); } function getAllTemplateLiterals() { var _context3; return _filterInstanceProperty(_context3 = sourceCode.ast.tokens).call(_context3, token => token.type === "Template"); } function getAllRegExpLiterals() { var _context4; return _filterInstanceProperty(_context4 = sourceCode.ast.tokens).call(_context4, token => token.type === "RegularExpression"); } function groupArrayByLineNumber(arr) { const obj = {}; for (let i = 0; i < arr.length; i++) { const node = arr[i]; for (let j = node.loc.start.line; j <= node.loc.end.line; ++j) ensureArrayAndPush(obj, j, node); } return obj; } function getAllComments() { var _context5; const comments = []; _forEachInstanceProperty(_context5 = sourceCode.getAllComments()).call(_context5, commentNode => { const containingNode = sourceCode.getNodeByRangeIndex(commentNode.range[0]); if (isJSXEmptyExpressionInSingleLineContainer(containingNode)) { if (comments[comments.length - 1] !== containingNode.parent) comments.push(containingNode.parent); } else { comments.push(commentNode); } }); return comments; } function checkProgramForMaxLength(node) { const lines = sourceCode.lines; const comments = ignoreComments || maxCommentLength || ignoreTrailingComments ? getAllComments() : []; let commentsIndex = 0; const strings = getAllStrings(); const stringsByLine = groupArrayByLineNumber(strings); const templateLiterals = getAllTemplateLiterals(); const templateLiteralsByLine = groupArrayByLineNumber(templateLiterals); const regExpLiterals = getAllRegExpLiterals(); const regExpLiteralsByLine = groupArrayByLineNumber(regExpLiterals); _forEachInstanceProperty(lines).call(lines, (line, i) => { const lineNumber = i + 1; let lineIsComment = false; let textToMeasure; if (commentsIndex < comments.length) { let comment = null; do comment = comments[++commentsIndex]; while (comment && comment.loc.start.line <= lineNumber); comment = comments[--commentsIndex]; if (isFullLineComment(line, lineNumber, comment)) { lineIsComment = true; textToMeasure = line; } else if (ignoreTrailingComments && isTrailingComment(line, lineNumber, comment)) { textToMeasure = stripTrailingComment(line, comment); let lastIndex = commentsIndex; while (isTrailingComment(textToMeasure, lineNumber, comments[--lastIndex])) textToMeasure = stripTrailingComment(textToMeasure, comments[lastIndex]); } else { textToMeasure = line; } } else { textToMeasure = line; } if (ignorePattern && ignorePattern.test(textToMeasure) || ignoreUrls && URL_REGEXP.test(textToMeasure) || ignoreStrings && stringsByLine[lineNumber] || ignoreTemplateLiterals && templateLiteralsByLine[lineNumber] || ignoreRegExpLiterals && regExpLiteralsByLine[lineNumber]) { return; } const lineLength = computeLineLength(textToMeasure, tabWidth); const commentLengthApplies = lineIsComment && maxCommentLength; if (lineIsComment && ignoreComments) return; const loc = { start: { line: lineNumber, column: 0 }, end: { line: lineNumber, column: textToMeasure.length } }; if (commentLengthApplies) { if (lineLength > maxCommentLength) { context.report({ node, loc, messageId: "maxComment", data: { lineLength, maxCommentLength } }); } } else if (lineLength > maxLength) { context.report({ node, loc, messageId: "max", data: { lineLength, maxLength } }); } }); } return { Program: checkProgramForMaxLength }; } }); module.exports = maxLen;