eslint-plugin-react-form-fields
Version:
React Form Fields specific linting rules for ESLint
98 lines (97 loc) • 6.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAttrsProperties = exports.getStyledComponentName = exports.isCreateStyledComponentWithAttrs = exports.isCreateStyledTagsWithAttrs = exports.isStyledTagsWithAttrs = exports.isCreateStyledComponent = exports.isCreateStyledTags = exports.isStyledTags = exports.isStyledCSS = void 0;
const experimental_utils_1 = require("@typescript-eslint/experimental-utils");
// css``
const isStyledCSS = (node) => node.tag.type === experimental_utils_1.AST_NODE_TYPES.Identifier && node.tag.name === 'css';
exports.isStyledCSS = isStyledCSS;
// styled.tag``
const isStyledTags = (node) => node.tag.type === experimental_utils_1.AST_NODE_TYPES.MemberExpression &&
node.tag.object.type === experimental_utils_1.AST_NODE_TYPES.Identifier &&
node.tag.object.name === 'styled';
exports.isStyledTags = isStyledTags;
// styled('tag')``
const isCreateStyledTags = (node) => node.tag.type === experimental_utils_1.AST_NODE_TYPES.CallExpression &&
node.tag.callee.type === experimental_utils_1.AST_NODE_TYPES.Identifier &&
node.tag.callee.name === 'styled' &&
node.tag.arguments[0].type === experimental_utils_1.AST_NODE_TYPES.Literal;
exports.isCreateStyledTags = isCreateStyledTags;
// styled(Component)``
const isCreateStyledComponent = (node) => node.tag.type === experimental_utils_1.AST_NODE_TYPES.CallExpression &&
node.tag.callee.type === experimental_utils_1.AST_NODE_TYPES.Identifier &&
node.tag.callee.name === 'styled' &&
node.tag.arguments[0].type === experimental_utils_1.AST_NODE_TYPES.Identifier;
exports.isCreateStyledComponent = isCreateStyledComponent;
// styled.tag.attrs(...)``
const isStyledTagsWithAttrs = (node) => node.tag.type === experimental_utils_1.AST_NODE_TYPES.CallExpression &&
node.tag.callee.type === experimental_utils_1.AST_NODE_TYPES.MemberExpression &&
node.tag.callee.object.type === experimental_utils_1.AST_NODE_TYPES.MemberExpression &&
node.tag.callee.object.object.type === experimental_utils_1.AST_NODE_TYPES.Identifier &&
node.tag.callee.object.object.name === 'styled';
exports.isStyledTagsWithAttrs = isStyledTagsWithAttrs;
// styled('tag').attrs(...)``
const isCreateStyledTagsWithAttrs = (node) => node.tag.type === experimental_utils_1.AST_NODE_TYPES.CallExpression &&
node.tag.callee.type === experimental_utils_1.AST_NODE_TYPES.MemberExpression &&
node.tag.callee.object.type === experimental_utils_1.AST_NODE_TYPES.CallExpression &&
node.tag.callee.object.callee.type === experimental_utils_1.AST_NODE_TYPES.Identifier &&
node.tag.callee.object.callee.name === 'styled' &&
node.tag.callee.object.arguments[0].type === experimental_utils_1.AST_NODE_TYPES.Literal;
exports.isCreateStyledTagsWithAttrs = isCreateStyledTagsWithAttrs;
// styled(Component).attrs(...)``
const isCreateStyledComponentWithAttrs = (node) => node.tag.type === experimental_utils_1.AST_NODE_TYPES.CallExpression &&
node.tag.callee.type === experimental_utils_1.AST_NODE_TYPES.MemberExpression &&
node.tag.callee.object.type === experimental_utils_1.AST_NODE_TYPES.CallExpression &&
node.tag.callee.object.callee.type === experimental_utils_1.AST_NODE_TYPES.Identifier &&
node.tag.callee.object.callee.name === 'styled' &&
node.tag.callee.object.arguments[0].type === experimental_utils_1.AST_NODE_TYPES.Identifier;
exports.isCreateStyledComponentWithAttrs = isCreateStyledComponentWithAttrs;
const getStyledComponentName = (node) => {
var _a;
if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === experimental_utils_1.AST_NODE_TYPES.VariableDeclarator &&
node.parent.id.type === experimental_utils_1.AST_NODE_TYPES.Identifier) {
return node.parent.id.name;
}
return null;
};
exports.getStyledComponentName = getStyledComponentName;
const getAttrsNode = (node) => {
var _a;
if (!(0, exports.isStyledTagsWithAttrs)(node) &&
!(0, exports.isCreateStyledComponentWithAttrs)(node) &&
!(0, exports.getStyledComponentName)(node)) {
return null;
}
if (node.tag.type !== experimental_utils_1.AST_NODE_TYPES.CallExpression) {
return null;
}
return ((_a = node.tag.arguments) === null || _a === void 0 ? void 0 : _a[0]) || null;
};
const getAttrsProperties = (node) => {
var _a;
const attrsNode = getAttrsNode(node);
let attrsNodeProperties = [];
// styled.tag.attrs(props => ({}))``
if ((attrsNode === null || attrsNode === void 0 ? void 0 : attrsNode.type) === experimental_utils_1.AST_NODE_TYPES.ArrowFunctionExpression) {
attrsNodeProperties =
(attrsNode.body.type === experimental_utils_1.AST_NODE_TYPES.ObjectExpression &&
attrsNode.body.properties) ||
[];
}
// styled.tag.attrs(function (props) { return () })``
if ((attrsNode === null || attrsNode === void 0 ? void 0 : attrsNode.type) === experimental_utils_1.AST_NODE_TYPES.FunctionExpression) {
const attrsReturnStatement = (attrsNode.body.type === experimental_utils_1.AST_NODE_TYPES.BlockStatement &&
attrsNode.body.body.find((statement) => statement.type === experimental_utils_1.AST_NODE_TYPES.ReturnStatement)) ||
undefined;
attrsNodeProperties =
(((_a = attrsReturnStatement === null || attrsReturnStatement === void 0 ? void 0 : attrsReturnStatement.argument) === null || _a === void 0 ? void 0 : _a.type) ===
experimental_utils_1.AST_NODE_TYPES.ObjectExpression &&
attrsReturnStatement.argument.properties) ||
[];
}
// styled.tag.attrs({})``
if ((attrsNode === null || attrsNode === void 0 ? void 0 : attrsNode.type) === experimental_utils_1.AST_NODE_TYPES.ObjectExpression) {
attrsNodeProperties = attrsNode.properties || [];
}
return attrsNodeProperties.filter((property) => property.type === experimental_utils_1.AST_NODE_TYPES.Property);
};
exports.getAttrsProperties = getAttrsProperties;