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
JavaScript
'use strict';
var _Array$from = require('/runtime-corejs3/core-js/array/from');
var _Object$assign = require('/runtime-corejs3/core-js/object/assign');
var _trimInstanceProperty = require('/runtime-corejs3/core-js/instance/trim');
var _sliceInstanceProperty = require('/runtime-corejs3/core-js/instance/slice');
var _Array$isArray = require('/runtime-corejs3/core-js/array/is-array');
var _filterInstanceProperty = require('/runtime-corejs3/core-js/instance/filter');
var _forEachInstanceProperty = require('/runtime-corejs3/core-js/instance/for-each');
var utils = require('../utils.js');
require('/runtime-corejs3/core-js/object/from-entries');
require('/runtime-corejs3/core-js/instance/map');
require('/runtime-corejs3/core-js/object/entries');
require('/runtime-corejs3/core-js/set');
require('/runtime-corejs3/core-js/instance/flags');
require('/runtime-corejs3/core-js/instance/includes');
require('/runtime-corejs3/core-js/instance/starts-with');
require('../vendor.js');
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/object/keys');
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/object/define-property');
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/find');
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/concat');
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/keys');
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-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');
require('/runtime-corejs3/core-js/instance/reduce');
require('/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;