UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

45 lines (41 loc) 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getJsxElementByName = void 0; var _eslintCodemodUtils = require("eslint-codemod-utils"); /** * Given a component name finds its JSX usage */ var getJsxElementByName = exports.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 (0, _eslintCodemodUtils.isNodeOfType)(identifier, 'JSXIdentifier'); }).map(function (identifier) { return identifier.parent; }).filter(function (parent) { return (0, _eslintCodemodUtils.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 !(0, _eslintCodemodUtils.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]; };