eslint-plugin-stylistic-compat
Version:
Compatibility version of @stylistic/eslint-plugin for Node.js >= 12 and ESLint v8
467 lines (463 loc) • 20.5 kB
JavaScript
'use strict';
var _includesInstanceProperty = require('/runtime-corejs3/core-js/instance/includes');
var _sliceInstanceProperty = require('/runtime-corejs3/core-js/instance/slice');
var _mapInstanceProperty = require('/runtime-corejs3/core-js/instance/map');
var _sortInstanceProperty = require('/runtime-corejs3/core-js/instance/sort');
var _forEachInstanceProperty = require('/runtime-corejs3/core-js/instance/for-each');
var _Array$isArray = require('/runtime-corejs3/core-js/array/is-array');
var _filterInstanceProperty = require('/runtime-corejs3/core-js/instance/filter');
var _WeakMap = require('/runtime-corejs3/core-js/weak-map');
var _reduceInstanceProperty = require('/runtime-corejs3/core-js/instance/reduce');
var utils = require('../utils.js');
require('/runtime-corejs3/core-js/object/from-entries');
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/starts-with');
require('../vendor.js');
require('/runtime-corejs3/core-js/object/assign');
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/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/find');
require('/runtime-corejs3/core-js/instance/every');
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');
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/reverse');
function isCallbackPropName(name) {
return /^on[A-Z]/.test(name);
}
function isMultilineProp(node) {
return node.loc.start.line !== node.loc.end.line;
}
const messages = {
noUnreservedProps: "A customized reserved first list must only contain a subset of React reserved props. Remove: {{unreservedWords}}",
listIsEmpty: "A customized reserved first list must not be empty",
listReservedPropsFirst: "Reserved props must be listed before all other props",
listCallbacksLast: "Callbacks must be listed after all other props",
listShorthandFirst: "Shorthand props must be listed before all other props",
listShorthandLast: "Shorthand props must be listed after all other props",
listMultilineFirst: "Multiline props must be listed before all other props",
listMultilineLast: "Multiline props must be listed after all other props",
sortPropsByAlpha: "Props should be sorted alphabetically"
};
const RESERVED_PROPS_LIST = ["children", "dangerouslySetInnerHTML", "key", "ref"];
function isReservedPropName(name, list) {
return _includesInstanceProperty(list).call(list, name);
}
let attributeMap;
function shouldSortToEnd(node) {
const attr = attributeMap.get(node);
return !!attr && !!attr.hasComment;
}
function contextCompare(a, b, options) {
let aProp = utils.getPropName(a);
let bProp = utils.getPropName(b);
const aSortToEnd = shouldSortToEnd(a);
const bSortToEnd = shouldSortToEnd(b);
if (aSortToEnd && !bSortToEnd) return 1;
if (!aSortToEnd && bSortToEnd) return -1;
if (options.reservedFirst) {
const aIsReserved = isReservedPropName(aProp, options.reservedList);
const bIsReserved = isReservedPropName(bProp, options.reservedList);
if (aIsReserved && !bIsReserved) return -1;
if (!aIsReserved && bIsReserved) return 1;
}
if (options.callbacksLast) {
const aIsCallback = isCallbackPropName(aProp);
const bIsCallback = isCallbackPropName(bProp);
if (aIsCallback && !bIsCallback) return 1;
if (!aIsCallback && bIsCallback) return -1;
}
if (options.shorthandFirst || options.shorthandLast) {
const shorthandSign = options.shorthandFirst ? -1 : 1;
if (!a.value && b.value) return shorthandSign;
if (a.value && !b.value) return -shorthandSign;
}
if (options.multiline !== "ignore") {
const multilineSign = options.multiline === "first" ? -1 : 1;
const aIsMultiline = isMultilineProp(a);
const bIsMultiline = isMultilineProp(b);
if (aIsMultiline && !bIsMultiline) return multilineSign;
if (!aIsMultiline && bIsMultiline) return -multilineSign;
}
if (options.noSortAlphabetically) return 0;
const actualLocale = options.locale === "auto" ? undefined : options.locale;
if (options.ignoreCase) {
aProp = aProp.toLowerCase();
bProp = bProp.toLowerCase();
return aProp.localeCompare(bProp, actualLocale);
}
if (aProp === bProp) return 0;
if (options.locale === "auto") return aProp < bProp ? -1 : 1;
return aProp.localeCompare(bProp, actualLocale);
}
function getGroupsOfSortableAttributes(attributes, context) {
const sourceCode = context.sourceCode;
const sortableAttributeGroups = [];
let groupCount = 0;
function addtoSortableAttributeGroups(attribute) {
sortableAttributeGroups[groupCount - 1].push(attribute);
}
for (let i = 0; i < attributes.length; i++) {
const attribute = attributes[i];
const nextAttribute = attributes[i + 1];
const attributeline = attribute.loc.start.line;
let comment = [];
try {
comment = sourceCode.getCommentsAfter(attribute);
} catch {}
const lastAttr = attributes[i - 1];
const attrIsSpread = attribute.type === "JSXSpreadAttribute";
if (!lastAttr || lastAttr.type === "JSXSpreadAttribute" && !attrIsSpread) {
groupCount += 1;
sortableAttributeGroups[groupCount - 1] = [];
}
if (!attrIsSpread) {
if (comment.length === 0) {
attributeMap.set(attribute, {
end: attribute.range[1],
hasComment: false
});
addtoSortableAttributeGroups(attribute);
} else {
const firstComment = comment[0];
const commentline = firstComment.loc.start.line;
if (comment.length === 1) {
if (attributeline + 1 === commentline && nextAttribute) {
attributeMap.set(attribute, {
end: nextAttribute.range[1],
hasComment: true
});
addtoSortableAttributeGroups(attribute);
i += 1;
} else if (attributeline === commentline) {
if (firstComment.type === "Block" && nextAttribute) {
attributeMap.set(attribute, {
end: nextAttribute.range[1],
hasComment: true
});
i += 1;
} else if (firstComment.type === "Block") {
attributeMap.set(attribute, {
end: firstComment.range[1],
hasComment: true
});
} else {
attributeMap.set(attribute, {
end: firstComment.range[1],
hasComment: false
});
}
addtoSortableAttributeGroups(attribute);
}
} else if (comment.length > 1 && attributeline + 1 === comment[1].loc.start.line && nextAttribute) {
const commentNextAttribute = sourceCode.getCommentsAfter(nextAttribute);
attributeMap.set(attribute, {
end: nextAttribute.range[1],
hasComment: true
});
if (commentNextAttribute.length === 1 && nextAttribute.loc.start.line === commentNextAttribute[0].loc.start.line) {
attributeMap.set(attribute, {
end: commentNextAttribute[0].range[1],
hasComment: true
});
}
addtoSortableAttributeGroups(attribute);
i += 1;
}
}
}
}
return sortableAttributeGroups;
}
function generateFixerFunction(node, context, reservedList) {
var _context, _context2;
const sourceCode = context.sourceCode;
const attributes = _sliceInstanceProperty(_context = node.attributes).call(_context, 0);
const configuration = context.options[0] || {};
const ignoreCase = configuration.ignoreCase || false;
const callbacksLast = configuration.callbacksLast || false;
const shorthandFirst = configuration.shorthandFirst || false;
const shorthandLast = configuration.shorthandLast || false;
const multiline = configuration.multiline || "ignore";
const noSortAlphabetically = configuration.noSortAlphabetically || false;
const reservedFirst = configuration.reservedFirst || false;
const locale = configuration.locale || "auto";
const options = {
ignoreCase,
callbacksLast,
shorthandFirst,
shorthandLast,
multiline,
noSortAlphabetically,
reservedFirst,
reservedList,
locale
};
const sortableAttributeGroups = getGroupsOfSortableAttributes(attributes, context);
const sortedAttributeGroups = _mapInstanceProperty(_context2 = _sliceInstanceProperty(sortableAttributeGroups).call(sortableAttributeGroups, 0)).call(_context2, group => {
var _context3;
return _sortInstanceProperty(_context3 = [...group]).call(_context3, (a, b) => contextCompare(a, b, options));
});
return function fixFunction(fixer) {
const fixers = [];
let source = sourceCode.getText();
_forEachInstanceProperty(sortableAttributeGroups).call(sortableAttributeGroups, (sortableGroup, ii) => {
_forEachInstanceProperty(sortableGroup).call(sortableGroup, (attr, jj) => {
const sortedAttr = sortedAttributeGroups[ii][jj];
const sortedAttrText = _sliceInstanceProperty(source).call(source, sortedAttr.range[0], attributeMap.get(sortedAttr).end);
fixers.push({
range: [attr.range[0], attributeMap.get(attr).end],
text: sortedAttrText
});
});
});
_sortInstanceProperty(fixers).call(fixers, (a, b) => b.range[0] - a.range[0]);
const firstFixer = fixers[0];
const lastFixer = fixers[fixers.length - 1];
const rangeStart = lastFixer ? lastFixer.range[0] : 0;
const rangeEnd = firstFixer ? firstFixer.range[1] : -0;
_forEachInstanceProperty(fixers).call(fixers, fix => {
source = `${_sliceInstanceProperty(source).call(source, 0, fix.range[0])}${fix.text}${_sliceInstanceProperty(source).call(source, fix.range[1])}`;
});
return fixer.replaceTextRange([rangeStart, rangeEnd], _sliceInstanceProperty(source).call(source, rangeStart, rangeEnd));
};
}
function validateReservedFirstConfig(context, reservedFirst) {
if (reservedFirst) {
if (_Array$isArray(reservedFirst)) {
const nonReservedWords = _filterInstanceProperty(reservedFirst).call(reservedFirst, word => !isReservedPropName(word, RESERVED_PROPS_LIST));
if (reservedFirst.length === 0) {
return function Report(decl) {
context.report({
node: decl,
messageId: "listIsEmpty"
});
};
}
if (nonReservedWords.length > 0) {
return function Report(decl) {
context.report({
node: decl,
messageId: "noUnreservedProps",
data: {
unreservedWords: nonReservedWords.toString()
}
});
};
}
}
}
}
const reportedNodeAttributes = /* @__PURE__ */new _WeakMap();
function reportNodeAttribute(nodeAttribute, errorType, node, context, reservedList) {
var _nodeAttribute$name;
const errors = reportedNodeAttributes.get(nodeAttribute) || [];
if (_includesInstanceProperty(errors).call(errors, errorType)) return;
errors.push(errorType);
reportedNodeAttributes.set(nodeAttribute, errors);
context.report({
node: (_nodeAttribute$name = nodeAttribute.name) != null ? _nodeAttribute$name : "",
messageId: errorType,
fix: generateFixerFunction(node, context, reservedList)
});
}
var jsxSortProps = utils.createRule({
name: "jsx-sort-props",
package: "jsx",
meta: {
type: "layout",
docs: {
description: "Enforce props alphabetical sorting"
},
fixable: "code",
messages,
schema: [{
type: "object",
properties: {
// Whether callbacks (prefixed with "on") should be listed at the very end,
// after all other props. Supersedes shorthandLast.
callbacksLast: {
type: "boolean"
},
// Whether shorthand properties (without a value) should be listed first
shorthandFirst: {
type: "boolean"
},
// Whether shorthand properties (without a value) should be listed last
shorthandLast: {
type: "boolean"
},
// Whether multiline properties should be listed first or last
multiline: {
type: "string",
enum: ["ignore", "first", "last"],
default: "ignore"
},
ignoreCase: {
type: "boolean"
},
// Whether alphabetical sorting should be enforced
noSortAlphabetically: {
type: "boolean"
},
reservedFirst: {
type: ["array", "boolean"]
},
locale: {
type: "string",
default: "auto"
}
},
additionalProperties: false
}]
},
create(context) {
const configuration = context.options[0] || {};
const ignoreCase = configuration.ignoreCase || false;
const callbacksLast = configuration.callbacksLast || false;
const shorthandFirst = configuration.shorthandFirst || false;
const shorthandLast = configuration.shorthandLast || false;
const multiline = configuration.multiline || "ignore";
const noSortAlphabetically = configuration.noSortAlphabetically || false;
const reservedFirst = configuration.reservedFirst || false;
const reservedFirstError = validateReservedFirstConfig(context, reservedFirst);
const reservedList = _Array$isArray(reservedFirst) ? reservedFirst : RESERVED_PROPS_LIST;
const locale = configuration.locale || "auto";
return {
Program() {
attributeMap = /* @__PURE__ */new _WeakMap();
},
JSXOpeningElement(node) {
var _context4;
const nodeReservedList = reservedFirst && !utils.isDOMComponent(node) ? _filterInstanceProperty(reservedList).call(reservedList, prop => prop !== "dangerouslySetInnerHTML") : reservedList;
_reduceInstanceProperty(_context4 = node.attributes).call(_context4, (memo, decl, idx, attrs) => {
if (decl.type === "JSXSpreadAttribute") return attrs[idx + 1];
let previousPropName = utils.getPropName(memo);
let currentPropName = utils.getPropName(decl);
const previousValue = memo.value;
const currentValue = decl.value;
const previousIsCallback = isCallbackPropName(previousPropName);
const currentIsCallback = isCallbackPropName(currentPropName);
if (ignoreCase) {
previousPropName = previousPropName.toLowerCase();
currentPropName = currentPropName.toLowerCase();
}
if (reservedFirst) {
if (reservedFirstError) {
reservedFirstError(decl);
return memo;
}
const previousIsReserved = isReservedPropName(previousPropName, nodeReservedList);
const currentIsReserved = isReservedPropName(currentPropName, nodeReservedList);
if (previousIsReserved && !currentIsReserved) return decl;
if (!previousIsReserved && currentIsReserved) {
reportNodeAttribute(decl, "listReservedPropsFirst", node, context, nodeReservedList);
return memo;
}
}
if (callbacksLast) {
if (!previousIsCallback && currentIsCallback) {
return decl;
}
if (previousIsCallback && !currentIsCallback) {
reportNodeAttribute(memo, "listCallbacksLast", node, context, nodeReservedList);
return memo;
}
}
if (shorthandFirst) {
if (currentValue && !previousValue) return decl;
if (!currentValue && previousValue) {
reportNodeAttribute(decl, "listShorthandFirst", node, context, nodeReservedList);
return memo;
}
}
if (shorthandLast) {
if (!currentValue && previousValue) return decl;
if (currentValue && !previousValue) {
reportNodeAttribute(memo, "listShorthandLast", node, context, nodeReservedList);
return memo;
}
}
const previousIsMultiline = isMultilineProp(memo);
const currentIsMultiline = isMultilineProp(decl);
if (multiline === "first") {
if (previousIsMultiline && !currentIsMultiline) {
return decl;
}
if (!previousIsMultiline && currentIsMultiline) {
reportNodeAttribute(decl, "listMultilineFirst", node, context, nodeReservedList);
return memo;
}
} else if (multiline === "last") {
if (!previousIsMultiline && currentIsMultiline) {
return decl;
}
if (previousIsMultiline && !currentIsMultiline) {
reportNodeAttribute(memo, "listMultilineLast", node, context, nodeReservedList);
return memo;
}
}
if (!noSortAlphabetically && (ignoreCase || locale !== "auto" ? previousPropName.localeCompare(currentPropName, locale === "auto" ? undefined : locale) > 0 : previousPropName > currentPropName)) {
reportNodeAttribute(decl, "sortPropsByAlpha", node, context, nodeReservedList);
return memo;
}
return decl;
}, node.attributes[0]);
}
};
}
});
module.exports = jsxSortProps;