From fb0268b4d135902088be0027c08a00e79017d4cf Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Sun, 26 Jan 2020 08:00:59 +0100 Subject: [PATCH] Allow disabling of regexp character sorting (#468) Co-authored-by: Sindre Sorhus Co-authored-by: fisker Cheung --- docs/rules/regex-shorthand.md | 11 +++++++++-- rules/regex-shorthand.js | 23 +++++++++++++++++++++-- test/regex-shorthand.js | 29 ++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/docs/rules/regex-shorthand.md b/docs/rules/regex-shorthand.md index a00d13abc4..db42628bc5 100644 --- a/docs/rules/regex-shorthand.md +++ b/docs/rules/regex-shorthand.md @@ -2,7 +2,6 @@ This rule is fixable. - ## Fail ```js @@ -15,7 +14,6 @@ const regex = /[^a-z0-9_]/i; const regex = /[0-9]\.[a-zA-Z0-9_]\-[^0-9]/i; ``` - ## Pass ```js @@ -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]`. diff --git a/rules/regex-shorthand.js b/rules/regex-shorthand.js index 39f4455b82..0c20a74bb0 100644 --- a/rules/regex-shorthand.js +++ b/rules/regex-shorthand.js @@ -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; @@ -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) { @@ -69,6 +77,16 @@ const create = context => { }; }; +const schema = [{ + type: 'object', + properties: { + sortCharacterClasses: { + type: 'boolean', + default: true + } + } +}]; + module.exports = { create, meta: { @@ -76,6 +94,7 @@ module.exports = { docs: { url: getDocumentationUrl(__filename) }, - fixable: 'code' + fixable: 'code', + schema } }; diff --git a/test/regex-shorthand.js b/test/regex-shorthand.js index dcceeab680..c43a18a6f3 100644 --- a/test/regex-shorthand.js +++ b/test/regex-shorthand.js @@ -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/', @@ -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: [ { @@ -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]/' } ] });