eslint-plugin-mui-sx-order
Version:
Auto-sorts MUI sx/style props for clean, consistent code with ESLint
97 lines • 4.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const preferredOrder_1 = require("../utils/preferredOrder");
const propertyUtils_1 = require("../utils/propertyUtils");
const checkAndReport_1 = require("../utils/checkAndReport");
const rule = {
meta: {
type: 'suggestion',
docs: {
description: 'Sort MUI sx properties or style objects according to best practice',
recommended: false,
},
fixable: 'code',
schema: [],
messages: {
incorrectOrder: 'Properties in sx/style should be sorted by priority.',
},
},
create(context) {
// Collect local identifiers for createStyles imported from MUI packages
const muiCreateStylesNames = new Set();
try {
const ast = context.sourceCode.ast;
if (ast && Array.isArray(ast.body)) {
for (const node of ast.body) {
if (node.type === 'ImportDeclaration' &&
node.source &&
typeof node.source.value === 'string') {
const src = node.source.value;
if (/^@mui\//.test(src)) {
for (const spec of node.specifiers || []) {
if (spec.type === 'ImportSpecifier' &&
spec.imported &&
spec.imported.name === 'createStyles') {
muiCreateStylesNames.add(spec.local?.name || 'createStyles');
}
}
}
}
}
}
}
catch { }
return {
JSXAttribute(node) {
if (node.name.name !== 'sx' ||
!node.value ||
node.value.type !== 'JSXExpressionContainer' ||
node.value.expression.type !== 'ObjectExpression') {
return;
}
(0, checkAndReport_1.checkAndReport)(context, node.value.expression, preferredOrder_1.getOrder);
},
VariableDeclarator(node) {
// Skip if this is inside an ExportNamedDeclaration (handled separately)
if (node.parent &&
node.parent.type === 'VariableDeclaration' &&
node.parent.parent &&
node.parent.parent.type === 'ExportNamedDeclaration') {
return;
}
if (node.id &&
node.id.type === 'Identifier' &&
(0, propertyUtils_1.isStyleObjectName)(node.id.name) &&
node.init &&
node.init.type === 'ObjectExpression') {
(0, checkAndReport_1.checkAndReport)(context, node.init, preferredOrder_1.getOrder);
}
},
ExportNamedDeclaration(node) {
if (node.declaration &&
node.declaration.type === 'VariableDeclaration') {
for (const decl of node.declaration.declarations) {
if (decl.id &&
decl.id.type === 'Identifier' &&
(0, propertyUtils_1.isStyleObjectName)(decl.id.name) &&
decl.init &&
decl.init.type === 'ObjectExpression') {
(0, checkAndReport_1.checkAndReport)(context, decl.init, preferredOrder_1.getOrder);
}
}
}
},
CallExpression(node) {
if (node.callee.type === 'Identifier' &&
muiCreateStylesNames.has(node.callee.name) &&
node.arguments.length &&
node.arguments[0] &&
node.arguments[0].type === 'ObjectExpression') {
(0, checkAndReport_1.checkAndReport)(context, node.arguments[0], preferredOrder_1.getOrder);
}
},
};
},
};
exports.default = rule;
//# sourceMappingURL=sort-sx-properties.js.map