Skip to content

Commit

Permalink
reuse rootImport and fix method
Browse files Browse the repository at this point in the history
  • Loading branch information
broccolinisoup committed Nov 7, 2024
1 parent db3bc20 commit b663b9d
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/rules/a11y-use-accessible-tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ module.exports = {

const sourceCode = context.getSourceCode()
// Checking to see if there is an existing root (@primer/react) import
const rootImport = sourceCode.ast.body.filter(statement => {
// Assuming there is one root import per file
const rootImport = sourceCode.ast.body.find(statement => {
return statement.type === 'ImportDeclaration' && statement.source.value === '@primer/react'
})

const tooltipSpecifier = node.specifiers.find(
specifier => specifier.imported && specifier.imported.name === 'Tooltip',
)

const hasRootImport = rootImport.length >= 1
const hasRootImport = rootImport !== undefined

context.report({
node,
Expand All @@ -62,9 +63,6 @@ module.exports = {
// 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`))
}
Expand All @@ -91,9 +89,6 @@ module.exports = {
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`))
}
Expand Down

0 comments on commit b663b9d

Please sign in to comment.