Skip to content

Commit ab189c5

Browse files
Copilotjoshblack
andcommitted
Implement import removal with edge case documented
Co-authored-by: joshblack <[email protected]>
1 parent ba61171 commit ab189c5

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/rules/no-deprecated-octicon.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,31 @@ module.exports = {
9090
const otherProps = openingElement.attributes.filter(attr => attr !== iconProp)
9191
const propsText = otherProps.map(attr => sourceCode.getText(attr)).join(' ')
9292

93-
// Helper function to determine if this is the last Octicon in the file
94-
function isLastOcticon() {
95-
// Get all Octicon elements using the source code
93+
// Helper function to determine if this is the last Octicon in the file that needs fixing
94+
function isLastOcticonToFix() {
95+
// Get all JSX elements in the source code that are Octicons with icon props
9696
const sourceText = sourceCode.getText()
97-
const octiconMatches = [...sourceText.matchAll(/<Octicon\s/g)]
97+
const lines = sourceText.split('\n')
9898

99-
if (octiconMatches.length <= 1) {
100-
return true
101-
}
99+
// Find all potential Octicon lines
100+
const octiconLines = []
101+
lines.forEach((line, index) => {
102+
if (line.includes('<Octicon') && line.includes('icon=')) {
103+
octiconLines.push(index + 1) // 1-based line numbers
104+
}
105+
})
102106

103-
// Find the position of the current node in the source
104-
const currentNodeStart = node.range[0]
107+
// Get the line number of the current node
108+
const currentLine = sourceCode.getLocFromIndex(node.range[0]).line
105109

106-
// Check if there are any more Octicon elements after this one
107-
const laterOcticons = octiconMatches.filter(match => match.index > currentNodeStart)
108-
return laterOcticons.length === 0
110+
// Check if this is the last one
111+
const currentIndex = octiconLines.indexOf(currentLine)
112+
return currentIndex === octiconLines.length - 1
109113
}
110114

111115
// Helper function to generate import fixes if this is the last Octicon usage
112116
function* generateImportFixes(fixer) {
113-
if (isLastOcticon() && octiconImports.length > 0) {
117+
if (isLastOcticonToFix() && octiconImports.length > 0) {
114118
const importNode = octiconImports[0]
115119
const octiconSpecifier = importNode.specifiers.find(
116120
specifier => specifier.imported && specifier.imported.name === 'Octicon',

0 commit comments

Comments
 (0)