@@ -40,15 +40,16 @@ module.exports = {
40
40
41
41
const sourceCode = context . getSourceCode ( )
42
42
// Checking to see if there is an existing root (@primer/react) import
43
- const rootImport = sourceCode . ast . body . filter ( statement => {
43
+ // Assuming there is one root import per file
44
+ const rootImport = sourceCode . ast . body . find ( statement => {
44
45
return statement . type === 'ImportDeclaration' && statement . source . value === '@primer/react'
45
46
} )
46
47
47
48
const tooltipSpecifier = node . specifiers . find (
48
49
specifier => specifier . imported && specifier . imported . name === 'Tooltip' ,
49
50
)
50
51
51
- const hasRootImport = rootImport . length >= 1
52
+ const hasRootImport = rootImport !== undefined
52
53
53
54
context . report ( {
54
55
node,
@@ -62,9 +63,6 @@ module.exports = {
62
63
// remove the entire import statement
63
64
fixes . push ( fixer . remove ( node ) )
64
65
// 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
66
const lastSpecifier = rootImport . specifiers [ rootImport . specifiers . length - 1 ]
69
67
fixes . push ( fixer . insertTextAfter ( lastSpecifier , `, Tooltip` ) )
70
68
}
@@ -91,9 +89,6 @@ module.exports = {
91
89
fixes . push ( fixer . insertTextAfter ( node , `\nimport {Tooltip} from '@primer/react';` ) )
92
90
} else {
93
91
// 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
92
const lastSpecifier = rootImport . specifiers [ rootImport . specifiers . length - 1 ]
98
93
fixes . push ( fixer . insertTextAfter ( lastSpecifier , `, Tooltip` ) )
99
94
}
0 commit comments