Skip to content

Commit fb0268b

Browse files
voxpellisindresorhusfisker
committed
Allow disabling of regexp character sorting (#468)
Co-authored-by: Sindre Sorhus <[email protected]> Co-authored-by: fisker Cheung <[email protected]>
1 parent fd46adc commit fb0268b

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

docs/rules/regex-shorthand.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
This rule is fixable.
44

5-
65
## Fail
76

87
```js
@@ -15,7 +14,6 @@ const regex = /[^a-z0-9_]/i;
1514
const regex = /[0-9]\.[a-zA-Z0-9_]\-[^0-9]/i;
1615
```
1716

18-
1917
## Pass
2018

2119
```js
@@ -27,3 +25,12 @@ const regex = /\W/;
2725
const regex = /\W/i;
2826
const regex = /\d\.\w\-\D/i;
2927
```
28+
29+
## Options
30+
31+
### sortCharacterClasses
32+
33+
Type: `boolean`\
34+
Default: `true`
35+
36+
Disables optimizations that affect the sorting of character classes. For example, preserves the order of the characters in `[AaQqTt]` rather than sorting it to `[AQTaqt]`.

rules/regex-shorthand.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ const quoteString = require('./utils/quote-string');
77
const message = 'Use regex shorthands to improve readability.';
88

99
const create = context => {
10+
const {sortCharacterClasses} = context.options[0] || {};
11+
12+
const blacklist = [];
13+
14+
if (sortCharacterClasses === false) {
15+
blacklist.push('charClassClassrangesMerge');
16+
}
17+
1018
return {
1119
'Literal[regex]': node => {
1220
const {raw: original, regex} = node;
@@ -20,7 +28,7 @@ const create = context => {
2028
let optimized = original;
2129

2230
try {
23-
optimized = optimize(original).toString();
31+
optimized = optimize(original, undefined, {blacklist}).toString();
2432
} catch (_) {}
2533

2634
if (original === optimized) {
@@ -69,13 +77,24 @@ const create = context => {
6977
};
7078
};
7179

80+
const schema = [{
81+
type: 'object',
82+
properties: {
83+
sortCharacterClasses: {
84+
type: 'boolean',
85+
default: true
86+
}
87+
}
88+
}];
89+
7290
module.exports = {
7391
create,
7492
meta: {
7593
type: 'suggestion',
7694
docs: {
7795
url: getDocumentationUrl(__filename)
7896
},
79-
fixable: 'code'
97+
fixable: 'code',
98+
schema
8099
}
81100
};

test/regex-shorthand.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const error = {
1313
message: 'Use regex shorthands to improve readability.'
1414
};
1515

16+
const disableSortCharacterClassesOptions = [{
17+
sortCharacterClasses: false
18+
}];
19+
1620
ruleTester.run('regex-shorthand', rule, {
1721
valid: [
1822
'const foo = /\\d/',
@@ -36,7 +40,12 @@ ruleTester.run('regex-shorthand', rule, {
3640

3741
// Should not suggest wrong regex (#447)
3842
'/(\\s|\\.|@|_|-)/u',
39-
'/[\\s.@_-]/u'
43+
'/[\\s.@_-]/u',
44+
45+
{
46+
code: '/[GgHhIiå.Z:a-f"0-8%A*ä]/',
47+
options: disableSortCharacterClassesOptions
48+
}
4049
],
4150
invalid: [
4251
{
@@ -250,6 +259,24 @@ ruleTester.run('regex-shorthand', rule, {
250259
message: '/^by @([a-zA-Z0-9-]+)/ can be optimized to /^by @([\\d-A-Za-z]+)/'
251260
}],
252261
output: 'const foo = /^by @([\\d-A-Za-z]+)/'
262+
},
263+
{
264+
code: '/[GgHhIiå.Z:a-f"0-8%A*ä]/',
265+
errors: [{
266+
...error,
267+
message: '/[GgHhIiå.Z:a-f"0-8%A*ä]/ can be optimized to /["%*.0-8:AG-IZa-iäå]/'
268+
}],
269+
output: '/["%*.0-8:AG-IZa-iäå]/'
270+
},
271+
// Should still use shorthand when disabling sort character classes
272+
{
273+
code: '/[a0-9b]/',
274+
options: disableSortCharacterClassesOptions,
275+
errors: [{
276+
...error,
277+
message: '/[a0-9b]/ can be optimized to /[a\\db]/'
278+
}],
279+
output: '/[a\\db]/'
253280
}
254281
]
255282
});

0 commit comments

Comments
 (0)