Skip to content

Commit 295439e

Browse files
just make the code a lil better
1 parent d56bb69 commit 295439e

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

src/rules/a11y-use-accessible-tooltip.js

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,20 @@ module.exports = {
5555
messageId: 'useAccessibleTooltip',
5656
fix(fixer) {
5757
const fixes = []
58-
// If Tooltip is the only import and no existing @primer/react import, replace the whole import statement
59-
if (!hasOtherImports && !hasRootImport) {
60-
fixes.push(fixer.replaceText(node.source, `'@primer/react'`))
61-
} else if (hasOtherImports && !hasRootImport) {
58+
if (!hasOtherImports) {
59+
// If Tooltip is the only import and no existing @primer/react import, replace the whole import statement
60+
if (!hasRootImport) fixes.push(fixer.replaceText(node.source, `'@primer/react'`))
61+
if (hasRootImport) {
62+
// remove the entire import statement
63+
fixes.push(fixer.remove(node))
64+
// find the last specifier in the existing @primer/react import and insert Tooltip after that
65+
const rootImport = sourceCode.ast.body.find(statement => {
66+
return statement.type === 'ImportDeclaration' && statement.source.value === '@primer/react'
67+
})
68+
const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1]
69+
fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`))
70+
}
71+
} else {
6272
// 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.
6373
const previousToken = sourceCode.getTokenBefore(tooltipSpecifier)
6474
const nextToken = sourceCode.getTokenAfter(tooltipSpecifier)
@@ -74,39 +84,20 @@ module.exports = {
7484
} else {
7585
rangeToRemove = [tooltipSpecifier.range[0], tooltipSpecifier.range[1]]
7686
}
77-
87+
// Remove Tooltip from the import statement
7888
fixes.push(fixer.removeRange(rangeToRemove))
79-
fixes.push(fixer.insertTextAfter(node, `\nimport {Tooltip} from '@primer/react';`))
80-
} else {
81-
if (!hasOtherImports) {
82-
// remove the entire import statement
83-
fixes.push(fixer.remove(node))
84-
} else {
85-
const previousToken = sourceCode.getTokenBefore(tooltipSpecifier)
86-
const nextToken = sourceCode.getTokenAfter(tooltipSpecifier)
87-
const hasTrailingComma = nextToken && nextToken.value === ','
88-
const hasLeadingComma = previousToken && previousToken.value === ','
89-
90-
let rangeToRemove
9189

92-
if (hasTrailingComma) {
93-
rangeToRemove = [tooltipSpecifier.range[0], nextToken.range[1] + 1]
94-
} else if (hasLeadingComma) {
95-
rangeToRemove = [previousToken.range[0], tooltipSpecifier.range[1]]
96-
} else {
97-
rangeToRemove = [tooltipSpecifier.range[0], tooltipSpecifier.range[1]]
98-
}
99-
100-
fixes.push(fixer.removeRange(rangeToRemove))
90+
if (!hasRootImport) {
91+
fixes.push(fixer.insertTextAfter(node, `\nimport {Tooltip} from '@primer/react';`))
92+
} else {
93+
// find the last specifier in the existing @primer/react import and insert Tooltip after that
94+
const rootImport = sourceCode.ast.body.find(statement => {
95+
return statement.type === 'ImportDeclaration' && statement.source.value === '@primer/react'
96+
})
97+
const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1]
98+
fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`))
10199
}
102-
// find the last specifier in the existing @primer/react import and insert Tooltip after that
103-
const rootImport = sourceCode.ast.body.find(statement => {
104-
return statement.type === 'ImportDeclaration' && statement.source.value === '@primer/react'
105-
})
106-
const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1]
107-
fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`))
108100
}
109-
110101
return fixes
111102
},
112103
})

0 commit comments

Comments
 (0)