UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

21 lines 924 B
import { closestOfType } from 'eslint-codemod-utils'; import { getImportedNodeBySource } from '../utils/get-imported-node-by-source'; import { IMPORT_NAME, VISUALLY_HIDDEN_IMPORT, VISUALLY_HIDDEN_SOURCE } from './constants'; import { getFirstImport } from './get-first-import'; const fixJsx = (source, node) => fixer => { const fixes = []; const importedNode = getFirstImport(source); const visuallyHiddenNode = getImportedNodeBySource(source, VISUALLY_HIDDEN_SOURCE); if (!importedNode) { return []; } const jsxOpeningElement = closestOfType(node, 'JSXOpeningElement'); if (visuallyHiddenNode) { fixes.push(fixer.replaceText(jsxOpeningElement, visuallyHiddenNode.specifiers[0].local.name)); } else { fixes.push(fixer.insertTextBefore(importedNode, VISUALLY_HIDDEN_IMPORT)); fixes.push(fixer.replaceText(jsxOpeningElement, `<${IMPORT_NAME} />`)); } return fixes; }; export default fixJsx;