Skip to content

Commit 7ba8490

Browse files
Fixed prefer-range for adjacent bot not-allowed ranges (#219)
1 parent e5eab5b commit 7ba8490

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

lib/rules/prefer-range.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,25 @@ export default createRule("prefer-range", {
103103
} else {
104104
continue
105105
}
106-
const group = groups.find(
107-
(gp) =>
106+
107+
const group = groups.find((gp) => {
108+
const adjacent =
108109
gp.min.value - 1 <= data.max.value &&
109-
data.min.value <= gp.max.value + 1,
110-
)
110+
data.min.value <= gp.max.value + 1
111+
112+
if (!adjacent) {
113+
// the ranges have to be adjacent
114+
return false
115+
}
116+
117+
// the bounds of the union of the two ranges
118+
const min = Math.min(gp.min.value, data.min.value)
119+
const max = Math.max(gp.max.value, data.max.value)
120+
121+
// the union has to be an allowed range as well
122+
return inRange(allowedRanges, min, max)
123+
})
124+
111125
if (group) {
112126
if (data.min.value < group.min.value) {
113127
group.min = data.min

tests/lib/rules/prefer-range.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ tester.run("prefer-range", rule as any, {
5151
code: `/[0123456789 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ]/`,
5252
options: [{ target: ["😀-😏"] }],
5353
},
54+
{
55+
// issue #218
56+
code: `/[а-яА-Я][А-Яа-я]/`,
57+
options: [{ target: ["alphanumeric", "а-я", "А-Я"] }],
58+
},
5459
],
5560
invalid: [
5661
{
@@ -103,6 +108,20 @@ tester.run("prefer-range", rule as any, {
103108
},
104109
],
105110
},
111+
{
112+
code: `/[d-fa-c]/`,
113+
output: `/[a-f]/`,
114+
errors: [
115+
{
116+
message:
117+
"Unexpected multiple adjacent characters. Use 'a-f' instead.",
118+
line: 1,
119+
column: 3,
120+
endLine: 1,
121+
endColumn: 9,
122+
},
123+
],
124+
},
106125
{
107126
code: `/[abc_d-f]/`,
108127
output: `/[a-f_]/`,

0 commit comments

Comments
 (0)