UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

40 lines (36 loc) 1.69 kB
import { isNodeOfType } from 'eslint-codemod-utils'; /** * Given a component name finds its JSX usage */ export var getJsxElementByName = function getJsxElementByName(componentName, scope) { var variableDeclaration = scope.variables.find(function (v) { return v.name === componentName; }); if (!variableDeclaration) { return; } // `@typescript-eslint/scope-manager` v8 records a reference for both the opening *and* the // closing tag of an element, where v7 only recorded the opening one. So key off the opening // elements rather than the raw reference count, which differs between the two. var jsxOpeningElements = variableDeclaration.references.map(function (ref) { return ref === null || ref === void 0 ? void 0 : ref.identifier; }).filter(function (identifier) { return isNodeOfType(identifier, 'JSXIdentifier'); }).map(function (identifier) { return identifier.parent; }).filter(function (parent) { return isNodeOfType(parent, 'JSXOpeningElement'); }); // Anything that isn't a JSX reference, beyond the declaration itself, means the component is // also used somewhere we can't reason about. var nonJsxReferences = variableDeclaration.references.filter(function (ref) { return !isNodeOfType(ref === null || ref === void 0 ? void 0 : ref.identifier, 'JSXIdentifier'); }); // there should be exactly one JSX call site, and the declaration as the only other reference. // we might consider handling multiple local JSX call sites in the future // but "this is good enough for now"™️ if (jsxOpeningElements.length !== 1 || nonJsxReferences.length !== 1) { return; } return jsxOpeningElements[0]; };