Skip to content

Commit ae3aefa

Browse files
committed
feat(no-irregular-whitespace): add error end positions
1 parent 45ba358 commit ae3aefa

File tree

2 files changed

+148
-70
lines changed

2 files changed

+148
-70
lines changed

lib/rules/no-irregular-whitespace.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ module.exports = {
5050
* @returns {RuleListener} AST event handlers.
5151
*/
5252
create(context) {
53-
// Module store of error indexes that we have found
54-
/** @type {number[]} */
55-
let errorIndexes = []
53+
// Module store of error ranges that we have found
54+
/** @type {{start: number, end: number}[]} */
55+
let errorIndices = []
5656

5757
// Lookup the `skipComments` option, which defaults to `false`.
5858
const options = context.options[0] || {}
@@ -74,8 +74,8 @@ module.exports = {
7474
function removeWhitespaceError(node) {
7575
const [startIndex, endIndex] = node.range
7676

77-
errorIndexes = errorIndexes.filter(
78-
(errorIndex) => errorIndex < startIndex || endIndex <= errorIndex
77+
errorIndices = errorIndices.filter(
78+
(error) => error.start < startIndex || endIndex <= error.start
7979
)
8080
}
8181

@@ -155,16 +155,19 @@ module.exports = {
155155
const source = sourceCode.getText()
156156
let match
157157
while ((match = IRREGULAR_WHITESPACE.exec(source)) !== null) {
158-
errorIndexes.push(match.index)
158+
errorIndices.push({
159+
start: match.index,
160+
end: match.index + match[0].length
161+
})
159162
}
160163
while ((match = IRREGULAR_LINE_TERMINATORS.exec(source)) !== null) {
161-
errorIndexes.push(match.index)
164+
errorIndices.push({ start: match.index, end: match.index + 1 })
162165
}
163166
}
164167

165168
checkForIrregularWhitespace()
166169

167-
if (errorIndexes.length === 0) {
170+
if (errorIndices.length === 0) {
168171
return {}
169172
}
170173
const bodyVisitor = utils.defineTemplateBodyVisitor(context, {
@@ -212,16 +215,19 @@ module.exports = {
212215
const [templateStart, templateEnd] = templateBody
213216
? templateBody.range
214217
: [0, 0]
215-
errorIndexes = errorIndexes.filter(
216-
(errorIndex) =>
217-
(scriptStart <= errorIndex && errorIndex < scriptEnd) ||
218-
(templateStart <= errorIndex && errorIndex < templateEnd)
218+
errorIndices = errorIndices.filter(
219+
(error) =>
220+
(scriptStart <= error.start && error.start < scriptEnd) ||
221+
(templateStart <= error.start && error.start < templateEnd)
219222
)
220223

221224
// If we have any errors remaining, report on them
222-
for (const errorIndex of errorIndexes) {
225+
for (const { start, end } of errorIndices) {
223226
context.report({
224-
loc: sourceCode.getLocFromIndex(errorIndex),
227+
loc: {
228+
start: sourceCode.getLocFromIndex(start),
229+
end: sourceCode.getLocFromIndex(end)
230+
},
225231
messageId: 'disallow'
226232
})
227233
}

0 commit comments

Comments
 (0)