eslint-plugin-clsx
Version:
An ESLint plugin for clsx/classnames
80 lines (79 loc) • 3.67 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var import_types = require("@typescript-eslint/types");
var R = __toESM(require("remeda"));
var import_createRule = require("../createRule");
var utils = __toESM(require("../utils"));
module.exports = (0, import_createRule.createRule)({
name: "forbid-true-inside-object-expressions",
defaultOptions: ["allowMixed"],
meta: {
type: "suggestion",
docs: {
description: "forbid usage of true literal inside object expressions of clsx",
recommended: true
},
fixable: "code",
schema: [{ type: "string", enum: ["always", "allowMixed"] }],
messages: {
default: "Object expression inside clsx should not contain true literals"
}
},
create(context, [allowTrueLiterals]) {
const { sourceCode } = context;
const clsxOptions = utils.extractClsxOptions(context);
return {
ImportDeclaration(importNode) {
const assignedClsxName = utils.findClsxImport(importNode, clsxOptions);
if (!assignedClsxName) {
return;
}
const clsxUsages = utils.getClsxUsages(importNode, sourceCode, assignedClsxName);
clsxUsages.flatMap((clsxCallNode) => clsxCallNode.arguments).forEach((argumentNode) => {
if (argumentNode.type !== import_types.TSESTree.AST_NODE_TYPES.ObjectExpression) return;
const [trueLiteralProps, otherProps] = R.partition(
argumentNode.properties,
(prop) => prop.type === import_types.TSESTree.AST_NODE_TYPES.Property && prop.value.type === import_types.TSESTree.AST_NODE_TYPES.Literal && prop.value.value === true
);
if (trueLiteralProps.length !== 0 && (allowTrueLiterals === "always" || allowTrueLiterals === "allowMixed" && otherProps.length === 0)) {
const trueLiteralPropsText = trueLiteralProps.map((el) => {
const keyText = sourceCode.getText(el.key);
return el.computed ? keyText : `'${keyText}'`;
}).join(", ");
const otherPropsText = otherProps.map((prop) => sourceCode.getText(prop)).join(", ");
const otherPropsWrappedInObject = otherPropsText ? `{ ${otherPropsText} }` : void 0;
context.report({
messageId: "default",
node: argumentNode,
fix: (fixer) => fixer.replaceText(
argumentNode,
[trueLiteralPropsText, otherPropsWrappedInObject].filter(R.isDefined).join(", ")
)
});
}
});
}
};
}
});