Skip to content

Commit

Permalink
just make the code a lil better
Browse files Browse the repository at this point in the history
  • Loading branch information
broccolinisoup committed Nov 6, 2024
1 parent d56bb69 commit 295439e
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions src/rules/a11y-use-accessible-tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,20 @@ module.exports = {
messageId: 'useAccessibleTooltip',
fix(fixer) {
const fixes = []
// If Tooltip is the only import and no existing @primer/react import, replace the whole import statement
if (!hasOtherImports && !hasRootImport) {
fixes.push(fixer.replaceText(node.source, `'@primer/react'`))
} else if (hasOtherImports && !hasRootImport) {
if (!hasOtherImports) {
// If Tooltip is the only import and no existing @primer/react import, replace the whole import statement
if (!hasRootImport) fixes.push(fixer.replaceText(node.source, `'@primer/react'`))
if (hasRootImport) {
// remove the entire import statement
fixes.push(fixer.remove(node))
// find the last specifier in the existing @primer/react import and insert Tooltip after that
const rootImport = sourceCode.ast.body.find(statement => {
return statement.type === 'ImportDeclaration' && statement.source.value === '@primer/react'
})
const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1]
fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`))
}
} else {
// 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.
const previousToken = sourceCode.getTokenBefore(tooltipSpecifier)
const nextToken = sourceCode.getTokenAfter(tooltipSpecifier)
Expand All @@ -74,39 +84,20 @@ module.exports = {
} else {
rangeToRemove = [tooltipSpecifier.range[0], tooltipSpecifier.range[1]]
}

// Remove Tooltip from the import statement
fixes.push(fixer.removeRange(rangeToRemove))
fixes.push(fixer.insertTextAfter(node, `\nimport {Tooltip} from '@primer/react';`))
} else {
if (!hasOtherImports) {
// remove the entire import statement
fixes.push(fixer.remove(node))
} else {
const previousToken = sourceCode.getTokenBefore(tooltipSpecifier)
const nextToken = sourceCode.getTokenAfter(tooltipSpecifier)
const hasTrailingComma = nextToken && nextToken.value === ','
const hasLeadingComma = previousToken && previousToken.value === ','

let rangeToRemove

if (hasTrailingComma) {
rangeToRemove = [tooltipSpecifier.range[0], nextToken.range[1] + 1]
} else if (hasLeadingComma) {
rangeToRemove = [previousToken.range[0], tooltipSpecifier.range[1]]
} else {
rangeToRemove = [tooltipSpecifier.range[0], tooltipSpecifier.range[1]]
}

fixes.push(fixer.removeRange(rangeToRemove))
if (!hasRootImport) {
fixes.push(fixer.insertTextAfter(node, `\nimport {Tooltip} from '@primer/react';`))
} else {
// find the last specifier in the existing @primer/react import and insert Tooltip after that
const rootImport = sourceCode.ast.body.find(statement => {
return statement.type === 'ImportDeclaration' && statement.source.value === '@primer/react'
})
const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1]
fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`))
}
// find the last specifier in the existing @primer/react import and insert Tooltip after that
const rootImport = sourceCode.ast.body.find(statement => {
return statement.type === 'ImportDeclaration' && statement.source.value === '@primer/react'
})
const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1]
fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`))
}

return fixes
},
})
Expand Down

0 comments on commit 295439e

Please sign in to comment.