Skip to content

Commit 8047f1e

Browse files
authored
refactor: replace findOffsets helper with native methods (eslint#272)
* refactor: replace `findOffsets` helper with native methods * wip * wip * wip: apply feedback
1 parent fb65800 commit 8047f1e

File tree

2 files changed

+22
-84
lines changed

2 files changed

+22
-84
lines changed

src/rules/no-important.js

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
* @author Yann Bertrand
55
*/
66

7-
//-----------------------------------------------------------------------------
8-
// Imports
9-
//-----------------------------------------------------------------------------
10-
11-
import { findOffsets } from "../util.js";
12-
137
//-----------------------------------------------------------------------------
148
// Type Definitions
159
//-----------------------------------------------------------------------------
@@ -52,77 +46,46 @@ export default {
5246
},
5347

5448
create(context) {
49+
const { sourceCode } = context;
50+
5551
return {
5652
Declaration(node) {
5753
if (node.important) {
58-
const declarationText = context.sourceCode.getText(node);
54+
const declarationText = sourceCode.getText(node);
5955
const textWithoutComments = declarationText.replace(
6056
commentPattern,
6157
match => match.replace(/[^\n]/gu, " "),
6258
);
6359
const importantMatch =
6460
importantPattern.exec(textWithoutComments);
65-
66-
const {
67-
lineOffset: startLineOffset,
68-
columnOffset: startColumnOffset,
69-
} = findOffsets(declarationText, importantMatch.index);
70-
71-
const {
72-
lineOffset: endLineOffset,
73-
columnOffset: endColumnOffset,
74-
} = findOffsets(
75-
declarationText,
76-
importantMatch.index + importantMatch[0].length,
77-
);
78-
79-
const nodeStartLine = node.loc.start.line;
80-
const nodeStartColumn = node.loc.start.column;
81-
const startLine = nodeStartLine + startLineOffset;
82-
const endLine = nodeStartLine + endLineOffset;
83-
const startColumn =
84-
(startLine === nodeStartLine ? nodeStartColumn : 1) +
85-
startColumnOffset;
86-
const endColumn =
87-
(endLine === nodeStartLine ? nodeStartColumn : 1) +
88-
endColumnOffset;
61+
const importantStartOffset = importantMatch.index;
62+
const importantEndOffset =
63+
importantStartOffset + importantMatch[0].length;
64+
const nodeStartOffset = node.loc.start.offset;
8965

9066
context.report({
9167
loc: {
92-
start: {
93-
line: startLine,
94-
column: startColumn,
95-
},
96-
end: {
97-
line: endLine,
98-
column: endColumn,
99-
},
68+
start: sourceCode.getLocFromIndex(
69+
nodeStartOffset + importantStartOffset,
70+
),
71+
end: sourceCode.getLocFromIndex(
72+
nodeStartOffset + importantEndOffset,
73+
),
10074
},
10175
messageId: "unexpectedImportant",
10276
suggest: [
10377
{
10478
messageId: "removeImportant",
10579
fix(fixer) {
106-
const importantStart = importantMatch.index;
107-
const importantEnd =
108-
importantStart +
109-
importantMatch[0].length;
110-
111-
// Find any trailing whitespace before the !important
112-
const valuePart = declarationText.slice(
113-
0,
114-
importantStart,
115-
);
116-
const whitespaceEnd = valuePart.search(
117-
trailingWhitespacePattern,
118-
);
119-
120-
const start =
121-
node.loc.start.offset + whitespaceEnd;
122-
const end =
123-
node.loc.start.offset + importantEnd;
124-
125-
return fixer.removeRange([start, end]);
80+
// Find any trailing whitespace before the `!important`
81+
const whitespaceEndOffset = declarationText
82+
.slice(0, importantStartOffset)
83+
.search(trailingWhitespacePattern);
84+
85+
return fixer.removeRange([
86+
nodeStartOffset + whitespaceEndOffset,
87+
nodeStartOffset + importantEndOffset,
88+
]);
12689
},
12790
},
12891
],

src/util.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,3 @@ export function isSyntaxMatchError(error) {
3232
export function isSyntaxReferenceError(error) {
3333
return typeof error.reference === "string";
3434
}
35-
36-
/**
37-
* Finds the line and column offsets for a given offset in a string.
38-
* @param {string} text The text to search.
39-
* @param {number} offset The offset to find.
40-
* @returns {{lineOffset:number,columnOffset:number}} The location of the offset.
41-
*/
42-
export function findOffsets(text, offset) {
43-
let lineOffset = 0;
44-
let columnOffset = 0;
45-
46-
for (let i = 0; i < offset; i++) {
47-
if (text[i] === "\n") {
48-
lineOffset++;
49-
columnOffset = 0;
50-
} else {
51-
columnOffset++;
52-
}
53-
}
54-
55-
return {
56-
lineOffset,
57-
columnOffset,
58-
};
59-
}

0 commit comments

Comments
 (0)