|
4 | 4 | * @author Yann Bertrand |
5 | 5 | */ |
6 | 6 |
|
7 | | -//----------------------------------------------------------------------------- |
8 | | -// Imports |
9 | | -//----------------------------------------------------------------------------- |
10 | | - |
11 | | -import { findOffsets } from "../util.js"; |
12 | | - |
13 | 7 | //----------------------------------------------------------------------------- |
14 | 8 | // Type Definitions |
15 | 9 | //----------------------------------------------------------------------------- |
@@ -52,77 +46,46 @@ export default { |
52 | 46 | }, |
53 | 47 |
|
54 | 48 | create(context) { |
| 49 | + const { sourceCode } = context; |
| 50 | + |
55 | 51 | return { |
56 | 52 | Declaration(node) { |
57 | 53 | if (node.important) { |
58 | | - const declarationText = context.sourceCode.getText(node); |
| 54 | + const declarationText = sourceCode.getText(node); |
59 | 55 | const textWithoutComments = declarationText.replace( |
60 | 56 | commentPattern, |
61 | 57 | match => match.replace(/[^\n]/gu, " "), |
62 | 58 | ); |
63 | 59 | const importantMatch = |
64 | 60 | 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; |
89 | 65 |
|
90 | 66 | context.report({ |
91 | 67 | 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 | + ), |
100 | 74 | }, |
101 | 75 | messageId: "unexpectedImportant", |
102 | 76 | suggest: [ |
103 | 77 | { |
104 | 78 | messageId: "removeImportant", |
105 | 79 | 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 | + ]); |
126 | 89 | }, |
127 | 90 | }, |
128 | 91 | ], |
|
0 commit comments