UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

42 lines (39 loc) 1.26 kB
import { closestOfType, isNodeOfType } from 'eslint-codemod-utils'; /** * Returns a styled component */ export var getStyledComponentCall = function getStyledComponentCall(node) { // halts unless we are dealing with a styled component if (!isStyledCallExpression(node)) { return; } // halts if the component is being exported directly if (closestOfType(node, 'ExportNamedDeclaration')) { return; } var styledComponentVariableRef = node.parent; // halts if the styled component is not assigned to a variable immediately if (!isNodeOfType(styledComponentVariableRef, 'VariableDeclarator')) { return; } return styledComponentVariableRef; }; /** * Some verbose precondition checks but all it does is check * a call expression is of form `styled.<element>` or `styled2.<element>` */ var isStyledCallExpression = function isStyledCallExpression(call) { if (!isNodeOfType(call, 'CallExpression')) { return false; } if (!isNodeOfType(call.callee, 'MemberExpression')) { return false; } if (!isNodeOfType(call.callee.object, 'Identifier') || !isNodeOfType(call.callee.property, 'Identifier')) { return false; } if (/^styled2?$/.test(call.callee.object.name)) { return true; } return false; };