eslint-plugin-react-form-fields
Version:
React Form Fields specific linting rules for ESLint
108 lines (107 loc) • 5.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectStyledComponentData = void 0;
const experimental_utils_1 = require("@typescript-eslint/experimental-utils");
const styledJSX_1 = require("./styledJSX");
const collectStyledComponentData = (styledComponents) => ({
TaggedTemplateExpression(node) {
const scName = (0, styledJSX_1.getStyledComponentName)(node);
if (!scName)
return;
let attrs = [];
let tag = '';
if ((0, styledJSX_1.isStyledTags)(node)) {
tag =
(node.tag.type === experimental_utils_1.AST_NODE_TYPES.MemberExpression &&
node.tag.property.type === experimental_utils_1.AST_NODE_TYPES.Identifier &&
node.tag.property.name) ||
'';
styledComponents[scName] = {
name: scName,
tag,
attrs,
};
}
// styled('div')``
if ((0, styledJSX_1.isCreateStyledTags)(node)) {
tag =
(node.tag.type === experimental_utils_1.AST_NODE_TYPES.CallExpression &&
node.tag.arguments[0].type === experimental_utils_1.AST_NODE_TYPES.Literal &&
typeof node.tag.arguments[0].value === 'string' &&
node.tag.arguments[0].value) ||
'';
styledComponents[scName] = {
name: scName,
tag,
attrs,
};
return;
}
// styled(Component)``
if ((0, styledJSX_1.isCreateStyledComponent)(node)) {
const ancestorScName = node.tag.type === experimental_utils_1.AST_NODE_TYPES.CallExpression &&
node.tag.arguments[0].type === experimental_utils_1.AST_NODE_TYPES.Identifier &&
node.tag.arguments[0].name;
if (!ancestorScName || !styledComponents[ancestorScName])
return;
tag = styledComponents[ancestorScName].tag;
attrs = styledComponents[ancestorScName].attrs;
styledComponents[scName] = { name: scName, attrs, tag };
return;
}
// styled.div.attrs(...)``
if ((0, styledJSX_1.isStyledTagsWithAttrs)(node)) {
tag =
(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.property.type === experimental_utils_1.AST_NODE_TYPES.Identifier &&
node.tag.callee.object.property.name) ||
'';
attrs = (0, styledJSX_1.getAttrsProperties)(node);
styledComponents[scName] = {
name: scName,
tag,
attrs,
};
return;
}
if ((0, styledJSX_1.isCreateStyledTagsWithAttrs)(node)) {
tag =
(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.arguments[0].type === experimental_utils_1.AST_NODE_TYPES.Literal &&
typeof node.tag.callee.object.arguments[0].value === 'string' &&
node.tag.callee.object.arguments[0].value) ||
'';
attrs = (0, styledJSX_1.getAttrsProperties)(node);
styledComponents[scName] = {
name: scName,
tag,
attrs,
};
return;
}
// styled(Component).attrs(...)``
if ((0, styledJSX_1.isCreateStyledComponentWithAttrs)(node)) {
const ancestorScName = 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.arguments[0].type ===
experimental_utils_1.AST_NODE_TYPES.Identifier &&
node.tag.callee.object.arguments[0].name;
if (!ancestorScName || !styledComponents[ancestorScName])
return;
tag = styledComponents[ancestorScName].tag;
attrs = styledComponents[ancestorScName].attrs.concat((0, styledJSX_1.getAttrsProperties)(node));
styledComponents[scName] = {
name: scName,
tag,
attrs,
};
return;
}
},
});
exports.collectStyledComponentData = collectStyledComponentData;