eslint-plugin-stylistic-compat
Version:
Compatibility version of @stylistic/eslint-plugin for Node.js >= 12 and ESLint v8
248 lines (244 loc) • 12.7 kB
JavaScript
'use strict';
var _Object$assign = require('/runtime-corejs3/core-js/object/assign');
var _findInstanceProperty = require('/runtime-corejs3/core-js/instance/find');
var _forEachInstanceProperty = require('/runtime-corejs3/core-js/instance/for-each');
var _Object$keys = require('/runtime-corejs3/core-js/object/keys');
var _parseInt = require('/runtime-corejs3/core-js/parse-int');
var _endsWithInstanceProperty = require('/runtime-corejs3/core-js/instance/ends-with');
var _startsWithInstanceProperty = require('/runtime-corejs3/core-js/instance/starts-with');
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/instance/filter');
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('../vendor.js');
require('/runtime-corejs3/core-js/object/create');
require('/runtime-corejs3/core-js/object/freeze');
require('/runtime-corejs3/core-js/array/is-array');
require('/runtime-corejs3/core-js/instance/last-index-of');
require('/runtime-corejs3/core-js/instance/slice');
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-float');
require('/runtime-corejs3/core-js/weak-map');
require('/runtime-corejs3/core-js/object/define-property');
require('/runtime-corejs3/core-js/global-this');
require('/runtime-corejs3/core-js/array/from');
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/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/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');
require('/runtime-corejs3/core-js/instance/reduce');
require('/runtime-corejs3/core-js/instance/reverse');
const optionDefaults = {
allow: "none"
};
const messages = {
moveToNewLine: "`{{descriptor}}` must be placed on a new line"
};
var jsxOneExpressionPerLine = utils.createRule({
name: "jsx-one-expression-per-line",
package: "jsx",
meta: {
type: "layout",
docs: {
description: "Require one JSX element per line"
},
fixable: "whitespace",
messages,
schema: [{
type: "object",
properties: {
allow: {
type: "string",
enum: ["none", "literal", "single-child", "single-line", "non-jsx"]
}
},
default: optionDefaults,
additionalProperties: false
}]
},
create(context) {
const options = _Object$assign({}, optionDefaults, context.options[0]);
function nodeKey(node) {
return `${node.loc.start.line},${node.loc.start.column}`;
}
function nodeDescriptor(n) {
return "openingElement" in n && n.openingElement && "name" in n.openingElement.name ? String(n.openingElement.name.name) : context.sourceCode.getText(n).replace(/\n/g, "");
}
function handleJSX(node) {
var _context, _context7;
const children = node.children;
if (!children || !children.length) return;
if (options.allow === "non-jsx" && !_findInstanceProperty(children).call(children, child => child.type === "JSXFragment" || child.type === "JSXElement")) {
return;
}
const openingElement = node.openingElement || node.openingFragment;
const closingElement = node.closingElement || node.closingFragment;
const openingElementStartLine = openingElement.loc.start.line;
const openingElementEndLine = openingElement.loc.end.line;
const closingElementStartLine = closingElement.loc.start.line;
const closingElementEndLine = closingElement.loc.end.line;
if (children.length === 1) {
const child = children[0];
if (openingElementStartLine === openingElementEndLine && openingElementEndLine === closingElementStartLine && closingElementStartLine === closingElementEndLine && closingElementEndLine === child.loc.start.line && child.loc.start.line === child.loc.end.line) {
if (options.allow === "single-child" || options.allow === "literal" && (child.type === "Literal" || child.type === "JSXText") || options.allow === "single-line") {
return;
}
}
}
if (options.allow === "single-line") {
const firstChild = children[0];
const lastChild = children[children.length - 1];
const lineDifference = lastChild.loc.end.line - firstChild.loc.start.line;
let lineBreaks = 0;
if (firstChild.type === "Literal" || firstChild.type === "JSXText") {
if (/^\s*?\n/.test(firstChild.raw)) lineBreaks += 1;
}
if (lastChild.type === "Literal" || lastChild.type === "JSXText") {
if (/\n\s*$/.test(lastChild.raw)) lineBreaks += 1;
}
if (lineDifference === 0 && lineBreaks === 0 || lineDifference === 2 && lineBreaks === 2) return;
}
const childrenGroupedByLine = {};
const fixDetailsByNode = {};
_forEachInstanceProperty(children).call(children, child => {
let countNewLinesBeforeContent = 0;
let countNewLinesAfterContent = 0;
if (child.type === "Literal" || child.type === "JSXText") {
if (utils.isWhiteSpaces(child.raw)) return;
countNewLinesBeforeContent = (child.raw.match(/^\s*\n/g) || []).length;
countNewLinesAfterContent = (child.raw.match(/\n\s*$/g) || []).length;
}
const startLine = child.loc.start.line + countNewLinesBeforeContent;
const endLine = child.loc.end.line - countNewLinesAfterContent;
if (startLine === endLine) {
if (!childrenGroupedByLine[startLine]) childrenGroupedByLine[startLine] = [];
childrenGroupedByLine[startLine].push(child);
} else {
if (!childrenGroupedByLine[startLine]) childrenGroupedByLine[startLine] = [];
childrenGroupedByLine[startLine].push(child);
if (!childrenGroupedByLine[endLine]) childrenGroupedByLine[endLine] = [];
childrenGroupedByLine[endLine].push(child);
}
});
_forEachInstanceProperty(_context = _Object$keys(childrenGroupedByLine)).call(_context, _line => {
var _context2;
const line = _parseInt(_line, 10);
const firstIndex = 0;
const lastIndex = childrenGroupedByLine[line].length - 1;
_forEachInstanceProperty(_context2 = childrenGroupedByLine[line]).call(_context2, (child, i) => {
let prevChild;
let nextChild;
if (i === firstIndex) {
if (line === openingElementEndLine) prevChild = openingElement;
} else {
prevChild = childrenGroupedByLine[line][i - 1];
}
if (i === lastIndex) {
if (line === closingElementStartLine) nextChild = closingElement;
}
if (!prevChild && !nextChild) return;
const spaceBetweenPrev = () => {
var _context3, _context4;
const tokenBetweenNodes = context.sourceCode.getTokensBetween(prevChild, child)[0];
return (prevChild.type === "Literal" || prevChild.type === "JSXText") && _endsWithInstanceProperty(_context3 = prevChild.raw).call(_context3, " ") || (child.type === "Literal" || child.type === "JSXText") && _startsWithInstanceProperty(_context4 = child.raw).call(_context4, " ") || utils.isWhiteSpaces(tokenBetweenNodes == null ? undefined : tokenBetweenNodes.value);
};
const spaceBetweenNext = () => {
var _context5, _context6;
const tokenBetweenNodes = context.sourceCode.getTokensBetween(child, nextChild)[0];
return (nextChild.type === "Literal" || nextChild.type === "JSXText") && _startsWithInstanceProperty(_context5 = nextChild.raw).call(_context5, " ") || (child.type === "Literal" || child.type === "JSXText") && _endsWithInstanceProperty(_context6 = child.raw).call(_context6, " ") || utils.isWhiteSpaces(tokenBetweenNodes == null ? undefined : tokenBetweenNodes.value);
};
const source = context.sourceCode.getText(child);
const leadingSpace = !!(prevChild && spaceBetweenPrev());
const trailingSpace = !!(nextChild && spaceBetweenNext());
const leadingNewLine = !!prevChild;
const trailingNewLine = !!nextChild;
const key = nodeKey(child);
if (!fixDetailsByNode[key]) {
fixDetailsByNode[key] = {
node: child,
source,
descriptor: nodeDescriptor(child)
};
}
if (leadingSpace) fixDetailsByNode[key].leadingSpace = true;
if (leadingNewLine) fixDetailsByNode[key].leadingNewLine = true;
if (trailingNewLine) fixDetailsByNode[key].trailingNewLine = true;
if (trailingSpace) fixDetailsByNode[key].trailingSpace = true;
});
});
_forEachInstanceProperty(_context7 = _Object$keys(fixDetailsByNode)).call(_context7, key => {
const details = fixDetailsByNode[key];
const nodeToReport = details.node;
const descriptor = details.descriptor;
const source = details.source.replace(/(^ +| +$)/g, "");
const leadingSpaceString = details.leadingSpace ? "\n{' '}" : "";
const trailingSpaceString = details.trailingSpace ? "{' '}\n" : "";
const leadingNewLineString = details.leadingNewLine ? "\n" : "";
const trailingNewLineString = details.trailingNewLine ? "\n" : "";
const replaceText = `${leadingSpaceString}${leadingNewLineString}${source}${trailingNewLineString}${trailingSpaceString}`;
context.report({
messageId: "moveToNewLine",
node: nodeToReport,
data: {
descriptor
},
fix(fixer) {
return fixer.replaceText(nodeToReport, replaceText);
}
});
});
}
return {
JSXElement: handleJSX,
JSXFragment: handleJSX
};
}
});
module.exports = jsxOneExpressionPerLine;