Skip to content

Commit

Permalink
Allow disabling of regexp character sorting (#468)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
Co-authored-by: fisker Cheung <lionkay@gmail.com>
  • Loading branch information
3 people committed Jan 26, 2020
1 parent fd46adc commit fb0268b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
11 changes: 9 additions & 2 deletions docs/rules/regex-shorthand.md
Expand Up @@ -2,7 +2,6 @@

This rule is fixable.


## Fail

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


## Pass

```js
Expand All @@ -27,3 +25,12 @@ const regex = /\W/;
const regex = /\W/i;
const regex = /\d\.\w\-\D/i;
```

## Options

### sortCharacterClasses

Type: `boolean`\
Default: `true`

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]`.
23 changes: 21 additions & 2 deletions rules/regex-shorthand.js
Expand Up @@ -7,6 +7,14 @@ const quoteString = require('./utils/quote-string');
const message = 'Use regex shorthands to improve readability.';

const create = context => {
const {sortCharacterClasses} = context.options[0] || {};

const blacklist = [];

if (sortCharacterClasses === false) {
blacklist.push('charClassClassrangesMerge');
}

return {
'Literal[regex]': node => {
const {raw: original, regex} = node;
Expand All @@ -20,7 +28,7 @@ const create = context => {
let optimized = original;

try {
optimized = optimize(original).toString();
optimized = optimize(original, undefined, {blacklist}).toString();
} catch (_) {}

if (original === optimized) {
Expand Down Expand Up @@ -69,13 +77,24 @@ const create = context => {
};
};

const schema = [{
type: 'object',
properties: {
sortCharacterClasses: {
type: 'boolean',
default: true
}
}
}];

module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
url: getDocumentationUrl(__filename)
},
fixable: 'code'
fixable: 'code',
schema
}
};
29 changes: 28 additions & 1 deletion test/regex-shorthand.js
Expand Up @@ -13,6 +13,10 @@ const error = {
message: 'Use regex shorthands to improve readability.'
};

const disableSortCharacterClassesOptions = [{
sortCharacterClasses: false
}];

ruleTester.run('regex-shorthand', rule, {
valid: [
'const foo = /\\d/',
Expand All @@ -36,7 +40,12 @@ ruleTester.run('regex-shorthand', rule, {

// Should not suggest wrong regex (#447)
'/(\\s|\\.|@|_|-)/u',
'/[\\s.@_-]/u'
'/[\\s.@_-]/u',

{
code: '/[GgHhIiå.Z:a-f"0-8%A*ä]/',
options: disableSortCharacterClassesOptions
}
],
invalid: [
{
Expand Down Expand Up @@ -250,6 +259,24 @@ ruleTester.run('regex-shorthand', rule, {
message: '/^by @([a-zA-Z0-9-]+)/ can be optimized to /^by @([\\d-A-Za-z]+)/'
}],
output: 'const foo = /^by @([\\d-A-Za-z]+)/'
},
{
code: '/[GgHhIiå.Z:a-f"0-8%A*ä]/',
errors: [{
...error,
message: '/[GgHhIiå.Z:a-f"0-8%A*ä]/ can be optimized to /["%*.0-8:AG-IZa-iäå]/'
}],
output: '/["%*.0-8:AG-IZa-iäå]/'
},
// Should still use shorthand when disabling sort character classes
{
code: '/[a0-9b]/',
options: disableSortCharacterClassesOptions,
errors: [{
...error,
message: '/[a0-9b]/ can be optimized to /[a\\db]/'
}],
output: '/[a\\db]/'
}
]
});

0 comments on commit fb0268b

Please sign in to comment.