|
| 1 | +'use strict' |
| 2 | +const url = require('../url') |
| 3 | +const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute') |
| 4 | +const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name') |
| 5 | + |
| 6 | +module.exports = { |
| 7 | + meta: { |
| 8 | + type: 'suggestion', |
| 9 | + docs: { |
| 10 | + description: 'recommends the use of @primer/react Tooltip component', |
| 11 | + category: 'Best Practices', |
| 12 | + recommended: true, |
| 13 | + url: url(module), |
| 14 | + }, |
| 15 | + fixable: true, |
| 16 | + schema: [], |
| 17 | + messages: { |
| 18 | + useAccessibleTooltip: 'Please use @primer/react Tooltip component that has accessibility improvements', |
| 19 | + useTextProp: 'Please use the text prop instead of aria-label', |
| 20 | + noDelayRemoved: 'noDelay prop is removed. Tooltip now has no delay by default', |
| 21 | + wrapRemoved: 'wrap prop is removed. Tooltip now wraps by default', |
| 22 | + alignRemoved: 'align prop is removed. Please use the direction prop instead.', |
| 23 | + }, |
| 24 | + }, |
| 25 | + create(context) { |
| 26 | + return { |
| 27 | + ImportDeclaration(node) { |
| 28 | + if (node.source.value !== '@primer/react/deprecated') { |
| 29 | + return |
| 30 | + } |
| 31 | + const hasTooltip = node.specifiers.some( |
| 32 | + specifier => specifier.imported && specifier.imported.name === 'Tooltip', |
| 33 | + ) |
| 34 | + |
| 35 | + if (!hasTooltip) { |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + const hasOtherImports = node.specifiers.length > 1 |
| 40 | + |
| 41 | + const sourceCode = context.getSourceCode() |
| 42 | + // Checking to see if there is an existing root (@primer/react) import |
| 43 | + // Assuming there is one root import per file |
| 44 | + const rootImport = sourceCode.ast.body.find(statement => { |
| 45 | + return statement.type === 'ImportDeclaration' && statement.source.value === '@primer/react' |
| 46 | + }) |
| 47 | + |
| 48 | + const tooltipSpecifier = node.specifiers.find( |
| 49 | + specifier => specifier.imported && specifier.imported.name === 'Tooltip', |
| 50 | + ) |
| 51 | + |
| 52 | + const hasRootImport = rootImport !== undefined |
| 53 | + |
| 54 | + context.report({ |
| 55 | + node, |
| 56 | + messageId: 'useAccessibleTooltip', |
| 57 | + fix(fixer) { |
| 58 | + const fixes = [] |
| 59 | + if (!hasOtherImports) { |
| 60 | + // If Tooltip is the only import and no existing @primer/react import, replace the whole import statement |
| 61 | + if (!hasRootImport) fixes.push(fixer.replaceText(node.source, `'@primer/react'`)) |
| 62 | + if (hasRootImport) { |
| 63 | + // remove the entire import statement |
| 64 | + fixes.push(fixer.remove(node)) |
| 65 | + // find the last specifier in the existing @primer/react import and insert Tooltip after that |
| 66 | + const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1] |
| 67 | + fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`)) |
| 68 | + } |
| 69 | + } else { |
| 70 | + // There are other imports from the deprecated bundle but no existing @primer/react import, so remove the Tooltip import and add a new import statement with the correct path. |
| 71 | + const previousToken = sourceCode.getTokenBefore(tooltipSpecifier) |
| 72 | + const nextToken = sourceCode.getTokenAfter(tooltipSpecifier) |
| 73 | + const hasTrailingComma = nextToken && nextToken.value === ',' |
| 74 | + const hasLeadingComma = previousToken && previousToken.value === ',' |
| 75 | + |
| 76 | + let rangeToRemove |
| 77 | + |
| 78 | + if (hasTrailingComma) { |
| 79 | + rangeToRemove = [tooltipSpecifier.range[0], nextToken.range[1] + 1] |
| 80 | + } else if (hasLeadingComma) { |
| 81 | + rangeToRemove = [previousToken.range[0], tooltipSpecifier.range[1]] |
| 82 | + } else { |
| 83 | + rangeToRemove = [tooltipSpecifier.range[0], tooltipSpecifier.range[1]] |
| 84 | + } |
| 85 | + // Remove Tooltip from the import statement |
| 86 | + fixes.push(fixer.removeRange(rangeToRemove)) |
| 87 | + |
| 88 | + if (!hasRootImport) { |
| 89 | + fixes.push(fixer.insertTextAfter(node, `\nimport {Tooltip} from '@primer/react';`)) |
| 90 | + } else { |
| 91 | + // find the last specifier in the existing @primer/react import and insert Tooltip after that |
| 92 | + const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1] |
| 93 | + fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`)) |
| 94 | + } |
| 95 | + } |
| 96 | + return fixes |
| 97 | + }, |
| 98 | + }) |
| 99 | + }, |
| 100 | + JSXOpeningElement(node) { |
| 101 | + const openingElName = getJSXOpeningElementName(node) |
| 102 | + if (openingElName !== 'Tooltip') { |
| 103 | + return |
| 104 | + } |
| 105 | + const ariaLabel = getJSXOpeningElementAttribute(node, 'aria-label') |
| 106 | + if (ariaLabel !== undefined) { |
| 107 | + context.report({ |
| 108 | + node, |
| 109 | + messageId: 'useTextProp', |
| 110 | + fix(fixer) { |
| 111 | + return fixer.replaceText(ariaLabel.name, 'text') |
| 112 | + }, |
| 113 | + }) |
| 114 | + } |
| 115 | + const noDelay = getJSXOpeningElementAttribute(node, 'noDelay') |
| 116 | + if (noDelay !== undefined) { |
| 117 | + context.report({ |
| 118 | + node, |
| 119 | + messageId: 'noDelayRemoved', |
| 120 | + fix(fixer) { |
| 121 | + return fixer.remove(noDelay) |
| 122 | + }, |
| 123 | + }) |
| 124 | + } |
| 125 | + const wrap = getJSXOpeningElementAttribute(node, 'wrap') |
| 126 | + if (wrap !== undefined) { |
| 127 | + context.report({ |
| 128 | + node, |
| 129 | + messageId: 'wrapRemoved', |
| 130 | + fix(fixer) { |
| 131 | + return fixer.remove(wrap) |
| 132 | + }, |
| 133 | + }) |
| 134 | + } |
| 135 | + const align = getJSXOpeningElementAttribute(node, 'align') |
| 136 | + if (align !== undefined) { |
| 137 | + context.report({ |
| 138 | + node, |
| 139 | + messageId: 'alignRemoved', |
| 140 | + fix(fixer) { |
| 141 | + return fixer.remove(align) |
| 142 | + }, |
| 143 | + }) |
| 144 | + } |
| 145 | + }, |
| 146 | + } |
| 147 | + }, |
| 148 | +} |
0 commit comments