Skip to content

Commit 889a5eb

Browse files
authored
Merge pull request #12 from Maxim-Mazurok/master
shouldFoundAcrossMultilineInTheMiddleOfLineMultipleTimes - Improves the specification of the number of the line of the found error for the final report
2 parents 1b46375 + 25600e0 commit 889a5eb

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/rules/invalid-regex-rule.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ function foundStart(source, nextLine, foundAt) {
5454
{ line: nextLine, column: 0 })
5555
}
5656

57-
function checkRegex(source, sourceLines, nextLine, nextChar, rootChar, pattern, replace, report) {
57+
function checkRegex(source, sourceLines, nextLine, nextChar, rootChar, pattern, replace, report, originalSource) {
58+
originalSource = originalSource || source
5859
if (source.length !== 0) {
5960
const sourceDetail = checkRegexInSource(source, pattern.regex)
6061
if (sourceDetail.patternIndex !== -1) {
@@ -76,18 +77,19 @@ function checkRegex(source, sourceLines, nextLine, nextChar, rootChar, pattern,
7677
fix: replace(rootChar, sourceDetail)
7778
})
7879
checkRegex(remainingSource, sourceLines, foundDetail.nextLine, foundDetail.nextChar, rootChar + remainingSourceStart,
79-
pattern, replace, report)
80+
pattern, replace, report, originalSource)
8081
}
8182
else {
8283
report({
83-
loc: { start: foundStart(source, nextLine + 1, sourceDetail.patternIndex) },
84+
loc: { start: foundStart(originalSource, 1, sourceDetail.patternIndex + rootChar) },
8485
message: formatReportMessage(
8586
pattern,
8687
from => `Invalid regular expression ${from} found`
8788
),
8889
fix: replace(rootChar, sourceDetail)
8990
})
90-
checkRegex(remainingSource, sourceLines, nextLine + 1, nextChar, rootChar + remainingSourceStart, pattern, replace, report)
91+
checkRegex(remainingSource, sourceLines, nextLine + 1, nextChar, rootChar + remainingSourceStart, pattern, replace, report,
92+
originalSource)
9193
}
9294
}
9395
}

tests/lib/rules/invalid-regex-rule.e2e-test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,37 @@ const shouldFoundAcrossMultilineInTheMiddleOfLine = {
146146
}]
147147
}
148148

149+
const shouldFoundAcrossMultilineInTheMiddleOfLineMultipleTimes = {
150+
code: [
151+
'const one = `{',
152+
' "TotalItems":5,',
153+
'}`;',
154+
'const two = `{',
155+
' "TotalItems":5,',
156+
'}`;',
157+
'const three = `{',
158+
' "TotalItems":5,',
159+
'}`;'
160+
].join('\n'),
161+
filename: 'some.js',
162+
options: [
163+
['[`\'"][\\s\\n]*\\{[\\s\\n]*"']
164+
],
165+
errors: [{
166+
message: 'Invalid regular expression /[`\'"][\\s\\n]*\\{[\\s\\n]*"/gm found',
167+
line: 1,
168+
column: 13
169+
}, {
170+
message: 'Invalid regular expression /[`\'"][\\s\\n]*\\{[\\s\\n]*"/gm found',
171+
line: 4,
172+
column: 13
173+
}, {
174+
message: 'Invalid regular expression /[`\'"][\\s\\n]*\\{[\\s\\n]*"/gm found',
175+
line: 7,
176+
column: 15
177+
}]
178+
}
179+
149180
ruleTester.run(
150181
'invalid',
151182
invalidRegexRule, {
@@ -161,7 +192,8 @@ ruleTester.run(
161192
shouldFoundSameMultiline,
162193
shouldFoundAcrossLines,
163194
shouldFoundAcrossMultiline,
164-
shouldFoundAcrossMultilineInTheMiddleOfLine
195+
shouldFoundAcrossMultilineInTheMiddleOfLine,
196+
shouldFoundAcrossMultilineInTheMiddleOfLineMultipleTimes
165197
]
166198
}
167199
)

0 commit comments

Comments
 (0)