UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

47 lines (44 loc) 1.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getStyledComponentCall = void 0; var _eslintCodemodUtils = require("eslint-codemod-utils"); /** * Returns a styled component */ var getStyledComponentCall = exports.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 ((0, _eslintCodemodUtils.closestOfType)(node, 'ExportNamedDeclaration')) { return; } var styledComponentVariableRef = node.parent; // halts if the styled component is not assigned to a variable immediately if (!(0, _eslintCodemodUtils.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 (!(0, _eslintCodemodUtils.isNodeOfType)(call, 'CallExpression')) { return false; } if (!(0, _eslintCodemodUtils.isNodeOfType)(call.callee, 'MemberExpression')) { return false; } if (!(0, _eslintCodemodUtils.isNodeOfType)(call.callee.object, 'Identifier') || !(0, _eslintCodemodUtils.isNodeOfType)(call.callee.property, 'Identifier')) { return false; } if (/^styled2?$/.test(call.callee.object.name)) { return true; } return false; };