Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling of regexp character sorting #468

Merged
merged 12 commits into from Jan 26, 2020
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]/'
}
]
});