Skip to content

Commit

Permalink
Fix(ESLint): Causes incorrect class selection when there is only one …
Browse files Browse the repository at this point in the history
…character, resolved #345
  • Loading branch information
0Miles committed May 8, 2024
1 parent 807aadf commit ab88ebd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/eslint-plugin/src/utils/find-loc.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { indexOfAll } from "./index-of-all"

export default function findLoc(text, lines, startLine, endLine) {
const targetLines = text.match(/.+(?:\r\n|\n)?/g)
Expand All @@ -9,16 +10,24 @@ export default function findLoc(text, lines, startLine, endLine) {
for (let i = startLine; i <= endLine; i++) {
const sourceCodeLine = lines[i - 1]
const content = targetLines[checkingTargetLine].replace(/\r\n|\n/, '')
const index = sourceCodeLine.indexOf(content)
if (index !== -1) {
if (index === 0 || sourceCodeLine[index - 1].match(/^|[\s"'']/)) {

const indexes = indexOfAll(sourceCodeLine, content)
if (indexes.length > 0) {
for (const index of indexes) {

if ((index !== 0 && sourceCodeLine[index - 1].match(/\w/)) ||
(index + content.length < sourceCodeLine.length && sourceCodeLine[index + content.length].match(/\w/))) {
continue
}

if (checkingTargetLine === 0) {
resultStart = {
line: i,
column: index
}
}
if (checkingTargetLine === targetLines.length - 1 && (sourceCodeLine[index + content.length - 1].match(/$|[\s"'']/) || sourceCodeLine[index + content.length].match(/[\s"'']/))) {

if (checkingTargetLine === targetLines.length - 1) {
return {
start: resultStart,
end: {
Expand Down
8 changes: 8 additions & 0 deletions packages/eslint-plugin/src/utils/index-of-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function indexOfAll(arr: any[] | string, value: any): number[] {
const result = []
let i = -1
while ((i = arr.indexOf(value, i + 1)) !== -1) {
result.push(i)
}
return result
}
10 changes: 10 additions & 0 deletions packages/eslint-plugin/tests/find-loc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import findLoc from '../src/utils/find-loc'

describe('findLoc', () => {
it('Causes incorrect class selection when there is only one character #345', () => {
const text = 'f'
const lines = ['font:12 font:24@sm m:32 block font:32@md mb:48 f', 'bg:indigo font:']
const result = findLoc(text, lines, 1, 2)
expect(result).toEqual({ start: { line: 1, column: 47 }, end: { line: 1, column: 48 } })
})
})

0 comments on commit ab88ebd

Please sign in to comment.